Accordion
List of expandable items built from an array, in three variants, with a chevron or plus icon and optional single-open behavior.
pnpm dlx shadcn@latest add @haze/accordion---
import { ChevronDown, Plus } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Item {
title: string;
content: string;
open?: boolean;
}
interface Props {
items: Item[];
/** When set, only one item is open at a time. */
name?: string;
/** contained: one bordered panel. plain: hairlines only, for long FAQ lists. separated: each item is its own panel. */
variant?: "contained" | "plain" | "separated";
/** chevron turns over when the item opens. plus turns into a cross. */
icon?: "chevron" | "plus";
class?: string;
}
const { items, name, variant = "contained", icon = "chevron", class: className } = Astro.props;
const roots = {
contained: "divide-y divide-border overflow-hidden rounded-xl border border-border bg-card",
plain: "divide-y divide-border border-y border-border",
separated: "grid gap-2",
};
const itemLooks = {
contained: "",
plain: "",
separated:
"overflow-hidden rounded-xl border border-border bg-card transition-[border-color] duration-200 ease-snappy open:border-border-strong",
};
const summaries = {
contained: "px-4 [details:not([open])>&]:hover:bg-accent",
plain: "px-0 hover:text-foreground/80",
separated: "px-4 [details:not([open])>&]:hover:bg-accent",
};
const bodies = { contained: "px-4", plain: "px-0", separated: "px-4" };
// Where the browser can animate to height:auto, the panel slides open and shut. Elsewhere its content rises in.
const slide =
"supports-[interpolate-size:allow-keywords]:[interpolate-size:allow-keywords] supports-[interpolate-size:allow-keywords]:details-content:h-0 supports-[interpolate-size:allow-keywords]:details-content:overflow-clip supports-[interpolate-size:allow-keywords]:details-content:transition-[height,content-visibility] supports-[interpolate-size:allow-keywords]:details-content:transition-discrete supports-[interpolate-size:allow-keywords]:details-content:duration-300 supports-[interpolate-size:allow-keywords]:details-content:ease-snappy supports-[interpolate-size:allow-keywords]:open:details-content:h-auto";
const Icon = icon === "plus" ? Plus : ChevronDown;
---
<div class={cn(roots[variant], className)}>
{
items.map((item) => (
<details name={name} open={item.open} class={cn(slide, itemLooks[variant])}>
<summary
class={cn(
"flex min-h-11 cursor-pointer list-none items-center justify-between gap-3 py-3 text-[0.8125rem]/5 font-medium text-foreground transition-colors duration-150 ease-snappy focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring [&::-webkit-details-marker]:hidden",
summaries[variant],
)}
>
<span>{item.title}</span>
<Icon
class={cn(
"size-4 shrink-0 text-muted-foreground transition-[rotate,color] duration-200 ease-spring motion-reduce:transition-none [[open]>summary_&]:text-foreground",
icon === "plus" ? "[[open]>summary_&]:rotate-45" : "[[open]>summary_&]:rotate-180",
)}
aria-hidden="true"
/>
</summary>
<div class={cn("pb-4 text-[0.8125rem]/5 text-muted-foreground not-supports-[interpolate-size:allow-keywords]:animate-rise", bodies[variant])}>
<p>{item.content}</p>
</div>
</details>
))
}
</div>When to use
- A short list of questions and answers or settings groups, written as plain text
- Keeping a long help page scannable with one open item at a time
- Content that comes from data rather than from hand-written markup
When not to use
- Items need rich content such as links or images; stack collapsible components instead
- A full-width FAQ section; use the faq blocks
Installation
Add the Haze registry to components.json once, then install Accordion with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/accordionnpx shadcn@latest add @haze/accordionyarn dlx shadcn@latest add @haze/accordionbunx --bun shadcn@latest add @haze/accordionFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| items* | { title: string; content: string; open?: boolean }[] | – | One entry per row; content renders as one paragraph. |
| name | string | – | Shared details name; when set, opening one item closes the others. |
| variant | "contained" | "plain" | "separated" | "contained" | contained: one bordered panel with dividers. plain: hairlines only, for long FAQ lists. separated: each item is its own panel. |
| icon | "chevron" | "plus" | "chevron" | chevron turns over when the item opens. plus turns into a cross. |
| class | string | – | Extra classes on the outer element. |
Keyboard interactions
- EnterSpace
- Open or close the focused item
- Tab
- Move to the next item's summary
Accessibility
- Each row is a native details element, so the summary is announced as an expandable button without extra ARIA.
- In the contained and plain variants rows are separated by hairline dividers, so the group reads as a single list; in separated each row is its own panel.
- The panel animates its height over 300 ms where the browser supports interpolate-size; elsewhere its content rises in. Both are switched off under prefers-reduced-motion.
- With a name set, the browser closes the previous item itself and no script runs.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Accordion" with `npx shadcn@latest add @haze/accordion`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Accordion from "@/components/haze/ui/accordion.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/accordion.md
Customize with real content
Adapt the Haze accordion ui for our product. Replace the demo values of these props with our real content: items, name, variant, icon, class. Keep the layout, the heading level rules and these accessibility details: Each row is a native details element, so the summary is announced as an expandable button without extra ARIA. In the contained and plain variants rows are separated by hairline dividers, so the group reads as a single list; in separated each row is its own panel. The panel animates its height over 300 ms where the browser supports interpolate-size; elsewhere its content rises in. Both are switched off under prefers-reduced-motion. With a name set, the browser closes the previous item itself and no script runs. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using accordion, check it fits: use it when a short list of questions and answers or settings groups, written as plain text; keeping a long help page scannable with one open item at a time; content that comes from data rather than from hand-written markup. Avoid it when items need rich content such as links or images; stack collapsible components instead; a full-width faq section; use the faq blocks. Alternatives: @haze/collapsible, @haze/tabs.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.