import { useEffect } from "react"; import gsap from "gsap"; import SplitText from "gsap/SplitText"; import ScrollTrigger from "gsap/ScrollTrigger"; gsap.registerPlugin(SplitText, ScrollTrigger); type UseSplitTextAnimationProps = { headingRef: React.RefObject; paraRef: React.RefObject; shinyRef?: React.RefObject; triggerId: string; }; export function useSplitTextAnimation({ headingRef, paraRef, shinyRef, triggerId, }: UseSplitTextAnimationProps) { useEffect(() => { const ctx = gsap.context(() => { const tl = gsap.timeline({ scrollTrigger: { trigger: `#${triggerId}`, start: "top 70%", }, }); const headingSplit = new SplitText(headingRef.current, { type: "lines, words", linesClass: "line", }); const paraSplit = new SplitText(paraRef.current, { type: "lines, words", linesClass: "line", }); const headingWords = headingSplit.words; const paraWords = paraSplit.words; if (shinyRef?.current) { tl.from(shinyRef.current, { opacity: 0, y: 20, duration: 0.6, ease: "power", }); } tl.from( headingWords, { duration: 0.8, opacity: 0, yPercent: 120, ease: "power", stagger: 0.015, }, "-=0.2" ); tl.from( paraWords, { duration: 0.7, opacity: 0, yPercent: 120, ease: "power", stagger: 0.01, }, "-=0.5" ); }); return () => { ctx.revert(); ScrollTrigger.getAll().forEach((trigger) => trigger.kill()); }; }, [headingRef, paraRef, shinyRef, triggerId]); }