Accordion
An animated React accordion built on shadcn/ui and Radix: the drop-in Accordion eases its height open, fades answers in and turns its chevron — plus a spring-physics Motion accordion with a gliding hover highlight, and an SEO-ready FAQ section with a plus-to-minus icon and FAQPage JSON-LD.
- Drop-in shadcn API
- Radix Accordion
- Spring height
- FAQ schema
- prefers-reduced-motion
import {
MotionAccordion,
MotionAccordionContent,
MotionAccordionItem,
MotionAccordionTrigger,
} from "@/components/dusk/accordion/motion-accordion";
const items = [
{
value: "install",
title: "How do I install a component?",
body: "Run the shadcn CLI with the component's registry URL. It copies the source into your project and installs its dependencies.",
},
{
value: "motion",
title: "Which animation library is used?",
body: "Motion (formerly Framer Motion). Heights use springs, and content fades in once the panel starts opening.",
},
{
value: "reduced",
title: "What about reduced motion?",
body: "With prefers-reduced-motion the panels open instantly and nothing slides. Colors still fade, so state changes stay clear.",
},
{
value: "seo",
title: "Are closed answers indexed?",
body: "Yes. Closed panels stay in the DOM (marked inert), so search engines can read every answer.",
},
];
export default function MotionAccordionDemo() {
return (
<MotionAccordion defaultValue={["install"]} className="max-w-lg">
{items.map((item) => (
<MotionAccordionItem key={item.value} value={item.value}>
<MotionAccordionTrigger>{item.title}</MotionAccordionTrigger>
<MotionAccordionContent>{item.body}</MotionAccordionContent>
</MotionAccordionItem>
))}
</MotionAccordion>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/accordion.json1. Install the dependencies.
npm install @radix-ui/react-accordion lucide-react2. Copy the source into
components/ui/accordion.tsx.accordion.tsx "use client"; import * as React from "react"; import * as AccordionPrimitive from "@radix-ui/react-accordion"; import { ChevronDownIcon } from "lucide-react"; import { cn } from "@/lib/utils"; /** * DuskUI Accordion — a drop-in upgrade of the shadcn/ui Accordion. * * Same parts, props and data-slot attributes as shadcn (Accordion, * AccordionItem, AccordionTrigger, AccordionContent) plus: * - A short ease-out height animation, with a quicker close. * - The answer fades in as the panel opens. * - The chevron rotates 180° when open. * - `icon` on AccordionTrigger to swap the chevron for your own indicator. * - Focus-visible ring on triggers; no hover underline. * - Under `prefers-reduced-motion` panels open and close instantly. * CSS only (tw-animate-css), powered by Radix for keyboard support and aria. */ function Accordion({ ...props }: React.ComponentProps<typeof AccordionPrimitive.Root>) { return <AccordionPrimitive.Root data-slot="accordion" {...props} />; } /** One question / answer pair. `value` must be unique within the accordion. */ function AccordionItem({ className, ...props }: React.ComponentProps<typeof AccordionPrimitive.Item>) { return ( <AccordionPrimitive.Item data-slot="accordion-item" className={cn("border-b last:border-b-0", className)} {...props} /> ); } /** The button that toggles its item. Wrapped in a heading (`h3`) by Radix. */ function AccordionTrigger({ className, children, icon, ...props }: React.ComponentProps<typeof AccordionPrimitive.Trigger> & { /** * Replaces the chevron. It is not rotated for you: style it with * `group-data-[state=open]/accordion-trigger:` classes. */ icon?: React.ReactNode; }) { return ( <AccordionPrimitive.Header className="flex"> <AccordionPrimitive.Trigger data-slot="accordion-trigger" className={cn( "group/accordion-trigger flex flex-1 items-start justify-between gap-4 rounded-md py-4 text-left text-sm font-medium outline-none", "transition-[color,box-shadow] duration-200 ease-out motion-reduce:transition-none", "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50", "disabled:pointer-events-none disabled:opacity-50", className )} {...props} > {children} {icon !== undefined ? ( <span data-slot="accordion-trigger-icon" aria-hidden className="pointer-events-none flex shrink-0 items-center" > {icon} </span> ) : ( <ChevronDownIcon data-slot="accordion-trigger-icon" aria-hidden className={cn( "pointer-events-none size-4 shrink-0 translate-y-0.5 text-muted-foreground", "transition-[rotate,color] duration-200 ease-out motion-reduce:transition-none", "group-hover/accordion-trigger:text-foreground", "group-data-[state=open]/accordion-trigger:rotate-180" )} /> )} </AccordionPrimitive.Trigger> </AccordionPrimitive.Header> ); } /** The collapsible panel. `className` styles the inner wrapper, like shadcn. */ function AccordionContent({ className, children, ...props }: React.ComponentProps<typeof AccordionPrimitive.Content>) { return ( <AccordionPrimitive.Content data-slot="accordion-content" className={cn( "group/accordion-content overflow-hidden text-sm", "ease-out data-[state=open]:animate-accordion-down data-[state=open]:duration-200", "data-[state=closed]:animate-accordion-up data-[state=closed]:duration-150", "motion-reduce:animate-none" )} {...props} > <div className={cn( "pt-0 pb-4 text-muted-foreground", "group-data-[state=open]/accordion-content:animate-in group-data-[state=open]/accordion-content:fade-in-0 group-data-[state=open]/accordion-content:duration-200", "motion-reduce:animate-none", className )} > {children} </div> </AccordionPrimitive.Content> ); } export { Accordion, AccordionItem, AccordionTrigger, AccordionContent };
Replaces components/ui/accordion.tsx with the same parts and data-slot attributes. The height animation uses the accordion-down / accordion-up keyframes from tw-animate-css, which shadcn projects already include.
Usage
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion"
export function Faq() {
return (
<Accordion type="single" collapsible>
<AccordionItem value="refunds">
<AccordionTrigger>Can I get a refund?</AccordionTrigger>
<AccordionContent>Yes, within 30 days, no questions asked.</AccordionContent>
</AccordionItem>
</Accordion>
)
}Examples
Default
type="single" with collapsible keeps at most one item open. The answer fades in while the panel grows; closing is quicker than opening.
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
export default function AccordionBasicDemo() {
return (
<Accordion type="single" collapsible defaultValue="item-1" className="w-full max-w-md">
<AccordionItem value="item-1">
<AccordionTrigger>Is it accessible?</AccordionTrigger>
<AccordionContent>
Yes. It follows the WAI-ARIA accordion pattern, with arrow-key navigation between
triggers.
</AccordionContent>
</AccordionItem>
<AccordionItem value="item-2">
<AccordionTrigger>Does it work with my shadcn/ui code?</AccordionTrigger>
<AccordionContent>
It has the same parts, props and data-slots, so it drops into an existing project.
</AccordionContent>
</AccordionItem>
<AccordionItem value="item-3">
<AccordionTrigger>Is it animated?</AccordionTrigger>
<AccordionContent>
The panel eases open, the answer fades in and the chevron turns with a small overshoot.
</AccordionContent>
</AccordionItem>
</Accordion>
);
}Multiple open
type="multiple" lets several sections stay open — handy for settings and filters. Triggers accept any content, like a leading icon.
import { Bell, Lock, User } from "lucide-react";
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
const sections = [
{ value: "profile", icon: User, title: "Profile", body: "Name, avatar and public bio." },
{ value: "security", icon: Lock, title: "Security", body: "Password, passkeys and two-factor authentication." },
{ value: "alerts", icon: Bell, title: "Notifications", body: "Email and push preferences for mentions and deploys." },
];
export default function AccordionMultipleDemo() {
return (
<Accordion type="multiple" defaultValue={["profile", "security"]} className="w-full max-w-md rounded-xl border bg-card px-4">
{sections.map(({ value, icon: Icon, title, body }) => (
<AccordionItem key={value} value={value}>
<AccordionTrigger>
<span className="flex items-center gap-2.5">
<Icon className="size-4 text-muted-foreground" aria-hidden />
{title}
</span>
</AccordionTrigger>
<AccordionContent className="pl-6.5">{body}</AccordionContent>
</AccordionItem>
))}
</Accordion>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Motion Accordion
Panels open on a spring and their content fades in. A soft highlight glides between items under the cursor (a shared layoutId) and the open item gets a quiet outline. Closed answers stay in the DOM as inert, so they are still indexed by search engines. value is always an array, in single and multiple mode.
import {
MotionAccordion,
MotionAccordionContent,
MotionAccordionItem,
MotionAccordionTrigger,
} from "@/components/dusk/accordion/motion-accordion";
const items = [
{
value: "install",
title: "How do I install a component?",
body: "Run the shadcn CLI with the component's registry URL. It copies the source into your project and installs its dependencies.",
},
{
value: "motion",
title: "Which animation library is used?",
body: "Motion (formerly Framer Motion). Heights use springs, and content fades in once the panel starts opening.",
},
{
value: "reduced",
title: "What about reduced motion?",
body: "With prefers-reduced-motion the panels open instantly and nothing slides. Colors still fade, so state changes stay clear.",
},
{
value: "seo",
title: "Are closed answers indexed?",
body: "Yes. Closed panels stay in the DOM (marked inert), so search engines can read every answer.",
},
];
export default function MotionAccordionDemo() {
return (
<MotionAccordion defaultValue={["install"]} className="max-w-lg">
{items.map((item) => (
<MotionAccordionItem key={item.value} value={item.value}>
<MotionAccordionTrigger>{item.title}</MotionAccordionTrigger>
<MotionAccordionContent>{item.body}</MotionAccordionContent>
</MotionAccordionItem>
))}
</MotionAccordion>
);
}$ npx shadcn@latest add https://duskui.com/r/motion-accordion.json1. Install the dependencies.
npm install motion @radix-ui/react-accordion2. Copy the source into
components/dusk/accordion/motion-accordion.tsx.motion-accordion.tsx "use client"; import * as React from "react"; import * as AccordionPrimitive from "@radix-ui/react-accordion"; import { LayoutGroup, motion, useReducedMotion, type Variants } from "motion/react"; import { Accordion, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"; import { cn } from "@/lib/utils"; type MotionAccordionContextValue = { open: string[]; hovered: string | null; setHovered: (value: string | null) => void; }; const MotionAccordionContext = React.createContext<MotionAccordionContextValue | null>(null); const MotionAccordionItemContext = React.createContext<{ open: boolean } | null>(null); function useMotionAccordion() { const context = React.useContext(MotionAccordionContext); if (!context) throw new Error("MotionAccordion parts must be used inside <MotionAccordion>."); return context; } export interface MotionAccordionProps extends Omit<React.ComponentProps<"div">, "defaultValue" | "dir"> { /** `single` keeps one item open at a time; `multiple` lets any number stay open. */ type?: "single" | "multiple"; /** Single mode only: allow closing the open item. */ collapsible?: boolean; /** Open item values. Always an array, in both modes. */ value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void; disabled?: boolean; dir?: "ltr" | "rtl"; } /** * An accordion with gentle spring physics: panels ease open, their content * fades in, a soft highlight glides between items under the cursor and the * open item gets a quiet outline. Answers stay in the DOM while closed * (inert), so search engines still index them. * Built on the DuskUI Accordion (Radix) for keyboard support and aria. */ export function MotionAccordion({ type = "single", collapsible = true, value: valueProp, defaultValue = [], onValueChange, className, children, onPointerLeave, ...props }: MotionAccordionProps) { const layoutId = React.useId(); const [uncontrolled, setUncontrolled] = React.useState(defaultValue); const [hovered, setHovered] = React.useState<string | null>(null); const open = valueProp ?? uncontrolled; const setOpen = (next: string[]) => { if (valueProp === undefined) setUncontrolled(next); onValueChange?.(next); }; const shared = { ...props, className: cn("flex w-full flex-col gap-1", className), onPointerLeave: (event: React.PointerEvent<HTMLDivElement>) => { onPointerLeave?.(event); setHovered(null); }, children, }; return ( <MotionAccordionContext.Provider value={{ open, hovered, setHovered }}> <LayoutGroup id={layoutId}> {type === "multiple" ? ( <Accordion type="multiple" value={open} onValueChange={setOpen} {...shared} /> ) : ( <Accordion type="single" collapsible={collapsible} value={open[0] ?? ""} onValueChange={(next) => setOpen(next ? [next] : [])} {...shared} /> )} </LayoutGroup> </MotionAccordionContext.Provider> ); } /** One item. Shows the gliding hover highlight and a quiet outline while open. */ export function MotionAccordionItem({ value, className, children, onPointerEnter, ...props }: React.ComponentProps<typeof AccordionItem>) { const { open, hovered, setHovered } = useMotionAccordion(); const isOpen = open.includes(value); return ( <MotionAccordionItemContext.Provider value={{ open: isOpen }}> <AccordionItem value={value} onPointerEnter={(event) => { onPointerEnter?.(event); if (event.pointerType === "mouse") setHovered(value); }} className={cn( "relative isolate rounded-xl border border-transparent px-4 last:border", "transition-[border-color,background-color] duration-200 ease-out motion-reduce:transition-none", "data-[state=open]:border-white/10 data-[state=open]:bg-white/[0.02]", className )} {...props} > {hovered === value && ( <motion.div layoutId="motion-accordion-hover" aria-hidden className="pointer-events-none absolute inset-0 -z-10 rounded-xl bg-white/[0.04]" transition={{ type: "spring", bounce: 0.15, duration: 0.4 }} /> )} {children} </AccordionItem> </MotionAccordionItemContext.Provider> ); } /** The item's toggle button (the DuskUI AccordionTrigger with a rotating chevron). */ export function MotionAccordionTrigger({ className, ...props }: React.ComponentProps<typeof AccordionTrigger>) { return <AccordionTrigger className={cn("text-[0.9375rem]", className)} {...props} />; } const panel = (instant: boolean): Variants => ({ open: { height: "auto", transition: instant ? { duration: 0 } : { type: "spring", bounce: 0, duration: 0.3 } }, closed: { height: 0, transition: instant ? { duration: 0 } : { type: "spring", bounce: 0, duration: 0.25 } }, }); const inner: Variants = { open: { opacity: 1, y: 0, transition: { duration: 0.2, delay: 0.05, ease: "easeOut" } }, closed: { opacity: 0, y: -4, transition: { duration: 0.15 } }, }; /** The panel. Animates its height and fades its content. `className` styles the inner wrapper. */ export function MotionAccordionContent({ className, children, ...props }: Omit<React.ComponentProps<typeof AccordionPrimitive.Content>, "forceMount" | "asChild">) { const item = React.useContext(MotionAccordionItemContext); if (!item) throw new Error("MotionAccordionContent must be used inside <MotionAccordionItem>."); const reduceMotion = useReducedMotion(); const state = item.open ? "open" : "closed"; return ( <AccordionPrimitive.Content forceMount asChild {...props}> <motion.div data-slot="accordion-content" initial={false} animate={state} variants={panel(!!reduceMotion)} // Closed panels stay mounted for the animation; inert hides them from focus and assistive tech. inert={!item.open} className="overflow-hidden text-sm" > <motion.div variants={inner} className={cn("pb-4 leading-relaxed text-muted-foreground", className)} > {children} </motion.div> </motion.div> </AccordionPrimitive.Content> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | "single" | One open item at a time, or any number. |
| collapsible | boolean | true | Single mode: allow closing the open item. |
| value / onValueChange | string[] / (value: string[]) => void | — | Controlled open items. |
| defaultValue | string[] | [] | Items open on first render. |
| disabled | boolean | false | Disable every item. |
| MotionAccordionItem / Trigger / Content | AccordionItem / AccordionTrigger / Radix Content props | — | The parts; Content's className styles its inner wrapper. |
FAQ Accordion
A data-driven FAQ section: numbered questions, a plus that folds into a minus, and answers that ease open. Set jsonLd to emit schema.org FAQPage structured data for rich results. It has no hooks, so it renders in a Server Component and the JSON-LD lands in the initial HTML.
import { FaqAccordion } from "@/components/dusk/accordion/faq-accordion";
const faqs = [
{
question: "Is DuskUI free to use?",
answer: "Yes. Every component is open source and free for personal and commercial projects.",
},
{
question: "Do I need shadcn/ui?",
answer:
"No, but it helps. Components install with the shadcn CLI and follow its conventions, so they fit right into a shadcn project.",
},
{
question: "Does it support dark mode?",
answer: "DuskUI is designed dark-first. Colors come from CSS variables, so you can retheme everything in one place.",
},
{
question: "Can I use it with the Next.js App Router?",
answer: "Yes. Interactive parts are marked \"use client\", and this FAQ block renders fine in a Server Component.",
},
];
export default function FaqAccordionDemo() {
return <FaqAccordion items={faqs} defaultValue={["item-0"]} className="max-w-xl" />;
}$ npx shadcn@latest add https://duskui.com/r/faq-accordion.json1. Copy the source into
components/dusk/accordion/faq-accordion.tsx.faq-accordion.tsx import * as React from "react"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger, } from "@/components/ui/accordion"; import { cn } from "@/lib/utils"; export interface FaqItem { question: string; answer: React.ReactNode; /** Plain-text answer for structured data when `answer` is JSX. */ answerText?: string; /** Stable id for the item. Defaults to its index. */ value?: string; } export interface FaqAccordionProps extends Omit<React.ComponentProps<"div">, "defaultValue" | "dir"> { items: FaqItem[]; /** Allow several answers open at once. */ multiple?: boolean; /** Values (or `item-<index>`) open on first render. */ defaultValue?: string[]; /** Prefix each question with 01, 02… */ numbered?: boolean; /** * Emit schema.org FAQPage JSON-LD for rich results. Uses `answer` when it is * a string, otherwise `answerText`; items with neither are skipped. */ jsonLd?: boolean; } /** The + that folds into a − : the vertical bar rotates flat when the item opens. */ function PlusMinus() { return ( <span className={cn( "relative grid size-7 place-items-center rounded-full border border-white/10 text-muted-foreground", "transition-[border-color,color] duration-200 ease-out motion-reduce:transition-none", "group-hover/accordion-trigger:border-white/20 group-hover/accordion-trigger:text-foreground", "group-data-[state=open]/accordion-trigger:border-white/20 group-data-[state=open]/accordion-trigger:text-foreground" )} > <span className="absolute h-[1.5px] w-3 rounded-full bg-current" /> <span className={cn( "absolute h-3 w-[1.5px] rounded-full bg-current", "transition-[rotate] duration-200 ease-out motion-reduce:transition-none", "group-data-[state=open]/accordion-trigger:rotate-90" )} /> </span> ); } function faqJsonLd(items: FaqItem[]) { const entities = items.flatMap((item) => { const text = typeof item.answer === "string" ? item.answer : item.answerText; return text ? [{ "@type": "Question", name: item.question, acceptedAnswer: { "@type": "Answer", text } }] : []; }); if (!entities.length) return null; // Escape `<` so answer text can never close the script tag. return JSON.stringify({ "@context": "https://schema.org", "@type": "FAQPage", mainEntity: entities }).replace( /</g, "\\u003c" ); } /** * A data-driven FAQ section: numbered questions, a plus that morphs into a * minus, answers that ease open, and optional FAQPage JSON-LD so the questions * can show up as rich results. No hooks, so it renders in Server Components * (the JSON-LD lands in the initial HTML). Built on the DuskUI Accordion (Radix). */ export function FaqAccordion({ items, multiple = false, defaultValue, numbered = true, jsonLd = false, className, ...props }: FaqAccordionProps) { const schema = jsonLd ? faqJsonLd(items) : null; const rows = items.map((item, index) => { const value = item.value ?? `item-${index}`; return ( <AccordionItem key={value} value={value} className="border-white/10 last:border-b" > <AccordionTrigger icon={<PlusMinus />} className="items-center py-5 text-base hover:text-foreground" > <span className="flex items-baseline gap-4"> {numbered && ( <span aria-hidden className="font-mono text-xs text-muted-foreground tabular-nums transition-colors duration-200 group-data-[state=open]/accordion-trigger:text-foreground" > {String(index + 1).padStart(2, "0")} </span> )} <span>{item.question}</span> </span> </AccordionTrigger> <AccordionContent className={cn("pb-5 leading-relaxed", numbered && "pl-[1.875rem]")}> {item.answer} </AccordionContent> </AccordionItem> ); }); return ( <div data-slot="faq-accordion" className={cn("w-full", className)} {...props}> {multiple ? ( <Accordion type="multiple" defaultValue={defaultValue} className="border-t border-white/10"> {rows} </Accordion> ) : ( <Accordion type="single" collapsible defaultValue={defaultValue?.[0]} className="border-t border-white/10" > {rows} </Accordion> )} {schema && <script type="application/ld+json" dangerouslySetInnerHTML={{ __html: schema }} />} </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| items | { question, answer, answerText?, value? }[] | — | Questions and answers. `answer` can be JSX. |
| multiple | boolean | false | Allow several answers open at once. |
| defaultValue | string[] | — | Open on first render: item values or `item-<index>`. |
| numbered | boolean | true | Prefix questions with 01, 02… |
| jsonLd | boolean | false | Render FAQPage JSON-LD. Uses string answers or `answerText`. |
API Reference
Same parts as shadcn/ui: Accordion, AccordionItem, AccordionTrigger and AccordionContent, each forwarding its Radix Accordion props. className on AccordionContent styles the inner padding wrapper, like shadcn.
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | — | On Accordion. Required by Radix. |
| collapsible | boolean | false | Single mode: allow closing the open item. |
| value / defaultValue / onValueChange | string | string[] | — | Open item(s); an array in multiple mode. |
| icon | ReactNode | — | On AccordionTrigger: replaces the chevron. Style it with group-data-[state=open]/accordion-trigger:. |
| disabled | boolean | false | On Accordion or a single AccordionItem. |
Accessibility
- Built on Radix Accordion: each trigger is a button inside a heading with aria-expanded and aria-controls; arrow keys, Home and End move between triggers.
- Triggers show a focus-visible ring; chevrons and plus / minus icons are aria-hidden.
- Motion Accordion keeps closed panels mounted but inert, so their links can't be focused and screen readers skip them.
- Under prefers-reduced-motion panels open and close instantly and the chevron snaps; color changes still fade.