{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "word-relay",
  "title": "Word relay",
  "description": "A sentence whose last word rolls on every 2.4 seconds, in the italic. Press or use the keys to take over, or pause to hold a word.",
  "registryDependencies": [
    "https://thedirectors.agency/ui/r/0nlytype.json"
  ],
  "files": [
    {
      "path": "registry/0nlytype/ui/word-relay.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { flushSync } from \"react-dom\"\n\nimport { cn } from \"@/lib/utils\"\n\ntype WordRelayProps = Omit<React.ComponentProps<\"button\">, \"children\" | \"onChange\"> & {\n  /** The sentence up to the word that changes: ours, in the roman. */\n  children?: React.ReactNode\n  /** The words it can end on, in order, each with its own punctuation (\"design.\"). Pressing goes round them. */\n  words: readonly string[]\n  /** The chosen word's place in `words`, when you hold it (controlled). */\n  index?: number\n  /** The word to start on, when the relay holds it. */\n  defaultIndex?: number\n  /** Requests the next index, whether moved by the person or by autoplay. */\n  onIndexChange?: (index: number) => void\n  /** sentence: the word on a hairline at the end of the line. statement: after \"It has to be design.\", the sentence heavy and\n   *  narrow, the word large under it in the italic and the accent, overlapping it. */\n  variant?: \"sentence\" | \"statement\"\n  autoplay?: boolean\n  /** Time between words, in milliseconds; at least 1000 for a readable rest. */\n  interval?: number\n  defaultPaused?: boolean\n  pauseLabel?: string\n  playLabel?: string\n  /** Pins a state for documentation (\"hover\", \"focus\"). */\n  \"data-force\"?: string\n}\n\nconst turn = (i: number, n: number) => ((i % n) + n) % n\n\n/**\n * A sentence whose last word is yours to change. Pressing it, or an arrow key, rolls the word on to the\n * next one (out at the top, in from below; back the other way), and the line narrows or widens to it.\n * The word is set in the expression italic. Autoplay gives each word a readable rest; your action takes over.\n */\nfunction WordRelay({ children, words, index, defaultIndex = 0, onIndexChange, variant = \"sentence\", autoplay = true, interval = 2400, defaultPaused = false, pauseLabel = \"pause\", playLabel = \"play\", className, onClick, onKeyDown, \"data-force\": force, ...props }: WordRelayProps) {\n  const ref = React.useRef<HTMLSpanElement>(null)\n  const [paused, setPaused] = React.useState(defaultPaused)\n  const elapsed = React.useRef(0)\n  const automatic = React.useRef(false)\n  const [announce, setAnnounce] = React.useState(false)\n  const n = Math.max(1, words.length)\n  const [own, setOwn] = React.useState(defaultIndex)\n  const now = turn(index ?? own, n)\n  const [shown, setShown] = React.useState(now)\n  const rendered = React.useRef(now)\n  const motion = React.useRef<Animation[]>([])\n  const way = React.useRef<1 | -1>(1)\n  const word = React.useRef<HTMLSpanElement>(null)\n\n  React.useEffect(() => {\n    const el = word.current\n    if (now === rendered.current || !el) return\n    const box = el.parentElement!\n    const reduced = matchMedia(\"(prefers-reduced-motion: reduce)\")\n    let cancelled = false, frame = 0\n    const clear = () => {\n      motion.current.forEach((animation) => animation.cancel())\n      motion.current = []\n    }\n    const apply = (animate: boolean) => {\n      if (cancelled) return\n      const from = box.offsetWidth\n      flushSync(() => setShown(now))\n      rendered.current = now\n      const to = box.offsetWidth\n      clear()\n      if (!animate) return\n      motion.current.push(el.animate([{ opacity: 0, translate: `0 ${way.current > 0 ? \"0.7em\" : \"-0.7em\"}` }, { opacity: 1, translate: \"0 0\" }], { duration: 320, easing: \"cubic-bezier(.16,1,.3,1)\" }))\n      if (from !== to) motion.current.push(box.animate([{ width: `${from}px` }, { width: `${to}px` }], { duration: 320, easing: \"cubic-bezier(.65,0,.35,1)\" }))\n      if (automatic.current && ref.current?.dataset.autoplay !== \"playing\") motion.current.forEach((animation) => animation.pause())\n    }\n    const still = () => {\n      if (reduced.matches) apply(false)\n    }\n    if (reduced.matches || force?.split(\" \").includes(\"reduced\")) frame = requestAnimationFrame(() => apply(false))\n    else {\n      const out = el.animate([{ opacity: 1, translate: \"0 0\" }, { opacity: 0, translate: `0 ${way.current > 0 ? \"-0.7em\" : \"0.7em\"}` }], { duration: 160, easing: \"cubic-bezier(.65,0,.35,1)\", fill: \"forwards\" })\n      motion.current = [out]\n      if (automatic.current && ref.current?.dataset.autoplay !== \"playing\") out.pause()\n      out.finished.then(() => apply(!reduced.matches), () => {})\n    }\n    reduced.addEventListener(\"change\", still)\n    return () => {\n      cancelled = true\n      cancelAnimationFrame(frame)\n      reduced.removeEventListener(\"change\", still)\n      clear()\n    }\n  }, [now, force])\n\n  const go = (next: number, dir: 1 | -1) => {\n    const to = turn(next, n)\n    if (to === now) return\n    way.current = dir\n    if (index === undefined) setOwn(to)\n    onIndexChange?.(to)\n  }\n\n  const advance = React.useEffectEvent((delta: number) => {\n    elapsed.current += delta\n    const wait = Number.isFinite(interval) ? Math.max(1000, interval) : 2400\n    if (elapsed.current < wait) return\n    elapsed.current = 0\n    automatic.current = true\n    setAnnounce(false)\n    go(now + 1, 1)\n  })\n\n  const disabled = props.disabled\n  const count = words.length\n  // A single clock for idle motion. Every pause cancels it; resuming starts with a fresh delta.\n  React.useEffect(() => {\n    const root = ref.current\n    if (!root) return\n    const reduced = matchMedia(\"(prefers-reduced-motion: reduce)\")\n    let running = false, held = false, visible = false, hovered = root.matches(\":hover\"), frame = 0, last = 0, timer = 0, restUntil = 0\n    const forced = force?.split(\" \") ?? []\n    const tick = (time: number) => {\n      if (!running) return\n      const delta = last ? Math.min(time - last, 64) : 0\n      last = time\n      advance(delta)\n      if (running) frame = requestAnimationFrame(tick)\n    }\n    const sync = () => {\n      running = false\n      cancelAnimationFrame(frame)\n      clearTimeout(timer)\n      last = 0\n      const resting = performance.now() < restUntil\n      const stopped = !autoplay || paused || reduced.matches || forced.includes(\"reduced\") || forced.includes(\"hover\") || forced.includes(\"focus\") || !visible || hovered || held || root.contains(document.activeElement) || document.hidden || disabled || count < 2\n      root.dataset.autoplay = stopped || resting ? \"paused\" : \"playing\"\n      if (automatic.current) motion.current.forEach((animation) => {\n        if (animation.playState === \"finished\") return\n        if (stopped || resting) animation.pause()\n        else if (animation.playState === \"paused\") animation.play()\n      })\n      if (stopped) return\n      if (resting) timer = window.setTimeout(sync, restUntil - performance.now())\n      else { running = true; frame = requestAnimationFrame(tick) }\n    }\n    const rest = () => {\n      restUntil = performance.now() + 1600\n      elapsed.current = 0\n      sync()\n    }\n    const enter = (event: PointerEvent) => {\n      if (event.pointerType === \"touch\") return\n      hovered = true\n      rest()\n    }\n    const leave = () => { hovered = false; rest() }\n    const down = () => { held = true; rest() }\n    const up = () => { if (held) { held = false; rest() } }\n    const seen = new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; sync() })\n    seen.observe(root)\n    root.addEventListener(\"pointerenter\", enter)\n    root.addEventListener(\"pointerleave\", leave)\n    root.addEventListener(\"pointerdown\", down)\n    addEventListener(\"pointerup\", up)\n    addEventListener(\"pointercancel\", up)\n    root.addEventListener(\"keydown\", rest)\n    root.addEventListener(\"focusin\", rest)\n    root.addEventListener(\"focusout\", rest)\n    addEventListener(\"scroll\", rest, { passive: true })\n    document.addEventListener(\"visibilitychange\", sync)\n    reduced.addEventListener(\"change\", sync)\n    sync()\n    return () => {\n      running = false\n      cancelAnimationFrame(frame)\n      clearTimeout(timer)\n      seen.disconnect()\n      root.removeEventListener(\"pointerenter\", enter)\n      root.removeEventListener(\"pointerleave\", leave)\n      root.removeEventListener(\"pointerdown\", down)\n      removeEventListener(\"pointerup\", up)\n      removeEventListener(\"pointercancel\", up)\n      root.removeEventListener(\"keydown\", rest)\n      root.removeEventListener(\"focusin\", rest)\n      root.removeEventListener(\"focusout\", rest)\n      removeEventListener(\"scroll\", rest)\n      document.removeEventListener(\"visibilitychange\", sync)\n      reduced.removeEventListener(\"change\", sync)\n    }\n  }, [autoplay, paused, force, disabled, count])\n\n  return (\n    <span ref={ref} className=\"ot-relay-frame\" data-force={force?.split(\" \").includes(\"reduced\") ? \"reduced\" : undefined} dir={props.dir}>\n      <button\n        type=\"button\"\n        data-slot=\"word-relay\"\n        data-variant={variant === \"sentence\" ? undefined : variant}\n        data-force={force}\n        className={cn(\"ot-relay\", className)}\n        onClick={(e) => {\n          onClick?.(e)\n          if (!e.defaultPrevented) {\n            automatic.current = false\n            setAnnounce(true)\n            elapsed.current = 0\n            go(now + 1, 1)\n          }\n        }}\n        onKeyDown={(e) => {\n          onKeyDown?.(e)\n          if (e.defaultPrevented) return\n          automatic.current = false\n          setAnnounce(true)\n          elapsed.current = 0\n          const rtl = getComputedStyle(e.currentTarget).direction === \"rtl\"\n          const k = e.key === \"ArrowLeft\" && rtl ? \"ArrowRight\" : e.key === \"ArrowRight\" && rtl ? \"ArrowLeft\" : e.key\n          if (k === \"ArrowDown\" || k === \"ArrowRight\") go(now + 1, 1)\n          else if (k === \"ArrowUp\" || k === \"ArrowLeft\") go(now - 1, -1)\n          else if (k === \"Home\") go(0, -1)\n          else if (k === \"End\") go(n - 1, 1)\n          else return\n          e.preventDefault()\n        }}\n        {...props}\n      >\n        {children ? <span className=\"ot-relay-lead\">{children} </span> : null}\n        <span className=\"ot-relay-word\" aria-hidden=\"true\">\n          <span ref={word}>{words[shown]}</span>\n        </span>\n        {/* The chosen word for readers: part of the button's name, and read again when it changes. */}\n        <span className=\"ot-sr\" aria-live={announce ? \"polite\" : \"off\"}>{words[now]}</span>\n      </button>\n      {autoplay && <button type=\"button\" className=\"ot-relay-pause\" disabled={props.disabled || words.length < 2} onClick={() => setPaused((value) => !value)}>{paused ? playLabel : pauseLabel}</button>}\n    </span>\n  )\n}\n\nexport { WordRelay, type WordRelayProps }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/0nlytype/styles/word-relay.css",
      "content": "@layer components {\n  /* ── ot-relay ── a sentence whose last word is yours to change: mode-toggle's sentence, for any set of words.\n     The lead is ours, in the roman; the word is the one you chose, in the italic, on a hairline that hugs it.\n     The word rolls on and its box eases to the new width; the autoplay clock yields to the person. */\n  .ot-relay {\n    appearance: none; border: 0; background: none; padding: 0; margin: 0; font: inherit; letter-spacing: inherit;\n    color: var(--ot-graphite); text-align: start; cursor: pointer; -webkit-tap-highlight-color: transparent;\n    transition: color var(--ot-allegro) var(--ot-breath);\n  }\n  .ot-relay:is(:hover, [data-force~=\"hover\"]) { color: var(--ot-ink); }\n  .ot-relay:focus-visible, .ot-relay[data-force~=\"focus\"] { outline: var(--ot-stroke) solid var(--ot-accent); outline-offset: 0.12em; }\n  .ot-relay:disabled { opacity: 0.55; cursor: not-allowed; }\n  .ot-relay-word {\n    display: inline-block; box-sizing: border-box; vertical-align: baseline; overflow: clip; white-space: nowrap;\n    padding: 0.12em 0.14em 0.24em; margin-block: -0.12em -0.24em; margin-inline: -0.08em -0.14em;\n    font-family: var(--ot-expression); font-style: italic; font-weight: 400; font-size: calc(var(--ot-expression-scale) * 1em); line-height: 1; color: var(--ot-ink);\n    background: linear-gradient(var(--ot-rule-strong), var(--ot-rule-strong)) no-repeat 0.14em calc(100% - 0.1em) / calc(100% - 0.28em) var(--ot-hairline);\n  }\n  .ot-relay-word > span { display: inline-block; }\n  .ot-relay:is(:hover, [data-force~=\"hover\"]) .ot-relay-word { background-image: linear-gradient(var(--ot-ink), var(--ot-ink)); }\n\n  /* statement: after \"It has to be design.\", where heavy narrow roman is answered by a large italic that overlaps it, the one\n     accent on the italic word and its full stop. The sentence stands in ink; the word drops under it, half again as large,\n     rising into the line above with a halo of the paper round its letters, so it cuts the roman where they cross. */\n  .ot-relay[data-variant=\"statement\"] { display: inline-grid; justify-items: start; color: var(--ot-ink); }\n  .ot-relay[data-variant=\"statement\"] .ot-relay-lead { font-weight: 800; font-stretch: 75%; letter-spacing: -0.035em; line-height: 0.92; }\n  .ot-relay[data-variant=\"statement\"] .ot-relay-word {\n    font-size: calc(1.5 * var(--ot-expression-scale) * 1em); line-height: 0.9; margin-block: -0.42em -0.24em; margin-inline-start: 0.3em;\n    color: var(--ot-accent); background-size: 0 var(--ot-hairline);\n    paint-order: stroke fill; -webkit-text-stroke: 0.06em var(--ot-relay-ground, var(--ot-paper));\n    transition: background-size var(--ot-moderato) var(--ot-exhale);\n  }\n  /* Pointing sketches the hairline under the word in pencil: it can be changed. */\n  .ot-relay[data-variant=\"statement\"]:is(:hover, [data-force~=\"hover\"]) .ot-relay-word { background-image: linear-gradient(var(--ot-pencil), var(--ot-pencil)); background-size: calc(100% - 0.28em) var(--ot-hairline); }\n  @media (forced-colors: active) {\n    .ot-relay-word { background-image: linear-gradient(CanvasText, CanvasText); }\n    .ot-relay[data-variant=\"statement\"] .ot-relay-word { color: CanvasText; -webkit-text-stroke: 0; margin-block-start: 0; margin-inline-start: 0; }\n  }\n  .ot-relay-frame { display: inline-grid; justify-items: start; max-inline-size: 100%; vertical-align: baseline; }\n\n  /* The same small word beneath each autoplaying phrase; the parentheses are its only frame. */\n  .ot-relay-pause {\n    display: block; justify-self: start; appearance: none; border: 0; background: none; margin: 0; margin-block-start: var(--ot-space-3); padding: var(--ot-space-1) 0;\n    min-block-size: var(--ot-space-5); color: var(--ot-pencil); font: 400 var(--ot-pp) / var(--ot-pp-lh) var(--ot-voice); letter-spacing: 0; text-transform: none; cursor: pointer;\n  }\n  .ot-relay-pause::before { content: \"( \"; }\n  .ot-relay-pause::after { content: \" )\"; }\n  .ot-relay-pause:hover { color: var(--ot-ink); }\n  .ot-relay-pause:focus-visible { outline: var(--ot-stroke) solid var(--ot-accent); outline-offset: var(--ot-space-1); }\n  .ot-relay-pause:disabled { color: var(--ot-pencil); cursor: not-allowed; }\n  .ot-relay-frame[data-force~=\"reduced\"] .ot-relay-pause { display: none; }\n  @media (prefers-reduced-motion: reduce) {\n    .ot-relay-pause { display: none; }\n  }\n  @media (forced-colors: active) {\n    .ot-relay-pause { color: ButtonText; }\n    .ot-relay-pause:focus-visible { outline-color: Highlight; }\n  }\n\n}\n",
      "type": "registry:file",
      "target": "styles/0nlytype/word-relay.css"
    }
  ],
  "css": {
    "@import \"../styles/0nlytype/word-relay.css\"": {}
  },
  "meta": {
    "movement": "VI",
    "underneath": "native",
    "contract": "ot-relay"
  },
  "type": "registry:ui"
}
