仕組み
The frame never changes while typing: every segment renders an invisible ghost in one grid cell, so the cell matches the longest one, and the typing layer holds the full text with the untyped part hidden, so every character starts in its final place. The cursor takes no width. Text is split by grapheme, so emoji and flags are never typed in halves. One timer per step; it stops in the background or when paused.
向いている場面
- Rotating search box hints such as “Try: wireless headphones”, showing what can be searched.
- A single tagline on a home or campaign page that should feel alive.
- Opening lines or example questions for a support bot or AI assistant.
向かない場面
- Body text, instructions or errors people must read at once: waiting for characters slows reading.
- Real streaming output from a model: just append the text as it arrives; no need to fake it.
- Several on one screen: the cursor and moving text are eye-catching, so keep one.
- Swapping a single word inside a sentence: a word rotator is cleaner.
使い方
import { useRef } from 'react'
import { Typewriter } from './Typewriter.jsx'
// Several segments in rotation: a search box hint
export function SearchHint() {
return (
<span>
Try:{' '}
<Typewriter text={['Wireless headphones', 'Trail running shoes', 'Coffee starter kit']} speed={70} pause={1600} />
</span>
)
}
// One segment: types, stops, and can be replayed
export function Slogan() {
const ref = useRef(null)
return (
<>
<Typewriter ref={ref} as="h1" text="Good things, found fast" startDelay={300} hideCursorOnDone onDone={() => console.log('done')} />
<button onClick={() => ref.current?.restart()}>Replay</button>
</>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
text | string | string[] | — | Text to type, may contain \n. An array types a segment, pauses, deletes it and types the next. Only the content is compared, so a new array literal on every render does not restart it. |
speed | number | 70 | Typing speed: interval per character in ms. |
deleteSpeed | number | 35 | Deleting speed: interval per character in ms. |
pause | number | 1600 | How long to wait after a segment is typed before deleting it, in ms. |
loop | boolean | true | Whether several segments loop; false stops after the last one. A single segment always stops when typed. |
cursor | boolean | true | Shows the cursor; set its colour with the CSS variable --ds-typewriter-cursor (default: primary colour). |
hideCursorOnDone | boolean | false | Fades the cursor out once everything is typed and stopped (it keeps blinking by default). |
startDelay | number | 0 | Wait before typing the first character, in ms. |
natural | boolean | true | Varies each interval between 0.6 and 1.4 times speed so it feels human; false is steady. |
label | string | the text; several segments joined with “, ” | Text read by screen readers. By default one segment is read as is and several are joined with a comma (an enumeration comma for CJK text). |
onDone | () => void | — | Called when everything is typed and stopped (a single segment, or the last one when loop is false). Not called while looping. |
paused | boolean | false | Pauses on the current character and continues from there on resume. It also pauses by itself when the page goes to the background. |
as | 'span' | 'p' | 'div' | 'h1' | 'h2' | 'h3' | 'h4' | 'span' | The tag to render. span is inline; the others are block-level. |
ref | Ref<{ restart }> | — | Exposes restart(), which types everything again from the start. |
...rest | HTMLAttributes<HTMLElement> | — | Other attributes go to the root element, such as className and style. |
アクセシビリティ
- Screen readers get one complete hidden text (several segments joined); the changing visible layer is aria-hidden, so there is no character-by-character announcement and no aria-live.
- The hidden text and ghosts cannot be selected, so copying gets only what is visible.
- Real h1 / h2 / p tags are kept, so heading levels still work.
- With reduced motion there is no typing and no cursor: a single segment shows in full and calls onDone, several fade in whole at the pause plus 1.2 seconds.
- The frame size is fixed, so changing text never pushes surrounding content, which helps magnifier and low-vision users.
モーション
Typing takes 70ms per character (varying 0.6 to 1.4 times); pause 1600ms; deleting 35ms per character; the cursor blinks on a 1.1s loop, solid while typing or deleting. With reduced motion a segment fades in over 240ms.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
Typewriter.jsxtypewriter.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。



