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

# Collapsible

Collapsible 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 Collapsible from "@rightui/ui"
```

## Usage

Import Collapsible from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, Checkbox from "@rightui/ui"
use Button, Alert, Collapsible from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state details = false
  action setDetails(v: bool)
    details = v
  action openExample
    details = true
  state settingsEnabled = true
  state filesOpen = false
  state sourceOpen = false
  action changeSettings(v: bool)
    settingsEnabled = v
  action changeFiles(v: bool)
    filesOpen = v
  action changeSource(v: bool)
    sourceOpen = v
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Button(id="collapsible-open", label="Open collapsible", variant="outline", disabled=false, press=openExample)
      Collapsible(id="catalog-details", label=(details ? "Hide details" : "Show details"), open=details, reduced=reduced, change=setDetails)
        Alert(id="catalog-details-alert", title="Content on demand", description="Cette section peut contenir vos propres composants.", variant="default")
      text "Settings and nested files" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Collapsible(id="settings-panel", label="Settings", open=details, reduced=reduced, change=setDetails)
        Checkbox(id="settings-enabled", label="Enable notifications", checked=settingsEnabled, disabled=false, change=changeSettings)
      Collapsible(id="files-root", label="Project files", open=filesOpen, reduced=reduced, change=changeFiles)
        Collapsible(id="files-source", label="Source folder", open=sourceOpen, reduced=reduced, change=changeSource)
          text "index.contract" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```

## API Reference

| Property | Type |
| --- | --- |
| Collapsible.id | string |
| Collapsible.label | string |
| Collapsible.open | bool |
| Collapsible.reduced | bool |
| Collapsible.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

```
