# GlassAccordion

> A list of glass pills that each open into a card: the pill grows in width and height into a card with its content while the others make room, and up and down buttons step from card to card. A block of tiles that part is its other form.

```tsx
import {
  GlassAccordion,
  GlassAccordionClose,
  GlassAccordionContent,
  GlassAccordionItem,
  GlassAccordionList,
  GlassAccordionNext,
  GlassAccordionPrevious,
  GlassAccordionTrigger,
} from '@/components/liquid-glass/glass-accordion'

const COLS = 15
const ROWS = 13

/** 1 around the middle of the grid, falling to 0 at its edge. */
const fade = (col: number, row: number) => {
  const distance = Math.hypot((col - (COLS - 1) / 2) / (COLS / 2), (row - (ROWS - 1) / 2) / (ROWS / 2))
  return Math.min(1, Math.max(0, (1 - distance) / 0.45))
}

const pillars = [
  { value: 'shahada', title: 'Shahada', text: 'The testimony that there is no god but Allah, and that Muhammad is His messenger.' },
  { value: 'salah', title: 'Salah', text: 'Five prayers a day, at dawn, noon, afternoon, sunset and night, facing the Kaaba.' },
  { value: 'zakat', title: 'Zakat', text: 'One fortieth of the savings held for a year, given to those in need.' },
  { value: 'sawm', title: 'Sawm', text: 'The fast of Ramadan: no food or drink from dawn until sunset, for a month.' },
  { value: 'hajj', title: 'Hajj', text: 'The pilgrimage to Mecca, once in a lifetime for those who are able to make it.' },
]

// Press a pill: it grows into a card with its text while the others make room. The arrows step
// from card to card without closing one; the cross, or Escape, hands the card back to its pill.
// Behind them, only the Press demo's fine grid on the page's own colour, centred on the list:
// plain DOM, every cell draws its own top and left line, fainter towards the edge.
export default function Demo() {
  return (
    <GlassAccordion className="flex h-[34rem] max-w-xl items-center gap-4 px-4 sm:gap-6">
      <div aria-hidden className="absolute top-1/2 left-1/2 -z-10 grid -translate-x-1/2 -translate-y-1/2 grid-cols-[repeat(15,40px)]">
        {Array.from({ length: COLS * ROWS }, (_, i) => (
          <span key={i} className="relative size-10" style={{ opacity: fade(i % COLS, Math.floor(i / COLS)) }}>
            <span className="absolute inset-x-0 top-0 h-px bg-black/10 dark:bg-white/10" />
            <span className="absolute inset-y-0 left-0 w-px bg-black/10 dark:bg-white/10" />
          </span>
        ))}
      </div>
      <div className="flex flex-col gap-5">
        <GlassAccordionPrevious aria-label="Previous pillar" />
        <GlassAccordionNext aria-label="Next pillar" />
      </div>
      <GlassAccordionList aria-label="The five pillars of Islam">
        {pillars.map((pillar) => (
          <GlassAccordionItem key={pillar.value} value={pillar.value}>
            <GlassAccordionTrigger>{pillar.title}</GlassAccordionTrigger>
            <GlassAccordionContent>
              <p>
                <span className="font-medium">{pillar.title}.</span> {pillar.text}
              </p>
            </GlassAccordionContent>
          </GlassAccordionItem>
        ))}
      </GlassAccordionList>
      <GlassAccordionClose aria-label="Close" className="absolute end-4 top-4" />
    </GlassAccordion>
  )
}
```

**Use it for**

- A short list of things to look at one at a time, each with a few lines to say: the five pillars of Islam, the features of a product on a landing page, the steps of a tour beside a picture.
- Questions and answers, the sections of a settings page, the details of an order: use the tiles (`variant="tiles"`).

**Not for**

- Switching between views of the same thing, where one is always shown: use Tabs.
- Actions, or a value to pick: use Menu, Dropdown Menu or Select.
- Content everyone needs to read: do not hide it behind a press.

```bash
npx shadcn@latest add https://liquidglass.onemorethink.dev/r/accordion.json
```

> The behaviour (which items are open, the keyboard, the headings, ARIA, find in page) comes from UI SDK, so Accordion needs `@ui-sdk/core` and `@ui-sdk/react`. The install command adds them at the exact version this was built on: UI SDK is in beta and its API may still change.

## Usage

```tsx
import {
  GlassAccordion,
  GlassAccordionClose,
  GlassAccordionContent,
  GlassAccordionItem,
  GlassAccordionList,
  GlassAccordionNext,
  GlassAccordionPrevious,
  GlassAccordionTrigger,
} from '@/components/liquid-glass/glass-accordion'

<GlassAccordion>
  <GlassAccordionPrevious />
  <GlassAccordionNext />

  <GlassAccordionList>
    <GlassAccordionItem value="salah">
      <GlassAccordionTrigger>Salah</GlassAccordionTrigger>
      <GlassAccordionContent>Five prayers a day, facing the Kaaba.</GlassAccordionContent>
    </GlassAccordionItem>
    <GlassAccordionItem value="sawm">
      <GlassAccordionTrigger>Sawm</GlassAccordionTrigger>
      <GlassAccordionContent>The fast of Ramadan, from dawn until sunset.</GlassAccordionContent>
    </GlassAccordionItem>
  </GlassAccordionList>

  <GlassAccordionClose />
</GlassAccordion>
```

