Skip to main content

Accordion

A vertically stacked set of interactive headings that each reveal an associated section of content.

import * as Accordion from '@gentleduck/primitives/accordion'
import * as Accordion from '@gentleduck/primitives/accordion'

Anatomy

<Accordion.Root>
  <Accordion.Item>
    <Accordion.Trigger />
    <Accordion.Content />
  </Accordion.Item>
</Accordion.Root>
<Accordion.Root>
  <Accordion.Item>
    <Accordion.Trigger />
    <Accordion.Content />
  </Accordion.Item>
</Accordion.Root>

Example

import * as Accordion from '@gentleduck/primitives/accordion'
 
function BasicAccordion() {
  return (
    <Accordion.Root type="single" collapsible>
      <Accordion.Item value="item-1">
        <Accordion.Trigger>Is it accessible?</Accordion.Trigger>
        <Accordion.Content>
          Yes. It adheres to the WAI-ARIA design pattern.
        </Accordion.Content>
      </Accordion.Item>
      <Accordion.Item value="item-2">
        <Accordion.Trigger>Is it unstyled?</Accordion.Trigger>
        <Accordion.Content>
          Yes. It is unstyled by default, giving you full control over styling.
        </Accordion.Content>
      </Accordion.Item>
    </Accordion.Root>
  )
}
import * as Accordion from '@gentleduck/primitives/accordion'
 
function BasicAccordion() {
  return (
    <Accordion.Root type="single" collapsible>
      <Accordion.Item value="item-1">
        <Accordion.Trigger>Is it accessible?</Accordion.Trigger>
        <Accordion.Content>
          Yes. It adheres to the WAI-ARIA design pattern.
        </Accordion.Content>
      </Accordion.Item>
      <Accordion.Item value="item-2">
        <Accordion.Trigger>Is it unstyled?</Accordion.Trigger>
        <Accordion.Content>
          Yes. It is unstyled by default, giving you full control over styling.
        </Accordion.Content>
      </Accordion.Item>
    </Accordion.Root>
  )
}

API

Accordion.Root

The root container. Accepts either type="single" (one item open at a time) or type="multiple" (multiple items open simultaneously). Renders a <div>.

Single mode

PropTypeDefaultDescription
type'single''single'Allows only one item to be open at a time
valuestring-Controlled open item value
defaultValuestring-Initial open item value (uncontrolled)
onValueChange(value: string) => void-Called when the open item changes
collapsiblebooleanfalseAllow the open item to be closed by clicking its trigger again
dir'ltr' | 'rtl'-Text direction

Multiple mode

PropTypeDefaultDescription
type'multiple'-Allows multiple items to be open simultaneously
valuestring[]-Controlled array of open item values
defaultValuestring[]-Initial array of open item values (uncontrolled)
onValueChange(value: string[]) => void-Called when the open items change
dir'ltr' | 'rtl'-Text direction

Exposes data-orientation (vertical) on the root element.


Accordion.Item

A single collapsible section. Must be a direct child of Accordion.Root. Renders a <div>.

PropTypeDefaultDescription
valuestring-Unique identifier for the item, used to control open state
disabledbooleanfalsePrevents the item from being opened or closed

Exposes data-state (open | closed) and data-disabled attributes.


Accordion.Trigger

The button that toggles the associated content panel. Must be a descendant of Accordion.Item. Renders a <button>.

Inherits all standard <button> props.

Exposes data-state (open | closed) and data-disabled attributes. Sets aria-expanded and aria-controls automatically.


Accordion.Content

The collapsible panel that contains the item's content. Must be a descendant of Accordion.Item. Renders a <div>.

PropTypeDefaultDescription
forceMountbooleanfalseKeep the content mounted in the DOM even when closed

Exposes data-state (open | closed) attributes. Sets role="region" and aria-labelledby automatically.


Keyboard Interaction

KeyDescription
EnterToggles the focused item open or closed
SpaceToggles the focused item open or closed
ArrowDownMoves focus to the next trigger
ArrowUpMoves focus to the previous trigger
HomeMoves focus to the first trigger
EndMoves focus to the last trigger