Accordion
Sections that open one or many at a time.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add accordionOr install the package
Don’t want the file? Import from
asta-ui/accordionand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { Accordion } from "asta-ui/accordion";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useId, useState, type KeyboardEvent, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); export interface AccordionItem { id: string; title: ReactNode; content: ReactNode; } export interface AccordionProps { items: AccordionItem[]; /** `single` keeps at most one open; `multiple` lets several stay open. */ type?: "single" | "multiple"; defaultOpen?: string[]; className?: string; } /** * Questions that open one at a time (or several, with `type="multiple"`). Plain buttons and regions, no * library: arrows, Home and End move between questions, and closed panels are `inert`. Panels open by * easing the row from 0fr to 1fr, so nothing is measured, and the plus turns into a minus. */ export function Accordion({ items, type = "single", defaultOpen = [], className }: AccordionProps) { const [open, setOpen] = useState<string[]>(() => (type === "single" ? defaultOpen.slice(0, 1) : defaultOpen)); const uid = useId(); const toggle = (id: string) => setOpen((o) => (o.includes(id) ? o.filter((x) => x !== id) : type === "single" ? [id] : [...o, id])); const move = (e: KeyboardEvent<HTMLButtonElement>) => { const step = e.key === "ArrowDown" ? 1 : e.key === "ArrowUp" ? -1 : 0; if (!step && e.key !== "Home" && e.key !== "End") return; const root = e.currentTarget.closest("[data-kit-accordion]"); if (!root) return; const all = [...root.querySelectorAll<HTMLButtonElement>("[data-kit-acc-trigger]")].filter((b) => b.closest("[data-kit-accordion]") === root); const i = all.indexOf(e.currentTarget); const n = e.key === "Home" ? 0 : e.key === "End" ? all.length - 1 : (i + step + all.length) % all.length; e.preventDefault(); all[n]?.focus(); }; return ( <div data-kit-accordion className={cx("flex w-full flex-col gap-1.5", className)}> {items.map((it) => { const on = open.includes(it.id); const t = `${uid}-${it.id}-t`; const p = `${uid}-${it.id}-p`; return ( <div key={it.id} data-state={on ? "open" : "closed"} className={cx( "group/acc rounded-xl bg-paper-2 transition-[background-color,box-shadow] duration-300", "data-[state=open]:bg-paper data-[state=open]:shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent),0_8px_24px_-14px_rgb(0_0_0/0.25)]", )} > <h3> <button id={t} type="button" data-kit-acc-trigger aria-expanded={on} aria-controls={p} onClick={() => toggle(it.id)} onKeyDown={move} className="group flex w-full items-center justify-between gap-4 rounded-xl px-4 py-3.5 text-left text-[14.5px] font-medium tracking-[-0.01em] text-ink outline-none focus-visible:ring-2 focus-visible:ring-brand/50" > {it.title} <span aria-hidden className="relative flex h-6 w-6 shrink-0 items-center justify-center rounded-full bg-paper text-ink-2 transition-colors duration-300 group-hover:text-ink group-data-[state=open]/acc:bg-ink group-data-[state=open]/acc:text-paper" > <span className="absolute h-[1.5px] w-2.5 rounded-full bg-current" /> <span className="absolute h-2.5 w-[1.5px] rounded-full bg-current transition-transform duration-500 ease-[cubic-bezier(.16,1,.3,1)] group-data-[state=open]/acc:rotate-90 group-data-[state=open]/acc:scale-y-0 motion-reduce:transition-none" /> </span> </button> </h3> <div id={p} role="region" aria-labelledby={t} inert={!on} // perf-ok: an accordion panel. The row eases from 0fr to 1fr, so nothing is measured. className="grid transition-[grid-template-rows] duration-[450ms] ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none" style={{ gridTemplateRows: on ? "1fr" : "0fr" }} > <div className="min-h-0 overflow-hidden"> <div className={cx("px-4 pb-4 text-[14px] leading-relaxed text-ink-2 transition-opacity duration-300", on ? "opacity-100" : "opacity-0")}>{it.content}</div> </div> </div> </div> ); })} </div> ); } export default Accordion;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Accordion component to my project. Sections that open one or many at a time. It includes: single, multiple. Install it: ```sh pnpm dlx asta-ui@latest add accordion --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/accordion.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { Accordion } from "@/components/astabyte/accordion"; ``` Usage example and props: https://ui.astabyte.com/accordion Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<div style="display:grid;gap:12px"> <div class="asta-accordion"><details class="asta-accordion-item" name="faq" open><summary class="asta-accordion-trigger">What is Astabyte UI?<span class="asta-accordion-icon" aria-hidden="true"><span></span><span></span></span></summary><div class="asta-accordion-content">A component kit.</div></details><details class="asta-accordion-item" name="faq"><summary class="asta-accordion-trigger">Is it free?<span class="asta-accordion-icon" aria-hidden="true"><span></span><span></span></span></summary><div class="asta-accordion-content">Yes, MIT.</div></details></div> </div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { Accordion } from "@/components/astabyte/accordion";
const FAQ = [
{ id: "own", title: "Do I own the code?", content: "Yes. The CLI copies the source into your repo. There is no package to update and nothing to override." },
{ id: "stack", title: "What does it need?", content: "React and Tailwind v4. A few motion pieces use GSAP; the kit itself has no extra dependencies." },
{ id: "dark", title: "Does it work in dark mode?", content: "Everything reads colour tokens like paper, ink and accent, so swapping the tokens re-themes every component." },
{ id: "a11y", title: "Is it accessible?", content: "Headers are real buttons with aria-expanded, panels are labelled regions, and arrow keys move between questions." },
];
export default function AccordionDemo() {
const [type, setType] = useState<"single" | "multiple">("single");
return (
<div className="relative flex min-h-[420px] w-full flex-col items-center justify-center gap-5 p-6 sm:p-10">
<div className="flex rounded-full bg-paper-2 p-1 text-[12.5px]" role="radiogroup" aria-label="Mode">
{(["single", "multiple"] as const).map((t) => (
<button
key={t}
type="button"
role="radio"
aria-checked={type === t}
onClick={() => setType(t)}
className={`h-7 rounded-full px-3 font-medium capitalize transition-colors ${type === t ? "bg-ink text-paper" : "text-mute hover:text-ink"}`}
>
{t}
</button>
))}
</div>
<Accordion key={type} type={type} items={FAQ} defaultOpen={["own"]} className="max-w-md" />
</div>
);
}