仕組み
The root is a one-cell inline-grid and every candidate word stays in the DOM, stacked in that cell: the width is the widest word and the baseline comes from the first, so surrounding text never moves and a long word cannot wrap the line. Enter and exit are CSS keyframes (translateY 0.5em, fade, blur), not transitions, so each always starts from the right side. JS only restarts a timeout after each switch, so a resume waits a full interval; it stops when the page is hidden.
向いている場面
- A hero headline that rotates selling points: “Shopping made [safe / simple / fast]”.
- A sentence where only one item changes, such as a list of supported cities.
- A slogan on a sign-in or empty page that needs some motion without moving the whole line.
向かない場面
- Words of very different lengths: the short ones leave a large gap; switch whole sentences or rewrite.
- Information people must see in full, such as prices, rules or risks: rotating text disappears.
- Body paragraphs, or several on one screen: constant motion disturbs reading.
- A letter-by-letter reveal: use a typewriter effect.
使い方
import { WordRotate } from './WordRotate.jsx'
export function Hero() {
return (
<h1>
Shopping made{' '}
<WordRotate words={['safe', 'simple', 'fast']} interval={2200} style={{ color: 'var(--ds-accent, #2563eb)' }} />
</h1>
)
}
// A word in the middle of a sentence reads better centered
<p>
Every day <WordRotate words={['3,200', '12,800', '860']} align="center" direction="down" /> people find what they need here
</p>プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
words | string[] | — | The words to rotate through, in a loop. With one word nothing rotates. Changing the list returns to the first word. |
interval | number | 2200 | How long each word stays, in ms. Never shorter than the enter duration + 240ms. |
duration | number | 360 | Slide-in duration in ms; the old word leaves in 0.7 times that. Follows --ds-duration-slow if omitted. |
direction | 'up' | 'down' | 'up' | up: the old word slides up and the new one comes from below; down is the reverse. |
align | 'start' | 'center' | 'end' | 'start' | Alignment of a word narrower than the reserved width (the widest word). Use start at the end of a sentence and center in the middle. |
blur | boolean | true | Adds a slight 6px blur while entering and leaving. |
pauseOnHover | boolean | false | Pauses rotation while a mouse pointer is over it. |
paused | boolean | false | Pauses from outside. |
label | string | all words joined | Text read by screen readers. By default all words joined with a comma. |
labels | object | — | Replaces built-in text (separator, separatorCjk): the strings used to join the words in the screen-reader text. Shallow-merged over the defaults. |
onChange | (index: number) => void | — | Called when a new word is shown. |
as | 'span' | 'strong' | 'em' | 'b' | 'i' | 'mark' | 'span' | Which inline tag to render. |
...rest | HTMLAttributes | — | Other attributes go to the root element, such as className and style (often used to color the rotating word). |
アクセシビリティ
- The rotating layer is aria-hidden; a fixed hidden text lists all the words joined with a comma (“safe, simple, fast”), so screen readers hear them once and the content never changes.
- No aria-live: interrupting a screen reader every two seconds is worse than silence. Use label to read something else.
- The hidden words and screen-reader text are not selectable, so copying the sentence gives only the visible word.
- Reduced motion removes the movement and blur and only cross-fades; pass paused to hold it completely still.
- pauseOnHover and paused let people stop it, and it stops when the page is hidden.
モーション
New word: translateY 0.5em, fade in and blur 6px to 0, 360ms ease-out, starting 54ms late. Old word: opposite 0.5em and fade out in 252ms ease-in. Each word stays 2200ms.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
WordRotate.jsxword-rotate.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。



