{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "accordion",
  "type": "registry:ui",
  "title": "Accordion",
  "description": "An accessible React accordion for Next.js: sections that open and close with a smooth height change, as one card, separate cards or plain rows. Built on Radix.",
  "author": "FewTone <https://fewtone.com>",
  "dependencies": [
    "class-variance-authority@^0.7.1",
    "lucide-react@^1.52.0",
    "motion@^14.0.0",
    "radix-ui@^1.7.0"
  ],
  "registryDependencies": [
    "https://ui.fewtone.com/r/tokens.json",
    "https://ui.fewtone.com/r/motion.json"
  ],
  "files": [
    {
      "path": "registry/fewtone/ui/accordion.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport { blur, duration, ease, spring } from \"@/lib/motion\";\nimport { cn } from \"@/lib/utils\";\nimport { cva, type VariantProps } from \"class-variance-authority\";\nimport { ChevronDownIcon } from \"lucide-react\";\nimport { motion, type Transition, useReducedMotion } from \"motion/react\";\nimport { Accordion as AccordionPrimitive } from \"radix-ui\";\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  useContext,\n  useEffect,\n  useLayoutEffect,\n  useRef,\n  useState,\n} from \"react\";\n\n/*\n * Accordion, built on Radix Accordion (roadmap, 2026-10-16: \"Sections that open and close with a smooth\n * height change\").\n * - Three looks (owner, 2026-10-19):\n *   - `card` (the default): one surface with Card's shadow and hairline, a quiet rule between sections\n *   - `separated`: each section is its own card, with no surface behind them (owner, 2026-10-19: no tray).\n *     An open section moves 4px further from its neighbours as it opens, so it reads as the one pulled out.\n *   - `plain`: rows and rules only, for inside a surface that's already a card (a Sheet, a Card)\n * - Hover: no underline (owner, 2026-10-19). In card and separated the question's row tints, a fill that\n *   only fades (motion.md); in every look the chevron brightens.\n * - Opening and closing run on motion (owner, 2026-10-19: \"more smoother\"; it was CSS keyframes before):\n *   - The height glides on spring.smooth, the preset for page sections. It's damped enough that it never\n *     visibly overshoots (about 0.1%), so the page below doesn't bounce, and it lands softly both ways. A\n *     section pressed again mid-glide turns round from where it is, keeping its speed, instead of jumping.\n *   - The answer is staged against it. Opening, the box leads and the answer follows a beat later\n *     (duration.instant), sharpening through the blur as it settles 6px down into place. Closing, the answer\n *     goes first and quickly (duration.fast), then the empty box folds.\n *   - The content is clipped only while it moves, so focus rings inside an open section aren't cut off.\n * - Closed answers stay in the page (Radix's forceMount) as hidden=\"until-found\": find in page sees their\n *   text and opens the section, and search engines read them. React only writes a plain `hidden`, so the\n *   value is set after it.\n * - The chevron turns on spring.snappy. A span around it turns, so the icon's own transform is never\n *   replaced (motion.md's SVG pitfall).\n * - Nothing animates on first paint (initial={false}). With reduced motion everything changes at once.\n * - One section open at a time (type=\"single\", plus `collapsible` to close it again) or several\n *   (type=\"multiple\"). Each question is a button in a heading; arrow keys move between them.\n */\n\nconst accordionVariants = cva(\"\", {\n  variants: {\n    variant: {\n      card: \"rounded-lg bg-card text-card-foreground shadow-sm ring-1 ring-border\",\n      separated: \"flex flex-col gap-2\",\n      plain: \"\",\n    },\n  },\n  defaultVariants: { variant: \"card\" },\n});\n\nexport type AccordionVariant = NonNullable<VariantProps<typeof accordionVariants>[\"variant\"]>;\n\nconst itemVariants = cva(\"group/accordion-item\", {\n  variants: {\n    variant: {\n      card: \"border-b border-border last:border-b-0\",\n      separated: [\n        \"rounded-lg bg-card text-card-foreground shadow-sm ring-1 ring-border\",\n        // Open, it moves apart from its neighbours: the gap glides from 8 to 12px as the height does, and back\n        \"[transition-property:margin] duration-(--duration-slow) ease-out\",\n        \"data-[state=open]:not-first:mt-1 data-[state=open]:not-last:mb-1\",\n      ],\n      plain: \"border-b border-border last:border-b-0\",\n    },\n  },\n  defaultVariants: { variant: \"card\" },\n});\n\nconst triggerVariants = cva(\n  [\n    \"group/accordion-trigger flex flex-1 cursor-pointer items-start justify-between gap-4 py-4 text-start text-sm font-medium\",\n    \"outline-ring focus-visible:outline-(length:--focus-ring-width)\",\n    \"disabled:pointer-events-none disabled:opacity-(--opacity-disabled)\",\n  ],\n  {\n    variants: {\n      variant: {\n        // The row runs edge to edge: it tints on hover, takes the card's corners at the ends, and its focus\n        // ring sits inside it\n        card: [\n          \"px-4 transition-colors duration-(--duration-fast) ease-out hover:bg-muted/50\",\n          \"focus-visible:-outline-offset-(--focus-ring-width)\",\n          \"group-first/accordion-item:rounded-t-lg\",\n          \"group-last/accordion-item:group-data-[state=closed]/accordion-item:rounded-b-lg\",\n        ],\n        separated: [\n          \"rounded-lg px-4 transition-colors duration-(--duration-fast) ease-out hover:bg-muted/50\",\n          \"focus-visible:-outline-offset-(--focus-ring-width)\",\n          \"group-data-[state=open]/accordion-item:rounded-b-none\",\n        ],\n        plain: \"rounded-md focus-visible:outline-offset-(--focus-ring-offset)\",\n      },\n    },\n    defaultVariants: { variant: \"card\" },\n  },\n);\n\nconst contentVariants = cva(\"pb-4 text-muted-foreground\", {\n  variants: {\n    variant: {\n      card: \"px-4\",\n      separated: \"px-4\",\n      plain: \"\",\n    },\n  },\n  defaultVariants: { variant: \"card\" },\n});\n\ntype AccordionContextValue = {\n  /** The open sections */\n  open: string[];\n  reduce: boolean;\n  variant: AccordionVariant;\n  /** Opens a section from outside its question: a match found in the page */\n  reveal: (value: string) => void;\n};\n\nconst AccordionContext = createContext<AccordionContextValue | null>(null);\nconst ItemContext = createContext({ value: \"\", open: false, disabled: false });\n\n/** A single or multiple accordion's value, as a list */\nfunction asList(value: string | string[] | undefined) {\n  if (Array.isArray(value)) return value;\n  return value ? [value] : [];\n}\n\nexport type AccordionProps = ComponentProps<typeof AccordionPrimitive.Root> & {\n  /** card (one surface), separated (each section its own card) or plain (rows and rules) */\n  variant?: AccordionVariant;\n};\n\n/**\n * Sections that open and close, built on Radix Accordion. Holds the open sections (controlled or not) so each\n * section knows whether it's open, and so a match found in the page can open one.\n */\nfunction Accordion(props: AccordionProps) {\n  const reduce = useReducedMotion() ?? false;\n  const variant = props.variant ?? \"card\";\n  const [internal, setInternal] = useState(() => asList(props.defaultValue));\n  const open = props.value !== undefined ? asList(props.value) : internal;\n  const own = {\n    \"data-slot\": \"accordion\",\n    \"data-variant\": variant,\n    className: cn(accordionVariants({ variant }), props.className),\n  };\n\n  // Every change comes through here: a question pressed (through Radix) or a match found in the page\n  const change = (next: string[]) => {\n    setInternal(next);\n    if (props.type === \"multiple\") props.onValueChange?.(next);\n    else props.onValueChange?.(next[0] ?? \"\");\n  };\n  const reveal = (item: string) => {\n    if (props.disabled || open.includes(item)) return;\n    change(props.type === \"multiple\" ? [...open, item] : [item]);\n  };\n\n  let root: ReactNode;\n  if (props.type === \"multiple\") {\n    const {\n      value: _value,\n      defaultValue: _defaultValue,\n      onValueChange: _change,\n      variant: _variant,\n      ...rest\n    } = props;\n    root = <AccordionPrimitive.Root {...rest} {...own} value={open} onValueChange={change} />;\n  } else {\n    const {\n      value: _value,\n      defaultValue: _defaultValue,\n      onValueChange: _change,\n      variant: _variant,\n      ...rest\n    } = props;\n    root = (\n      <AccordionPrimitive.Root\n        {...rest}\n        {...own}\n        value={open[0] ?? \"\"}\n        onValueChange={(next: string) => change(asList(next))}\n      />\n    );\n  }\n\n  return (\n    <AccordionContext.Provider value={{ open, reduce, variant, reveal }}>{root}</AccordionContext.Provider>\n  );\n}\n\nfunction AccordionItem({\n  className,\n  value,\n  disabled = false,\n  ...props\n}: ComponentProps<typeof AccordionPrimitive.Item>) {\n  const accordion = useContext(AccordionContext);\n  const open = accordion?.open.includes(value) ?? false;\n  return (\n    <ItemContext.Provider value={{ value, open, disabled }}>\n      <AccordionPrimitive.Item\n        data-slot=\"accordion-item\"\n        value={value}\n        disabled={disabled}\n        className={cn(itemVariants({ variant: accordion?.variant }), className)}\n        {...props}\n      />\n    </ItemContext.Provider>\n  );\n}\n\n/** The section's heading and the button that opens it */\nfunction AccordionTrigger({\n  className,\n  children,\n  ...props\n}: ComponentProps<typeof AccordionPrimitive.Trigger>) {\n  const accordion = useContext(AccordionContext);\n  const { open } = useContext(ItemContext);\n\n  return (\n    <AccordionPrimitive.Header data-slot=\"accordion-header\" className=\"flex\">\n      <AccordionPrimitive.Trigger\n        data-slot=\"accordion-trigger\"\n        className={cn(triggerVariants({ variant: accordion?.variant }), className)}\n        {...props}\n      >\n        {children}\n        {/* Lined up with the first line of the question; it brightens while the question is hovered */}\n        <motion.span\n          aria-hidden\n          data-slot=\"accordion-chevron\"\n          className={cn(\n            \"pointer-events-none mt-0.5 flex shrink-0 text-muted-foreground\",\n            \"transition-colors duration-(--duration-fast) ease-out group-hover/accordion-trigger:text-foreground\",\n          )}\n          initial={false}\n          animate={{ rotate: open ? 180 : 0 }}\n          transition={accordion?.reduce ? { duration: 0 } : spring.snappy}\n        >\n          <ChevronDownIcon className=\"size-(--size-icon-md)\" />\n        </motion.span>\n      </AccordionPrimitive.Trigger>\n    </AccordionPrimitive.Header>\n  );\n}\n\nconst shown = { opacity: 1, filter: blur.shown, y: 0, transitionEnd: { filter: \"none\" } };\nconst gone = { opacity: 0, filter: blur.hidden, y: -6 };\n\n/**\n * The section's content. `className` goes on the inner block, so padding and text styles never fight the\n * height animation.\n */\nfunction AccordionContent({\n  className,\n  children,\n  ...props\n}: ComponentProps<typeof AccordionPrimitive.Content>) {\n  const accordion = useContext(AccordionContext);\n  const item = useContext(ItemContext);\n  const { open } = item;\n  const reveal = accordion?.reveal;\n  const node = useRef<HTMLDivElement>(null);\n  // Still: not gliding. Cleared in the same render as the section opens or closes, so it's clipped from the\n  // first frame of the glide, and set again when the glide reaches the new state.\n  const [still, setStill] = useState(true);\n  const [wasOpen, setWasOpen] = useState(open);\n  // Opened by find in page: the browser scrolls to the match straight away, so it opens at once\n  const [instant, setInstant] = useState(false);\n  if (open !== wasOpen) {\n    setWasOpen(open);\n    setStill(false);\n  }\n  const hidden = !open && still;\n  const jump = (accordion?.reduce ?? false) || instant;\n\n  const glide: Transition = jump ? { duration: 0 } : spring.smooth;\n  const fade: Transition = jump\n    ? { duration: 0 }\n    : open\n      ? { duration: duration.slow, ease: ease.out, delay: duration.instant }\n      : { duration: duration.fast, ease: ease.in };\n\n  // A plain `hidden` from React becomes \"until-found\", so find in page can reach the text. A disabled section\n  // can't open, so it stays plainly hidden.\n  useLayoutEffect(() => {\n    if (hidden && !item.disabled) node.current?.setAttribute(\"hidden\", \"until-found\");\n  }, [hidden, item.disabled]);\n\n  useEffect(() => {\n    const element = node.current;\n    if (!element || !reveal) return;\n    const onMatch = () => {\n      // The browser has already removed `hidden` and scrolls to the match straight after this event, so the\n      // height is set by hand for that moment; motion takes over on the next frame\n      element.style.height = \"auto\";\n      setInstant(true);\n      reveal(item.value);\n    };\n    element.addEventListener(\"beforematch\", onMatch);\n    return () => element.removeEventListener(\"beforematch\", onMatch);\n  }, [reveal, item.value]);\n\n  return (\n    <AccordionPrimitive.Content data-slot=\"accordion-content\" {...props} hidden={hidden} forceMount asChild>\n      <motion.div\n        ref={node}\n        className={cn(\"text-sm\", !(open && still) && \"overflow-hidden\")}\n        initial={false}\n        animate={{ height: open ? \"auto\" : 0 }}\n        transition={glide}\n        onAnimationComplete={(definition) => {\n          // An interrupted glide reports in too: only the one that reached the current state counts\n          if ((definition as { height?: unknown }).height !== (open ? \"auto\" : 0)) return;\n          setStill(true);\n          setInstant(false);\n        }}\n      >\n        <motion.div\n          className={cn(contentVariants({ variant: accordion?.variant }), className)}\n          initial={false}\n          animate={open ? shown : gone}\n          transition={fade}\n        >\n          {children}\n        </motion.div>\n      </motion.div>\n    </AccordionPrimitive.Content>\n  );\n}\n\nexport { Accordion, AccordionContent, AccordionItem, AccordionTrigger, accordionVariants };\n"
    }
  ],
  "categories": [
    "navigation"
  ],
  "docs": "Usage and examples: https://ui.fewtone.com/components/accordion"
}
