---
title: "Accordion"
description: "Accordion with accessible presentation and local state."
---

# Accordion

Accordion with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Accordion, AccordionOptions, AccordionMultiple, AccordionMultipleOptions from "@rightui/ui"
```

## Usage

Import Accordion or AccordionMultiple from @rightui/ui and control expanded through change. Use AccordionOptions or AccordionMultipleOptions for disabledItems and collapsible. Pass every prop explicitly and forward the reduced-motion preference.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, Accordion from "@rightui/ui"
use AccordionMultiple, AccordionOptions, CardFrame, CardHeader, CardTitle, CardDescription, CardContent from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state expanded: number[] = [0]
  action setExpanded(value: number[])
    expanded = value
  state singleExpanded = 0
  action setSingleExpanded(v: number)
    singleExpanded = v
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      AccordionMultiple(id="advanced-accordion", titles=["Is it accessible?", "Is it styled?", "Is it animated?"], descriptions=["Yes. It supports keyboard navigation and exposes each panel state.", "Yes. It follows the shared RightUI design tokens.", "Yes. It uses the same height animation and reduced-motion preference."], expanded=expanded, reduced=reduced, change=setExpanded)
      text "Single expansion" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Accordion(id="single-expansion", titles=["Overview", "Details"], descriptions=["One panel at a time.", "Select a heading to expand."], expanded=singleExpanded, reduced=reduced, change=setSingleExpanded)
      text "Disabled item and required expansion" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      AccordionOptions(id="accordion-disabled", titles=["Account history", "Premium information", "Email preferences"], descriptions=["Review your account activity.", "This item is disabled.", "Choose which updates you receive."], expanded=singleExpanded, disabledItems=[1], collapsible=false, reduced=reduced, change=setSingleExpanded)
      CardFrame(id="accordion-card")
        CardHeader(id="accordion-card-header")
          CardTitle(id="accordion-card-title", title="RightUI components", level=3)
          CardDescription(id="accordion-card-description", description="Common questions about composition and accessibility.")
        CardContent(id="accordion-card-content")
          Accordion(id="accordion-card-items", titles=["Can I compose components?", "Does it follow my theme?"], descriptions=["Use public RightUI components inside your Exact views.", "Shared tokens control colours, spacing and typography."], expanded=singleExpanded, reduced=reduced, change=setSingleExpanded)

```

## API Reference

| Property | Type |
| --- | --- |
| AccordionMultiple.id | string |
| AccordionMultiple.titles | string[] |
| AccordionMultiple.descriptions | string[] |
| AccordionMultiple.expanded | number[] |
| AccordionMultiple.reduced | bool |
| AccordionMultiple.change | action |
| AccordionMultipleOptions.id | string |
| AccordionMultipleOptions.titles | string[] |
| AccordionMultipleOptions.descriptions | string[] |
| AccordionMultipleOptions.expanded | number[] |
| AccordionMultipleOptions.disabledItems | number[] |
| AccordionMultipleOptions.collapsible | bool |
| AccordionMultipleOptions.reduced | bool |
| AccordionMultipleOptions.change | action |
| Accordion.id | string |
| Accordion.titles | string[] |
| Accordion.descriptions | string[] |
| Accordion.expanded | number |
| Accordion.reduced | bool |
| Accordion.change | action |
| AccordionOptions.id | string |
| AccordionOptions.titles | string[] |
| AccordionOptions.descriptions | string[] |
| AccordionOptions.expanded | number |
| AccordionOptions.disabledItems | number[] |
| AccordionOptions.collapsible | bool |
| AccordionOptions.reduced | bool |
| AccordionOptions.change | action |

## Source

File: packages/ui/disclosure.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use uiPopupIn from "./popup-motion.contract"
use uiReveal, uiPopover from "./motion.contract"
use Control from "./styles.contract"
use DisclosureButton from "./button.contract"
use AccordionPanel from "./accordion-panel.contract"
component Accordion
  props
    id: string
    titles: string[]
    descriptions: string[]
    expanded: number
    reduced: bool
    change: action
  view
    AccordionOptions(id=id, titles=titles, descriptions=descriptions, expanded=expanded, disabledItems=[], collapsible=true, reduced=reduced, change=change)

