File size: 4,464 Bytes
65d68f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | "use client";
import { useEffect, useRef } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
type SplitTextProps = {
text: string;
className?: string;
delay?: number;
duration?: number;
ease?: string;
splitType?: "chars" | "words" | "lines" | "chars,words" | "words,lines" | "chars,words,lines";
from?: gsap.TweenVars;
to?: gsap.TweenVars;
threshold?: number;
rootMargin?: string;
textAlign?: "left" | "center" | "right";
tag?: "p" | "h1" | "h2" | "h3" | "span";
onLetterAnimationComplete?: () => void;
showCallback?: boolean;
};
export default function SplitText({
text = "",
className = "",
delay = 50,
duration = 1.25,
ease = "power3.out",
splitType = "chars",
from = { opacity: 0, y: 40 },
to = { opacity: 1, y: 0 },
threshold = 0.1,
rootMargin = "-100px",
textAlign = "center",
tag = "p",
onLetterAnimationComplete,
showCallback = false,
}: SplitTextProps) {
const ref = useRef<HTMLElement | null>(null);
const animationCompletedRef = useRef(false);
const onCompleteRef = useRef(onLetterAnimationComplete);
useEffect(() => {
onCompleteRef.current = onLetterAnimationComplete;
}, [onLetterAnimationComplete]);
useEffect(() => {
if (typeof window === "undefined" || !ref.current || !text) return;
if (animationCompletedRef.current) return;
gsap.registerPlugin(ScrollTrigger);
const el = ref.current;
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (reduceMotion) {
animationCompletedRef.current = true;
onCompleteRef.current?.();
return;
}
const startPct = (1 - threshold) * 100;
const marginMatch = /^(-?\d+(?:\.\d+)?)(px|em|rem|%)?$/.exec(rootMargin);
const marginValue = marginMatch ? parseFloat(marginMatch[1]) : 0;
const marginUnit = marginMatch ? marginMatch[2] || "px" : "px";
const sign =
marginValue === 0
? ""
: marginValue < 0
? `-=${Math.abs(marginValue)}${marginUnit}`
: `+=${marginValue}${marginUnit}`;
const start = `top ${startPct}%${sign}`;
// Select targets based on splitType
let targets: Element[] = [];
if (splitType.includes("chars")) {
targets = Array.from(el.querySelectorAll(".split-char"));
} else if (splitType.includes("words")) {
targets = Array.from(el.querySelectorAll(".split-word"));
} else {
targets = [el];
}
// Set initial state
gsap.set(targets, { ...from });
// Animate using scrollTrigger
const anim = gsap.to(targets, {
...to,
duration,
ease,
stagger: delay / 1000,
scrollTrigger: {
trigger: el,
start,
once: true,
fastScrollEnd: true,
anticipatePin: 0.4,
},
onComplete: () => {
animationCompletedRef.current = true;
onCompleteRef.current?.();
},
force3D: true,
});
return () => {
ScrollTrigger.getAll().forEach((trigger) => {
if (trigger.trigger === el) trigger.kill();
});
anim.kill();
};
}, [
delay,
duration,
ease,
from,
rootMargin,
showCallback,
splitType,
text,
threshold,
to,
]);
const Tag = tag;
const words = text.split(" ");
return (
<Tag
ref={(node) => {
ref.current = node;
}}
className={`split-parent ${className}`.trim()}
style={{
textAlign,
overflow: "hidden",
display: "inline-block",
whiteSpace: "normal",
wordWrap: "break-word",
willChange: "transform, opacity",
}}
>
{splitType.includes("chars") ? (
words.map((word, wIdx) => (
<span key={wIdx} className="split-word inline-block" style={{ whiteSpace: "nowrap" }}>
{word.split("").map((char, cIdx) => (
<span key={cIdx} className="split-char inline-block" style={{ display: "inline-block" }}>
{char}
</span>
))}
{wIdx < words.length - 1 ? "\u00A0" : ""}
</span>
))
) : splitType.includes("words") ? (
words.map((word, wIdx) => (
<span key={wIdx} className="split-word inline-block" style={{ display: "inline-block" }}>
{word}
{wIdx < words.length - 1 ? "\u00A0" : ""}
</span>
))
) : (
text
)}
</Tag>
);
}
|