Implementation:AIScientists Dev WorldSeed useTypewriter
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Animation |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete React hook implementing character-by-character text reveal with punctuation pauses, CJK-safe spreading, and pause/resume support, provided by the WorldSeed frontend.
Description
The useTypewriter hook returns `displayedText`, `isDone`, and `complete()`. It uses refs (not Zustand state) to avoid 60fps setState — `charsRef` holds the spread array of characters, `idxRef` tracks position, and `tick()` is a shared recursive `setTimeout` that reads `idxRef` so it resumes from wherever it stopped. Punctuation characters trigger a longer delay (configurable via `punctMultiplier`). CJK characters are handled correctly via the spread operator (`[...text]`). When `paused` is true, the timer is cleared; on resume, `tick` restarts from the current index. `complete()` force-completes the reveal.
Usage
Import this hook in components that need a typewriter text reveal effect — e.g., `TypewriterText` for subtitle bubbles and narrative text.
Code Reference
Source Location
- Repository: WorldSeed
- File: frontend/src/hooks/useTypewriter.ts
Signature
interface TypewriterOptions {
msPerChar: number;
punctMultiplier?: number;
speed?: number;
onDone?: () => void;
enabled?: boolean;
paused?: boolean;
}
interface TypewriterResult {
displayedText: string;
isDone: boolean;
complete: () => void;
}
export function useTypewriter(text: string, options: TypewriterOptions): TypewriterResult;
Import
import { useTypewriter } from '@/hooks/useTypewriter';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| text | string | Yes │ Text to reveal | |
| msPerChar | number | Yes │ Milliseconds per character | |
| punctMultiplier | number | No │ Delay multiplier for punctuation (default from SUBTITLE_TIMING) | |
| speed | number | No │ Speed multiplier (1.0 = normal, 2.0 = 2x faster) | |
| enabled | boolean | No │ Whether to run (default true) | |
| paused | boolean | No │ Freeze in place without resetting |
Outputs
| Name | Type | Description |
|---|---|---|
| displayedText | string │ Partial text during animation, full when done | |
| isDone | boolean │ Whether reveal is complete | |
| complete | () => void │ Force-complete the reveal |
Usage Examples
import { useTypewriter } from '@/hooks/useTypewriter';
function TypewriterText({ text, paused }: { text: string; paused: boolean }) {
const { displayedText, isDone, complete } = useTypewriter(text, {
msPerChar: 35,
paused,
onDone: () => console.log('reveal complete'),
});
return (
<span onClick={complete}>
{displayedText}
</span>
);
}