component AccordionOptions
  props
    id: string
    titles: string[]
    descriptions: string[]
    expanded: number
    disabledItems: number[]
    collapsible: bool
    reduced: bool
    change: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state hovered = -1
  action setHover(i: number, inside: bool)
    hovered = inside ? i : -1
  action toggle(i: number)
    if not disabledItems.includes(i) and (collapsible or expanded != i)
      change(expanded == i ? -1 : i)
  action keys(i: number, k: string)
    if k == "ArrowDown" or k == "ArrowUp" or k == "Home" or k == "End"
      preventDefault()
      let enabled = titles.map((title, item) => item).filter(item => not disabledItems.includes(item))
      let before = enabled.filter(item => item < i)
      let after = enabled.filter(item => item > i)
      let next = k == "Home" ? enabled.at(0) : k == "End" ? enabled.at(enabled.length - 1) : k == "ArrowDown" ? (after.length > 0 ? after.at(0) : enabled.at(0)) : (before.length > 0 ? before.at(before.length - 1) : enabled.at(enabled.length - 1))
      if enabled.length > 0
        let target = match next { case some(item) => item, case none => i }
        focus(id + "-trigger-" + target.toString())
  view
    column id=id testId=id
      for title, i in titles key=title
        column border-bottom-width=(i == titles.length - 1 ? "0px" : "ui.border-width") border-bottom-style=(i == titles.length - 1 ? "solid" : "solid") border-bottom-color=(i == titles.length - 1 ? "transparent" : "ui.border") min-width="0px"
          button id=(id + "-trigger-" + i.toString()) testId=(id + "-trigger-" + i.toString()) class=Control font-weight=uiWeights.medium disabled=disabledItems.includes(i)
            aria-expanded=(expanded == i) aria-controls=(id + "-panel-" + i.toString()) press=toggle(i) key=keys(i) hover=setHover(i)
            display="flex" align-items="flex-start" justify-content="space-between" gap="ui.space-16" padding="ui.space-16" border="0" background-color="transparent" color="ui.foreground"
            :hover
              background-color="transparent"
            :active
              background-color="transparent"
            text title flex=1 min-width="0px" text-align="left" text-decoration=(hovered == i ? "underline" : "none") font-size="ui.text-sm" font-weight=uiWeights.medium font-family=uiFontFamily(uiFont) data-rightui-font="body"
            svg width="ui.space-16" height="ui.space-16" margin-top="ui.space-2" flex-shrink=0 viewBox="0 0 24 24" fill="none" stroke="ui.muted-foreground" stroke-width="ui.stroke-standard" rotate=(expanded == i ? "180deg" : "0deg") transition=(reduced ? "none" : "rotate ui.duration-normal ui.easing") aria-hidden=true
              path d="m6 9 6 6 6-6"
          AccordionPanel(id=id + "-panel-" + i.toString(), labelledBy=id + "-trigger-" + i.toString(), open=expanded == i, reduced=reduced)
            column padding="0px ui.space-16 ui.space-16" min-width="0px" flex-shrink=0
              match descriptions.at(i)
                case some(description)
                  text description font-size="ui.text-sm" line-height="ui.leading-normal" color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
                case none
                  text "" font-size="ui.text-sm" font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Collapsible
  props
    id: string
    label: string
    open: bool
    reduced: bool
    change: action
  inject
    uiFont: string
  slot
  action toggle
    change(not open)
  view
    column gap="0px" min-width="0px"
      DisclosureButton(id=id, label=label, variant="outline", controls=id + "-content", expanded=open, icon=false, press=toggle)
        text label font-family=uiFontFamily(uiFont) data-rightui-font="body"
      column id=(id + "-content") testId=(open ? id + "-content" : "") height=(open ? "auto" : "0px") min-height="0px" overflow="hidden" aria-hidden=(not open) inert=(not open)
        -exact-layout-transition=(reduced ? "none" : "ui.duration-normal ui.easing")
        column padding-top="ui.space-12" gap="ui.space-8" min-width="0px"
          children

component PopoverFrame
  props
    id: string
    source: string
    label: string
    align: string
    reduced: bool
    dismiss: action
  slot
  action changed(e: ToggleEvent)
    if e.newState == "closed"
      dismiss()
  action keys(k: string, e: KeyboardEvent)
    if k == "Escape" and not e.isComposing
      preventDefault()
      stopPropagation()
      dismiss()
      focus(source)
  view
    column id=id testId=id role="dialog" aria-label=label tabindex=-1 popover="auto" toggle=changed keycapture=keys
      position-area=(align == "end" ? "bottom span-left" : align == "center" ? "bottom" : "bottom span-right") position-try-fallbacks="flip-inline, flip-block, flip-block flip-inline"
      animation=(reduced ? "none" : "uiPopupIn ui.duration-fast ui.easing both")
      // A top-layer popover must not become an absolutely positioned exit ghost.
      width="18rem" max-width="calc(100vw - ui.space-32)" max-height="min(30rem, 70vh)" min-width="0px" overflow="auto"
      box-sizing="border-box" margin="ui.space-4 0px" padding="ui.space-16" gap="ui.space-16" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-popover" background-color="ui.surface" color="ui.foreground" box-shadow="ui.shadow-menu"
      children

component Popover
  props
    id: string
    label: string
    focusId: string
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  slot
  state expanded = false
  state openEpoch = 0
  action toggle
    if expanded
      expanded = false
    else
      openEpoch = openEpoch + 1
      expanded = true
      showPopover(id + "-content", source=id)
      focus(focusId)
  action dismissed(epoch: number)
    if epoch == openEpoch
      expanded = false
  view
    column align-items="flex-start"
      button id=id testId=id class=Control font-weight=uiWeights.medium aria-haspopup="dialog" aria-expanded=expanded aria-controls=(id + "-content") popovertarget=(expanded ? id + "-content" : "") popovertargetaction="show" press=toggle
        padding="ui.space-8 ui.space-12" border-width="ui.border-width" border-style="solid" border-color="ui.border" background-color="ui.surface" color="ui.foreground"
        text label font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if expanded
        for epoch in [openEpoch] key=epoch
          PopoverFrame(id=id + "-content", source=id, label=label, align="start", reduced=reduced, dismiss=dismissed(epoch))
            children

```
