1"use client"
2
3import { useEffect, useState } from "react"
4import { AnimatePresence, motion, type MotionProps } from "motion/react"
5
6import { cn } from "@/lib/utils"
7
8interface WordRotateProps {
9 words: string[]
10 duration?: number
11 motionProps?: MotionProps
12 className?: string
13}
14
15export function WordRotate({
16 words,
17 duration = 2500,
18 motionProps = {
19 initial: { opacity: 0, y: -50 },
20 animate: { opacity: 1, y: 0 },
21 exit: { opacity: 0, y: 50 },
22 transition: { duration: 0.25, ease: "easeOut" },
23 },
24 className,
25}: WordRotateProps) {
26 const [index, setIndex] = useState(0)
27
28 useEffect(() => {
29 const interval = setInterval(() => {
30 setIndex((prevIndex) => (prevIndex + 1) % words.length)
31 }, duration)
32
33 // Clean up interval on unmount
34 return () => clearInterval(interval)
35 }, [words, duration])
36
37 return (
38 <div className="overflow-hidden py-2">
39 <AnimatePresence mode="wait">
40 <motion.h1
41 key={words[index]}
42 className={cn(className)}
43 {...motionProps}
44 >
45 {words[index]}
46 </motion.h1>
47 </AnimatePresence>
48 </div>
49 )
50}
51