Closed, every item is a pill as wide as its label. Press one and it grows into a card over 0.45 s (0.3 s with `tempo="quick"`, shrinking back in 0.25 s), width and height together, while the pills above and below make room. Its label leaves at once; the content arrives once the card has room for it, rising a little. Press another pill and the two change places. The motion is that of Chánh Đại's Apple Accordion (MIT), written on the engine's frame so each pill's glass is drawn at the size the pill has on that frame.

A card is closed by Escape, by `GlassAccordionClose`, or by opening another: its own label is gone while it is open. `GlassAccordionPrevious` and `GlassAccordionNext` open the item above or below without closing the card, and skip disabled items. The three buttons show only while a card is open and are yours to place, with classes; leave them out if you do not need them. The items go inside `GlassAccordionList`, and a card is at most 400 px wide and never wider than the accordion less 80 px. `value` and `onValueChange` say which card is open (a list, as UI SDK keeps it), to show something beside the list that goes with it.

The pills wear the glass of everything that opens over the page, Dialog's pane among them: a 60% tint (white, or black on a dark page), a 10 px frost and a rim that bends the page close to the edge. So put the accordion over something: a picture, a coloured section, a page of text. `tint`, `blur` and `optics` on `GlassAccordion` set your own, and the text follows the tone of the glass.

## Tiles

### A block of tiles that part (variant="tiles")

```tsx
import { GlassAccordion, GlassAccordionContent, GlassAccordionItem, GlassAccordionTrigger } from '@/components/liquid-glass/glass-accordion'

const COLS = 13
const ROWS = 11

/** 1 around the middle of the grid, falling to 0 at its edge. */
const fade = (col: number, row: number) => {
  const distance = Math.hypot((col - (COLS - 1) / 2) / (COLS / 2), (row - (ROWS - 1) / 2) / (ROWS / 2))
  return Math.min(1, Math.max(0, (1 - distance) / 0.45))
}

const questions = [
  {
    value: 'hours',
    question: 'When does the fast begin and end?',
    answer: 'It begins at dawn, with the call to the fajr prayer, and ends at sunset, with the call to maghrib.',
  },
  {
    value: 'excused',
    question: 'Who may leave the fast?',
    answer: 'The sick and the traveller may leave it and make up the days later. Those who cannot fast at all feed a person in need for each day.',
  },
  {
    value: 'fitr',
    question: 'What is zakat al-fitr?',
    answer: 'A small charity given for every member of the household before the Eid prayer, so that no one goes without on the day of Eid.',
  },
  {
    value: 'zakat',
    question: 'How is zakat worked out?',
    answer: 'One fortieth of the savings above the nisab that have been held for a lunar year.',
  },
]

// Press a question: its tile steps away from the block, its corners round, and its height springs
// open a little past its size while the answer clears from a blur. Press another and the two
// move together. Behind the tiles, only the Press demo's fine grid on the page's own colour,
// centred on them: plain DOM, every cell draws its own top and left line, fainter towards the edge.
export default function Demo() {
  return (
    <div className="relative isolate flex h-[30rem] w-full max-w-xl items-center justify-center">
      <div aria-hidden className="absolute top-1/2 left-1/2 -z-10 grid -translate-x-1/2 -translate-y-1/2 grid-cols-[repeat(13,40px)]">
        {Array.from({ length: COLS * ROWS }, (_, i) => (
          <span key={i} className="relative size-10" style={{ opacity: fade(i % COLS, Math.floor(i / COLS)) }}>
            <span className="absolute inset-x-0 top-0 h-px bg-black/10 dark:bg-white/10" />
            <span className="absolute inset-y-0 left-0 w-px bg-black/10 dark:bg-white/10" />
          </span>
        ))}
      </div>
      <GlassAccordion variant="tiles" defaultValue={['hours']} className="max-w-md">
        {questions.map((item) => (
          <GlassAccordionItem key={item.value} value={item.value}>
            <GlassAccordionTrigger>{item.question}</GlassAccordionTrigger>
            <GlassAccordionContent>{item.answer}</GlassAccordionContent>
          </GlassAccordionItem>
        ))}
      </GlassAccordion>
    </div>
  )
}
```

`variant="tiles"` is the accordion for questions and answers. Its items are the accordion's own children, with no list and no buttons. Closed, the tiles lie against each other as one block: only its outer corners are round. Press a question and its tile steps 10 px away from its neighbours, all four of its corners round, and the corners that face it round with them. Its height opens on a spring that goes about a tenth past its size and settles; closing, it sinks as far under its rest height and comes back. The answer clears from a 2 px blur as it fades in. Pressing the open tile closes it, and `bounce={false}` lets the height settle without going past its size.

