{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"motion-accordion","type":"registry:component","title":"Accordion (Motion)","description":"Accordion with motion: a section opens and closes on a spring, what's inside rises a little as it fades in, and the chevron turns with the panel as far as it has opened.","dependencies":["lucide-react","motion","radix-ui"],"registryDependencies":["https://dashral.com/r/utils.json","https://dashral.com/r/motion-reduced.json","https://dashral.com/r/motion-tokens.json","https://dashral.com/r/accordion.json"],"files":[{"path":"registry/dashral/motion/accordion.tsx","type":"registry:file","target":"components/dashral/motion/accordion.tsx","content":"\"use client\"\n\nimport * as React from \"react\"\nimport { Accordion as AccordionPrimitive } from \"radix-ui\"\nimport { ChevronDownIcon } from \"lucide-react\"\nimport { animate, motion, useMotionValue, useTransform, type MotionValue } from \"motion/react\"\nimport type { AccordionItem, AccordionProps } from \"../accordion\"\nimport { cn } from \"../lib/utils\"\n\nimport { useReducedMotion } from \"../lib/motion-reduced\"\nimport { duration, ease, spring } from \"../lib/motion-tokens\"\n\nexport type { AccordionItem, AccordionProps } from \"../accordion\"\n\n/**\n * The kit's Accordion with motion: the same items, variants and props. A section opens and closes on a spring, its\n * height gliding with no overshoot, while what's inside rises a little as it fades in. The chevron turns with the\n * panel itself, as far as it has opened, so a section reversed halfway turns back from where it is; hovered, it leans\n * the way the section will go. An open section keeps following what's inside: a line added or removed, an answer swapped for a thank\n * you, and it glides to the new height. Closed sections stay out of reach of the keyboard. With reduced motion they\n * open at once.\n */\nexport function Accordion({ items, multiple = false, value, defaultValue, onValueChange, variant = \"default\", className, classNames, ...props }: AccordionProps) {\n  const reduced = useReducedMotion()\n  const [own, setOwn] = React.useState<string[]>(defaultValue ?? [])\n  const open = value ?? own\n  const change = (next: string[]) => {\n    if (value === undefined) setOwn(next)\n    onValueChange?.(next)\n  }\n\n  const keyOf = (item: AccordionItem, i: number) => item.value ?? String(i)\n  const rows = items.map((item, i) => {\n    const key = keyOf(item, i)\n    return <AccordionRow key={key} value={key} item={item} open={open.includes(key)} reduced={reduced} variant={variant} classNames={classNames} />\n  })\n\n  const root = cn(variant === \"separated\" && \"grid gap-2\", className)\n  // Radix takes a string for one open item and an array for several; the outside always sees an array.\n  return multiple ? (\n    <AccordionPrimitive.Root data-slot=\"accordion\" type=\"multiple\" value={open} onValueChange={change} className={root} {...props}>\n      {rows}\n    </AccordionPrimitive.Root>\n  ) : (\n    <AccordionPrimitive.Root\n      data-slot=\"accordion\"\n      type=\"single\"\n      collapsible\n      value={open[0] ?? \"\"}\n      onValueChange={(next: string) => change(next ? [next] : [])}\n      className={root}\n      {...props}\n    >\n      {rows}\n    </AccordionPrimitive.Root>\n  )\n}\n\n/** One section: its trigger and panel, sharing how far the panel has opened so the chevron turns with it. */\nfunction AccordionRow({\n  value,\n  item,\n  open,\n  reduced,\n  variant,\n  classNames,\n}: {\n  value: string\n  item: AccordionItem\n  open: boolean\n  reduced: boolean\n  variant: AccordionProps[\"variant\"]\n  classNames: AccordionProps[\"classNames\"]\n}) {\n  // The panel's height now, and its height open: their ratio is how far it has opened.\n  const height = useMotionValue<number | \"auto\">(open ? \"auto\" : 0)\n  const full = useMotionValue(0)\n  const turn = useTransform(() => {\n    const now = height.get()\n    const whole = full.get()\n    if (now === \"auto\") return 180\n    return whole ? Math.min(1, now / whole) * 180 : open ? 180 : 0\n  })\n  return (\n    <AccordionPrimitive.Item\n      value={value}\n      disabled={item.disabled}\n      data-slot=\"accordion-item\"\n      className={cn(\"border-b last:border-b-0\", variant === \"separated\" && \"rounded-lg border px-4 last:border-b\", classNames?.item)}\n    >\n      <AccordionPrimitive.Header className=\"flex\">\n        <AccordionPrimitive.Trigger\n          data-slot=\"accordion-trigger\"\n          className={cn(\n            \"group/trigger flex flex-1 items-start justify-between gap-4 rounded-md py-4 text-start text-sm font-medium transition-colors outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50\",\n            classNames?.trigger\n          )}\n        >\n          {item.title}\n          {/* Hovered, it leans the way the section will go: down to open, up to close. */}\n          <span\n            aria-hidden\n            className={cn(\n              \"pointer-events-none shrink-0 translate-y-0.5 text-muted-foreground transition-[translate,color] duration-(--duration-fast) ease-standard group-hover/trigger:text-foreground motion-reduce:transition-none\",\n              open ? \"group-hover/trigger:-translate-y-px\" : \"group-hover/trigger:translate-y-1\"\n            )}\n          >\n            <motion.span className=\"flex\" style={{ rotate: turn }}>\n              <ChevronDownIcon className=\"size-4\" />\n            </motion.span>\n          </span>\n        </AccordionPrimitive.Trigger>\n      </AccordionPrimitive.Header>\n      <AccordionPanel open={open} reduced={reduced} height={height} full={full} className={classNames?.content}>\n        {item.content}\n      </AccordionPanel>\n    </AccordionPrimitive.Item>\n  )\n}\n\n/**\n * A section's body: always mounted, so closing can play out (Radix would hide it at once, so `hidden` is left to the\n * height). The height is what's inside measured, or 0 when closed, and glides to it whenever either changes.\n */\nfunction AccordionPanel({\n  open,\n  reduced,\n  height,\n  full,\n  className,\n  children,\n}: {\n  open: boolean\n  reduced: boolean\n  height: MotionValue<number | \"auto\">\n  full: MotionValue<number>\n  className?: string\n  children: React.ReactNode\n}) {\n  const inside = React.useRef<HTMLDivElement>(null)\n  const target = React.useRef<number | null>(null)\n  // The first height is set, not glided to, so a section open from the start doesn't grow on load.\n  const settled = React.useRef(false)\n\n  React.useEffect(() => {\n    const node = inside.current\n    if (!node) return\n    const glide = () => {\n      full.set(node.offsetHeight)\n      const next = open ? node.offsetHeight : 0\n      if (next === target.current) return\n      target.current = next\n      if (!settled.current || reduced) height.jump(next)\n      else animate(height, next, spring.glide)\n      settled.current = true\n    }\n    glide()\n    const observer = new ResizeObserver(glide)\n    observer.observe(node)\n    return () => observer.disconnect()\n  }, [full, height, reduced, open])\n\n  return (\n    <AccordionPrimitive.Content forceMount hidden={false} asChild>\n      <motion.div data-slot=\"accordion-content\" inert={!open} style={{ height }} className=\"overflow-hidden text-sm\">\n        <motion.div\n          ref={inside}\n          className={cn(\"pt-0 pb-4\", className)}\n          initial={false}\n          animate={open ? { opacity: 1, y: 0 } : { opacity: 0, y: reduced ? 0 : -4 }}\n          transition={{ duration: open ? duration.standard : duration.fast, ease: open ? ease.enter : ease.exit }}\n        >\n          {children}\n        </motion.div>\n      </motion.div>\n    </AccordionPrimitive.Content>\n  )\n}\n"}],"cssVars":{"theme":{"ease-standard":"var(--ease-standard)","ease-enter":"var(--ease-enter)","ease-exit":"var(--ease-exit)","ease-spring":"var(--ease-spring)"},"light":{"duration-fast":"160ms","duration-standard":"240ms","duration-considered":"480ms","ease-standard":"cubic-bezier(0.2, 0, 0, 1)","ease-enter":"cubic-bezier(0, 0, 0, 1)","ease-exit":"cubic-bezier(0.3, 0, 1, 1)","ease-spring":"linear(0, 0.071 4%, 0.234 8%, 0.433 12%, 0.626 17%, 0.791 21%, 0.917 25%, 1.004 29%, 1.055 33%, 1.079 38%, 1.083 42%, 1.075 46%, 1.06 50%, 1.043 54%, 1.027 58%, 1.015 62%, 1.005 67%, 0.998 71%, 0.995 75%, 0.993 79%, 0.993 83%, 0.994 88%, 0.995 92%, 0.997 96%, 1)"},"dark":{}}}