Accordion
A set of collapsible panels with headings.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Accordion, AccordionItem};
use function Phem\Ui\{AccordionHeader, AccordionPanel, AccordionTrigger};
#[Component]
function AccordionHeroDemo(): Node
{
return (
<Accordion>
<AccordionItem value="what">
<AccordionHeader>
<AccordionTrigger>What is Phem?</AccordionTrigger>
</AccordionHeader>
<AccordionPanel>Phem is a library of UI server components for Symfony web apps.</AccordionPanel>
</AccordionItem>
<AccordionItem value="start">
<AccordionHeader>
<AccordionTrigger>How do I get started?</AccordionTrigger>
</AccordionHeader>
<AccordionPanel>Head to the “Quickstart” guide in the docs. If you’ve used Symfony before, you’ll feel at home.</AccordionPanel>
</AccordionItem>
<AccordionItem value="use">
<AccordionHeader>
<AccordionTrigger>Can I use it for my project?</AccordionTrigger>
</AccordionHeader>
<AccordionPanel>Of course! Phem is free and open source.</AccordionPanel>
</AccordionItem>
</Accordion>
);
}
Examples#
Open multiple panels#
You can set up the accordion to allow multiple panels to be open at the same time using the multiple prop.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Accordion, AccordionItem};
use function Phem\Ui\{AccordionHeader, AccordionPanel, AccordionTrigger};
#[Component]
function AccordionMultipleDemo(): Node
{
return (
<Accordion id="multiple" multiple>
<AccordionItem value="what">
<AccordionHeader>
<AccordionTrigger>What is Phem?</AccordionTrigger>
</AccordionHeader>
<AccordionPanel>Phem is a library of UI server components for Symfony web apps.</AccordionPanel>
</AccordionItem>
<AccordionItem value="start">
<AccordionHeader>
<AccordionTrigger>How do I get started?</AccordionTrigger>
</AccordionHeader>
<AccordionPanel>Head to the “Quickstart” guide in the docs. If you’ve used Symfony before, you’ll feel at home.</AccordionPanel>
</AccordionItem>
<AccordionItem value="use">
<AccordionHeader>
<AccordionTrigger>Can I use it for my project?</AccordionTrigger>
</AccordionHeader>
<AccordionPanel>Of course! Phem is free and open source.</AccordionPanel>
</AccordionItem>
</Accordion>
);
}
#
The hiddenUntilFound prop hides closed panels with hidden="until-found" so the browser can search their contents and reveal the matching panel automatically. It can be set on each AccordionPanel, or once on Accordion to apply to all panels.
To try it, press Ctrl+F (Cmd+F on macOS) and search for “restocking”—the browser opens the closed panel containing the match. When hiddenUntilFound is enabled, closed panels always remain in the DOM, which also makes their contents indexable by search engines.
Older browsers that don’t support hidden="until-found" keep panels hidden until their trigger opens them, and find-in-page skips over the contents.
API reference#
Import the components of the family:
use Phem\Ui\Accordion;
use Phem\Ui\AccordionItem;
use function Phem\Ui\{AccordionHeader, AccordionTrigger, AccordionPanel};Accordion#
A list of sections that open and close: Base UI Accordion.Root, on the phem-accordion controller.
defaultValue lists the values of the open items. Give an id when the page has more than one accordion with the same item values: the ids of the triggers and the panels start with it.
| Prop | Type | Default |
|---|---|---|
defaultValue | array | [] |
multiple | bool | false |
disabled | bool | false |
hiddenUntilFound | bool | false |
orientation | 'horizontal' | 'vertical' | Orientation::Vertical |
loopFocus | bool | true |
rovingFocus | bool | false |
id | ?string | null |
children | ?Html | null |
...attrs | array | [] |
AccordionItem#
An item: a header with a trigger, and a panel. value names the item, and gives the ids of its parts.
| Prop | Type | Default |
|---|---|---|
value (required) | string | — |
disabled | bool | false |
children | ?Html | null |
...attrs | array | [] |
AccordionHeader#
The heading of an accordion item: an <h3> around its AccordionTrigger.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AccordionTrigger#
The button that opens and closes the item. It has a plus icon that turns when the item is open.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
AccordionPanel#
The panel of the item. Its height animates with --accordion-panel-height. With hiddenUntilFound, this closed panel has hidden="until-found", as with the prop of the root.
| Prop | Type | Default |
|---|---|---|
hiddenUntilFound | bool | false |
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/Accordion.phem.