{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fold",
  "title": "Fold",
  "description": "A box that folds to a set height and unfolds to its whole height on the layout role, from the height it is at, and brings its top back into view as it folds: the Code block and the Testimonials wall.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/scroll.json"
  ],
  "files": [
    {
      "path": "registry/lib/fold.ts",
      "content": "import { animateSize, startRun } from '@/registry/lib/resize'\nimport { revealBehavior } from '@/registry/lib/scroll'\nimport { type RefObject, useLayoutEffect, useRef } from 'react'\n\n/*\n * A box that folds to a set height, with more past its foot, and unfolds to\n * its whole height: the Code block's lines and the Testimonials wall. It\n * moves on `layout` from the height it is at, so a change cut short turns\n * back where it is.\n */\n\n/**\n * Moves the box to its new height when it folds or unfolds. While it moves\n * it is `data-moving`, so a cap on its folded height (styled on\n * `not-data-moving`, or on `data-folded`, which goes on only once a fold has\n * finished) never cuts the move short.\n */\nfunction useFoldMove(ref: RefObject<HTMLElement | null>, folded: boolean) {\n  const first = useRef(true)\n\n  useLayoutEffect(() => {\n    const element = ref.current\n    if (!element) return\n    if (first.current) {\n      first.current = false\n      if (folded) element.dataset.folded = ''\n      return\n    }\n    const from = element.getBoundingClientRect().height\n    element.style.height = ''\n    // The height it comes to rest at, measured with the cap on.\n    if (folded) element.dataset.folded = ''\n    else delete element.dataset.folded\n    const to = element.offsetHeight\n    delete element.dataset.folded\n    if (Math.abs(to - from) < 0.5) {\n      startRun(element)\n      delete element.dataset.moving\n      if (folded) element.dataset.folded = ''\n      return\n    }\n    element.dataset.moving = ''\n    void animateSize(element, 'height', from, to).then((latest) => {\n      if (!latest) return\n      element.style.height = ''\n      delete element.dataset.moving\n      if (folded) element.dataset.folded = ''\n    })\n  }, [ref, folded])\n}\n\n/**\n * Before a box folds: if its top has scrolled away, or sits under a sticky\n * header its scroll margin clears, brings it back, so whoever folded it\n * isn't left below what they were reading.\n */\nfunction bringTopBack(root: HTMLElement) {\n  if (\n    root.getBoundingClientRect().top < parseFloat(getComputedStyle(root).scrollMarginTop)\n  ) {\n    root.scrollIntoView({ block: 'start', behavior: revealBehavior(root) })\n  }\n}\n\nexport { bringTopBack, useFoldMove }\n",
      "type": "registry:lib",
      "target": "@lib/fold.ts"
    }
  ],
  "type": "registry:lib"
}