Marquee

An infinite scroller for any children: horizontal or vertical, either direction, speed in pixels per second (measured, so long and short rows move at the same pace), adjustable gap, edge fades, pause on hover or focus and a paused prop for your own pause button. It adds as many copies as the track needs, hides the copies from screen readers and keyboard, and shows one static wrapped copy when reduced motion is on.

Animation & motionComponentTailwind v4

Source

"use client";

import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from "react";
import { cn } from "@/lib/utils";

export type MarqueeProps = {
  children: ReactNode;
  /** Scroll top to bottom instead of left to right. */
  vertical?: boolean;
  /** Run the other way (right to left becomes left to right, up becomes down). */
  reverse?: boolean;
  /** Speed in pixels per second, so long and short rows move at the same pace. */
  speed?: number;
  /** Space between items and between copies, any CSS length. */
  gap?: string;
  pauseOnHover?: boolean;
  /** Stop the motion from outside, for example from a pause button. */
  paused?: boolean;
  /** Fade the leading and trailing edges into the background. */
  fade?: boolean;
  /** Minimum number of copies. More are added automatically when the content is shorter than the track. */
  repeat?: number;
  className?: string;
  /** Classes for each copy of the children, for example to change the item alignment. */
  trackClassName?: string;
  "aria-label"?: string;
};

const KEYFRAMES = `@keyframes chm-marquee-x{from{transform:translate3d(0,0,0)}to{transform:translate3d(calc(-100% - var(--chm-marquee-gap)),0,0)}}
@keyframes chm-marquee-y{from{transform:translate3d(0,0,0)}to{transform:translate3d(0,calc(-100% - var(--chm-marquee-gap)),0)}}`;

export function Marquee({
  children,
  vertical = false,
  reverse = false,
  speed = 40,
  gap = "1.5rem",
  pauseOnHover = true,
  paused = false,
  fade = true,
  repeat = 2,
  className,
  trackClassName,
  "aria-label": ariaLabel,
}: MarqueeProps) {
  const rootRef = useRef<HTMLDivElement>(null);
  const copyRef = useRef<HTMLDivElement>(null);
  const [copies, setCopies] = useState(Math.max(2, repeat));
  const [duration, setDuration] = useState(30);

  // Measure one copy: its length sets the duration (constant px/s) and how many copies fill the track.
  useEffect(() => {
    const root = rootRef.current;
    const copy = copyRef.current;
    if (!root || !copy || typeof ResizeObserver === "undefined") return;
    const measure = () => {
      const gapPx = parseFloat(getComputedStyle(root).rowGap || "0") || 0;
      const length = vertical ? copy.offsetHeight : copy.offsetWidth;
      const track = vertical ? root.offsetHeight : root.offsetWidth;
      if (!length) return;
      setDuration((length + gapPx) / Math.max(speed, 1));
      setCopies(Math.max(2, repeat, Math.ceil(track / (length + gapPx)) + 1));
    };
    measure();
    const observer = new ResizeObserver(measure);
    observer.observe(root);
    observer.observe(copy);
    return () => observer.disconnect();
  }, [vertical, speed, repeat]);

  const mask = vertical
    ? "motion-safe:[mask-image:linear-gradient(to_bottom,transparent,black_12%,black_88%,transparent)]"
    : "motion-safe:[mask-image:linear-gradient(to_right,transparent,black_8%,black_92%,transparent)]";

  return (
    <div
      ref={rootRef}
      role={ariaLabel ? "region" : undefined}
      aria-label={ariaLabel}
      style={{ "--chm-marquee-gap": gap, "--chm-marquee-duration": `${duration}s` } as CSSProperties}
      className={cn(
        "group/marquee flex gap-[var(--chm-marquee-gap)] overflow-hidden",
        vertical ? "flex-col" : "flex-row",
        fade && mask,
        className,
      )}
    >
      <style href="chm-marquee" precedence="default">{KEYFRAMES}</style>
      {Array.from({ length: copies }, (_, index) => (
        <div
          key={index}
          ref={index === 0 ? copyRef : undefined}
          aria-hidden={index > 0 || undefined}
          inert={index > 0 || undefined}
          className={cn(
            "flex shrink-0 justify-around gap-[var(--chm-marquee-gap)] [will-change:transform]",
            vertical ? "flex-col" : "flex-row items-center",
            vertical
              ? "motion-safe:animate-[chm-marquee-y_var(--chm-marquee-duration)_linear_infinite]"
              : "motion-safe:animate-[chm-marquee-x_var(--chm-marquee-duration)_linear_infinite]",
            reverse && "[animation-direction:reverse]",
            paused && "[animation-play-state:paused]",
            pauseOnHover && "group-hover/marquee:[animation-play-state:paused] group-focus-within/marquee:[animation-play-state:paused]",
            // Reduced motion: one static copy that wraps instead of scrolling.
            index > 0 ? "motion-reduce:hidden" : "motion-reduce:flex-wrap motion-reduce:justify-center motion-reduce:shrink",
            trackClassName,
          )}
        >
          {children}
        </div>
      ))}
    </div>
  );
}