Abdulkader Safi

LaralCN-UI FAQ blocks: an accordion filled from an array

LaralCN-UI has ten FAQ blocks, and the collapsible ones use the library's accordion as it is. The accordion takes its answers as named slots, and the trick was filling those slots from an array of questions. Two Blade quirks showed up on the way.

4 min read

Share
The LaralCN-UI docs showing a component preview beside its Blade source.

LaralCN-UI has ten FAQ blocks, and the collapsible ones use the library's accordion as it is. The accordion takes its answers as named slots, and the trick was filling those slots from an array of questions. Two Blade quirks showed up on the way.

The accordion wants slots, the block has an array

The accordion component has always taken its labels as an array and each answer as a named slot:

<x-ui.accordion :items="['a' => 'Is it accessible?']">
    <x-slot:item_a>Yes. It follows the WAI-ARIA disclosure pattern.</x-slot:item_a>
</x-ui.accordion>

That reads well when you write an FAQ by hand. A block is different: its questions arrive as a prop, ['question' => ..., 'answer' => ...] per item, possibly straight from a database. The rule for these blocks was to use the library's components, not to copy the accordion's markup into each block, so the question was whether Blade can generate named slots in a loop.

It can. x-slot takes a dynamic :name, and it works inside @foreach:

<x-ui.accordion :items="array_column($items, 'question')">
    @foreach ($items as $i => $item)
        <x-slot :name="'item_' . $i">{{ $item['answer'] ?? '' }}</x-slot>
    @endforeach
</x-ui.accordion>

array_column turns the items into the label list, keyed 0, 1, 2, and the loop produces item_0, item_1 and so on to match. The accordion never knows the content came from an array, and the block never duplicates the accordion's keyboard handling or ARIA wiring.

Styling a component from the outside

Relume's FAQ questions are bold and larger than the accordion's default, with more padding per row. Changing the accordion itself would change every accordion in every app, so the blocks style it through its class with descendant variants that target its data attributes:

class="w-full max-w-3xl border-y border-border
       [&_[data-ui-accordion-trigger]]:py-4 [&_[data-ui-accordion-trigger]]:font-bold
       md:[&_[data-ui-accordion-trigger]]:text-lg [&_[data-ui-accordion-content]]:pb-5"

Every one of those classes is written out in full in the source, because Tailwind scans files as text and never sees a class built at runtime. The card layouts use the same approach with [&>div]:border to box each item.

Two Blade quirks

The first: binding the class with :class="..." instead of a static class="..." broke every arbitrary variant. The & in [&_...] came out as &amp;amp;, escaped twice on the way through the component's attribute bag, and Tailwind's classes no longer matched. A static class attribute passes through untouched.

The second came with structured data. Each block has a schema prop that prints FAQPage JSON-LD, which search engines can show as expandable questions. The array needs an @context key, and Blade compiles @context as a directive even inside an echo statement. Building the array inside a @php block keeps the compiler away from it:

@php
    // Built in a php block: Blade would read the '@context' key below as a directive anywhere else.
    $faqSchema = [
        '@context' => 'https://schema.org',
        '@type' => 'FAQPage',
        // ...
    ];
@endphp
<script type="application/ld+json">{!! json_encode($faqSchema, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) !!}</script>

JSON_HEX_TAG matters here. The questions are content, maybe written by a client in a CMS, and without it an answer containing </script> would close the tag early and spill the rest into the page.

Ten layouts

The blocks cover what the Relume kit has: an accordion with lines, centred or start-aligned; the same beside a title and a contact button; questions in bordered cards; two columns of questions; and open layouts where every answer is always visible, as a list, as question-beside-answer rows separated by x-ui.separator, or as a grid. Most end with a "Still have questions?" prompt whose heading, text and button are props.

Where they differ from the design

Using the accordion unchanged has a price, and I would rather list it than hide it. The icon is the accordion's chevron, where Relume uses a plus in the card layouts. Items start closed and only one opens at a time, where Relume draws them all open. Answers keep the accordion's muted text colour. Matching any of these means a change to the accordion component itself, which is a decision for the library, not for one block.

Where it is

composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add-block faq-05

Pass :schema="true" to add the structured data. Previews: https://laralcn-ui.abdulkadersafi.com/blocks, Arabic under /ar/blocks. Source: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.

Next: 18 pricing blocks, and a monthly/yearly switch that is just the tabs component.

Last updated 1 Dec 2026 · filed under Laravel, blade, laralcn-ui, accessibility

Every entry on this project

26 build notes, in order

Including the ones where nothing worked. You are on part 25.

Read the series →

FAQ

Frequently asked questions

Can I load FAQ questions from the database?

Yes. Pass :items as a list of ['question' => ..., 'answer' => ...], for example from Faq::all(['question', 'answer'])->toArray().

Do the FAQ blocks add FAQPage structured data?

Only when you pass :schema="true". The JSON-LD is built from the same items and escaped with JSON_HEX_TAG.

Can I open several answers at once?

The accordion supports it with its multiple prop, but the blocks keep the default of one at a time. After ui:add-block the file is yours, so add :multiple="true" to its x-ui.accordion. The open layouts show every answer at once.

Why not style the accordion with a bound :class?

A bound class double-escapes the & in arbitrary variants such as [&_...], so they stop matching. Use a static class attribute.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from LaralCN-UI: shadcn-style Blade components and blocks you own

All 26 entries →