{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"marquee-ticker","type":"registry:component","title":"Marquee Ticker","description":"An endless strip of words or logos you can grab: it glides to a stop under the pointer, drags with momentum, leans into fast movement and fades out at both edges.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/marquee-ticker.tsx","content":"\"use client\";\n\nimport { useEffect, useRef, useState, useSyncExternalStore } from \"react\";\nimport type { CSSProperties, KeyboardEvent, ReactNode } from \"react\";\n\n/**\n * MarqueeTicker: an endless strip of words or logos.\n *\n * A hand-written rAF loop moves the track, so it can do what a CSS keyframe\n * cannot: hover and keyboard focus brake it with an eased glide instead of a\n * hard stop, you can grab the strip and drag it, a flick keeps its momentum\n * and settles back to cruising speed, and fast movement leans the text a few\n * degrees into the direction of travel. The loop is seamless because one\n * \"set\" of items is measured and repeated just enough times to cover the\n * container; the offset wraps by exactly that measured width.\n *\n * Pass `items` (comma separated words) or `entries` (any nodes, e.g. inline\n * SVG wordmarks). Left and right edges fade out with a CSS mask, so it sits\n * on any background. Space pauses, arrow keys nudge. Respects\n * prefers-reduced-motion: the strip stops and becomes a scrollable row.\n * No dependencies beyond React.\n */\n\nconst ITEMS = [\"Design\", \"Ship\", \"Iterate\", \"Repeat\", \"Vibe Code\"];\n\nconst HEX = /^#[0-9a-fA-F]{6}$/;\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\n/** True when the visitor asked for reduced motion. False on the server. */\nfunction useReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nconst clamp = (n: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, n));\n\nexport type MarqueeTickerProps = {\n  /** Comma-separated words. Ignored when `entries` is given. */\n  items?: string;\n  /** Seconds for one full pass of the item set. */\n  speed?: number;\n  direction?: \"left\" | \"right\";\n  /** Eases the strip to a stop while the pointer is over it. */\n  pauseOnHover?: boolean;\n  /** Custom nodes (for example inline SVG wordmarks) instead of words. */\n  entries?: ReactNode[];\n  /** Grab and drag the strip, flick for momentum. */\n  draggable?: boolean;\n  /** Width of the edge fade in px. 0 turns it off. */\n  fadeWidth?: number;\n  /** Space between items in px. */\n  gap?: number;\n  /** Colour of the separator dot, #rrggbb. */\n  accent?: string;\n  /** Replace the dot between items; null removes it. */\n  separator?: ReactNode;\n  /** \"inherit\" uses the parent text colour; \"light\" paints light text, \"dark\" dark text. */\n  tone?: \"inherit\" | \"light\" | \"dark\";\n  /** Classes for each item (type size and weight). */\n  itemClassName?: string;\n  /** Accessible name of the region. */\n  label?: string;\n  className?: string;\n};\n\nexport function MarqueeTicker({\n  items = ITEMS.join(\", \"),\n  speed = 24,\n  direction = \"left\",\n  pauseOnHover = true,\n  entries,\n  draggable = true,\n  fadeWidth = 64,\n  gap = 48,\n  accent = \"#ef4444\",\n  separator,\n  tone = \"inherit\",\n  itemClassName = \"text-2xl font-bold tracking-tight\",\n  label = \"Scrolling ticker\",\n  className = \"\",\n}: MarqueeTickerProps) {\n  const reduced = useReducedMotion();\n  const rootRef = useRef<HTMLDivElement>(null);\n  const trackRef = useRef<HTMLDivElement>(null);\n  const groupRef = useRef<HTMLUListElement>(null);\n  const kick = useRef<(dv: number) => void>(() => {});\n  const [copies, setCopies] = useState(3);\n  const [paused, setPaused] = useState(false);\n\n  const live = useRef({ speed, direction, pauseOnHover, draggable, paused });\n  useEffect(() => {\n    live.current = { speed, direction, pauseOnHover, draggable, paused };\n  });\n\n  const nodes: ReactNode[] =\n    entries && entries.length > 0\n      ? entries\n      : items\n          .split(\",\")\n          .map((w) => w.trim())\n          .filter(Boolean);\n\n  useEffect(() => {\n    if (reduced) return;\n    const root = rootRef.current;\n    const track = trackRef.current;\n    const group = groupRef.current;\n    if (!root || !track || !group) return;\n\n    let W = 0;\n    let C = 0;\n    let o = 0;\n    let vel = 0;\n    let k = 7;\n    let last = 0;\n    let raf = 0;\n    let visible = true;\n    let hover = false;\n    let focusHold = false;\n    let down = false;\n    let lastX = 0;\n    let lastT = 0;\n    let scale = 1;\n\n    const wake = () => {\n      if (raf === 0 && visible && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n\n    function frame(t: number) {\n      raf = 0;\n      const dt = last ? Math.min((t - last) / 1000, 0.05) : 0;\n      last = t;\n      const p = live.current;\n      const base = W > 0 ? ((p.direction === \"right\" ? -1 : 1) * W) / Math.max(p.speed, 1) : 0;\n      if (down) {\n        vel *= Math.exp(-dt * 10);\n      } else {\n        const held = p.paused || focusHold || (p.pauseOnHover && hover);\n        vel += ((held ? 0 : base) - vel) * (1 - Math.exp(-dt * k));\n        o += vel * dt;\n      }\n      if (W > 0) {\n        const m = ((o % W) + W) % W;\n        const skew = clamp(vel / 300, -6, 6);\n        track!.style.transform =\n          \"translate3d(\" + (-m).toFixed(2) + \"px,0,0) skewX(\" + skew.toFixed(2) + \"deg)\";\n      }\n      wake();\n    }\n\n    const ro = new ResizeObserver((list) => {\n      for (const e of list) {\n        const size = e.borderBoxSize?.[0]?.inlineSize ?? (e.target as HTMLElement).offsetWidth;\n        if (e.target === group) W = size;\n        else C = size;\n      }\n      if (W > 0) {\n        const n = Math.max(2, Math.ceil(C / W) + 1);\n        setCopies((prev) => (prev === n ? prev : n));\n      }\n    });\n    ro.observe(group);\n    ro.observe(root);\n\n    const io = new IntersectionObserver(\n      (es) => {\n        visible = es.some((e) => e.isIntersecting);\n        if (visible) {\n          last = 0;\n          wake();\n        }\n      },\n      { rootMargin: \"200px\" },\n    );\n    io.observe(root);\n\n    const onVis = () => {\n      last = 0;\n      wake();\n    };\n    document.addEventListener(\"visibilitychange\", onVis);\n\n    const layoutScale = () => {\n      const r = root.getBoundingClientRect();\n      return r.width > 0 ? root.offsetWidth / r.width : 1;\n    };\n\n    const onEnter = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") return;\n      hover = true;\n      k = 7;\n    };\n    const onLeave = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") return;\n      hover = false;\n      k = 4;\n    };\n    const onDown = (e: PointerEvent) => {\n      if (!live.current.draggable) return;\n      if (e.pointerType === \"mouse\" && e.button !== 0) return;\n      down = true;\n      scale = layoutScale();\n      lastX = e.clientX;\n      lastT = e.timeStamp;\n      vel = 0;\n      root.setPointerCapture(e.pointerId);\n      root.dataset.drag = \"true\";\n      wake();\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!down) return;\n      const dx = (lastX - e.clientX) * scale;\n      const dt = (e.timeStamp - lastT) / 1000;\n      lastX = e.clientX;\n      lastT = e.timeStamp;\n      o += dx;\n      if (dt > 0.001) vel = vel * 0.5 + (dx / dt) * 0.5;\n      wake();\n    };\n    const onUp = (e: PointerEvent) => {\n      if (!down) return;\n      down = false;\n      vel = clamp(vel, -4000, 4000);\n      k = 2.4;\n      delete root.dataset.drag;\n      if (root.hasPointerCapture(e.pointerId)) root.releasePointerCapture(e.pointerId);\n      wake();\n    };\n    const onFocusIn = () => {\n      if (root.matches(\":focus-visible\")) {\n        focusHold = true;\n        k = 7;\n      }\n    };\n    const onFocusOut = () => {\n      focusHold = false;\n      k = 4;\n    };\n\n    root.addEventListener(\"pointerenter\", onEnter);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"pointerdown\", onDown);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerup\", onUp);\n    root.addEventListener(\"pointercancel\", onUp);\n    root.addEventListener(\"focusin\", onFocusIn);\n    root.addEventListener(\"focusout\", onFocusOut);\n\n    kick.current = (dv: number) => {\n      vel += dv;\n      k = 2.6;\n      wake();\n    };\n    wake();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n      root.removeEventListener(\"pointerenter\", onEnter);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"pointerdown\", onDown);\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerup\", onUp);\n      root.removeEventListener(\"pointercancel\", onUp);\n      root.removeEventListener(\"focusin\", onFocusIn);\n      root.removeEventListener(\"focusout\", onFocusOut);\n      kick.current = () => {};\n    };\n  }, [reduced]);\n\n  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n    if (e.target !== e.currentTarget) return;\n    if (e.key === \" \" || e.key === \"Enter\") {\n      e.preventDefault();\n      setPaused((p) => !p);\n    } else if (e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      kick.current(-900);\n    } else if (e.key === \"ArrowRight\") {\n      e.preventDefault();\n      kick.current(900);\n    }\n  };\n\n  const ink =\n    tone === \"light\"\n      ? \"rgba(255,255,255,0.58)\"\n      : tone === \"dark\"\n        ? \"rgba(10,10,12,0.55)\"\n        : \"color-mix(in srgb, currentColor 55%, transparent)\";\n  const inkHi = tone === \"light\" ? \"#ffffff\" : tone === \"dark\" ? \"#0a0a0c\" : \"currentColor\";\n  const dot = HEX.test(accent) ? accent : \"#ef4444\";\n  const fade = Math.max(0, fadeWidth);\n  const mask =\n    fade > 0\n      ? \"linear-gradient(90deg, transparent 0, #000 \" +\n        fade +\n        \"px, #000 calc(100% - \" +\n        fade +\n        \"px), transparent 100%)\"\n      : undefined;\n\n  const rootStyle = { \"--mq-c\": ink, \"--mq-hi\": inkHi } as CSSProperties;\n\n  const group = (hidden: boolean, i: number) => (\n    <ul\n      key={i}\n      ref={i === 0 ? groupRef : undefined}\n      role={hidden ? undefined : \"list\"}\n      aria-hidden={hidden || undefined}\n      className=\"m-0 flex shrink-0 list-none p-0\"\n    >\n      {nodes.map((node, j) => (\n        <li\n          key={j}\n          className={\n            \"flex shrink-0 items-center whitespace-nowrap text-[color:var(--mq-c)] transition-colors duration-200 hover:text-[color:var(--mq-hi)] \" +\n            itemClassName\n          }\n          style={{ gap: gap / 2, paddingRight: gap / 2 }}\n        >\n          <span className=\"inline-flex items-center\">{node}</span>\n          {separator === null ? null : separator !== undefined ? (\n            <span aria-hidden=\"true\" className=\"inline-flex items-center\">\n              {separator}\n            </span>\n          ) : (\n            <span\n              aria-hidden=\"true\"\n              className=\"block size-1.5 shrink-0 rounded-full\"\n              style={{ background: dot, boxShadow: \"0 0 10px 1px \" + dot + \"80\" }}\n            />\n          )}\n        </li>\n      ))}\n    </ul>\n  );\n\n  return (\n    <div\n      ref={rootRef}\n      role=\"group\"\n      aria-roledescription=\"marquee\"\n      aria-label={label}\n      tabIndex={0}\n      onKeyDown={onKeyDown}\n      style={rootStyle}\n      className={\n        \"group relative w-full select-none rounded-lg py-3 focus-visible:!rounded-xl focus-visible:!outline-2 focus-visible:!outline-offset-[-3px] focus-visible:!outline-[color:var(--mq-hi)] \" +\n        (draggable && !reduced ? \"cursor-grab data-[drag=true]:cursor-grabbing \" : \"\") +\n        className\n      }\n    >\n      <div\n        className={\n          \"w-full \" +\n          (reduced\n            ? \"overflow-x-auto [scrollbar-width:none]\"\n            : \"overflow-hidden [touch-action:pan-y]\")\n        }\n        style={{ maskImage: mask, WebkitMaskImage: mask }}\n      >\n        <div ref={trackRef} className=\"flex w-max will-change-transform\">\n          {group(false, 0)}\n          {reduced ? null : Array.from({ length: copies - 1 }, (_, i) => group(true, i + 1))}\n        </div>\n      </div>\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {paused ? \"Paused\" : \"\"}\n      </span>\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/marquee-ticker.tsx"}],"meta":{"tier":"free","tags":["marquee","ticker","scroll","loop","drag","logos","inertia"],"href":"https://viberdy.dev/library/marquee-ticker","aiPrompt":"Create a React + Tailwind \"use client\" component named MarqueeTicker: an endless horizontal strip of words or logos, hand-driven (no CSS keyframe, no libraries beyond React).\n\nProps (all optional): items (comma-separated string, default \"Design, Ship, Iterate, Repeat, Vibe Code\"), speed (seconds for one full pass of the item set, default 24), direction (\"left\" | \"right\", default \"left\"), pauseOnHover (default true), entries (ReactNode[] that replaces items, e.g. inline SVG wordmarks), draggable (default true), fadeWidth (px, default 64, 0 = none), gap (px, default 48), accent (#rrggbb validated by regex, default #ef4444, colours the separator dot), separator (ReactNode, null removes it), tone (\"inherit\" = parent text colour via currentColor, default; \"light\" = light text; \"dark\" = dark text), itemClassName (default \"text-2xl font-bold tracking-tight\"), label (aria-label, default \"Scrolling ticker\"), className.\n\nStructure: root div (role=\"group\", aria-roledescription=\"marquee\", tabIndex 0, w-full, py-3, rounded-lg) > viewport div (overflow-hidden, touch-action pan-y, CSS mask-image linear-gradient(90deg, transparent, #000 fadeWidth, #000 calc(100% - fadeWidth), transparent)) > track div (flex w-max, will-change-transform) > N copies of a <ul>. Copy 0 is the real list (role=\"list\"); the rest are aria-hidden. Each <li> is flex, whitespace-nowrap, gap and padding-right = gap/2, with the content then a 6px round separator dot (accent colour, box-shadow glow 0 0 10px 1px accent at 50%). Item text colour is color-mix(in srgb, currentColor 55%, transparent) (tone inherit; light = rgba(255,255,255,0.58), dark = rgba(10,10,12,0.55)) and goes to full colour (currentColor, #fff or #0a0a0c) on item hover, with a 200ms colour transition.\n\nSeamless loop: observe the first copy with a ResizeObserver and read its border-box inline size W (fractional); observe the root for its width C. Render n = max(2, ceil(C / W) + 1) copies. Keep a signed offset o; the track transform is translate3d(-(o mod W), 0, 0).\n\nMotion: one requestAnimationFrame loop inside a single mount effect. Cruise velocity base = (direction left ? +1 : -1) * W / speed px/s. Each frame: vel += (target - vel) * (1 - exp(-dt * k)), o += vel * dt, where target is 0 when held (pointer over and pauseOnHover, keyboard :focus-visible on the root, or the persistent pause) else base. k = 7 when hover or focus starts, 4 when it ends, 2.4 after a drag release, 2.6 after an arrow-key nudge. dt is clamped to 50ms. The track also gets skewX(clamp(vel / 300, -6, 6) deg), leaning into the direction of travel. Read props through a ref synced in an effect.\n\nDrag: pointerdown (mouse primary button or touch/pen) sets pointer capture, sets vel = 0 and stops integrating; pointermove adds (lastX - clientX) * layoutScale to o (layoutScale = root.offsetWidth / root.getBoundingClientRect().width, so scaled previews stay 1:1) and keeps a smoothed velocity vel = vel*0.5 + (dx/dt)*0.5 from event timestamps; while down and still, vel decays with exp(-dt*10). On release clamp vel to +-4000 px/s and let the easing carry it (a flick coasts, then settles to cruising speed or to a stop if held). Cursor is grab / grabbing. Ignore touch for hover.\n\nKeyboard: only when the root itself has focus. Space or Enter toggles a persistent pause (state, with a sr-only aria-live=\"polite\" region saying \"Paused\"); ArrowLeft / ArrowRight add -/+900 px/s to vel. Focus ring: 2px outline, offset -3px, colour = the hover text colour, written as important utilities.\n\nSleep the loop when the root is outside the viewport (IntersectionObserver, rootMargin 200px) or document.hidden; restart on return.\n\nReduced motion (matchMedia via useSyncExternalStore): no loop, no copies, the viewport becomes overflow-x-auto (hidden scrollbar) so the single set scrolls by hand.\n\nThe component paints no background of its own (the mask makes it work on any ground) and uses no site colour tokens; text inherits the font.","license":"https://viberdy.dev/license"}}