160 lines
3.7 KiB
TypeScript
160 lines
3.7 KiB
TypeScript
import React, { useRef, useEffect } from "react";
|
|
import { gsap } from "gsap";
|
|
import { ScrollTrigger } from "gsap/ScrollTrigger";
|
|
|
|
gsap.registerPlugin(ScrollTrigger);
|
|
|
|
interface AnimatedContentProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
children: React.ReactNode;
|
|
container?: Element | string | null;
|
|
distance?: number;
|
|
direction?: "vertical" | "horizontal" | "none";
|
|
reverse?: boolean;
|
|
duration?: number;
|
|
ease?: string;
|
|
initialOpacity?: number;
|
|
animateOpacity?: boolean;
|
|
scale?: number;
|
|
threshold?: number;
|
|
delay?: number;
|
|
disappearAfter?: number;
|
|
disappearDuration?: number;
|
|
disappearEase?: string;
|
|
onComplete?: () => void;
|
|
onDisappearanceComplete?: () => void;
|
|
}
|
|
|
|
const AnimatedContent: React.FC<AnimatedContentProps> = ({
|
|
children,
|
|
container,
|
|
distance = 100,
|
|
direction = "vertical",
|
|
reverse = false,
|
|
duration = 0.8,
|
|
ease = "power3.out",
|
|
initialOpacity = 0,
|
|
animateOpacity = true,
|
|
scale = 1,
|
|
threshold = 0.1,
|
|
delay = 0,
|
|
disappearAfter = 0,
|
|
disappearDuration = 0.5,
|
|
disappearEase = "power3.in",
|
|
onComplete,
|
|
onDisappearanceComplete,
|
|
className = "",
|
|
...props
|
|
}) => {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
const el = ref.current;
|
|
if (!el) return;
|
|
|
|
let scrollerTarget: Element | string | null =
|
|
container || document.getElementById("snap-main-container") || null;
|
|
|
|
if (typeof scrollerTarget === "string") {
|
|
scrollerTarget = document.querySelector(scrollerTarget);
|
|
}
|
|
|
|
const startPct = (1 - threshold) * 100;
|
|
const setConfig: gsap.TweenVars = {
|
|
opacity: animateOpacity ? initialOpacity : 1,
|
|
visibility: "visible",
|
|
};
|
|
|
|
const toConfig: gsap.TweenVars = {
|
|
opacity: 1,
|
|
duration,
|
|
ease,
|
|
};
|
|
if (direction !== "none") {
|
|
const axis = direction === "horizontal" ? "x" : "y";
|
|
const offset = reverse ? -distance : distance;
|
|
setConfig[axis] = offset;
|
|
toConfig[axis] = 0;
|
|
}
|
|
if (scale !== 1) {
|
|
setConfig.scale = scale;
|
|
toConfig.scale = 1;
|
|
}
|
|
|
|
gsap.set(el, setConfig);
|
|
|
|
const tl = gsap.timeline({
|
|
paused: true,
|
|
delay,
|
|
onComplete: () => {
|
|
if (onComplete) onComplete();
|
|
if (disappearAfter > 0) {
|
|
const disappearConfig: gsap.TweenVars = {
|
|
opacity: animateOpacity ? initialOpacity : 0,
|
|
delay: disappearAfter,
|
|
duration: disappearDuration,
|
|
ease: disappearEase,
|
|
onComplete: () => onDisappearanceComplete?.(),
|
|
};
|
|
|
|
if (direction !== "none") {
|
|
const axis = direction === "horizontal" ? "x" : "y";
|
|
disappearConfig[axis] = reverse ? distance : -distance;
|
|
}
|
|
|
|
if (scale !== 1) {
|
|
disappearConfig.scale = 0.8;
|
|
}
|
|
|
|
gsap.to(el, disappearConfig);
|
|
}
|
|
},
|
|
});
|
|
|
|
tl.to(el, toConfig);
|
|
|
|
const st = ScrollTrigger.create({
|
|
trigger: el,
|
|
scroller: scrollerTarget || window,
|
|
start: `top ${startPct}%`,
|
|
once: true,
|
|
onEnter: () => tl.play(),
|
|
});
|
|
const resizeObserver = new ResizeObserver(() => {
|
|
ScrollTrigger.refresh();
|
|
});
|
|
|
|
resizeObserver.observe(document.body);
|
|
|
|
return () => {
|
|
resizeObserver.disconnect();
|
|
st.kill();
|
|
tl.kill();
|
|
};
|
|
}, [
|
|
container,
|
|
distance,
|
|
direction,
|
|
reverse,
|
|
duration,
|
|
ease,
|
|
initialOpacity,
|
|
animateOpacity,
|
|
scale,
|
|
threshold,
|
|
delay,
|
|
disappearAfter,
|
|
disappearDuration,
|
|
disappearEase,
|
|
onComplete,
|
|
onDisappearanceComplete,
|
|
]);
|
|
|
|
return (
|
|
<div ref={ref} className={`invisible ${className}`} {...props}>
|
|
{children}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default AnimatedContent;
|