{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sheet-physics",
  "title": "Sheet physics",
  "description": "The feel's spring for a role as the spring ui-sdk moves a drawer with, read where the drawer is, so a data-feel or data-motion around it re-times or stops it.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/motion-tokens.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/lib/sheet-physics.tsx",
      "content": "'use client'\n\nimport { useMotionSpeed } from '@/registry/lib/motion-speed'\nimport {\n  atSpeed,\n  DEFAULT_FEEL,\n  FEEL_NAMES,\n  type FeelName,\n  feels,\n  type MotionRole,\n  type MotionSpring,\n} from '@/registry/lib/motion-tokens'\nimport { reducesMotion, watchReducedMotion } from '@/registry/lib/reduce-motion'\nimport type { SheetPhysics } from '@ui-sdk/react/drawer'\nimport { useMemo, useSyncExternalStore } from 'react'\n\n/*\n * ui-sdk moves a drawer itself, on a spring of its own (`physics`), so the\n * feel's CSS variables can't time it. This turns the feel's spring for a\n * role into that spring, read where the drawer is: a nested `data-feel`\n * re-times it and a nested `data-motion='reduce'` stops it, as they do CSS.\n */\n\n/**\n * ui-sdk's spring for one of the feel's: the same spring `motion` plays for a\n * `visualDuration` and `bounce`, whose period is 1.2 times the visual\n * duration and whose damping ratio is `1 - bounce`, so a drawer settles in\n * step with CSS on the same role. `speed` plays it slower or faster, as the\n * playground's Speed does. With `reduced`, it lands at once.\n */\nfunction sheetPhysicsOf(\n  { visualDuration, bounce }: MotionSpring,\n  speed = 1,\n  reduced = false,\n): Pick<SheetPhysics, 'response' | 'dampingRatio' | 'momentumDampingRatio'> {\n  const dampingRatio = Math.min(1, Math.max(0.05, 1 - bounce))\n  return {\n    // ui-sdk settles a spring within a millisecond at this period.\n    response: reduced ? 0.0001 : atSpeed(visualDuration * 1.2, speed),\n    dampingRatio,\n    // A flick keeps ui-sdk's small overshoot only where the feel bounces.\n    momentumDampingRatio: dampingRatio,\n  }\n}\n\n/** The feel set on `element` or above it, or on the page. */\nfunction feelAt(element: Element | null): FeelName {\n  const feel = (element ?? document.documentElement)\n    .closest('[data-feel]')\n    ?.getAttribute('data-feel')\n  return FEEL_NAMES.find((name) => name === feel) ?? DEFAULT_FEEL\n}\n\nlet settingsChanged = 0\n\nfunction watchMotionSettings(onChange: () => void) {\n  return watchReducedMotion(() => {\n    settingsChanged++\n    onChange()\n  }, ['data-feel'])\n}\n\n/**\n * The `physics` for a drawer in this role, at the playground's Speed, in the\n * feel and the motion preference where `at()` is (its content once it shows,\n * else its trigger). ui-sdk reads it as the drawer opens, once its content\n * is in the page, so a drawer opened with no trigger reads where it is too.\n */\nfunction useSheetPhysics(role: MotionRole, at: () => Element | null) {\n  const changed = useSyncExternalStore(\n    watchMotionSettings,\n    () => settingsChanged,\n    () => 0,\n  )\n  const speed = useMotionSpeed()\n  return useMemo(() => {\n    const spring = () => {\n      const element = at()\n      return sheetPhysicsOf(feels[feelAt(element)][role], speed, reducesMotion(element))\n    }\n    return {\n      get response() {\n        return spring().response\n      },\n      get dampingRatio() {\n        return spring().dampingRatio\n      },\n      get momentumDampingRatio() {\n        return spring().momentumDampingRatio\n      },\n      // A new spring when a setting changes, which ui-sdk reads again.\n      changed,\n    }\n  }, [role, speed, at, changed])\n}\n\nexport { sheetPhysicsOf, useSheetPhysics }\n",
      "type": "registry:lib",
      "target": "@lib/sheet-physics.tsx"
    }
  ],
  "type": "registry:lib"
}