51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { motion, AnimatePresence } from 'framer-motion';
|
|
|
|
interface MorphingTextProps {
|
|
texts: string[];
|
|
interval?: number;
|
|
className?: string;
|
|
}
|
|
|
|
/**
|
|
* Animate UI - Morphing Text
|
|
* Smoothly morphs and transitions between phrases using spring physics and blur filters.
|
|
* Reference: https://animate-ui.com/docs
|
|
*/
|
|
export function MorphingText({
|
|
texts,
|
|
interval = 2800,
|
|
className = '',
|
|
}: MorphingTextProps) {
|
|
const [index, setIndex] = useState(0);
|
|
|
|
useEffect(() => {
|
|
if (!texts || texts.length <= 1) return;
|
|
const timer = setInterval(() => {
|
|
setIndex((prev) => (prev + 1) % texts.length);
|
|
}, interval);
|
|
return () => clearInterval(timer);
|
|
}, [texts, interval]);
|
|
|
|
if (!texts || texts.length === 0) return null;
|
|
|
|
return (
|
|
<span className={`inline-block relative overflow-hidden align-baseline ${className}`}>
|
|
<AnimatePresence mode="wait">
|
|
<motion.span
|
|
key={texts[index]}
|
|
initial={{ opacity: 0, y: 16, filter: 'blur(6px)', scale: 0.96 }}
|
|
animate={{ opacity: 1, y: 0, filter: 'blur(0px)', scale: 1 }}
|
|
exit={{ opacity: 0, y: -16, filter: 'blur(6px)', scale: 0.96 }}
|
|
transition={{ duration: 0.42, ease: [0.16, 1, 0.3, 1] }}
|
|
className="inline-block whitespace-nowrap"
|
|
>
|
|
{texts[index]}
|
|
</motion.span>
|
|
</AnimatePresence>
|
|
</span>
|
|
);
|
|
}
|