Accordion
Opening an item lifts it off the page and springs its height open. Click again mid-flight and it reverses from where it is, without a jump.
npx shadcn@latest add https://captain-ui.vercel.app/r/accordion.jsonOnly if you allow it for that repository. By default an agent opens a pull request and waits; you merge it, or it merges itself once the checks you chose have passed.
The agent stops at the end of its current step, commits what it has to a branch and tells you what is left. A $12.50 cap with four agents in parallel means a worst case of $50.00.
In a fresh sandbox per run, with your repository cloned and nothing else. Secrets come from your vault at run time and are never written to disk or to the log.
Every run works on its own branch, so undoing it is closing the pull request. Runs that touched a database record each migration, and you can roll one back from the run page.
"use client";
// Captain UI · Accordion, MIT. Opening lifts the item off the page; its height is a spring you can reverse mid-flight.
import { motion, useReducedMotion } from "motion/react";
import { useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { EASE, cx } from "@/registry/captain/ui/captain";
import { Icon } from "@/registry/captain/ui/icons";
export type AccordionItem = { value: string; title: ReactNode; content: ReactNode; disabled?: boolean };
type AccordionProps = {
items: AccordionItem[];
/** "single" keeps one open at a time; "multiple" lets any number open. */
type?: "single" | "multiple";
value?: string[];
defaultValue?: string[];
onValueChange?: (value: string[]) => void;
/** Single only: whether the open item can be closed again. */
collapsible?: boolean;
className?: string;
};
const HEIGHT = { type: "spring", stiffness: 420, damping: 40, mass: 0.9 } as const;
export function Accordion({ items, type = "single", value, defaultValue = [], onValueChange, collapsible = true, className }: AccordionProps) {
const [inner, setInner] = useState<string[]>(defaultValue);
const open = value ?? inner;
const root = useRef<HTMLDivElement>(null);
const reduce = useReducedMotion();
const id = useId();
const toggle = (v: string) => {
const isOpen = open.includes(v);
let next: string[];
if (type === "multiple") next = isOpen ? open.filter((o) => o !== v) : [...open, v];
else next = isOpen ? (collapsible ? [] : open) : [v];
if (value === undefined) setInner(next);
onValueChange?.(next);
};
const key = (e: KeyboardEvent<HTMLButtonElement>) => {
const all = Array.from(root.current?.querySelectorAll<HTMLButtonElement>("[data-accordion-trigger]:not(:disabled)") ?? []);
const at = all.indexOf(e.currentTarget);
const to =
e.key === "ArrowDown" ? (at + 1) % all.length : e.key === "ArrowUp" ? (at - 1 + all.length) % all.length : e.key === "Home" ? 0 : e.key === "End" ? all.length - 1 : -1;
if (to < 0) return;
e.preventDefault();
all[to]?.focus();
};
return (
<div ref={root} className={cx("flex flex-col gap-1", className)}>
{items.map((item, i) => {
const on = open.includes(item.value);
const head = `${id}-h${i}`;
const body = `${id}-b${i}`;
return (
<div key={item.value} className="relative">
<motion.div
aria-hidden
className="raised absolute inset-0 rounded-inner"
initial={false}
animate={{ opacity: on ? 1 : 0, scale: on || reduce ? 1 : 0.985 }}
transition={{ duration: 0.22, ease: EASE }}
/>
<h3 className="relative">
<button
id={head}
type="button"
data-accordion-trigger
aria-expanded={on}
aria-controls={body}
disabled={item.disabled}
onClick={() => toggle(item.value)}
onKeyDown={key}
className={cx(
"flex w-full items-center gap-4 rounded-inner px-4 py-3.5 text-left text-[14px] font-medium text-ink transition-colors duration-150 disabled:cursor-not-allowed disabled:opacity-45",
!on && "hover:bg-hover",
"focus-visible:bg-hover",
)}
>
<span className="flex-1">{item.title}</span>
<motion.span
className="shrink-0 text-ink-3"
initial={false}
animate={{ rotate: on ? 180 : 0 }}
transition={reduce ? { duration: 0 } : HEIGHT}
>
<Icon name="chevron" />
</motion.span>
</button>
</h3>
<motion.div
id={body}
role="region"
aria-labelledby={head}
inert={!on}
initial={false}
animate={{ height: on ? "auto" : 0 }}
transition={reduce ? { duration: 0 } : HEIGHT}
className="relative overflow-hidden"
>
<motion.div
initial={false}
animate={{ opacity: on ? 1 : 0, y: on || reduce ? 0 : -6 }}
transition={{ duration: on ? 0.24 : 0.14, ease: EASE, delay: on ? 0.04 : 0 }}
className="px-4 pb-4 text-[13.5px] leading-relaxed text-ink-2"
>
{item.content}
</motion.div>
</motion.div>
</div>
);
})}
</div>
);
}Usage
import { Accordion } from "@/components/ui/accordion"
<Accordion
type="multiple"
items={[{ value: "cap", title: "What happens at the spend cap?", content: "The agent stops after its current step." }]}
/>Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| items | { value: string; title: ReactNode; content: ReactNode; disabled?: boolean }[] | The items. | |
| type | "single" | "multiple" | "single" | One open at a time, or any number. |
| value | string[] | Controlled open items. | |
| defaultValue | string[] | [] | Initially open items when uncontrolled. |
| onValueChange | (value: string[]) => void | Called when an item opens or closes. | |
| collapsible | boolean | true | Single only: whether the open item can close. |
Keyboard
- Enter / Space
- Open or close
- ↑ ↓
- Previous / next item
- Home / End
- First / last item
Accessibility
- Each title is a button inside a heading, with aria-expanded and aria-controls.
- Each panel is a region labelled by its title, and is inert while closed so its links cannot be tabbed into.
- Reduced motion drops the spring and the lift; the panel opens at once.