|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { cn } from "@/lib/utils"; |
| 4 | +import { motion, stagger, useAnimate, useInView } from "framer-motion"; |
| 5 | +import { useEffect } from "react"; |
| 6 | + |
| 7 | +export const TypewriterEffect = ({ |
| 8 | + words, |
| 9 | + className, |
| 10 | +}: { |
| 11 | + words: { |
| 12 | + text: string; |
| 13 | + className?: string; |
| 14 | + }[]; |
| 15 | + className?: string; |
| 16 | +}) => { |
| 17 | + // split text inside of words into array of characters |
| 18 | + const wordsArray = words.map((word) => { |
| 19 | + return { |
| 20 | + ...word, |
| 21 | + text: word.text.split(""), |
| 22 | + }; |
| 23 | + }); |
| 24 | + |
| 25 | + const [scope, animate] = useAnimate(); |
| 26 | + const isInView = useInView(scope); |
| 27 | + useEffect(() => { |
| 28 | + if (isInView) { |
| 29 | + animate( |
| 30 | + "span", |
| 31 | + { |
| 32 | + display: "inline-block", |
| 33 | + opacity: 1, |
| 34 | + width: "fit-content", |
| 35 | + }, |
| 36 | + { |
| 37 | + duration: 0.3, |
| 38 | + delay: stagger(0.1), |
| 39 | + ease: "easeInOut", |
| 40 | + } |
| 41 | + ); |
| 42 | + } |
| 43 | + }, [isInView, animate]); |
| 44 | + |
| 45 | + const renderWords = () => { |
| 46 | + return ( |
| 47 | + <motion.div ref={scope} className="inline"> |
| 48 | + {wordsArray.map((word, idx) => { |
| 49 | + return ( |
| 50 | + <div key={`word-${idx}`} className="inline-block"> |
| 51 | + {word.text.map((char, index) => ( |
| 52 | + <motion.span |
| 53 | + initial={{}} |
| 54 | + key={`char-${index}`} |
| 55 | + className={cn( |
| 56 | + `text-ox-purple opacity-0 hidden`, |
| 57 | + word.className |
| 58 | + )} |
| 59 | + > |
| 60 | + {char} |
| 61 | + </motion.span> |
| 62 | + ))} |
| 63 | + |
| 64 | + </div> |
| 65 | + ); |
| 66 | + })} |
| 67 | + </motion.div> |
| 68 | + ); |
| 69 | + }; |
| 70 | + return ( |
| 71 | + <div |
| 72 | + className={cn( |
| 73 | + "text-xs lg:text-base lg:font-medium font-medium text-ox-purple text-center", |
| 74 | + className |
| 75 | + )} |
| 76 | + > |
| 77 | + {renderWords()} |
| 78 | + {/* <motion.span |
| 79 | + initial={{ |
| 80 | + opacity: 0, |
| 81 | + }} |
| 82 | + animate={{ |
| 83 | + opacity: 1, |
| 84 | + }} |
| 85 | + transition={{ |
| 86 | + duration: 0.8, |
| 87 | + repeat: Infinity, |
| 88 | + repeatType: "reverse", |
| 89 | + }} |
| 90 | + className={cn( |
| 91 | + "inline-block rounded-sm w-[4px] h-4 md:h-6 lg:h-10 bg-blue-500", |
| 92 | + )} |
| 93 | + ></motion.span> */} |
| 94 | + </div> |
| 95 | + ); |
| 96 | +}; |
| 97 | + |
| 98 | +export const TypewriterEffectSmooth = ({ |
| 99 | + words, |
| 100 | + className, |
| 101 | +}: { |
| 102 | + words: { |
| 103 | + text: string; |
| 104 | + className?: string; |
| 105 | + }[]; |
| 106 | + className?: string; |
| 107 | +}) => { |
| 108 | + // split text inside of words into array of characters |
| 109 | + const wordsArray = words.map((word) => { |
| 110 | + return { |
| 111 | + ...word, |
| 112 | + text: word.text.split(""), |
| 113 | + }; |
| 114 | + }); |
| 115 | + const renderWords = () => { |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + {wordsArray.map((word, idx) => { |
| 119 | + return ( |
| 120 | + <div key={`word-${idx}`} className="inline-block"> |
| 121 | + {word.text.map((char, index) => ( |
| 122 | + <span |
| 123 | + key={`char-${index}`} |
| 124 | + className={cn(`text-ox-purple`, word.className)} |
| 125 | + > |
| 126 | + {char} |
| 127 | + </span> |
| 128 | + ))} |
| 129 | + |
| 130 | + </div> |
| 131 | + ); |
| 132 | + })} |
| 133 | + </div> |
| 134 | + ); |
| 135 | + }; |
| 136 | + |
| 137 | + return ( |
| 138 | + <div className={cn("flex space-x-1 my-6", className)}> |
| 139 | + <motion.div |
| 140 | + className="overflow-hidden pb-2" |
| 141 | + whileInView={{ |
| 142 | + width: "fit-content", |
| 143 | + opacity: 1, |
| 144 | + }} |
| 145 | + initial={{ |
| 146 | + width: "0%", |
| 147 | + opacity: 0, |
| 148 | + }} |
| 149 | + transition={{ |
| 150 | + duration: 2, |
| 151 | + ease: "linear", |
| 152 | + delay: 1, |
| 153 | + }} |
| 154 | + > |
| 155 | + <div |
| 156 | + className="text-xs lg:text-base lg:font-medium font-medium text-ox-purple text-center" |
| 157 | + style={{ |
| 158 | + whiteSpace: "nowrap", |
| 159 | + }} |
| 160 | + > |
| 161 | + {renderWords()}{" "} |
| 162 | + </div>{" "} |
| 163 | + </motion.div> |
| 164 | + {/* <motion.span |
| 165 | + initial={{ |
| 166 | + opacity: 0, |
| 167 | + }} |
| 168 | + animate={{ |
| 169 | + opacity: 1, |
| 170 | + }} |
| 171 | + transition={{ |
| 172 | + duration: 0.8, |
| 173 | +
|
| 174 | + repeat: Infinity, |
| 175 | + repeatType: "reverse", |
| 176 | + }} |
| 177 | + className={cn( |
| 178 | + "block rounded-sm w-[4px] h-4 sm:h-6 xl:h-12 bg-blue-500" |
| 179 | + )} |
| 180 | + ></motion.span> */} |
| 181 | + </div> |
| 182 | + ); |
| 183 | +}; |
0 commit comments