136 lines
3.3 KiB
TypeScript
136 lines
3.3 KiB
TypeScript
'use client';
|
|
|
|
import React, { useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
|
import { cn } from '@/lib/utils';
|
|
import { AuroraText } from './aurora-text';
|
|
|
|
export interface TypingAnimationProps {
|
|
children?: string;
|
|
words?: string[];
|
|
className?: string;
|
|
duration?: number;
|
|
typeSpeed?: number;
|
|
delay?: number;
|
|
showCursor?: boolean;
|
|
blinkCursor?: boolean;
|
|
cursorStyle?: 'line' | 'block' | 'underscore';
|
|
/**
|
|
* When true (default), automatically applies Magic UI AuroraText effect all the time once typing finishes.
|
|
*/
|
|
auroraAfter?: boolean;
|
|
auroraColors?: string[];
|
|
onComplete?: () => void;
|
|
}
|
|
|
|
/**
|
|
* Magic UI - Typing Animation
|
|
* Characters appearing in typed animation letter-by-letter.
|
|
* Once typing finishes, automatically transitions to Magic UI AuroraText all the time.
|
|
* Reference: https://magicui.design/docs/components/typing-animation
|
|
* Reference: https://magicui.design/docs/components/aurora-text
|
|
*/
|
|
export function TypingAnimation({
|
|
children,
|
|
words,
|
|
className,
|
|
duration = 80,
|
|
typeSpeed,
|
|
delay = 0,
|
|
showCursor = true,
|
|
blinkCursor = true,
|
|
cursorStyle = 'line',
|
|
auroraAfter = true,
|
|
auroraColors,
|
|
onComplete,
|
|
}: TypingAnimationProps) {
|
|
const targetText = useMemo(
|
|
() => (words && words[0]) || children || '',
|
|
[words, children]
|
|
);
|
|
|
|
const [displayedText, setDisplayedText] = useState<string>('');
|
|
const [isCompleted, setIsCompleted] = useState(false);
|
|
const onCompleteRef = useRef(onComplete);
|
|
onCompleteRef.current = onComplete;
|
|
|
|
const typingSpeed = typeSpeed ?? duration;
|
|
|
|
useEffect(() => {
|
|
let timeout: ReturnType<typeof setTimeout> | null = null;
|
|
const graphemes = Array.from(targetText);
|
|
|
|
if (graphemes.length === 0) {
|
|
setIsCompleted(true);
|
|
return;
|
|
}
|
|
|
|
let currentIndex = 0;
|
|
|
|
const startTyping = () => {
|
|
const typeNextChar = () => {
|
|
if (currentIndex < graphemes.length) {
|
|
currentIndex++;
|
|
setDisplayedText(graphemes.slice(0, currentIndex).join(''));
|
|
if (currentIndex < graphemes.length) {
|
|
timeout = setTimeout(typeNextChar, typingSpeed);
|
|
} else {
|
|
setIsCompleted(true);
|
|
onCompleteRef.current?.();
|
|
}
|
|
}
|
|
};
|
|
|
|
typeNextChar();
|
|
};
|
|
|
|
if (delay > 0) {
|
|
timeout = setTimeout(startTyping, delay);
|
|
} else {
|
|
startTyping();
|
|
}
|
|
|
|
return () => {
|
|
if (timeout !== null) clearTimeout(timeout);
|
|
};
|
|
}, [targetText, delay, typingSpeed]);
|
|
|
|
const getCursorChar = () => {
|
|
switch (cursorStyle) {
|
|
case 'block':
|
|
return '▌';
|
|
case 'underscore':
|
|
return '_';
|
|
case 'line':
|
|
default:
|
|
return '|';
|
|
}
|
|
};
|
|
|
|
// Once typing finishes, use Magic UI AuroraText all the time!
|
|
if (isCompleted && auroraAfter) {
|
|
return (
|
|
<AuroraText colors={auroraColors} className={className}>
|
|
{targetText}
|
|
</AuroraText>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<span className={cn('inline-block leading-[1.08] tracking-[-0.02em]', className)}>
|
|
<span>{displayedText}</span>
|
|
{showCursor && !isCompleted && (
|
|
<span
|
|
className={cn(
|
|
'inline-block ml-0.5 text-accent font-light',
|
|
blinkCursor && 'animate-blink-cursor'
|
|
)}
|
|
>
|
|
{getCursorChar()}
|
|
</span>
|
|
)}
|
|
</span>
|
|
);
|
|
}
|
|
|
|
export default TypingAnimation;
|