{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-animated-height",
  "title": "useAnimatedHeight",
  "description": "A custom React hook for animated height.",
  "files": [
    {
      "path": "registry/default/hooks/use-animated-height.ts",
      "content": "import { useCallback, useEffect, useRef } from \"react\";\n\nconst MIN_FADE_DURATION = 0.15;\nconst MAX_FADE_DURATION = 0.27;\n\n/**\n * Tracks the inner element's height and writes it onto the outer element, plus\n * a size-adaptive `--fade-duration`.\n *\n * `onResize` runs after each write with the measured height and the outer\n * element. Consumers driving their own height animation use it to retarget when\n * content resizes mid-flight. It's held in a ref, so passing an inline function\n * won't tear down and rebuild the observer on every render. Pass a stable\n * callback if it reads state: the ref syncs in a passive effect (after paint)\n * while the observer fires before it, so an inline closure can be a render late.\n */\nexport function useAnimatedHeight(\n  onResize?: (height: number, outer: HTMLElement) => void,\n) {\n  const outerRef = useRef<HTMLDivElement>(null);\n  const observerRef = useRef<ResizeObserver | null>(null);\n  const previousHeight = useRef(0);\n\n  // Held in a ref rather than `useEffectEvent`: the observer is attached from a\n  // callback ref, and an effect event closed over inside that `useCallback`\n  // trips the React Compiler's memoization check (it wants the event in the\n  // deps, which effect events must never go in). Refs are recognized as stable,\n  // so this keeps the observer from rebuilding on every render.\n  const onResizeRef = useRef(onResize);\n  useEffect(() => {\n    onResizeRef.current = onResize;\n  }, [onResize]);\n\n  const innerRef = useCallback((node: HTMLDivElement | null) => {\n    if (observerRef.current) {\n      observerRef.current.disconnect();\n      observerRef.current = null;\n    }\n\n    if (!node) return;\n\n    const observer = new ResizeObserver((entries) => {\n      const outer = outerRef.current;\n      if (!outer) return;\n      // Use the layout border-box height, not `getBoundingClientRect`. The\n      // bounding rect includes ancestor transforms (e.g. a parent popover's\n      // open-time `scale-95` → `scale-100`), which previously caused this\n      // hook to write the *scaled* height onto the outer element on first\n      // observation — under-sizing the container until a later layout\n      // change happened to trigger another ResizeObserver tick.\n      const entry = entries[0];\n      const height =\n        entry.borderBoxSize?.[0]?.blockSize ??\n        (entry.target as HTMLElement).offsetHeight;\n      if (height > 0) {\n        const diff = Math.abs(height - previousHeight.current);\n        previousHeight.current = height;\n        const fadeDuration = Math.min(\n          Math.max(diff / 500, MIN_FADE_DURATION),\n          MAX_FADE_DURATION,\n        );\n\n        outer.style.height = `${height}px`;\n        outer.style.setProperty(\"--fade-duration\", `${fadeDuration}s`);\n        onResizeRef.current?.(height, outer);\n      }\n    });\n\n    observer.observe(node);\n    observerRef.current = observer;\n  }, []);\n\n  useEffect(() => {\n    return () => {\n      observerRef.current?.disconnect();\n    };\n  }, []);\n\n  return { outerRef, innerRef };\n}\n",
      "type": "registry:hook",
      "target": "hooks/cubby-ui/use-animated-height.ts"
    }
  ],
  "type": "registry:hook"
}