Accordion
This is a headless disclosure component: a vertical or horizontal stack of sections that open and close. It has roving focus and disabled items. One section can be open, or more than one section can be open. You can control the open state, or you can let the component control it.
Anatomy
Give each Accordion.Item a unique value. The Header, the Trigger, and the Panel of that item read the value from the context of the item. Thus you write it one time only.
<script>
import { Accordion } from '@human-kit/ui';
</script>
<Accordion.Root defaultValue={['overview']}>
<Accordion.Item value="overview">
<Accordion.Header>
<Accordion.Trigger>Overview</Accordion.Trigger>
</Accordion.Header>
<Accordion.Panel>Overview content</Accordion.Panel>
</Accordion.Item>
</Accordion.Root>Many open panels, and disabled items
Set selectionMode="multiple" to let more than one panel stay open. The default is "single". Use disabled on an Accordion.Item to stop one item, or on Accordion.Root to stop the full accordion.
Usage guidelines
- Use
valueandonChangewhen your own code controls the state. UsedefaultValuewhen the component controls the state. Both props are arrays of the values of the open items. - Use
disallowEmptySelectionto keep one panel open. - Use
orientation="horizontal"to change the axis of the arrow keys. - Use
loop={false}to stop the focus at the first trigger and at the last trigger. - Use
forceMountonAccordion.Panelwhen the content must stay in the DOM while the panel is closed. - Set
levelonAccordion.Header(from 1 to 6, the default is3) to agree with the headings around the accordion.
Accessibility
Accordion.Headermakes a true heading element. The default is<h3>. The heading contains the trigger button. This obeys the WAI-ARIA accordion pattern.Accordion.Triggerhas button semantics witharia-expandedandaria-controls.Accordion.Panelhasrole="region", and itsaria-labelledbyattribute points at its trigger. While the panel is closed, the panel ishiddenandinert.- The arrow keys move the focus between the triggers. The
Homekey and theEndkey move the focus to the first enabled trigger and to the last enabled trigger. At the ends, the focus goes to the opposite end, but not withloop={false}. - The
Enterkey and theSpacekey open and close the panel with the focus, like a native button.
API reference
Root
Contains all of the items. It holds the selection state and controls the roving focus.
* required. Native HTML attributes of the underlying element are also accepted.
Item
Contains one section that opens and closes. It gives its value to Header, Trigger and Panel through the context.
* required. Native HTML attributes of the underlying element are also accepted.
Header
A true heading element that contains the trigger. The default is h3.
* required. Native HTML attributes of the underlying element are also accepted.
Trigger
The button that opens and closes its panel. It sets aria-expanded and aria-controls.
* required. Native HTML attributes of the underlying element are also accepted.
Panel
The region that opens and closes. While it is closed, it is hidden and inert.
* required. Native HTML attributes of the underlying element are also accepted.