Accordion
Hand-ruled sections with a drawn chevron that turns over as they open.
Yes. Every component is built on Base UI, checked with axe in both themes, and keyboard-tested. The drawing is decoration; the semantics are the primitive's.
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
const faq = [
["Is it accessible?", "Yes. Every component is built on Base UI, checked with axe in both themes, and keyboard-tested. The drawing is decoration; the semantics are the primitive's."],
["Does it work without JavaScript?", "The page renders with every stroke already drawn. Scripts add the measuring and the drawing in."],
["Can I change the pen?", "Per component, for a section, or for the whole app: see the customizer."],
];
export default function AccordionDemo() {
return (
<Accordion defaultValue={["item-0"]} className="max-w-lg">
{faq.map(([question, answer], i) => (
<AccordionItem key={i} value={`item-${i}`}>
<AccordionTrigger seed={`acc-${i}`}>{question}</AccordionTrigger>
<AccordionContent>{answer}</AccordionContent>
</AccordionItem>
))}
</Accordion>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ accordion"use client";
import { useMemo } from "react";
import { Accordion as AccordionPrimitive } from "@base-ui/react/accordion";
import { cn } from "@/lib/utils";
import { useInkSeed } from "@/hooks/use-ink-box";
import { inkClassName, InkSvg, Stroke, inkRules } from "@/lib/ink";
import { chevronStroke } from "@/lib/ink-sketch";
function Accordion({ className, style, ...props }: AccordionPrimitive.Root.Props) {
return (
<AccordionPrimitive.Root
data-slot="accordion"
className={inkClassName("flex w-full flex-col", className)}
style={typeof style === "function" ? (state) => ({ ...inkRules, ...style(state) }) : { ...inkRules, ...style }}
{...props}
/>
);
}
/** Items are ruled off from each other by hand (see inkRules). */
function AccordionItem({ className, ...props }: AccordionPrimitive.Item.Props) {
return (
<AccordionPrimitive.Item
data-slot="accordion-item"
className={inkClassName(
"relative not-last:after:pointer-events-none not-last:after:absolute not-last:after:inset-x-0 not-last:after:-bottom-[2.5px] not-last:after:h-[5px] not-last:after:bg-ink-4 not-last:after:[mask-image:var(--ink-rule-1)] not-last:after:[mask-size:100%_100%] nth-[3n+2]:after:[mask-image:var(--ink-rule-2)] nth-[3n]:after:[mask-image:var(--ink-rule-3)]",
className,
)}
{...props}
/>
);
}
/**
* The heading row; its drawn chevron turns over when the item opens. The row
* is an `<h3>`; `header` reaches it, e.g. `header={{ render: <div /> }}` when
* the row shouldn't join the page's outline.
*/
function AccordionTrigger({
className,
children,
seed,
header,
...props
}: AccordionPrimitive.Trigger.Props & { seed?: string | number; header?: AccordionPrimitive.Header.Props }) {
return (
<AccordionPrimitive.Header {...header} className={inkClassName("flex", header?.className)}>
<AccordionPrimitive.Trigger
data-slot="accordion-trigger"
className={inkClassName(
"group/accordion-trigger relative flex flex-1 cursor-pointer items-center justify-between gap-4 py-3.5 text-left text-base font-bold transition-colors outline-none",
"hover:text-ink-2 focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-2 focus-visible:outline-ring focus-visible:[border-radius:var(--hand-radius)]",
"aria-disabled:pointer-events-none aria-disabled:opacity-50",
className,
)}
{...props}
>
{children}
<Chevron seed={seed} />
</AccordionPrimitive.Trigger>
</AccordionPrimitive.Header>
);
}
function Chevron({ seed }: { seed?: string | number }) {
const s = useInkSeed(seed);
const d = useMemo(() => chevronStroke(s, 12, 7), [s]);
return (
<span
data-slot="accordion-trigger-icon"
aria-hidden="true"
className="relative size-4 shrink-0 text-ink-3 transition-transform duration-(--dur-state) ease-out group-data-panel-open/accordion-trigger:rotate-180 motion-reduce:transition-none"
>
<InkSvg box={[-2, -4.5, 16, 16]} className="inset-0 size-full">
<Stroke d={d} width={1.6} />
</InkSvg>
</span>
);
}
function AccordionContent({ className, children, ...props }: AccordionPrimitive.Panel.Props) {
return (
<AccordionPrimitive.Panel
data-slot="accordion-content"
className="h-(--accordion-panel-height) overflow-hidden text-base text-ink-2 transition-[height] duration-(--dur-state) ease-out data-ending-style:h-0 data-starting-style:h-0 motion-reduce:transition-none"
{...props}
>
<div className={cn("pb-4 [&_a]:underline [&_a]:underline-offset-3 [&_p:not(:last-child)]:mb-4", className)}>{children}</div>
</AccordionPrimitive.Panel>
);
}
export { Accordion, AccordionItem, AccordionTrigger, AccordionContent };Usage
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
<Accordion>
<AccordionItem value="a">
<AccordionTrigger>Is it accessible?</AccordionTrigger>
<AccordionContent>Yes.</AccordionContent>
</AccordionItem>
</Accordion>Examples
Several open
With multiple, items open independently.
Orders leave the studio on Tuesdays and Fridays, wrapped in brown paper.
Cap the pen, keep it out of the sun, and never lend it.
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
const sections = [
["Shipping", "Orders leave the studio on Tuesdays and Fridays, wrapped in brown paper."],
["Returns", "Send anything back within thirty days; unused ink is always welcome."],
["Care", "Cap the pen, keep it out of the sun, and never lend it."],
];
// `multiple` lets several items stay open at once; by default opening one closes the others.
export default function AccordionMultiple() {
return (
<Accordion multiple defaultValue={["item-0", "item-2"]} className="max-w-lg">
{sections.map(([title, text], i) => (
<AccordionItem key={i} value={`item-${i}`}>
<AccordionTrigger seed={`am-${i}`}>{title}</AccordionTrigger>
<AccordionContent>{text}</AccordionContent>
</AccordionItem>
))}
</Accordion>
);
}With icons
A glyph in front of each title.
Four to start with, each with a night colour that clears 4.5:1 on every paper.
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { InkIcon, type IconName } from "@/components/ui/ink-icons";
const items: { icon: IconName; title: string; body: string }[] = [
{ icon: "pen", title: "Pens", body: "Four to start with, each with a night colour that clears 4.5:1 on every paper." },
{ icon: "notebook", title: "Papers", body: "Cream, white and legal pad, ruled, squared or dotted, with coffee rings if you like." },
{ icon: "moon", title: "Night", body: "Every drawing has a night side. The lamp comes on over the top of the page." },
];
export default function AccordionIcons() {
return (
<Accordion defaultValue={["Pens"]} className="max-w-lg">
{items.map(({ icon, title, body }) => (
<AccordionItem key={title} value={title}>
<AccordionTrigger seed={`ai-${title}`}>
<span className="flex items-center gap-3">
<InkIcon name={icon} className="size-5 text-ink-2" />
{title}
</span>
</AccordionTrigger>
<AccordionContent>{body}</AccordionContent>
</AccordionItem>
))}
</Accordion>
);
}Open them all
Own the open items to expand and collapse everything at once.
"use client";
import { useState } from "react";
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { Button } from "@/components/ui/button";
const steps = [
["Install", "Run init once, then add what you need."],
["Draw", "Every component draws itself in the first time it is seen."],
["Customize", "Pick a pen and a paper, and the whole page follows."],
];
export default function AccordionControlled() {
const [open, setOpen] = useState<string[]>([]);
return (
<div className="flex w-full max-w-lg flex-col gap-4">
<div className="flex gap-3">
<Button variant="outline" size="sm" seed="ac-all" onClick={() => setOpen(steps.map(([title]) => title))}>
Open all
</Button>
<Button variant="ghost" size="sm" seed="ac-none" onClick={() => setOpen([])}>
Close all
</Button>
</div>
<Accordion multiple value={open} onValueChange={(value) => setOpen(value as string[])}>
{steps.map(([title, body]) => (
<AccordionItem key={title} value={title}>
<AccordionTrigger seed={`ac-${title}`}>{title}</AccordionTrigger>
<AccordionContent>{body}</AccordionContent>
</AccordionItem>
))}
</Accordion>
</div>
);
}API reference
- multipledefault
false booleanOn Accordion: lets more than one item stay open.
- defaultValue
string[]On Accordion: the items open to begin with.
- AccordionTrigger header
Accordion.Header propsReaches the heading row, an h3 by default. Pass { render: <div /> } to keep it out of the page outline.
- AccordionTrigger seed
string | numberPins the chevron's drawing.
Every other prop goes to Base UI's Accordion.