Skip to content

Commit 55afe66

Browse files
authored
Merge pull request #723 from freakdaniel/main
2 parents 4510c5d + 551dae2 commit 55afe66

File tree

4 files changed

+273
-106
lines changed

4 files changed

+273
-106
lines changed

src/content/Animations/AnimatedContent/AnimatedContent.jsx

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gsap.registerPlugin(ScrollTrigger);
66

77
const AnimatedContent = ({
88
children,
9+
container,
910
distance = 100,
1011
direction = 'vertical',
1112
reverse = false,
@@ -16,59 +17,94 @@ const AnimatedContent = ({
1617
scale = 1,
1718
threshold = 0.1,
1819
delay = 0,
19-
onComplete
20+
disappearAfter = 0,
21+
disappearDuration = 0.5,
22+
disappearEase = 'power3.in',
23+
onComplete,
24+
onDisappearanceComplete,
25+
className = '',
26+
...props
2027
}) => {
2128
const ref = useRef(null);
2229

2330
useEffect(() => {
2431
const el = ref.current;
2532
if (!el) return;
2633

34+
let scrollerTarget = container || document.getElementById('snap-main-container') || null;
35+
36+
if (typeof scrollerTarget === 'string') {
37+
scrollerTarget = document.querySelector(scrollerTarget);
38+
}
39+
2740
const axis = direction === 'horizontal' ? 'x' : 'y';
2841
const offset = reverse ? -distance : distance;
2942
const startPct = (1 - threshold) * 100;
3043

3144
gsap.set(el, {
3245
[axis]: offset,
3346
scale,
34-
opacity: animateOpacity ? initialOpacity : 1
47+
opacity: animateOpacity ? initialOpacity : 1,
48+
visibility: 'visible'
49+
});
50+
51+
const tl = gsap.timeline({
52+
paused: true,
53+
delay,
54+
onComplete: () => {
55+
if (onComplete) onComplete();
56+
if (disappearAfter > 0) {
57+
gsap.to(el, {
58+
[axis]: reverse ? distance : -distance,
59+
scale: 0.8,
60+
opacity: animateOpacity ? initialOpacity : 0,
61+
delay: disappearAfter,
62+
duration: disappearDuration,
63+
ease: disappearEase,
64+
onComplete: () => onDisappearanceComplete?.()
65+
});
66+
}
67+
}
3568
});
3669

37-
gsap.to(el, {
70+
tl.to(el, {
3871
[axis]: 0,
3972
scale: 1,
4073
opacity: 1,
4174
duration,
42-
ease,
43-
delay,
44-
onComplete,
45-
scrollTrigger: {
46-
trigger: el,
47-
start: `top ${startPct}%`,
48-
toggleActions: 'play none none none',
49-
once: true
50-
}
75+
ease
76+
});
77+
78+
const st = ScrollTrigger.create({
79+
trigger: el,
80+
scroller: scrollerTarget,
81+
start: `top ${startPct}%`,
82+
once: true,
83+
onEnter: () => tl.play()
5184
});
5285

5386
return () => {
54-
ScrollTrigger.getAll().forEach(t => t.kill());
55-
gsap.killTweensOf(el);
87+
st.kill();
88+
tl.kill();
5689
};
5790
}, [
58-
distance,
59-
direction,
60-
reverse,
61-
duration,
62-
ease,
63-
initialOpacity,
64-
animateOpacity,
65-
scale,
66-
threshold,
67-
delay,
68-
onComplete
91+
container,
92+
distance, direction, reverse, duration, ease,
93+
initialOpacity, animateOpacity, scale, threshold,
94+
delay, disappearAfter, disappearDuration,
95+
disappearEase, onComplete, onDisappearanceComplete
6996
]);
7097

71-
return <div ref={ref}>{children}</div>;
98+
return (
99+
<div
100+
ref={ref}
101+
className={className}
102+
style={{ visibility: 'hidden' }}
103+
{...props}
104+
>
105+
{children}
106+
</div>
107+
);
72108
};
73109

74-
export default AnimatedContent;
110+
export default AnimatedContent;

src/tailwind/Animations/AnimatedContent/AnimatedContent.jsx

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ gsap.registerPlugin(ScrollTrigger);
66

77
const AnimatedContent = ({
88
children,
9+
container,
910
distance = 100,
1011
direction = 'vertical',
1112
reverse = false,
@@ -16,59 +17,93 @@ const AnimatedContent = ({
1617
scale = 1,
1718
threshold = 0.1,
1819
delay = 0,
19-
onComplete
20+
disappearAfter = 0,
21+
disappearDuration = 0.5,
22+
disappearEase = 'power3.in',
23+
onComplete,
24+
onDisappearanceComplete,
25+
className = '',
26+
...props
2027
}) => {
2128
const ref = useRef(null);
2229

2330
useEffect(() => {
2431
const el = ref.current;
2532
if (!el) return;
2633

34+
let scrollerTarget = container || document.getElementById('snap-main-container') || null;
35+
36+
if (typeof scrollerTarget === 'string') {
37+
scrollerTarget = document.querySelector(scrollerTarget);
38+
}
39+
2740
const axis = direction === 'horizontal' ? 'x' : 'y';
2841
const offset = reverse ? -distance : distance;
2942
const startPct = (1 - threshold) * 100;
3043

3144
gsap.set(el, {
3245
[axis]: offset,
3346
scale,
34-
opacity: animateOpacity ? initialOpacity : 1
47+
opacity: animateOpacity ? initialOpacity : 1,
48+
visibility: 'visible'
49+
});
50+
51+
const tl = gsap.timeline({
52+
paused: true,
53+
delay,
54+
onComplete: () => {
55+
if (onComplete) onComplete();
56+
if (disappearAfter > 0) {
57+
gsap.to(el, {
58+
[axis]: reverse ? distance : -distance,
59+
scale: 0.8,
60+
opacity: animateOpacity ? initialOpacity : 0,
61+
delay: disappearAfter,
62+
duration: disappearDuration,
63+
ease: disappearEase,
64+
onComplete: () => onDisappearanceComplete?.()
65+
});
66+
}
67+
}
3568
});
3669

37-
gsap.to(el, {
70+
tl.to(el, {
3871
[axis]: 0,
3972
scale: 1,
4073
opacity: 1,
4174
duration,
42-
ease,
43-
delay,
44-
onComplete,
45-
scrollTrigger: {
46-
trigger: el,
47-
start: `top ${startPct}%`,
48-
toggleActions: 'play none none none',
49-
once: true
50-
}
75+
ease
76+
});
77+
78+
const st = ScrollTrigger.create({
79+
trigger: el,
80+
scroller: scrollerTarget,
81+
start: `top ${startPct}%`,
82+
once: true,
83+
onEnter: () => tl.play()
5184
});
5285

5386
return () => {
54-
ScrollTrigger.getAll().forEach(t => t.kill());
55-
gsap.killTweensOf(el);
87+
st.kill();
88+
tl.kill();
5689
};
5790
}, [
58-
distance,
59-
direction,
60-
reverse,
61-
duration,
62-
ease,
63-
initialOpacity,
64-
animateOpacity,
65-
scale,
66-
threshold,
67-
delay,
68-
onComplete
91+
container,
92+
distance, direction, reverse, duration, ease,
93+
initialOpacity, animateOpacity, scale, threshold,
94+
delay, disappearAfter, disappearDuration,
95+
disappearEase, onComplete, onDisappearanceComplete
6996
]);
7097

71-
return <div ref={ref}>{children}</div>;
98+
return (
99+
<div
100+
ref={ref}
101+
className={`invisible ${className}`}
102+
{...props}
103+
>
104+
{children}
105+
</div>
106+
);
72107
};
73108

74-
export default AnimatedContent;
109+
export default AnimatedContent;

0 commit comments

Comments
 (0)