TypewriterText Component
TheTypewriterText
component provides an animated typing effect with adaptive speed based on text length. It supports markdown rendering, customizable delays, and optional prefix styling.
Features
- Adaptive Speed: Automatically adjusts typing speed based on text length
- Markdown Support: Built-in markdown rendering with prefix support
- Customizable Cursor: Optional blinking cursor with styling
- Prefix Support: Special styling for prefixed content
- Performance Optimized: Uses Framer Motion for smooth animations
Import
Basic Usage
Props
Prop | Type | Default | Description |
---|---|---|---|
text | string | - | The text to be typed out (required) |
className | string | '' | Additional CSS classes |
charDelay | number | 0.025 | Base character delay in seconds. If default, adaptive speed is used |
minCharDelay | number | 0.002 | Minimum delay for very long texts (fastest speed) |
maxCharDelay | number | 0.04 | Maximum delay for short texts (slowest speed) |
showCursor | boolean | true | Whether to show the typing cursor |
onTypingStart | () => void | - | Callback when typing animation starts |
onTypingComplete | () => void | - | Callback when typing animation completes |
blinking | boolean | false | Whether the cursor should blink |
renderAsMarkdown | boolean | true | Whether to render content as markdown |
prefix | string | '' | Optional prefix text with special styling |
Adaptive Speed
The component automatically adjusts typing speed based on text length:- Short texts (≤30 words): Use
maxCharDelay
(slower) - Long texts (≥400 characters): Use
minCharDelay
(faster) - Medium texts: Exponential interpolation between min and max
Custom Speed
Override adaptive speed by setting a customcharDelay
:
Prefix Styling
Use prefixes for special highlighting (useful for chat interfaces):Callbacks
Markdown Support
The component supports full markdown rendering whenrenderAsMarkdown
is true:
Custom Cursor
Advanced Example
Integration with Cedar OS
The component automatically integrates with Cedar OS styling system:- Uses
useStyling()
hook for consistent theming - Cursor color matches the current color scheme
- Prefix styling uses accent colors
- Markdown rendering follows Cedar OS design patterns
Performance Notes
- Uses Framer Motion’s
animate
function for optimal performance - Automatically cleans up animations on unmount
- Efficient re-rendering with dependency tracking
- Smooth 60fps animations with hardware acceleration