{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion",
  "type": "registry:lib",
  "title": "Motion",
  "description": "The motion tokens as numbers for the motion package (easing, durations, a pointer spring), the useMotionSafe gate, and the entrance engine the reveal components share.",
  "categories": [
    "lib"
  ],
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "@rivier-ui/theme"
  ],
  "files": [
    {
      "path": "registry/lib/motion.ts",
      "type": "registry:lib",
      "target": "lib/motion.ts",
      "content": "\"use client\";\n/**\n * @name motion\n * @category lib\n * @description The motion tokens as numbers for the motion package (easing, durations, a pointer spring), the useMotionSafe gate, and the entrance engine the reveal components share.\n * @when Any component animated with the motion package: take timing from here so it matches the CSS tokens, and gate on useMotionSafe so server markup is the final state. For CSS transitions use the duration-* and ease-out utilities instead.\n * @example\n * const safe = useMotionSafe();\n * return (\n *   <motion.div\n *     initial={safe ? { opacity: 0 } : false}\n *     animate={{ opacity: 1 }}\n *     transition={{ duration: DURATION.slow, ease: EASE }}\n *   />\n * );\n */\nimport { animate, useInView, useReducedMotion, type UseInViewOptions } from \"motion/react\";\nimport { useEffect, useLayoutEffect, useRef, useSyncExternalStore, type RefObject } from \"react\";\n\n/** Mirrors --ease: cubic-bezier(0.2, 0, 0, 1). */\nexport const EASE = [0.2, 0, 0, 1] as const;\n\n/** Mirrors --duration-instant, --duration-base, --duration-slow and --duration-scene, in seconds. */\nexport const DURATION = { instant: 0.12, base: 0.2, slow: 0.32, scene: 0.7 } as const;\n\n/** The spring pointer effects (magnetic, tilt, spotlight) settle with: quick, no visible overshoot. */\nexport const SPRING = { stiffness: 260, damping: 28, mass: 0.6 } as const;\n\nconst subscribe = (): (() => void) => () => {};\nconst onClient = (): boolean => true;\nconst onServer = (): boolean => false;\n\n/**\n * True only when motion is allowed (the visitor has not asked for reduced motion) and the component is on the\n * client. It is false on the server and for the hydration render, so server markup is always the final,\n * readable state; it is already true on the first render of a component that mounts on the client later\n * (a route change, conditional UI), so an entrance can start without a flash.\n */\nexport function useMotionSafe(): boolean {\n  const mounted = useSyncExternalStore(subscribe, onClient, onServer);\n  const reduced = useReducedMotion();\n  return mounted && !reduced;\n}\n\n/** Whether the device has a pointer that hovers precisely (a mouse or trackpad). Call it in an effect or handler. */\nexport function isFinePointer(): boolean {\n  return typeof window !== \"undefined\" && window.matchMedia(\"(hover: hover) and (pointer: fine)\").matches;\n}\n\n/** Whether the element's box is entirely outside the viewport. Call it in an effect or handler. */\nexport function isOffscreen(element: Element): boolean {\n  const rect = element.getBoundingClientRect();\n  return rect.top >= window.innerHeight || rect.bottom <= 0 || rect.left >= window.innerWidth || rect.right <= 0;\n}\n\n/** The way content travels as it arrives. \"start\" and \"end\" follow the writing direction. */\nexport type EntranceDirection = \"up\" | \"down\" | \"start\" | \"end\" | \"none\";\n\nexport type EntranceOptions = {\n  /** The way the content travels as it arrives. Default \"up\". */\n  direction?: EntranceDirection;\n  /** How far it travels, in pixels. Default 24. */\n  distance?: number;\n  /** Seconds each element takes. Default DURATION.scene. */\n  duration?: number;\n  /** Seconds before it starts: a number, or a function of the element's index for a stagger. */\n  delay?: number | ((index: number, elements: HTMLElement[]) => number);\n  /** Play once (default), or again each time it comes back into view. */\n  once?: boolean;\n  /**\n   * What happens when the content is already in view as the page loads. false (default): it is left as it is,\n   * so nothing that was readable disappears. true: it is hidden and then played, for a hero that should arrive.\n   */\n  playOnLoad?: boolean;\n  /** Grows or shrinks the viewport edge used for \"in view\", as a CSS margin (\"0px 0px -10% 0px\"). */\n  margin?: UseInViewOptions[\"margin\"];\n  /** How much of the root must be in view: \"some\" (default), \"all\", or a fraction. */\n  amount?: UseInViewOptions[\"amount\"];\n};\n\nfunction startOffset(root: HTMLElement, direction: EntranceDirection, distance: number): string | null {\n  if (direction === \"none\" || distance === 0) return null;\n  if (direction === \"up\") return `translate3d(0px, ${distance}px, 0px)`;\n  if (direction === \"down\") return `translate3d(0px, ${-distance}px, 0px)`;\n  const rtl = getComputedStyle(root).direction === \"rtl\";\n  const towardsLeft = (direction === \"start\") !== rtl;\n  return `translate3d(${towardsLeft ? distance : -distance}px, 0px, 0px)`;\n}\n\nfunction clearEntrance(element: HTMLElement): void {\n  element.style.removeProperty(\"opacity\");\n  element.style.removeProperty(\"transform\");\n}\n\n/**\n * The entrance shared by reveal, stagger and text-reveal. It watches `rootRef` (an element that is never\n * transformed, so one that starts clipped or moved out of view is still detected) and fades and moves the\n * elements `getTargets` returns. Only opacity and transform are touched, and only from effects: the markup\n * React renders is always the final state, and the inline styles are removed again when the entrance ends.\n */\nexport function useEntrance(\n  rootRef: RefObject<HTMLElement | null>,\n  getTargets: (root: HTMLElement) => HTMLElement[],\n  options: EntranceOptions = {},\n): void {\n  const { once = true, margin, amount } = options;\n  const safe = useMotionSafe();\n  const inView = useInView(rootRef, { once, margin, amount });\n\n  const latest = useRef({ getTargets, options });\n  const state = useRef<{ phase: \"idle\" | \"hidden\" | \"shown\"; seen: boolean; run: number; stop: (() => void) | null }>({\n    phase: \"idle\",\n    seen: false,\n    run: 0,\n    stop: null,\n  });\n  useLayoutEffect(() => {\n    latest.current = { getTargets, options };\n  });\n\n  const controls = useRef({\n    hide(root: HTMLElement): void {\n      const current = state.current;\n      const { direction = \"up\", distance = 24 } = latest.current.options;\n      const offset = startOffset(root, direction, distance);\n      current.stop?.();\n      current.stop = null;\n      current.run += 1;\n      for (const element of latest.current.getTargets(root)) {\n        element.style.opacity = \"0\";\n        if (offset) element.style.transform = offset;\n      }\n      current.phase = \"hidden\";\n    },\n    play(root: HTMLElement): void {\n      const current = state.current;\n      const { direction = \"up\", distance = 24, duration = DURATION.scene, delay = 0 } = latest.current.options;\n      const offset = startOffset(root, direction, distance);\n      const elements = latest.current.getTargets(root);\n      const run = (current.run += 1);\n      const delays = elements.map((_, index) => (typeof delay === \"function\" ? delay(index, elements) : delay));\n      const animations = elements.map((element, index) => {\n        const keyframes = offset ? { opacity: [0, 1], transform: [offset, \"translate3d(0px, 0px, 0px)\"] } : { opacity: [0, 1] };\n        const animation = animate(element, keyframes, { duration, ease: EASE, delay: delays[index] ?? 0 });\n        animation.finished.then(\n          () => {\n            if (state.current.run === run) clearEntrance(element);\n          },\n          () => {},\n        );\n        return animation;\n      });\n      current.stop = () => {\n        for (const animation of animations) animation.stop();\n      };\n      current.phase = \"shown\";\n    },\n    reset(root: HTMLElement): void {\n      const current = state.current;\n      current.stop?.();\n      current.stop = null;\n      current.run += 1;\n      for (const element of latest.current.getTargets(root)) clearEntrance(element);\n      current.phase = \"idle\";\n    },\n  });\n\n  // Before paint: hide only what the visitor cannot see yet (or everything, when asked to play on load).\n  useLayoutEffect(() => {\n    const root = rootRef.current;\n    if (!safe || !root) return;\n    const { hide, reset } = controls.current;\n    if (latest.current.options.playOnLoad || isOffscreen(root)) hide(root);\n    return () => reset(root);\n  }, [safe, rootRef]);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!safe || !root) return;\n    const current = state.current;\n    if (inView) {\n      current.seen = true;\n      if (current.phase === \"hidden\") controls.current.play(root);\n    } else if (!once && current.seen && current.phase !== \"hidden\") {\n      controls.current.hide(root);\n    }\n  }, [inView, safe, once, rootRef]);\n}\n"
    }
  ],
  "meta": {
    "category": "lib",
    "when": "Any component animated with the motion package: take timing from here so it matches the CSS tokens, and gate on useMotionSafe so server markup is the final state. For CSS transitions use the duration-* and ease-out utilities instead.",
    "example": "const safe = useMotionSafe();\nreturn (\n  <motion.div\n    initial={safe ? { opacity: 0 } : false}\n    animate={{ opacity: 1 }}\n    transition={{ duration: DURATION.slow, ease: EASE }}\n  />\n);",
    "client": true
  }
}
