{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"slices","title":"Slice text","description":"Strips fly in from both sides and align into letters. Move over it and it cuts apart around the pointer.","type":"registry:component","author":"Detent","dependencies":["motion"],"registryDependencies":["https://detentui.com/r/theme.json"],"files":[{"path":"components/detent/components/slices/SliceText.tsx","target":"components/detent/components/slices/SliceText.tsx","type":"registry:component","content":"'use client'\n\nimport { useEffect, useLayoutEffect, useRef } from 'react'\nimport type { PointerEvent } from 'react'\nimport { motion, useInView, useReducedMotion } from 'motion/react'\n\nconst OUT = [0.16, 1, 0.3, 1] as const\n\nexport function SliceText({\n  text,\n  strips = 16,\n  fill,\n  reach = 26,\n  interactive = true,\n  className = '',\n}: {\n  text: string\n  strips?: number\n  fill?: string\n  reach?: number\n  interactive?: boolean\n  className?: string\n}) {\n  const box = useRef<HTMLSpanElement>(null)\n  const parts = useRef<(HTMLSpanElement | null)[]>([])\n  const aim = useRef<number | null>(null)\n  const wake = useRef(() => {})\n  const seen = useInView(box, { once: true, amount: 0.4 })\n  const reduce = useReducedMotion()\n  const mid = (strips - 1) / 2\n\n  useLayoutEffect(() => {\n    const el = box.current\n    const host = el?.parentElement\n    if (!el || !host) return\n    const fit = () => {\n      el.style.fontSize = ''\n      const pad = getComputedStyle(host)\n      const room = host.clientWidth - parseFloat(pad.paddingLeft) - parseFloat(pad.paddingRight)\n      const own = el.offsetWidth\n      if (room > 0 && own > room) el.style.fontSize = `${(parseFloat(getComputedStyle(el).fontSize) * room) / own}px`\n    }\n    fit()\n    const ro = new ResizeObserver(fit)\n    ro.observe(host)\n    return () => ro.disconnect()\n  }, [text])\n\n  useEffect(() => {\n    if (!interactive || reduce) return\n    const now = Array.from({ length: strips }, () => 0)\n    let raf = 0\n    let last = 0\n    let shown = true\n    const loop = (time: number) => {\n      raf = 0\n      const dt = Math.min(0.05, (time - last) / 1000 || 0.016)\n      last = time\n      let moving = false\n      for (let i = 0; i < strips; i++) {\n        const at = (i + 0.5) / strips\n        const near = aim.current === null ? 0 : Math.exp(-(((at - aim.current) / 0.1) ** 2))\n        const want = (i % 2 ? 1 : -1) * near * reach\n        now[i] += (want - now[i]) * (1 - Math.exp(-dt * 11))\n        if (Math.abs(want - now[i]) < 0.02) now[i] = want\n        else moving = true\n        const node = parts.current[i]\n        if (node) node.style.transform = `translate3d(${now[i].toFixed(2)}px, 0, 0)`\n      }\n      if (moving && shown) raf = requestAnimationFrame(loop)\n    }\n    wake.current = () => {\n      if (raf || !shown) return\n      last = performance.now()\n      raf = requestAnimationFrame(loop)\n    }\n    const io = new IntersectionObserver(([e]) => {\n      shown = e.isIntersecting\n      if (shown) wake.current()\n    })\n    if (box.current) io.observe(box.current)\n    return () => {\n      cancelAnimationFrame(raf)\n      io.disconnect()\n      wake.current = () => {}\n    }\n  }, [strips, reach, interactive, reduce])\n\n  const aimAt = (e: PointerEvent<HTMLSpanElement>) => {\n    const r = box.current!.getBoundingClientRect()\n    aim.current = (e.clientY - r.top) / r.height\n    wake.current()\n  }\n\n  return (\n    <span\n      ref={box}\n      role=\"img\"\n      aria-label={text}\n      onPointerDown={aimAt}\n      onPointerMove={aimAt}\n      onPointerLeave={() => {\n        aim.current = null\n        wake.current()\n      }}\n      className={`relative inline-block whitespace-nowrap leading-[1.12] ${className}`}\n    >\n      <span className=\"invisible\">{text}</span>\n      {Array.from({ length: strips }, (_, i) => (\n        <span\n          key={i}\n          ref={(n) => {\n            parts.current[i] = n\n          }}\n          aria-hidden\n          className=\"absolute inset-0\"\n          style={{ clipPath: `inset(${(i / strips) * 100}% -20% ${((strips - 1 - i) / strips) * 100 - 0.3}% -20%)` }}\n        >\n          <motion.span\n            className={`absolute inset-0 ${fill ? 'bg-clip-text text-transparent' : ''}`}\n            style={fill ? { backgroundImage: fill } : undefined}\n            initial={reduce ? false : { x: `${(i % 2 ? 1 : -1) * (90 + Math.abs(i - mid) * 16)}%` }}\n            animate={seen || reduce ? { x: '0%' } : undefined}\n            transition={{ duration: 1.25, delay: Math.abs(i - mid) * 0.028, ease: OUT }}\n          >\n            {text}\n          </motion.span>\n        </span>\n      ))}\n    </span>\n  )\n}\n"}]}