Skip to the content

Motion

word-rotate

One word in a sentence that changes to the next of a list in place, rising and fading, once through the list (or round and round with loop, pausing while hovered or focused); screen readers hear the whole list once, and under reduced motion the last word simply stands.

Effects and 3D data

When to use it

A headline that names several things one after another: "Charts for teams / schools / newsrooms". Keep the words about the same length. For a whole heading appearing use text-reveal; for a number counting up use number-ticker.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Effects and 3D data page.

Word rotate and pattern

Maps for teams, schools, or newsrooms

Animated beam

A map becomes a film.

Orbit

  • Maps
  • Charts
  • Films
  • Motion
Rivier

Confetti

Not yet published.

Point cloud

Ninety finds
#widthheightdepthkind
11.22.52.5Clay
23.22.73.2Stone
32.92.53.8Glass
40.30.64.3Clay
52.71.24.5Stone
642.54.4Glass
70.52.14.1Clay
81.63.13.5Stone
94.13.32.9Glass
101.61.32.1Clay
111.21.21.4Stone
123.11.80.9Glass
132.21.30.6Clay
142.12.70.5Stone
152.23.70.7Glass
161.62.11.2Clay
173.11.81.8Stone
182.61.82.5Glass
190.50.63.2Clay
2031.93.9Stone
213.73.44.3Glass
220.32.54.5Clay
231.92.74.4Stone
244.22.44.1Glass
251.20.63.5Clay
261.21.22.8Stone
273.42.52.1Glass
282.12.21.4Clay
291.73.10.9Stone
302.43.30.6Glass
311.91.20.5Clay
322.81.20.7Stone
332.41.81.2Glass
340.81.41.8Clay
353.12.82.5Stone
363.43.73.2Glass
370.22.13.9Clay
382.31.84.3Stone
394.21.84.5Glass
400.90.64.4Clay
411.324.1Stone
423.83.43.5Glass
4322.52.8Clay
441.52.62.1Stone
452.62.41.4Glass
462.10.60.9Clay
472.51.30.6Stone
482.22.60.5Glass
491.12.20.7Clay
503.23.11.2Stone
5133.21.8Glass
520.21.22.5Clay
532.61.23.3Stone
5441.93.9Glass
550.61.44.3Clay
561.52.84.5Stone
5743.74.4Glass
581.724.1Clay
591.31.73.5Stone
6031.72.8Glass
612.20.72.1Clay
622.221.4Stone
632.23.40.9Glass
641.52.50.6Clay
653.12.60.5Stone
662.72.30.7Glass
670.40.51.2Clay
682.91.31.8Stone
693.82.62.6Glass
700.32.23.3Clay
711.83.13.9Stone
724.23.24.3Glass
731.31.14.5Clay
741.21.14.4Stone
753.31.94.1Glass
762.21.53.5Clay
771.82.92.8Stone
782.33.72.1Glass
791.821.4Clay
802.91.70.9Stone
812.41.70.6Glass
820.70.70.5Clay
833.12.10.7Stone
843.53.51.2Glass
850.22.51.8Clay
862.22.62.6Stone
874.22.33.3Glass
8810.53.9Clay
891.31.34.3Stone
903.72.74.5Glass

Model annotations

  1. Lid — Hinged at the back.
  2. Side — A handle was here.
  3. Front — The maker's mark.

Add it

npx shadcn@latest add @rivier-ui/word-rotate

The registry is named once in components.json: see Installation.

Example

Import it from @/components/motion/word-rotate.

<h1>Maps for <WordRotate words={["teams", "schools", "newsrooms"]} /></h1>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: motion.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/motion/word-rotate.tsx
"use client";
/**
 * @name word-rotate
 * @category motion
 * @description One word in a sentence that changes to the next of a list in place, rising and fading, once through the list (or round and round with `loop`, pausing while hovered or focused); screen readers hear the whole list once, and under reduced motion the last word simply stands.
 * @when A headline that names several things one after another: "Charts for teams / schools / newsrooms". Keep the words about the same length. For a whole heading appearing use text-reveal; for a number counting up use number-ticker.
 * @example
 * <h1>Maps for <WordRotate words={["teams", "schools", "newsrooms"]} /></h1>
 */
import { AnimatePresence, motion } from "motion/react";
import { useEffect, useState, type ComponentProps } from "react";
import { cn } from "@/lib/cn";
import { useMotionSafe } from "@/lib/motion";
 
export type WordRotateProps = Omit<ComponentProps<"span">, "children"> & {
  words: readonly string[];
  /** Milliseconds each word stays. Default 2200. */
  interval?: number;
  /** Go round again and again. Default false: once through, then the last word stays (no motion lasts past a few seconds). */
  loop?: boolean;
  /** Stops it where it is. */
  paused?: boolean;
  /** Joins the words for screen readers. Default: Intl's list format in `locale`. */
  locale?: string;
};
 
export function WordRotate({ words, interval = 2200, loop = false, paused = false, locale = "en-US", className, ...props }: WordRotateProps) {
  const safe = useMotionSafe();
  const [index, setIndex] = useState(0);
  const [hold, setHold] = useState(false);
  const last = words.length - 1;
  const running = safe && !paused && !hold && words.length > 1 && (loop || index < last);
  useEffect(() => {
    if (!running) return;
    const timer = window.setTimeout(() => setIndex((i) => (i >= last ? 0 : i + 1)), interval);
    return () => window.clearTimeout(timer);
  }, [running, index, interval, last]);
  const shown = safe ? (words[index] ?? "") : (words[last] ?? "");
  const spoken = new Intl.ListFormat(locale, { type: "disjunction" }).format(words);
  return (
    <span data-slot="word-rotate" className={cn("relative inline-grid align-bottom", className)} onPointerEnter={() => setHold(true)} onPointerLeave={() => setHold(false)} onFocus={() => setHold(true)} onBlur={() => setHold(false)} {...props}>
      <span className="sr-only">{spoken}</span>
      {/* The widest word holds the space, so the sentence does not jump. */}
      <span aria-hidden="true" className="invisible col-start-1 row-start-1">
        {words.reduce((a, b) => (b.length > a.length ? b : a), "")}
      </span>
      <span aria-hidden="true" className="col-start-1 row-start-1 overflow-hidden">
        <AnimatePresence mode="popLayout" initial={false}>
          <motion.span
            key={shown}
            className="inline-block"
            initial={safe ? { opacity: 0, y: "60%" } : false}
            animate={{ opacity: 1, y: "0%" }}
            exit={safe ? { opacity: 0, y: "-60%" } : { opacity: 0 }}
            transition={{ duration: 0.35, ease: [0.2, 0, 0, 1] }}
          >
            {shown}
          </motion.span>
        </AnimatePresence>
      </span>
    </span>
  );
}