|
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import classNames from "classnames"; |
| 4 | + |
| 5 | +import styles from "@/components/ui/Text/Text.module.scss"; |
| 6 | + |
| 7 | +interface Letter { |
| 8 | + char: string; |
| 9 | + id: number; |
| 10 | +} |
| 11 | + |
| 12 | +const AnimationText: React.FC = () => { |
| 13 | + const [firstLineLetters, setFirstLineLetters] = useState<Letter[]>([]); |
| 14 | + const [secondLineLetters, setSecondLineLetters] = useState<Letter[]>([]); |
| 15 | + const [hasAnimated, setHasAnimated] = useState<boolean>(false); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + if (hasAnimated) return; |
| 19 | + setHasAnimated(true); |
| 20 | + |
| 21 | + const firstLine = "영수증으로"; |
| 22 | + const firstLetters = firstLine.split("").map((char, index) => ({ |
| 23 | + char, |
| 24 | + id: index, |
| 25 | + })); |
| 26 | + setFirstLineLetters(firstLetters); |
| 27 | + |
| 28 | + const firstLineDelay = firstLine.length * 20 + 100; |
| 29 | + setTimeout(() => { |
| 30 | + const secondLine = "AI 음식 리뷰 남겨요"; |
| 31 | + const secondLetters = [...secondLine].map((char, index) => ({ |
| 32 | + // split("") 대신 [...string] 사용 |
| 33 | + char, |
| 34 | + id: index, |
| 35 | + })); |
| 36 | + setSecondLineLetters(secondLetters); |
| 37 | + }, firstLineDelay); |
| 38 | + }, [hasAnimated]); |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className={styles.TitleWrapper}> |
| 42 | + <h1 className={classNames(styles.word)}> |
| 43 | + {firstLineLetters.map((letter, index) => ( |
| 44 | + <span |
| 45 | + key={letter.id} |
| 46 | + className={classNames( |
| 47 | + styles.letter, |
| 48 | + styles.Text, |
| 49 | + styles["color-gradient"], |
| 50 | + styles[`variant-titleLg`], |
| 51 | + styles[`align-center`], |
| 52 | + letter.char === " " && styles.space, |
| 53 | + )} |
| 54 | + style={{ |
| 55 | + animationDelay: `${index * 50}ms`, |
| 56 | + animationName: letter.char === " " ? "none" : styles.textBlur, |
| 57 | + animationDuration: "0.5s", |
| 58 | + animationFillMode: "forwards", |
| 59 | + animationTimingFunction: "cubic-bezier(0.4, 0, 0.2, 1)", |
| 60 | + }} |
| 61 | + > |
| 62 | + {letter.char} |
| 63 | + </span> |
| 64 | + ))} |
| 65 | + </h1> |
| 66 | + <h1 className={styles.word}> |
| 67 | + {secondLineLetters.map((letter, index) => ( |
| 68 | + <span |
| 69 | + key={letter.id} |
| 70 | + className={classNames( |
| 71 | + styles.letter, |
| 72 | + styles.Text, |
| 73 | + styles["color-gradient"], |
| 74 | + styles[`variant-titleLg`], |
| 75 | + styles[`align-center`], |
| 76 | + letter.char === " " && styles.space, |
| 77 | + )} |
| 78 | + style={{ |
| 79 | + animationDelay: `${index * 20}ms`, |
| 80 | + animationName: letter.char === " " ? "none" : styles.textBlur, |
| 81 | + animationDuration: "0.5s", |
| 82 | + animationFillMode: "forwards", |
| 83 | + animationTimingFunction: "cubic-bezier(0.4, 0, 0.2, 1)", |
| 84 | + }} |
| 85 | + > |
| 86 | + {letter.char} |
| 87 | + </span> |
| 88 | + ))} |
| 89 | + </h1> |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +export default AnimationText; |
0 commit comments