Rotating words
One word in a headline rolls to the next every few seconds, and the slot glides to fit each word so the sentence never jumps.
"use client";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import "./rotating-words.css";
type RotatingWordsProps = {
/** The words that take turns. Two to six short ones work best. */
words: string[];
/** Milliseconds each word stays. */
interval?: number;
/** Stop on the current word (wire this to a pause button). */
paused?: boolean;
/** What screen readers hear instead of the changing word. */
label?: string;
};
/**
* RotatingWords
* One slot inside a sentence. Each word rolls up out of the slot while the
* next rolls in, and the slot's width glides to fit the new word, so the
* rest of the sentence slides along instead of jumping.
*/
export function RotatingWords({
words,
interval = 2200,
paused = false,
label,
}: RotatingWordsProps) {
const [index, setIndex] = useState(0);
const slot = useRef<HTMLSpanElement>(null);
const items = useRef<(HTMLSpanElement | null)[]>([]);
// Advance on a timer, but never while paused, hidden or off screen.
useEffect(() => {
if (paused || words.length < 2) return;
let visible = true;
const io = new IntersectionObserver(([e]) => (visible = e.isIntersecting));
if (slot.current) io.observe(slot.current);
const id = window.setInterval(() => {
if (visible && !document.hidden) setIndex((i) => (i + 1) % words.length);
}, interval);
return () => {
window.clearInterval(id);
io.disconnect();
};
}, [paused, interval, words.length]);
// Measure the incoming word so the slot can glide to its width.
useLayoutEffect(() => {
const measure = () => {
const w = items.current[index]?.offsetWidth;
if (slot.current && w) slot.current.style.width = `${w}px`;
};
measure();
window.addEventListener("resize", measure);
return () => window.removeEventListener("resize", measure);
}, [index, words]);
const prev = (index - 1 + words.length) % words.length;
return (
<span className="gu-rotate">
<span className="gu-rotate__sr">{label ?? words.join(", ")}</span>
<span ref={slot} className="gu-rotate__slot" aria-hidden="true">
{/* Invisible copy of the current word: keeps the baseline and line height. */}
<span className="gu-rotate__sizer">{words[index]}</span>
{words.map((word, i) => (
<span
key={word + i}
ref={(el) => {
items.current[i] = el;
}}
className="gu-rotate__word"
data-state={i === index ? "in" : i === prev ? "out" : "wait"}
>
{word}
</span>
))}
</span>
</span>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Rotating words
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
One slot inside a headline where a word rolls up and out while the next rolls in, every few seconds. The slot's width glides to fit each new word, so the rest of the sentence slides along instead of jumping.
## Behavior and motion
- A timer advances the word every `interval` ms (default 2200). It does nothing while paused, while the tab is hidden, or while the slot is off screen (IntersectionObserver).
- Each word is absolutely positioned in the slot. States: in (translateY 0, opacity 1), out (translateY −105%, opacity 0), waiting (translateY 105%, opacity 0, no transition). Transitions are 0.6s cubic-bezier(0.7, 0, 0.2, 1).
- On every change, measure the incoming word's width and set the slot's width to it; the width transitions over 0.6s.
- An invisible copy of the current word sits in normal flow so the slot keeps the sentence's baseline and line height.
- The slot is clipped with clip-path (inset −0.05em −0.2em −0.2em −0.05em), not overflow: hidden, because overflow would move the inline-block's baseline.
## Look
The rotating word uses --gu-rotate-color (default orange #ff5a1f). Demo: "Websites that [load fast / sell / feel calm / last years] on every phone." at up to 4.4rem, weight 900, tracking −0.05em, with a Pause/Play outline button.
## Accessibility
- Screen readers get one hidden line with every word ("load fast, sell, feel calm, last years") or a custom `label`; the moving slot is aria-hidden, so nothing is announced every two seconds.
- Moving content needs a way to stop it (WCAG 2.2.2): wire the `paused` prop to a visible button, as the demo does (aria-pressed).
- prefers-reduced-motion: words swap with a 0.25s fade, with no rolling and no width animation.
## Props
words (string[]), interval (ms), paused (boolean), label (string for screen readers).
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### RotatingWords.tsx
```tsx
"use client";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import "./rotating-words.css";
type RotatingWordsProps = {
/** The words that take turns. Two to six short ones work best. */
words: string[];
/** Milliseconds each word stays. */
interval?: number;
/** Stop on the current word (wire this to a pause button). */
paused?: boolean;
/** What screen readers hear instead of the changing word. */
label?: string;
};
/**
* RotatingWords
* One slot inside a sentence. Each word rolls up out of the slot while the
* next rolls in, and the slot's width glides to fit the new word, so the
* rest of the sentence slides along instead of jumping.
*/
export function RotatingWords({
words,
interval = 2200,
paused = false,
label,
}: RotatingWordsProps) {
const [index, setIndex] = useState(0);
const slot = useRef<HTMLSpanElement>(null);
const items = useRef<(HTMLSpanElement | null)[]>([]);
// Advance on a timer, but never while paused, hidden or off screen.
useEffect(() => {
if (paused || words.length < 2) return;
let visible = true;
const io = new IntersectionObserver(([e]) => (visible = e.isIntersecting));
if (slot.current) io.observe(slot.current);
const id = window.setInterval(() => {
if (visible && !document.hidden) setIndex((i) => (i + 1) % words.length);
}, interval);
return () => {
window.clearInterval(id);
io.disconnect();
};
}, [paused, interval, words.length]);
// Measure the incoming word so the slot can glide to its width.
useLayoutEffect(() => {
const measure = () => {
const w = items.current[index]?.offsetWidth;
if (slot.current && w) slot.current.style.width = `${w}px`;
};
measure();
window.addEventListener("resize", measure);
return () => window.removeEventListener("resize", measure);
}, [index, words]);
const prev = (index - 1 + words.length) % words.length;
return (
<span className="gu-rotate">
<span className="gu-rotate__sr">{label ?? words.join(", ")}</span>
<span ref={slot} className="gu-rotate__slot" aria-hidden="true">
{/* Invisible copy of the current word: keeps the baseline and line height. */}
<span className="gu-rotate__sizer">{words[index]}</span>
{words.map((word, i) => (
<span
key={word + i}
ref={(el) => {
items.current[i] = el;
}}
className="gu-rotate__word"
data-state={i === index ? "in" : i === prev ? "out" : "wait"}
>
{word}
</span>
))}
</span>
</span>
);
}
```
### rotating-words.css
```css
.gu-rotate {
--gu-rotate-ease: cubic-bezier(0.7, 0, 0.2, 1);
}
.gu-rotate__sr {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}
/* The slot keeps the sentence's baseline: clip-path cuts the words off
at the edges without changing where the text sits (overflow would). */
.gu-rotate__slot {
position: relative;
display: inline-block;
white-space: nowrap;
color: var(--gu-rotate-color, #ff5a1f);
clip-path: inset(-0.05em -0.2em -0.2em -0.05em);
transition: width 0.6s var(--gu-rotate-ease);
}
.gu-rotate__sizer {
visibility: hidden;
}
.gu-rotate__word {
position: absolute;
left: 0;
top: 0;
transition:
transform 0.6s var(--gu-rotate-ease),
opacity 0.6s var(--gu-rotate-ease);
}
.gu-rotate__word[data-state="wait"] {
transform: translateY(105%);
opacity: 0;
transition: none;
}
.gu-rotate__word[data-state="in"] {
transform: none;
opacity: 1;
}
.gu-rotate__word[data-state="out"] {
transform: translateY(-105%);
opacity: 0;
}
/* Reduced motion: words swap with a short fade and the slot resizes at once. */
@media (prefers-reduced-motion: reduce) {
.gu-rotate__slot {
transition: none;
}
.gu-rotate__word,
.gu-rotate__word[data-state] {
transform: none;
transition: opacity 0.25s linear;
}
}
```
You might also want
Next piece: Scroll reveal setLetter scatter headline
Animations, beginner
Split text reveal
Animations, intermediate, needs motion
Aurora background
Animations, beginner