{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"accordion","type":"registry:component","title":"Accordion","description":"Answers unfold from under their question like a folded flap, and the row you clicked never moves.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"components/accordion.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useRef, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport type AccordionItem = { question: string; answer: string };\n\n// Panels open independently. Closing one automatically when another opens\n// would shift the clicked header up by the closed panel's height; this way a\n// header never moves, so it can be closed again without moving the mouse.\nexport function Accordion({\n  items,\n  defaultOpen = [],\n  className,\n}: {\n  items: AccordionItem[];\n  defaultOpen?: number[];\n  className?: string;\n}) {\n  const [open, setOpen] = useState(() => new Set(defaultOpen));\n  const toggle = (index: number) =>\n    setOpen((current) => {\n      const next = new Set(current);\n      if (!next.delete(index)) next.add(index);\n      return next;\n    });\n  const headers = useRef<(HTMLButtonElement | null)[]>([]);\n  const baseId = useId();\n\n  const focusHeader = (index: number) => {\n    const count = items.length;\n    headers.current[(index + count) % count]?.focus();\n  };\n\n  return (\n    <div className={cn(\"w-[480px] max-w-full divide-y divide-border\", className)}>\n      {items.map((item, i) => {\n        const isOpen = open.has(i);\n        const headerId = `${baseId}-header-${i}`;\n        const panelId = `${baseId}-panel-${i}`;\n\n        return (\n          <div key={item.question}>\n            <h3>\n              <button\n                ref={(el) => {\n                  headers.current[i] = el;\n                }}\n                id={headerId}\n                type=\"button\"\n                aria-expanded={isOpen}\n                aria-controls={panelId}\n                onClick={() => toggle(i)}\n                onKeyDown={(e) => {\n                  const target = {\n                    ArrowDown: i + 1,\n                    ArrowUp: i - 1,\n                    Home: 0,\n                    End: items.length - 1,\n                  }[e.key];\n                  if (target === undefined) return;\n                  e.preventDefault();\n                  focusHeader(target);\n                }}\n                className=\"group flex h-14 w-full items-center justify-between gap-5 rounded-md text-left text-[15px] font-medium text-foreground outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-foreground\"\n              >\n                {item.question}\n                <svg\n                  viewBox=\"0 0 16 16\"\n                  aria-hidden\n                  className={cn(\n                    \"size-[18px] shrink-0 text-muted-foreground transition-[rotate,color] duration-250 ease-[cubic-bezier(0.23,1,0.32,1)] group-hover:text-foreground motion-reduce:transition-[color]\",\n                    isOpen && \"rotate-180 text-foreground\",\n                  )}\n                  fill=\"none\"\n                  stroke=\"currentColor\"\n                  strokeWidth={1.5}\n                  strokeLinecap=\"round\"\n                  strokeLinejoin=\"round\"\n                >\n                  <path d=\"m4 6 4 4 4-4\" />\n                </svg>\n              </button>\n            </h3>\n            {/* Animating grid-template-rows between 0fr and 1fr is the one\n                place height animates: the row resolves to the content's real\n                height, so nothing is measured and text that reflows at a new\n                width is never clipped. */}\n            <div\n              id={panelId}\n              role=\"region\"\n              aria-labelledby={headerId}\n              inert={!isOpen}\n              className={cn(\n                \"grid ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:transition-none\",\n                isOpen\n                  ? \"grid-rows-[1fr] transition-[grid-template-rows] duration-250\"\n                  : \"grid-rows-[0fr] transition-[grid-template-rows] duration-200\",\n              )}\n            >\n              <div className=\"min-h-0 overflow-hidden\">\n                {/* The answer is a flap folded up under its question: it\n                    swings down on a hinge along the header's bottom edge\n                    while the row parts, so the motion says where the text\n                    came from. The drawer curve lets it fall fast and settle\n                    flat; 280ms because the swing has ~70 degrees to cover.\n                    Closing folds only partway back and fades out in 150ms,\n                    so the text is gone before the row finishes shutting. */}\n                <div\n                  className={cn(\n                    \"relative origin-top transition-[transform,opacity] motion-reduce:transform-none motion-reduce:transition-[opacity]\",\n                    isOpen\n                      ? \"[transform:perspective(640px)_rotateX(0deg)] opacity-100 duration-280 ease-[cubic-bezier(0.32,0.72,0,1)]\"\n                      : \"[transform:perspective(640px)_rotateX(-72deg)] opacity-0 duration-150 ease-[cubic-bezier(0.23,1,0.32,1)]\",\n                  )}\n                >\n                  <p className=\"pr-10 pb-5 text-[15px] leading-relaxed text-pretty text-muted-foreground\">\n                    {item.answer}\n                  </p>\n                  {/* The crease: text nearest the hinge stays veiled in the\n                      page color until the flap lies flat, so the answer\n                      reads as unfolding out of the header, not sliding. */}\n                  <span\n                    aria-hidden\n                    className={cn(\n                      \"pointer-events-none absolute inset-0 bg-linear-to-b from-background to-transparent to-80% transition-[opacity] ease-out motion-reduce:hidden\",\n                      isOpen ? \"opacity-0 duration-280\" : \"opacity-100 duration-150\",\n                    )}\n                  />\n                </div>\n              </div>\n            </div>\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n\nconst FAQ: AccordionItem[] = [\n  {\n    question: \"What is this lab?\",\n    answer:\n      \"A collection of small interface components, each built to get the details right: timing, easing, focus and both themes.\",\n  },\n  {\n    question: \"How are components built?\",\n    answer:\n      \"React with Tailwind, CSS transitions wherever they can do the job, and Motion only for springs and gestures CSS cannot express.\",\n  },\n  {\n    question: \"Can I use the code?\",\n    answer:\n      \"Yes. Every component is a single file with no setup, so copy it into your project and adjust the tokens to match.\",\n  },\n  {\n    question: \"Does it respect reduced motion?\",\n    answer:\n      \"It does. Movement drops out and only gentle fades remain, so every state change is still clear without anything sliding around.\",\n  },\n];\n\nexport default function AccordionDemo() {\n  return <Accordion items={FAQ} />;\n}\n"}],"categories":["navigation"],"docs":"From ui lab: https://lab.xevrion.dev/lab/accordion"}