UNPKG

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
# use-typewriter-animation A modern, performant React hook for creating typewriter animation effects with full TypeScript support, accessibility features, and React 19 compatibility. [![npm version](https://badge.fury.io/js/use-typewriter-animation.svg)](https://www.npmjs.com/package/use-typewriter-animation) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Downloads](https://img.shields.io/npm/dm/use-typewriter-animation.svg)](https://www.npmjs.com/package/use-typewriter-animation) [![Bundle Size](https://img.shields.io/bundlephobia/minzip/use-typewriter-animation?label=gzipped)](https://bundlephobia.com/package/use-typewriter-animation) [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/) [![React](https://img.shields.io/badge/React-16.8+-61DAFB.svg)](https://reactjs.org/) [![GitHub Stars](https://img.shields.io/github/stars/doguyilmaz/use-typewriter-animation.svg?style=social)](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_