use-typewriter-animation
Version:
A modern, performant React hook for creating typewriter animation effects with full TypeScript support and SSR compatibility.
295 lines (225 loc) โข 8.86 kB
Markdown
# use-typewriter-animation
A modern, performant React hook for creating typewriter animation effects with full TypeScript support, accessibility features, and React 19 compatibility.
[](https://www.npmjs.com/package/use-typewriter-animation)
[](https://opensource.org/licenses/MIT)
[](https://www.npmjs.com/package/use-typewriter-animation)
[](https://bundlephobia.com/package/use-typewriter-animation)
[](https://www.typescriptlang.org/)
[](https://reactjs.org/)
[](https://github.com/doguyilmaz/use-typewriter-animation/stargazers)
## โจ Features
- ๐ฏ **Modern React**: Built for React 16.8+ with full React 19 support
- ๐ง **TypeScript First**: Complete type safety and IntelliSense
- โฟ **Accessibility**: WCAG 2.1 AA compliant with screen reader support
- ๐ **Performance**: Optimized with virtualization for large text (~15KB bundle)
- ๐จ **Flexible**: Rich styling and animation control
- ๐ฑ **Responsive**: Mobile-friendly with touch support
- ๐ **Server-Side**: SSR and RSC compatible
- ๐ฎ **Interactive**: Keyboard controls and event handling
- ๐ **Universal**: Works in all modern browsers
## ๐ฆ Installation
```bash
# bun (recommended)
bun add use-typewriter-animation
# npm
npm install use-typewriter-animation
# yarn
yarn add use-typewriter-animation
# pnpm
pnpm add use-typewriter-animation
```
## ๐ Quick Start
```jsx
import { useEffect } from 'react';
import { useTypewriter } from 'use-typewriter-animation';
function App() {
const { typewriter, elements, cursor, keyframes } = useTypewriter();
useEffect(() => {
typewriter.type('Hello, World!').pauseFor(1000).deleteLetters(6).type('React!').start();
}, []);
return (
<>
<style>{keyframes}</style>
<div>
{elements}
{cursor}
</div>
</>
);
}
```
## ๐ฏ Key Examples
### Basic Animation
```jsx
const { typewriter, elements, cursor, keyframes } = useTypewriter({
typeSpeed: 50,
cursorStyle: 'bar',
});
useEffect(() => {
typewriter
.type('Welcome to React!')
.pauseFor(2000)
.deleteAll()
.type('Built with TypeScript!')
.start();
}, []);
```
### Colorful Text
```jsx
useEffect(() => {
typewriter
.type('This is ')
.colorize('#3b82f6')
.type('blue text')
.colorize('#ef4444')
.type(' and red text')
.start();
}, []);
```
### Accessibility First
```jsx
const { typewriter, elements, cursor, keyframes, accessibilityProps, screenReaderAnnouncement } =
useTypewriter({
respectReducedMotion: true,
ariaLabel: 'Welcome message',
announceCompletion: true,
});
return (
<>
<style>{keyframes}</style>
<div {...accessibilityProps}>
{elements}
{cursor}
{screenReaderAnnouncement}
</div>
</>
);
```
## ๐ Documentation
### ๐ Getting Started
- ๐ [Quick Start Guide](https://doguyilmaz.github.io/use-typewriter-animation/docs/getting-started/quick-start) - Get up and running in minutes
- โ๏ธ [Installation & Setup](https://doguyilmaz.github.io/use-typewriter-animation/docs/getting-started/installation) - Detailed installation guide
- ๐ฏ [Basic Usage](https://doguyilmaz.github.io/use-typewriter-animation/docs/getting-started/basic-usage) - Learn the fundamentals
### ๐ง API Reference
- ๐ฎ [useTypewriter Hook](https://doguyilmaz.github.io/use-typewriter-animation/docs/api/use-typewriter) - Complete API documentation
- ๐ [Type Definitions](https://doguyilmaz.github.io/use-typewriter-animation/docs/api/types) - TypeScript types and interfaces
- โ๏ธ [React 19 Features](https://doguyilmaz.github.io/use-typewriter-animation/docs/api/configuration) - Modern React features
### ๐ฏ Feature Guides
- โฟ [Accessibility Guide](https://doguyilmaz.github.io/use-typewriter-animation/docs/guides/accessibility) - WCAG 2.1 compliance
- โก [Performance Guide](https://doguyilmaz.github.io/use-typewriter-animation/docs/guides/performance) - Optimization techniques
- ๐ง [Troubleshooting](https://doguyilmaz.github.io/use-typewriter-animation/docs/guides/troubleshooting) - Common issues and solutions
### ๐ Examples
- ๐จ [Example Gallery](https://doguyilmaz.github.io/use-typewriter-animation/docs/examples) - Comprehensive examples
- ๐ป [Live Examples](https://doguyilmaz.github.io/use-typewriter-animation/examples) - Interactive examples you can run
## โก Performance
Optimized for production use:
- **Bundle Size**: 5.3KB gzipped (ESM) / 5.6KB gzipped (CJS)
- **Memory Efficient**: Virtualization for large text
- **Smooth Animations**: GPU-accelerated CSS
- **Zero Dependencies**: No external runtime dependencies
### Bundle Analysis
- **ESM Bundle**: 15KB raw โ 5.3KB gzipped
- **CJS Bundle**: 16KB raw โ 5.6KB gzipped
_Measurements taken with our actual build output. Run `bun run analyze` to verify these numbers yourself._
## โฟ Accessibility
Built with accessibility as a first-class citizen:
- โ
WCAG 2.1 AA compliant
- โ
Screen reader support with ARIA live regions
- โ
Reduced motion support respects user preferences
- โ
Keyboard navigation with customizable shortcuts
- โ
Focus management for interactive elements
- โ
Semantic HTML with proper ARIA attributes
## ๐ Browser Support
- **Modern Browsers**: Chrome 88+ | Firefox 85+ | Safari 14+ | Edge 88+
- **Mobile**: iOS Safari 14+ | Android Chrome 88+
- **React**: 16.8+ | 17+ | 18+ | 19+ (full compatibility)
## ๐ง Essential Configuration
```jsx
const { typewriter } = useTypewriter({
// Visual Settings
typeSpeed: 50, // Typing speed (ms per character)
deleteSpeed: 30, // Delete speed (ms per character)
cursorStyle: 'bar', // 'bar' | 'block' | 'underline'
cursorColor: '#000', // CSS color value
// Accessibility
respectReducedMotion: true, // Honor user preferences
ariaLabel: 'Typewriter', // ARIA label
announceCompletion: true, // Screen reader announcements
// Performance
enableVirtualization: true, // For large text
maxVisibleSegments: 100, // Virtualization limit
// Interaction
enableKeyboardControls: true, // Keyboard shortcuts
loop: false, // Continuous loop
});
```
## ๐ฎ Control Methods
```jsx
typewriter
.type('Hello, World!') // Type text
.pauseFor(1000) // Pause for duration
.deleteLetters(5) // Delete characters
.deleteWords(2) // Delete words
.deleteAll() // Clear all text
.colorize('#ff0000') // Change color
.newLine() // Line break
.on('end', callback) // Event listener
.start(); // Start animation
```
## ๐งช Testing
```jsx
// Mock for tests
Object.defineProperty(window, 'matchMedia', {
value: jest.fn(() => ({ matches: false })),
});
test('typewriter animation', async () => {
render(<TypewriterComponent />);
await waitFor(() => {
expect(screen.getByText('Hello, World!')).toBeInTheDocument();
});
});
```
## ๐จ Common Issues
### Animation Not Working?
```jsx
// โ Missing keyframes
return (
<div>
{elements}
{cursor}
</div>
);
// โ
Include keyframes
return (
<>
<style>{keyframes}</style>
<div>
{elements}
{cursor}
</div>
</>
);
```
### Performance Issues?
```jsx
// โ
Enable virtualization for large text
const { typewriter } = useTypewriter({
enableVirtualization: true,
maxVisibleSegments: 100,
});
```
[See full troubleshooting guide โ](https://doguyilmaz.github.io/use-typewriter-animation/docs/guides/troubleshooting)
## ๐ค Contributing
We welcome contributions! Please see our [Contributing Guide](https://doguyilmaz.github.io/use-typewriter-animation/docs/contributing/contributing) for details.
- ๐ [Report Issues](https://github.com/doguyilmaz/use-typewriter-animation/issues)
- ๐ก [Request Features](https://github.com/doguyilmaz/use-typewriter-animation/issues/new?template=feature_request.md)
- ๐ [Improve Docs](https://github.com/doguyilmaz/use-typewriter-animation/tree/main/docs)
## ๐ License
MIT ยฉ [Dogu Yilmaz](https://github.com/doguyilmaz)
## ๐ Links
- ๐ [Full Documentation](https://doguyilmaz.github.io/use-typewriter-animation/)
- ๐ฏ [Examples](https://doguyilmaz.github.io/use-typewriter-animation/examples)
- ๐ [GitHub](https://github.com/doguyilmaz/use-typewriter-animation)
- ๐ฆ [npm](https://www.npmjs.com/package/use-typewriter-animation)
---
_Made with โค๏ธ for the React community_