Accordion
Sections that open and close to show or hide their content.
Default
Is it accessible?
Does it need JavaScript?
Can I style it?
{ui accordion,
items: [
[
'title' => 'Is it accessible?',
'content' => 'Yes. Each section is a native details element, so the keyboard and screen readers work with no script.',
'open' => true,
],
[
'title' => 'Does it need JavaScript?',
'content' => 'No. Sections open and close on their own.',
],
[
'title' => 'Can I style it?',
'content' => 'It is plain CSS on plain elements, so yes.',
],
],
/}
Each section is a native details with a summary, so it opens with the keyboard and is announced by a screen reader, and works with no script. An item is a title and its content, and open starts it open. Any number can be open at once. The content is escaped, so text from a page field is safe to pass.
One at a time
Shipping
Returns
Warranty
{ui accordion,
exclusive: true,
items: [
[
'title' => 'Shipping',
'content' => 'Orders leave in three working days.',
'open' => true,
],
[
'title' => 'Returns',
'content' => 'Send anything back within thirty days.',
],
[
'title' => 'Warranty',
'content' => 'Two years on every product.',
],
],
/}
exclusive: true closes the other sections when one opens. That is the one thing the shared ui.js does for it. The name attribute would do it natively, but it is newer than the browsers the UI supports. Without the script the sections still open and close, they just do not close each other.
From pages
Tabs
Pagination
Modal
Toast
Carousel
Hero
Navbar
Calendar
Search
Facets
Wizard
{ui accordion, items: p('docs/ui/components')->children() /}
items takes pages through the shared items() function, with a page's description as its content. That suits a list of questions kept as pages.
An item with a src URL, such as "{$page->url()}:shipping", loads its content once, the first time it opens, and a spinner shows until then. Such a section is never open at first, since its content is not there yet. Without JavaScript it stays on the placeholder, so content that must work without it is rendered directly.
Your own sections
Invite someone
Account
{ui accordion, exclusive: true}
<details open>
<summary>Invite someone</summary>
<div>
{ui field, name: 'invite', label: 'Email'}
{ui input, name: 'invite', type: 'email' /}
{/ui}
{ui button, title: 'Send invite' /}
</div>
</details>
<details>
<summary>Account</summary>
<div>
{ui menu,
items: [
['title' => 'Profile', 'href' => '#'],
['title' => 'Sign out', 'href' => '#'],
],
/}
</div>
</details>
{/ui}
content is text. A section that holds a form, a menu or any other element is plain markup in the tag: a details with a summary and a div, styled like the rest. It sits after the sections from items, and exclusive closes it with the others.