Accordion

Sections that open and close to show or hide their content.

Display Added in v0.41.0 Updated

Default

Is it accessible?
Yes. Each section is a native details element, so the keyboard and screen readers work with no script.
Does it need JavaScript?
No. Sections open and close on their own.
Can I style it?
It is plain CSS on plain elements, so yes.

{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
Orders leave in three working days.
Returns
Send anything back within thirty days.
Warranty
Two years on every product.

{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
Links that swap a panel loaded from the server.
Pagination
Links to the pages of a long list.
Modal
A dialog that opens over the page, loaded from the server.
Toast
A short message that appears in the corner and goes away on its own.
Carousel
Slides in a row that scroll and snap.
Hero
The top of a page: a heading, a few lines and buttons, with an image beside them.
Navbar
A site's top bar: a brand, links with dropdowns, and actions, folding into a menu on small screens.
Calendar
A month of events laid out by day, with buttons to step between months.
Search
A search form with an input and a button, and room for a select before it.
Facets
Collapsible groups of checkboxes with counts, to filter a list of results.
Wizard
One step of a multi-step form, with the progress, the fields and Back and Next buttons.

{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.