{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hold",
  "title": "Hold",
  "description": "Whether something that changes on its own may change now: held while a mouse is over it, a finger presses it or anything inside has focus, and its turns timed by the feel while it is in view: the Testimonials and Logo cloud marquees and the Logo cloud swap.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/resize.json"
  ],
  "files": [
    {
      "path": "registry/lib/hold.ts",
      "content": "import { FOLLOWS_TRANSITION, roleDuration } from '@/registry/lib/resize'\nimport {\n  type FocusEvent,\n  type PointerEvent,\n  type RefObject,\n  useEffect,\n  useRef,\n  useState,\n} from 'react'\n\n/*\n * Whether something that changes on its own, such as a marquee's drift or a\n * logo that swaps, may change now: it holds while a mouse is over it, while\n * a finger or a pen presses it (there is no hover), and while anything\n * inside has focus, so nothing moves while someone reads by keyboard.\n *\n *   const hold = useHold()\n *   <div {...hold.handlers}>…</div>\n *   useEffect(() => { if (hold.running) … }, [hold.running])\n */\nfunction useHold() {\n  const [hovered, setHovered] = useState(false)\n  const [focused, setFocused] = useState(false)\n  const handlers = {\n    onPointerEnter: (event: PointerEvent) =>\n      event.pointerType === 'mouse' && setHovered(true),\n    onPointerDown: () => setHovered(true),\n    onPointerUp: (event: PointerEvent) =>\n      event.pointerType !== 'mouse' && setHovered(false),\n    onPointerCancel: () => setHovered(false),\n    onPointerLeave: () => setHovered(false),\n    onFocus: () => setFocused(true),\n    onBlur: (event: FocusEvent) => {\n      if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false)\n    },\n  }\n  return { running: !hovered && !focused, handlers }\n}\n\n/**\n * Calls `turn` every `every` `layout` durations while `running`, the element\n * is in view and the page is shown, such as a logo that swaps or a pulse\n * that runs: the next turn of something that changes on its own.\n */\nfunction useTurns(\n  ref: RefObject<HTMLElement | null>,\n  running: boolean,\n  every: number,\n  turn: () => void,\n) {\n  const turnRef = useRef(turn)\n  useEffect(() => {\n    turnRef.current = turn\n  })\n  useEffect(() => {\n    const element = ref.current\n    if (!element || !running) return\n    let seen = false\n    // The wait is `layout`'s, so a tool that slows the roles, such as the playground's Speed, slows it too.\n    let clock: Animation | undefined\n    const schedule = () => {\n      clock?.cancel()\n      clock = undefined\n      if (!seen || document.hidden) return\n      const wait = roleDuration(element, 'layout') * every\n      if (!(wait > 0)) return\n      const next = element.animate([], { duration: wait, id: FOLLOWS_TRANSITION })\n      clock = next\n      next.finished.then(\n        () => {\n          if (clock !== next) return\n          turnRef.current()\n          schedule()\n        },\n        // Cancelled: held, out of view or gone.\n        () => {},\n      )\n    }\n    const sight = new IntersectionObserver(([entry]) => {\n      seen = entry?.isIntersecting ?? false\n      schedule()\n    })\n    sight.observe(element)\n    document.addEventListener('visibilitychange', schedule)\n    return () => {\n      clock?.cancel()\n      sight.disconnect()\n      document.removeEventListener('visibilitychange', schedule)\n    }\n  }, [ref, running, every])\n}\n\nexport { useHold, useTurns }\n",
      "type": "registry:lib",
      "target": "@lib/hold.ts"
    }
  ],
  "type": "registry:lib"
}