Word Rotate
A rotating word animation for React and Tailwind CSS: words swap with a vertical slide and blur while the container's width springs between them, so the sentence around it glides instead of jumping. Every word stays readable by search engines and screen readers.
- SEO-friendly real text
- Springy width
- AnimatePresence popLayout
- Pause on hover
- prefers-reduced-motion
Interfaces for founders, developers, designers, night owls
import { WordRotate } from "@/components/dusk/text/word-rotate";
export default function WordRotateDemo() {
return (
<h2 className="text-center text-4xl font-bold tracking-tight sm:text-5xl">
Interfaces for{" "}
<WordRotate words={["founders", "developers", "designers", "night owls"]} className="text-white/60" />
</h2>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/word-rotate.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/text/word-rotate.tsx.word-rotate.tsx "use client"; import * as React from "react"; import { AnimatePresence, animate, motion, useInView, useMotionValue, usePageInView, useReducedMotion, type AnimationPlaybackControls, } from "motion/react"; import { cn } from "@/lib/utils"; type WordRotateTag = "span" | "strong" | "em" | "p" | "div" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; export interface WordRotateProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> { /** Words (or short phrases) to cycle through. The first one is server-rendered. */ words: string[]; /** Milliseconds each word stays on screen (minimum 1000). */ interval?: number; /** Element to render. `span` (default) sits inline in a sentence. */ as?: WordRotateTag; /** Slide distance as a fraction of the line height. 0 for a pure blur/fade swap. */ distance?: number; /** Blur applied to incoming and outgoing words, in px. */ blur?: number; /** Pause while the pointer is over the word. */ pauseOnHover?: boolean; /** * How assistive tech gets the words: * - `"all"` (default): every word is exposed once as visually hidden text (also best for SEO). * - `"polite"`: the visible word is a polite live region, announced each time it changes. */ announce?: "all" | "polite"; /** Classes for each word, e.g. a color. */ wordClassName?: string; } /** * Cycles through words with a vertical slide-and-blur swap. The container's * width springs between word lengths so the surrounding sentence glides * instead of jumping. * * The rotation pauses off-screen, in hidden tabs and (optionally) on hover. * Under reduced motion the first word stays put. Screen readers and search * engines get every word as real text, or polite announcements with * `announce="polite"`. */ export function WordRotate({ words, interval = 2400, as = "span", distance = 0.6, blur = 4, pauseOnHover = true, announce = "all", wordClassName, className, onPointerEnter, onPointerLeave, ...props }: WordRotateProps) { const Comp = as as React.ElementType; const rootRef = React.useRef<HTMLElement>(null); const boxRef = React.useRef<HTMLSpanElement>(null); const measured = React.useRef(false); const reduceMotion = useReducedMotion(); const pageVisible = usePageInView(); const inView = useInView(rootRef, { amount: 0.5 }); const [hovered, setHovered] = React.useState(false); const [index, setIndex] = React.useState(0); const width = useMotionValue<number | "auto">("auto"); const count = words.length; const safeIndex = count ? index % count : 0; const running = count > 1 && !reduceMotion && inView && pageVisible && !(pauseOnHover && hovered); React.useEffect(() => { if (!running) return; const timer = setTimeout(() => setIndex((i) => (i + 1) % count), Math.max(1000, interval)); return () => clearTimeout(timer); }, [running, safeIndex, count, interval]); // Spring the box to the incoming word's width, and follow later size changes (fonts, breakpoints). React.useLayoutEffect(() => { const word = boxRef.current?.querySelector<HTMLElement>(`[data-word="${safeIndex}"]`); if (!word) return; let controls: AnimationPlaybackControls | undefined; const target = word.offsetWidth; if (!measured.current || reduceMotion) { measured.current = true; width.jump(target); } else { controls = animate(width, target, { type: "spring", duration: 0.45, bounce: 0.1 }); } let first = true; const observer = new ResizeObserver(() => { if (first) { first = false; return; } controls?.stop(); width.jump(word.offsetWidth); }); observer.observe(word); return () => { controls?.stop(); observer.disconnect(); }; }, [safeIndex, reduceMotion, width]); const y = `${distance * 100}%`; const still = reduceMotion === true; return ( <Comp ref={rootRef} data-slot="word-rotate" className={cn(as === "span" || as === "strong" || as === "em" ? "inline-flex" : "flex", className)} onPointerEnter={(e: React.PointerEvent<HTMLElement>) => { setHovered(true); onPointerEnter?.(e); }} onPointerLeave={(e: React.PointerEvent<HTMLElement>) => { setHovered(false); onPointerLeave?.(e); }} {...props} > {announce === "all" && <span className="sr-only">{words.join(", ")}</span>} <motion.span ref={boxRef} aria-hidden={announce === "all" || undefined} aria-live={announce === "polite" ? "polite" : undefined} style={{ width }} // Vertical padding keeps ascenders/descenders and blur from being clipped. className="relative -my-[0.15em] inline-flex overflow-hidden py-[0.15em] whitespace-nowrap" > <AnimatePresence mode="popLayout" initial={false}> <motion.span key={safeIndex} data-word={safeIndex} className={cn("inline-block", wordClassName)} initial={still ? { opacity: 0 } : { opacity: 0, y, filter: `blur(${blur}px)` }} animate={{ opacity: 1, y: "0%", filter: "blur(0px)" }} exit={still ? { opacity: 0 } : { opacity: 0, y: `-${y}`, filter: `blur(${blur}px)` }} transition={{ type: "spring", duration: 0.45, bounce: 0.1 }} > {words[safeIndex]} </motion.span> </AnimatePresence> </motion.span> </Comp> ); }
Usage
import { WordRotate } from "@/components/dusk/text/word-rotate"
export function Tagline() {
return (
<h1 className="text-5xl font-bold">
Made for{" "}
<WordRotate
words={["builders", "designers", "teams"]}
/>
</h1>
)
}Examples
Motion & styling
distance sets the slide (0 for a pure blur swap), blur the blur radius and interval how long each word stays. Style the words with wordClassName.
Pure blur swap, fade, melt
Make it clear, quiet, fast, yours
Slow, Steady, Smooth and calm.
import { WordRotate } from "@/components/dusk/text/word-rotate";
export default function WordRotateStylesDemo() {
return (
<div className="grid gap-8 text-center text-3xl font-semibold tracking-tight">
<p>
Pure blur{" "}
<WordRotate
words={["swap", "fade", "melt"]}
distance={0}
blur={10}
interval={2000}
className="text-[#22d3ee]"
/>
</p>
<p>
Make it{" "}
<WordRotate
words={["clear", "quiet", "fast", "yours"]}
interval={1800}
wordClassName="text-white/60"
/>
</p>
<p className="font-serif text-4xl italic">
<WordRotate words={["Slow", "Steady", "Smooth"]} interval={3200} distance={1} blur={0} />{" "}
and calm.
</p>
</div>
);
}Announced updates
For live status text, announce="polite" turns the word into a polite live region so screen readers announce each change instead of hearing the whole list once.
Edge cache warm in Frankfurt
import { WordRotate } from "@/components/dusk/text/word-rotate";
export default function WordRotatePoliteDemo() {
return (
<p className="inline-flex items-center gap-2.5 rounded-full border border-white/10 bg-white/[0.04] px-4 py-2 text-sm text-white/70">
<span className="size-2 rounded-full bg-emerald-400" aria-hidden />
Edge cache warm in{" "}
<WordRotate
announce="polite"
words={["Frankfurt", "Tokyo", "São Paulo", "Virginia", "Sydney"]}
interval={2600}
className="font-medium text-foreground"
/>
</p>
);
}API Reference
Also accepts any HTML attribute for the rendered element.
| Prop | Type | Default | Description |
|---|---|---|---|
| words | string[] | — | Words or short phrases to cycle through. The first is server-rendered. |
| interval | number | 2400 | Milliseconds each word stays on screen (minimum 1000). |
| as | "span" | "strong" | "em" | "p" | "div" | "h1" … "h6" | "span" | Element to render. |
| distance | number | 0.6 | Slide distance as a fraction of the word height. 0 for blur/fade only. |
| blur | number | 4 | Blur of incoming and outgoing words, in px. |
| pauseOnHover | boolean | true | Pause the rotation while the pointer is over it. |
| announce | "all" | "polite" | "all" | all exposes every word once as hidden text; polite announces each change. |
| wordClassName | string | — | Classes for each word, e.g. a color. |
Accessibility
- By default every word is in the DOM once as visually hidden text and the animated copy is aria-hidden, so screen readers read the full sentence and crawlers index all words.
- With
announce="polite"the visible word is a polite live region instead, for content that is genuinely live. - Rotation pauses off-screen, in hidden tabs and on hover, which also gives readers time with a word.
- Width changes are animated, so surrounding text moves smoothly instead of jumping.
- With prefers-reduced-motion the first word stays put and nothing rotates.