{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "word-cycle",
  "title": "Word Cycle",
  "description": "One word at a time inside a line of text, each fading and rising into place in the width of the longest word. No dependencies.",
  "files": [
    {
      "path": "registry/mewo/ui/word-cycle.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cn } from \"@/lib/utils\"\n\n/** The longest word: the cycle keeps its width so the line around it never jumps. */\nexport function longestWord(words: string[]) {\n  return words.reduce((a, b) => (b.length > a.length ? b : a), \"\")\n}\n\nexport interface WordCycleProps extends React.ComponentProps<\"span\"> {\n  /** The words, shown one at a time in place. */\n  words?: string[]\n  /** Milliseconds each word stays. */\n  interval?: number\n}\n\n/** One word at a time in a line of text: each fades and rises into place, in the width of the longest word. */\nexport function WordCycle({ words = [\"political\", \"economic\", \"cultural\"], interval = 2200, className, ...props }: WordCycleProps) {\n  const [current, setCurrent] = React.useState(0)\n  const sizer = React.useRef<HTMLSpanElement>(null)\n\n  // the cycle runs only while the line is on screen (watched through its sizing span, so the root's ref stays the consumer's)\n  React.useEffect(() => {\n    if (words.length < 2) return\n    let timer = 0\n    const tick = () => {\n      timer = window.setTimeout(() => {\n        setCurrent((w) => (w + 1) % words.length)\n        tick()\n      }, interval)\n    }\n    const stop = () => {\n      window.clearTimeout(timer)\n      timer = 0\n    }\n    const el = sizer.current\n    const observer =\n      el && typeof IntersectionObserver !== \"undefined\"\n        ? new IntersectionObserver(\n            ([entry]) => {\n              if (entry?.isIntersecting) {\n                if (!timer) tick()\n              } else stop()\n            },\n            { rootMargin: \"50% 0px\" }\n          )\n        : null\n    // runs from the start (an observer reports its first state right after observe, and stops it when off screen)\n    tick()\n    observer?.observe(el!)\n    return () => {\n      stop()\n      observer?.disconnect()\n    }\n  }, [words.length, interval])\n\n  return (\n    <span data-slot=\"word-cycle\" className={cn(\"inline-grid align-top\", className)} {...props}>\n      {words.map((w, i) => (\n        <span\n          key={w}\n          aria-hidden={i !== current}\n          className={cn(\n            \"[grid-area:1/1] transition-[opacity,translate] duration-600 ease-[cubic-bezier(0.25,0.46,0.45,0.94)] motion-reduce:transition-none\",\n            i === current ? \"translate-y-0 opacity-100\" : \"translate-y-[0.3em] opacity-0\"\n          )}\n        >\n          {w}\n        </span>\n      ))}\n      <span ref={sizer} className=\"invisible [grid-area:1/1]\" aria-hidden=\"true\">\n        {longestWord(words)}\n      </span>\n    </span>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "meta": {
    "added": "2026-10-27",
    "unlisted": true
  },
  "categories": [
    "text"
  ],
  "type": "registry:ui"
}