{
  "$schema": "https://shadcn-svelte.com/schema/registry-item.json",
  "name": "collapse",
  "title": "Collapse",
  "type": "registry:ui",
  "description": "Expands and collapses content with measured height and a real exit animation.",
  "dependencies": [
    "@zag-js/presence@1.43.3",
    "@zag-js/svelte@1.43.3"
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/svelte",
        "exportName": "Collapse",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      },
      "framework": "svelte"
    }
  },
  "categories": [
    "primitives"
  ],
  "files": [
    {
      "content": "<script lang=\"ts\">\nimport './collapse.css'\nimport {\n  type CollapseSizeObserver,\n  observeCollapseSize,\n  prefersReducedMotion,\n} from './collapse.motion.js'\nimport * as presence from '@zag-js/presence'\nimport { normalizeProps, useMachine } from '@zag-js/svelte'\nimport { untrack } from 'svelte'\nimport type { CollapseProps } from './collapse.types.js'\n\nlet { open, onExitComplete, element = $bindable(null), children, ...rest }: CollapseProps = $props()\n\nconst service = useMachine(presence.machine, () => ({\n  present: open,\n  onExitComplete: () => onExitComplete?.(),\n}))\nconst api = $derived(presence.connect(service, normalizeProps))\nlet content = $state<HTMLDivElement | null>(null)\nlet size: CollapseSizeObserver | undefined\nconst root = $derived(element)\n\n$effect(() => {\n  const node = root\n  untrack(() => api.setNode(node))\n})\n\n$effect(() => {\n  const node = root\n  const inner = content\n  if (!node || !inner) return\n  const observer = observeCollapseSize(node, inner)\n  size = observer\n  return () => {\n    observer.disconnect()\n    if (size === observer) size = undefined\n  }\n})\n\n$effect(() => {\n  void { ...rest }\n  untrack(() => size?.measure())\n})\n\n$effect(() => {\n  if (!open) untrack(() => prefersReducedMotion() && api.unmount())\n})\n</script>\n\n{#if api.present}\n  <div\n    {...rest}\n    bind:this={element}\n    data-kida-collapse=\"\"\n    data-state={open ? 'open' : 'closed'}\n    data-skip-animation={api.skip ? '' : undefined}\n  >\n    <div bind:this={content}>{@render children?.()}</div>\n  </div>\n{/if}\n",
      "type": "registry:ui",
      "target": "kida/collapse.svelte"
    },
    {
      "content": "import type { HTMLAttributes } from 'svelte/elements';\n\nexport interface CollapseProps extends HTMLAttributes<HTMLDivElement> {\n    open: boolean;\n    onExitComplete?: () => void;\n    element?: HTMLDivElement | null;\n}\n",
      "type": "registry:ui",
      "target": "kida/collapse.types.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:ui",
      "target": "kida/collapse-size.motion.ts"
    },
    {
      "content": "export { type CollapseSizeObserver, observeCollapseSize } from './collapse-size.motion.js'\nexport { prefersReducedMotion } from './reduced-motion.motion.js'\n",
      "type": "registry:ui",
      "target": "kida/collapse.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:ui",
      "target": "kida/reduced-motion.motion.ts"
    },
    {
      "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": "kida/collapse.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": "kida/tokens.css"
    }
  ]
}