```tsx
<GlassAccordion variant="tiles" defaultValue={['hours']}>
  <GlassAccordionItem value="hours">
    <GlassAccordionTrigger>When does the fast begin and end?</GlassAccordionTrigger>
    <GlassAccordionContent>From dawn until sunset.</GlassAccordionContent>
  </GlassAccordionItem>
  <GlassAccordionItem value="fitr">
    <GlassAccordionTrigger>What is zakat al-fitr?</GlassAccordionTrigger>
    <GlassAccordionContent>A charity given before the Eid prayer.</GlassAccordionContent>
  </GlassAccordionItem>
</GlassAccordion>
```

## More than one open

One item is open at a time. `multiple` lets several stay open: each pill is its own card, and each open tile stands apart. `collapsible={false}` keeps one item open at all times. `value` and `onValueChange` control it; the value is always a list of the open items.

```tsx
const [open, setOpen] = useState(['profile'])

<GlassAccordion variant="tiles" multiple value={open} onValueChange={setOpen}>
  <GlassAccordionItem value="profile">…</GlassAccordionItem>
  <GlassAccordionItem value="billing">…</GlassAccordionItem>
  <GlassAccordionItem value="team" disabled>…</GlassAccordionItem>
</GlassAccordion>
```

## Content the page can find

Closed content leaves the page. `keepMounted` keeps it in the HTML, hidden, so a crawler that runs no script still reads it; `hiddenUntilFound` also lets the browser's find in page match its text, and the item opens when it does. Set either on `GlassAccordion` for every item, or on one `GlassAccordionContent`.

> Speed: the preview above has a Default / Quick switch. Quick plays Emil Kowalski's timings; the default is the speed the glass was made with. Set it on one component with `tempo`, or for the whole page once with `setGlassTempo('quick')`; the prop wins over the page. Nothing extra to install.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `tempo` | `'default' \| 'quick'` | `'default'` | On `GlassAccordion`: `quick`: a card opens in 0.3 s and closes in 0.25 instead of 0.45, and the tiles move on a stiffer spring (Emil Kowalski's timings). Left out: the page's tempo (`setGlassTempo`), else `default`, the speed it was made with. |
| `variant` | `'pills' \| 'tiles'` | `'pills'` | Pills that each open into a card, or a block of tiles that part to let one open. |
| `value / defaultValue / onValueChange` | `string[] / string[] / (value: string[], details) => void` | — | On `GlassAccordion`: the values of the open items. Control them yourself, or let the accordion keep its own state. |
| `multiple` | `boolean` | `false` | Whether several items can be open at once. |
| `collapsible` | `boolean` | `true` | Whether the open item can close. `false` keeps one open. |
| `disabled` | `boolean` | `false` | Stops every item from opening or closing. On `GlassAccordionItem`: that item. |
| `bounce` | `boolean` | `true` | Tiles: whether the height goes a little past its size before it settles. |
| `tint / blur / optics` | `number / number / GlassOptics` | `0.6 / 10.25` | The glass, as on Card: how much tint covers the page, how much it is frosted (px), and the fine-tuning. |
| `keepMounted / hiddenUntilFound` | `boolean` | `false` | Keep closed content in the page, hidden; let find in page open it. Also on `GlassAccordionContent`, for one item. |
| `controller` | `AccordionController` | — | From `createAccordionController()`: opens, closes and toggles items from anywhere (`open(value)`, `closeAll()`). |
| `GlassAccordionList className` | `string` | — | Pills: the column the items stand in (`flex flex-col items-start gap-3`). |
| `GlassAccordionItem value` | `string` | — | Names the item in the accordion's `value`; the up and down buttons step only to items that have one. `onOpenChange` runs before it opens or closes. |
| `GlassAccordionItem className` | `string` | — | Classes for the pill (its corners are 28 px; its size belongs to the motion) or for the tile (`px-6` by default; its 18 px corners and the 10 px around an open tile belong to the motion). |
| `GlassAccordionTrigger / GlassAccordionContent className` | `string` | — | Classes for the label (a pill's: `h-14 ps-3.5 pe-8 text-base font-medium`) and for the content (a card's: `p-7 text-base`). |
| `GlassAccordionPrevious / Next / Close` | `button props` | — | Pills: the kit's own `GlassButton`, as 36 px discs. `className` places them, `aria-label` names them in your language (default `Previous`, `Next`, `Close`), and `children` replaces the icon. |

## Accessibility

- Every label is a `<button>` inside a heading (`h3`), with `aria-expanded` and `aria-controls`; its content is a `region` named by the label.
- Pills: focus moves into the card as it opens, and Escape or Close returns it to the pill. The up and down buttons keep focus, so pressing one again keeps stepping; at an end they are `aria-disabled`, not removed.
- Tiles: Tab moves from question to question; Enter and Space open and close.
- Closed content is out of the page, or hidden with `keepMounted`, so it is never read or focused while closed.
- With reduced motion a card or a tile takes its size at once and the content only fades. Without WebGL2 the pills and the tiles are plain frosted panels.

Source: https://liquidglass.onemorethink.dev/docs/accordion
