Accordion
Features
- Full keyboard navigation.
- Supports horizontal/vertical orientation.
- Supports Right to Left direction.
- Can expand one or multiple items.
- Can be controlled or uncontrolled.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import { AccordionContent, AccordionHeader, AccordionItem, AccordionRoot, AccordionTrigger } from 'reka-ui'
</script>
<template>
<AccordionRoot>
<AccordionItem>
<AccordionHeader>
<AccordionTrigger />
</AccordionHeader>
<AccordionContent />
</AccordionItem>
</AccordionRoot>
</template>API Reference
Root
Contains all the parts of an Accordion
| Data Attribute | Value |
|---|---|
[data-orientation] | "vertical" | "horizontal" |
Item
Contains all the parts of a collapsible section.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-orientation] | "vertical" | "horizontal" |
Header
Wraps an AccordionTrigger. Use the asChild prop to update it to the appropriate heading level for your page.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-orientation] | "vertical" | "horizontal" |
Trigger
Toggles the collapsed state of its associated item. It should be nested inside of an AccordionHeader.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-orientation] | "vertical" | "horizontal" |
Content
Contains the collapsible content for an item.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
[data-orientation] | "vertical" | "horizontal" |
| CSS Variable | Description |
|---|---|
--reka-accordion-content-width | The width of the content when it opens/closes |
--reka-accordion-content-height | The height of the content when it opens/closes |
Examples
Expanded by default
Use the defaultValue prop to define the open item by default.
<template>
<AccordionRoot
type="single"
default-value="item-2"
>
<AccordionItem value="item-1">
…
</AccordionItem>
<AccordionItem value="item-2">
…
</AccordionItem>
</AccordionRoot>
</template>Allow collapsing all items
Use the collapsible prop to allow all items to close.
<template>
<AccordionRoot
type="single"
collapsible
>
<AccordionItem value="item-1">
…
</AccordionItem>
<AccordionItem value="item-2">
…
</AccordionItem>
</AccordionRoot>
</template>Multiple items open at the same time
Set the type prop to multiple to enable opening multiple items at once.
<template>
<AccordionRoot type="multiple">
<AccordionItem value="item-1">
…
</AccordionItem>
<AccordionItem value="item-2">
…
</AccordionItem>
</AccordionRoot>
</template>Rotated icon when open
You can add extra decorative elements, such as chevrons, and rotate it when the item is open.
// index.vue
<script setup>
import { Icon } from '@iconify/vue'
import { AccordionContent, AccordionHeader, AccordionItem, AccordionRoot, AccordionTrigger } from 'reka-ui'
import './styles.css'
</script>
<template>
<AccordionRoot type="single">
<AccordionItem value="item-1">
<AccordionHeader>
<AccordionTrigger class="AccordionTrigger">
<span>Trigger text</span>
<Icon
icon="radix-icons:chevron-down"
class="AccordionChevron"
/>
</AccordionTrigger>
</AccordionHeader>
<AccordionContent>…</AccordionContent>
</AccordionItem>
</AccordionRoot>
</template>/* styles.css */
.AccordionChevron {
transition: transform 300ms;
}
.AccordionTrigger[data-state="open"] > .AccordionChevron {
transform: rotate(180deg);
}Horizontal orientation
Use the orientation prop to create a horizontal Accordion
<template>
<AccordionRoot orientation="horizontal">
<AccordionItem value="item-1">
…
</AccordionItem>
<AccordionItem value="item-2">
…
</AccordionItem>
</AccordionRoot>
</template>Animating content size
Use the --reka-accordion-content-width and/or --reka-accordion-content-height CSS variables to animate the size of the content when it opens/closes:
// index.vue
<script setup>
import { AccordionContent, AccordionHeader, AccordionItem, AccordionRoot, AccordionTrigger } from 'reka-ui'
import './styles.css'
</script>
<template>
<AccordionRoot type="single">
<AccordionItem value="item-1">
<AccordionHeader>…</AccordionHeader>
<AccordionContent class="AccordionContent">
…
</AccordionContent>
</AccordionItem>
</AccordionRoot>
</template>/* styles.css */
.AccordionContent {
overflow: hidden;
}
.AccordionContent[data-state="open"] {
animation: slideDown 300ms ease-out;
}
.AccordionContent[data-state="closed"] {
animation: slideUp 300ms ease-out;
}
@keyframes slideDown {
from {
height: 0;
}
to {
height: var(--reka-accordion-content-height);
}
}
@keyframes slideUp {
from {
height: var(--reka-accordion-content-height);
}
to {
height: 0;
}
}Render content even when closed
By default hidden content will be removed, use :unmountOnHide="false" to keep the content always available.
This will also allow browser to search the hidden text, and open the accordion.
<template>
<AccordionRoot :unmount-on-hide="false">
<AccordionItem value="item-1">
…
</AccordionItem>
<AccordionItem value="item-2">
…
</AccordionItem>
</AccordionRoot>
</template>Expand and collapse all
Control the open items with v-model. With type="multiple" the model is an array of item values, so “Expand all” sets it to every value and “Collapse all” to []. The header renders an h3 by default; set as on AccordionHeader to the heading level that fits your page.
Reacting to changes
update:modelValue receives a second argument whose reason is 'trigger-press', 'content-found' (the browser's find-in-page revealed a closed item's text, with :unmount-on-hide="false") or 'imperative-action'. beforeUpdate:modelValue fires first; details.cancel() keeps the current items open.
<template>
<AccordionRoot
v-model="open"
type="multiple"
@update:model-value="(value, details) => analytics.track('faq-toggle', { value, reason: details.reason })"
>
<!-- ... -->
</AccordionRoot>
</template>Real-world examples
- Navigation Mobile Nav: One set of navigation links rendered as a desktop flyout and, below the breakpoint, as an Accordion inside a Drawer.
Accessibility
Adheres to the Accordion WAI-ARIA design pattern.
Keyboard Interactions
| Key | Description |
|---|---|
Space | When focus is on an AccordionTrigger of a collapsed section, expands the section. |
Enter | When focus is on an AccordionTrigger of a collapsed section, expands the section. |
Tab | Moves focus to the next focusable element. |
Shift + Tab | Moves focus to the previous focusable element. |
ArrowDown | Moves focus to the next AccordionTrigger when orientation is vertical. |
ArrowUp | Moves focus to the previous AccordionTrigger when orientation is vertical. |
ArrowRight | Moves focus to the next AccordionTrigger when orientation is horizontal. |
ArrowLeft | Moves focus to the previous AccordionTrigger when orientation is horizontal. |
Home | When focus is on an AccordionTrigger, moves focus to the start AccordionTrigger. |
End | When focus is on an AccordionTrigger, moves focus to the last AccordionTrigger. |
In a right-to-left layout (dir="rtl" on AccordionRoot, or on a ConfigProvider), ArrowLeft moves to the next trigger and ArrowRight to the previous one, in a horizontal accordion. See Internationalization.
Headless composable
useAccordion holds the open items (modelValue, changeModelValue(value)) for type single or multiple, and getItemSurface(value) returns the state and the header, trigger and content part surfaces of one item. Arrow-key navigation between the triggers and the content's mount and size animation stay in the components. The composable is experimental; see Headless composables.
