Spaces:
Sleeping
Sleeping
implement stagger blur-in BlurText component using Framer Motion
Browse files
frontend/src/components/BlurText.jsx
CHANGED
|
@@ -1 +1,73 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useRef, useEffect, useState } from "react";
|
| 2 |
+
import { motion } from "framer-motion";
|
| 3 |
+
|
| 4 |
+
export default function BlurText({ text, className }) {
|
| 5 |
+
const containerRef = useRef(null);
|
| 6 |
+
const [isInView, setIsInView] = useState(false);
|
| 7 |
+
|
| 8 |
+
useEffect(() => {
|
| 9 |
+
const observer = new IntersectionObserver(
|
| 10 |
+
([entry]) => {
|
| 11 |
+
if (entry.isIntersecting) {
|
| 12 |
+
setIsInView(true);
|
| 13 |
+
// Unobserve after triggering once to lock the animation
|
| 14 |
+
observer.unobserve(entry.target);
|
| 15 |
+
}
|
| 16 |
+
},
|
| 17 |
+
{ threshold: 0.1 } // Triggers on 10% visibility
|
| 18 |
+
);
|
| 19 |
+
|
| 20 |
+
if (containerRef.current) {
|
| 21 |
+
observer.observe(containerRef.current);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
return () => {
|
| 25 |
+
observer.disconnect();
|
| 26 |
+
};
|
| 27 |
+
}, []);
|
| 28 |
+
|
| 29 |
+
const words = text.split(" ");
|
| 30 |
+
|
| 31 |
+
return (
|
| 32 |
+
<p
|
| 33 |
+
ref={containerRef}
|
| 34 |
+
className={className}
|
| 35 |
+
style={{
|
| 36 |
+
display: "flex",
|
| 37 |
+
flexWrap: "wrap",
|
| 38 |
+
justifyContent: "center",
|
| 39 |
+
rowGap: "0.1em",
|
| 40 |
+
}}
|
| 41 |
+
>
|
| 42 |
+
{words.map((word, i) => {
|
| 43 |
+
return (
|
| 44 |
+
<motion.span
|
| 45 |
+
key={i}
|
| 46 |
+
initial={{ filter: "blur(10px)", opacity: 0, y: 50 }}
|
| 47 |
+
animate={
|
| 48 |
+
isInView
|
| 49 |
+
? {
|
| 50 |
+
filter: ["blur(10px)", "blur(5px)", "blur(0px)"],
|
| 51 |
+
opacity: [0, 0.5, 1],
|
| 52 |
+
y: [50, -5, 0],
|
| 53 |
+
}
|
| 54 |
+
: {}
|
| 55 |
+
}
|
| 56 |
+
transition={{
|
| 57 |
+
duration: 0.7, // stepDuration 0.35s * 2
|
| 58 |
+
times: [0, 0.5, 1],
|
| 59 |
+
ease: "easeOut",
|
| 60 |
+
delay: (i * 100) / 1000, // Stagger: 100ms per word
|
| 61 |
+
}}
|
| 62 |
+
style={{
|
| 63 |
+
display: "inline-block",
|
| 64 |
+
marginRight: "0.28em", // Letter spacing eats NBSP, this preserves spacing
|
| 65 |
+
}}
|
| 66 |
+
>
|
| 67 |
+
{word}
|
| 68 |
+
</motion.span>
|
| 69 |
+
);
|
| 70 |
+
})}
|
| 71 |
+
</p>
|
| 72 |
+
);
|
| 73 |
+
}
|