{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "collapse",
  "title": "Collapse",
  "description": "Expands and collapses content with measured height and a real exit animation.",
  "dependencies": [
    "@zag-js/presence@^1.43.3",
    "@zag-js/react@^1.43.3"
  ],
  "files": [
    {
      "path": "packages/react/src/collapse.tsx",
      "content": "'use client'\n\nimport './collapse.css'\nimport {\n  type CollapseSizeObserver,\n  observeCollapseSize,\n  prefersReducedMotion,\n} from './motion/collapse.js'\nimport * as presence from '@zag-js/presence'\nimport { normalizeProps, useMachine } from '@zag-js/react'\nimport type { HTMLAttributes, ReactNode } from 'react'\nimport { forwardRef, useRef } from 'react'\nimport { composeRefs } from './compose-refs.js'\nimport { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js'\n\nexport interface CollapseProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {\n  /** Whether the content is shown. */\n  open: boolean\n  children?: ReactNode\n  /** Fires after the exit animation has finished and the node has been removed. */\n  onExitComplete?: () => void\n}\n\n/**\n * Height collapse with a real exit animation.\n *\n * This is the CSS-driven half of ADR D5: `@zag-js/presence` keeps the node mounted until\n * the `kida-collapse-close` keyframes finish, and the keyframes themselves live in\n * `@kida-ui/styles`. No JS animation engine is involved — which is exactly why the same\n * stylesheet will drive this component in Svelte or Vue without changes.\n *\n * The natural height is measured into `--kida-collapse-height`, because CSS cannot\n * animate to `height: auto`.\n */\nexport const Collapse = forwardRef<HTMLDivElement, CollapseProps>(function Collapse(\n  { open, children, onExitComplete, ...props },\n  forwardedRef,\n) {\n  const service = useMachine(presence.machine, { present: open, onExitComplete })\n  const api = presence.connect(service, normalizeProps)\n\n  const nodeRef = useRef<HTMLDivElement>(null)\n  const contentRef = useRef<HTMLDivElement>(null)\n  const sizeRef = useRef<CollapseSizeObserver | null>(null)\n\n  useIsomorphicLayoutEffect(() => {\n    // A reduced-motion exit has no CSS animation to produce the animationend event that\n    // presence normally waits for. Complete the lifecycle synchronously instead of leaving\n    // an invisible closed node mounted indefinitely.\n    if (!open && prefersReducedMotion()) api.unmount()\n  }, [open])\n\n  useIsomorphicLayoutEffect(() => {\n    const node = nodeRef.current\n    const content = contentRef.current\n    if (!node || !content) return\n\n    const size = observeCollapseSize(node, content)\n    sizeRef.current = size\n    return () => {\n      size.disconnect()\n      sizeRef.current = null\n    }\n  }, [api.present])\n\n  useIsomorphicLayoutEffect(() => {\n    sizeRef.current?.measure()\n  })\n\n  if (!api.present) return null\n\n  return (\n    <div\n      {...props}\n      ref={composeRefs(nodeRef, api.setNode, forwardedRef)}\n      data-kida-collapse=\"\"\n      data-state={open ? 'open' : 'closed'}\n      // Set on the very first paint when the content starts open: there is nothing to\n      // animate from, and playing the enter keyframes would be a spurious flash.\n      data-skip-animation={api.skip ? '' : undefined}\n    >\n      <div ref={contentRef}>{children}</div>\n    </div>\n  )\n})\n",
      "type": "registry:ui",
      "target": "@ui/kida/collapse.tsx"
    },
    {
      "path": "packages/react/src/compose-refs.ts",
      "content": "import type { Ref } from 'react'\n\n/** Feed one node to several refs — ours for measuring, Zag's for animation detection. */\nexport function composeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === 'function') ref(node)\n      else if (ref) (ref as { current: T | null }).current = node\n    }\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/compose-refs.ts"
    },
    {
      "path": "packages/react/src/use-isomorphic-layout-effect.ts",
      "content": "'use client'\n\nimport { useEffect, useLayoutEffect } from 'react'\n\n/**\n * `useLayoutEffect` in the browser, `useEffect` on the server.\n *\n * Motion has to be applied before paint or the user sees an unstyled frame, but React\n * warns when `useLayoutEffect` runs during SSR — where it does nothing anyway.\n */\nexport const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect\n",
      "type": "registry:hook",
      "target": "@ui/kida/use-isomorphic-layout-effect.ts"
    },
    {
      "path": "packages/motion/src/collapse-size.ts",
      "content": "interface Frame {\n  paddingTop: number\n  paddingBottom: number\n  borderTopWidth: number\n  borderBottomWidth: number\n  borderBox: boolean\n}\n\nexport interface CollapseSizeObserver {\n  /** Remeasure after the adapter updates root styles or content. */\n  measure(): void\n  /** Stop observation when presence removes the node or the adapter unmounts. */\n  disconnect(): void\n}\n\n/**\n * Measure the natural content height and root frame into the shared Collapse CSS variables.\n * The adapter owns presence; this helper owns layout and container-size observation.\n */\nexport function observeCollapseSize(root: HTMLElement, content: HTMLElement): CollapseSizeObserver {\n  let frame: Frame | undefined\n  let active = true\n\n  const readFrame = (): Frame => {\n    // Opening keyframes zero the root's frame. Suppress them across this read so we measure\n    // authored padding/borders rather than the animated values.\n    const restore = root.style.animationName\n    root.style.animationName = 'none'\n    try {\n      const style = getComputedStyle(root)\n      return {\n        paddingTop: Number.parseFloat(style.paddingTop),\n        paddingBottom: Number.parseFloat(style.paddingBottom),\n        borderTopWidth: Number.parseFloat(style.borderTopWidth),\n        borderBottomWidth: Number.parseFloat(style.borderBottomWidth),\n        borderBox: style.boxSizing === 'border-box',\n      }\n    } finally {\n      root.style.animationName = restore\n    }\n  }\n\n  const measure = () => {\n    if (!active) return\n    // Keep the frame cached during motion: suppressing a running animation to read it would\n    // restart the animation. Refresh when settled so authored style changes still take effect.\n    if (!frame || root.getAnimations().length === 0) frame = readFrame()\n\n    // CSS height uses the root's box-sizing. Only border-box includes the root's own frame.\n    let height = content.getBoundingClientRect().height\n    if (frame.borderBox) {\n      height +=\n        frame.paddingTop + frame.paddingBottom + frame.borderTopWidth + frame.borderBottomWidth\n    }\n\n    root.style.setProperty('--kida-collapse-height', `${height}px`)\n    root.style.setProperty('--kida-collapse-padding-top', `${frame.paddingTop}px`)\n    root.style.setProperty('--kida-collapse-padding-bottom', `${frame.paddingBottom}px`)\n    root.style.setProperty('--kida-collapse-border-top-width', `${frame.borderTopWidth}px`)\n    root.style.setProperty('--kida-collapse-border-bottom-width', `${frame.borderBottomWidth}px`)\n  }\n\n  measure()\n  const observer = new ResizeObserver(measure)\n  observer.observe(content)\n\n  return {\n    measure,\n    disconnect() {\n      active = false\n      observer.disconnect()\n    },\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/collapse-size.ts"
    },
    {
      "path": "packages/motion/src/collapse.ts",
      "content": "export { type CollapseSizeObserver, observeCollapseSize } from './collapse-size.js'\nexport { prefersReducedMotion } from './reduced-motion.js'\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/collapse.ts"
    },
    {
      "path": "packages/motion/src/reduced-motion.ts",
      "content": "const QUERY = '(prefers-reduced-motion: reduce)'\n\n/**\n * Whether the user has asked for reduced motion.\n *\n * SSR-safe: returns `false` when there is no `window`, matching the CSS default\n * (the `prefers-reduced-motion` media query in `@kida-ui/styles` is the real guard;\n * this is for the JS-driven paths that CSS cannot express).\n */\nexport function prefersReducedMotion(): boolean {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false\n  return window.matchMedia(QUERY).matches\n}\n\n/**\n * Subscribe to changes of the reduced-motion preference.\n * Returns an unsubscribe function. No-op (and immediately unsubscribable) on the server.\n */\nexport function watchReducedMotion(onChange: (reduced: boolean) => void): () => void {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return () => {}\n\n  const list = window.matchMedia(QUERY)\n  const handler = (event: MediaQueryListEvent) => onChange(event.matches)\n  list.addEventListener('change', handler)\n  return () => list.removeEventListener('change', handler)\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/reduced-motion.ts"
    },
    {
      "path": "packages/styles/components/collapse.css",
      "content": "@import './tokens.css';\n\n/*\n * Collapse — the CSS-driven half of ADR D5.\n *\n * `@zag-js/presence` reads the computed animation on this element to decide how long to\n * keep it mounted, so the exit transition MUST be a CSS animation. If it were a JS\n * animation the node would be torn out on the first frame.\n *\n * Height is animated rather than transform, which is not compositor-friendly — the usual\n * and unavoidable trade for collapsing to natural height. Content inside should animate\n * with opacity/transform if it needs motion of its own.\n */\n\n[data-kida-collapse] {\n  overflow: hidden;\n}\n\n[data-kida-collapse][data-state=\"open\"] {\n  animation: kida-collapse-open var(--kida-collapse-duration, var(--kida-duration-md))\n    var(--kida-collapse-ease, var(--kida-ease-out));\n}\n\n/*\n * Deliberately not `--kida-ease-in`. Both of that curve's control points sit at y=0, so\n * it is pinned near zero for most of its length: measured over the old 150ms close, the\n * panel was 4% collapsed at the halfway mark and dropped the last 53% inside ~26ms —\n * a frame and a half. It reads as the panel blinking out rather than closing. Reversing\n * the open curve is exact on paper and wrong in the eye, because the fast part lands\n * where nobody is still looking. Here the shrinking IS the information, so it has to be\n * legible, which also means matching the open duration instead of halving it.\n *\n * `forwards` holds the final frame: presence unmounts on `animationend`, and without a\n * fill mode any gap between the two lets the element revert to its natural height for a\n * frame — a full-height flash on the way out.\n */\n[data-kida-collapse][data-state=\"closed\"] {\n  animation: kida-collapse-close var(--kida-collapse-duration, var(--kida-duration-md))\n    var(--kida-collapse-ease, var(--kida-ease-in-out)) forwards;\n}\n\n/*\n * Mounted already-open: there is no previous state to animate from.\n * Declared last so it wins on order — both selectors have the same specificity, which is\n * why this needs no `!important`.\n */\n[data-kida-collapse][data-skip-animation] {\n  animation: none;\n}\n\n/* Presence checks computed animation styles before deciding whether to hold the node for an\n   exit. A 1ms reduced-motion animation can finish before that listener is attached, leaving the\n   closed node mounted forever. With no animation, presence removes it immediately. */\n@media (prefers-reduced-motion: reduce) {\n  [data-kida-collapse] {\n    animation: none;\n  }\n}\n\n/*\n * The root's vertical frame collapses with the content, because `height: 0` does not mean\n * \"invisible\": a root with a 1px border still renders a 2px box, and one with\n * `padding: 1rem` a 32px slab. Left in, that residue sits perfectly static at the end of\n * the close until presence unmounts — a hairline that hangs for a few frames and then\n * pops out.\n *\n * Both ends of every property are written out on purpose. An omitted end is supposed to\n * be filled in from the element's own value, and for `opacity` it is, but Chrome does not\n * synthesise one for border widths: a keyframe naming only `border-top-width: 0` leaves\n * the border sitting at its authored width for the whole animation. Hence the four\n * variables, which the component measures off the root and writes back. They are outputs,\n * not knobs — the `0px` fallbacks only stand in for the frame before the first\n * measurement lands, and a root with no border or padding animates 0 to 0 for free.\n *\n * Padding collapses cleanly, sub-pixel and all the way down. A border does not: Chrome\n * renders any width between 0 and 1px as a full hairline, so a 1px border reads as 1px\n * for the entire close and only disappears on the final frame. That is enough to keep the\n * last frame honest, but a bordered root still hits a 2px floor slightly before the\n * animation ends and holds it for a few frames. There is no CSS answer to that, so the\n * rule is: keep borders on the content, and let this root be the thing that clips it.\n */\n@keyframes kida-collapse-open {\n  from {\n    height: 0;\n    padding-top: 0;\n    padding-bottom: 0;\n    border-top-width: 0;\n    border-bottom-width: 0;\n  }\n  to {\n    height: var(--kida-collapse-height);\n    padding-top: var(--kida-collapse-padding-top, 0px);\n    padding-bottom: var(--kida-collapse-padding-bottom, 0px);\n    border-top-width: var(--kida-collapse-border-top-width, 0px);\n    border-bottom-width: var(--kida-collapse-border-bottom-width, 0px);\n  }\n}\n\n@keyframes kida-collapse-close {\n  from {\n    height: var(--kida-collapse-height);\n    padding-top: var(--kida-collapse-padding-top, 0px);\n    padding-bottom: var(--kida-collapse-padding-bottom, 0px);\n    border-top-width: var(--kida-collapse-border-top-width, 0px);\n    border-bottom-width: var(--kida-collapse-border-bottom-width, 0px);\n  }\n  to {\n    height: 0;\n    padding-top: 0;\n    padding-bottom: 0;\n    border-top-width: 0;\n    border-bottom-width: 0;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/kida/collapse.css"
    },
    {
      "path": "packages/styles/tokens.css",
      "content": "/*\n * Kida UI — motion tokens.\n * Plain CSS custom properties (ADR D7). No preprocessor, no Tailwind, no build step.\n */\n\n:root {\n  /* Durations */\n  --kida-duration-xs: 100ms;\n  --kida-duration-sm: 150ms;\n  --kida-duration-md: 300ms;\n  --kida-duration-lg: 500ms;\n  --kida-duration-xl: 800ms;\n\n  /* Easings */\n  --kida-ease-out: cubic-bezier(0.16, 1, 0.3, 1);\n  --kida-ease-in: cubic-bezier(0.7, 0, 0.84, 0);\n  --kida-ease-in-out: cubic-bezier(0.65, 0, 0.35, 1);\n  --kida-ease-spring: linear(0, 0.5, 0.9, 1.02, 1);\n\n  /* Travel distances used by the enter/exit keyframes */\n  --kida-distance-sm: 4px;\n  --kida-distance-md: 8px;\n  --kida-distance-lg: 16px;\n\n  /* Stagger step between siblings */\n  --kida-stagger: 60ms;\n}\n\n/*\n * The shared reduced-motion guard for CSS-driven animation in the library.\n * Durations collapse to ~0 by default. Components whose presence lifecycle cannot safely observe\n * a 1ms animation, such as Collapse, disable it explicitly and complete presence in their adapter.\n */\n@media (prefers-reduced-motion: reduce) {\n  :root {\n    --kida-duration-xs: 1ms;\n    --kida-duration-sm: 1ms;\n    --kida-duration-md: 1ms;\n    --kida-duration-lg: 1ms;\n    --kida-duration-xl: 1ms;\n    --kida-distance-sm: 0px;\n    --kida-distance-md: 0px;\n    --kida-distance-lg: 0px;\n    --kida-stagger: 0ms;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/kida/tokens.css"
    }
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/react",
        "exportName": "Collapse",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      }
    }
  },
  "categories": [
    "primitives"
  ],
  "type": "registry:ui"
}
