{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"true-focus","type":"registry:ui","title":"True Focus","description":"A sentence where one word at a time is sharp, framed by corner brackets that glide from word to word.","dependencies":["motion"],"registryDependencies":["https://ui.mekaigames.com/r/theme.json"],"files":[{"path":"components/ui/true-focus.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { motion, useAnimate, useReducedMotion } from \"motion/react\";\nimport { Fragment, useEffect, useRef, useState, type CSSProperties } from \"react\";\n\ntype TrueFocusProps = {\n  text: string;\n  /** \"auto\" moves the focus on a timer, \"manual\" follows the pointer and keyboard only. */\n  mode?: \"auto\" | \"manual\";\n  /** Milliseconds per word in auto mode. */\n  interval?: number;\n  /** Blur in pixels for the words out of focus. */\n  blur?: number;\n  frameColor?: string;\n  /** Space in pixels between a word and its frame. */\n  padding?: number;\n  className?: string;\n};\n\nconst CORNERS: { className: string; sides: (keyof CSSProperties)[] }[] = [\n  { className: \"left-0 top-0 rounded-tl-[0.1em]\", sides: [\"borderTopWidth\", \"borderLeftWidth\"] },\n  { className: \"right-0 top-0 rounded-tr-[0.1em]\", sides: [\"borderTopWidth\", \"borderRightWidth\"] },\n  { className: \"bottom-0 left-0 rounded-bl-[0.1em]\", sides: [\"borderBottomWidth\", \"borderLeftWidth\"] },\n  { className: \"bottom-0 right-0 rounded-br-[0.1em]\", sides: [\"borderBottomWidth\", \"borderRightWidth\"] },\n];\n\n/** A sentence where one word at a time is sharp, framed by corner brackets that glide between words. */\nexport function TrueFocus({\n  text,\n  mode = \"auto\",\n  interval = 1800,\n  blur = 5,\n  frameColor = \"#ff784f\",\n  padding = 10,\n  className = \"\",\n}: TrueFocusProps) {\n  const words = text.split(/\\s+/).filter(Boolean);\n  const count = words.length;\n  const reduce = useReducedMotion();\n  const [index, setIndex] = useState(0);\n  const [hovering, setHovering] = useState(false);\n  const [focused, setFocused] = useState(false);\n  const root = useRef<HTMLSpanElement>(null);\n  const [frame, animate] = useAnimate<HTMLSpanElement>();\n  const placed = useRef(false);\n  const currentRef = useRef(0);\n  const current = count ? index % count : 0;\n\n  // Auto mode: step through the words, pausing while the reader is pointing or tabbing.\n  useEffect(() => {\n    if (mode !== \"auto\" || hovering || focused || reduce || count < 2) return;\n    const id = setInterval(() => setIndex((i) => (i + 1) % count), Math.max(400, interval));\n    return () => clearInterval(id);\n  }, [mode, hovering, focused, reduce, count, interval]);\n\n  useEffect(() => {\n    const place = (i: number, instant: boolean) => {\n      const word = root.current?.querySelector<HTMLElement>(`[data-word=\"${i}\"]`);\n      if (!word || !frame.current) return;\n      animate(\n        frame.current,\n        {\n          x: word.offsetLeft - padding,\n          y: word.offsetTop - padding,\n          width: word.offsetWidth + padding * 2,\n          height: word.offsetHeight + padding * 2,\n          opacity: 1,\n        },\n        instant || reduce ? { duration: 0 } : { type: \"spring\", stiffness: 260, damping: 28 },\n      );\n    };\n    currentRef.current = current;\n    place(current, !placed.current);\n    placed.current = true;\n  }, [current, padding, reduce, animate, frame]);\n\n  // Keep the frame on its word when the text reflows.\n  useEffect(() => {\n    const el = root.current;\n    if (!el) return;\n    const observer = new ResizeObserver(() => {\n      const word = el.querySelector<HTMLElement>(`[data-word=\"${currentRef.current}\"]`);\n      if (!word || !frame.current) return;\n      animate(\n        frame.current,\n        {\n          x: word.offsetLeft - padding,\n          y: word.offsetTop - padding,\n          width: word.offsetWidth + padding * 2,\n          height: word.offsetHeight + padding * 2,\n        },\n        { duration: 0 },\n      );\n    });\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, [padding, animate, frame]);\n\n  const border = \"max(2px, 0.045em)\";\n\n  return (\n    <span\n      ref={root}\n      className={`relative inline-block ${className}`}\n      onPointerEnter={() => setHovering(true)}\n      onPointerLeave={() => setHovering(false)}\n    >\n      {words.map((word, i) => {\n        const sharp = reduce || i === current;\n        return (\n          <Fragment key={i}>\n            {i > 0 ? \" \" : null}\n            <motion.span\n              data-word={i}\n              tabIndex={0}\n              className=\"relative inline-block focus-visible:outline-none\"\n              onPointerEnter={() => setIndex(i)}\n              onFocus={() => {\n                setIndex(i);\n                setFocused(true);\n              }}\n              onBlur={() => setFocused(false)}\n              initial={false}\n              animate={{ filter: sharp ? \"blur(0px)\" : `blur(${blur}px)`, opacity: sharp ? 1 : 0.55 }}\n              transition={{ duration: reduce ? 0 : 0.45, ease: \"easeOut\" }}\n            >\n              {word}\n            </motion.span>\n          </Fragment>\n        );\n      })}\n      <span\n        ref={frame}\n        aria-hidden=\"true\"\n        className=\"pointer-events-none absolute left-0 top-0\"\n        style={{ opacity: 0, filter: `drop-shadow(0 0 0.12em ${frameColor}99)` }}\n      >\n        {CORNERS.map((corner, i) => (\n          <span\n            key={i}\n            className={`absolute size-[0.3em] min-h-2.5 min-w-2.5 border-solid ${corner.className}`}\n            style={{\n              borderColor: frameColor,\n              borderWidth: 0,\n              ...Object.fromEntries(corner.sides.map((side) => [side, border])),\n            }}\n          />\n        ))}\n      </span>\n    </span>\n  );\n}\n"}]}