Skip to content

Commit

Permalink
make sure disabled state from root cascades
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbenegas committed Jul 24, 2023
1 parent ee25209 commit 61c816b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-flies-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bsmnt/scrollytelling": patch
---

add disabled prop to Root
5 changes: 3 additions & 2 deletions scrollytelling/src/components/animation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function Animation(props: AnimationProps): React.ReactElement | null {
start: tween.start,
end: tween.end,
});
if (!timelineSpace) return; // root is probably disabled.
const cleanup = buildDeclarativeTween({
id,
timeline,
Expand All @@ -80,12 +81,12 @@ export function Animation(props: AnimationProps): React.ReactElement | null {
return cleanup;
});
return () => {
cleanupTweens.forEach((cleanup) => cleanup());
cleanupTweens.forEach((cleanup) => cleanup?.());
};
} else {
const cleanup = addTweenToTimeline(props.tween);
return () => {
cleanup();
cleanup?.();
};
}
}, [getTimelineSpace, id, props.tween, timeline, props.disabled]);
Expand Down
2 changes: 1 addition & 1 deletion scrollytelling/src/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ScrollytellingDispatchersContextType = {
duration: number;
position: number;
cleanup: () => void;
};
} | null;
scopedQuerySelector: gsap.utils.SelectorFunc | undefined;
};

Expand Down
11 changes: 9 additions & 2 deletions scrollytelling/src/primitive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ const Scrollytelling = ({

// initialize timeline
React.useEffect(() => {
if (!ref.current || disabled) return;
if (!ref.current) return;

if (disabled) {
setTimeline(undefined);
return;
}

gsap.registerPlugin(ScrollTrigger);

const tl = gsap.timeline({
Expand Down Expand Up @@ -121,6 +127,7 @@ const Scrollytelling = ({
const getTimelineSpace: ScrollytellingDispatchersContextType["getTimelineSpace"] =
React.useCallback(
({ start, end }) => {
if (disabled) return null;
if (!timeline) throw new Error("timeline not initialized");
const duration = end - start;
if (start < 0) {
Expand All @@ -143,7 +150,7 @@ const Scrollytelling = ({
},
};
},
[addRestToTimeline, timeline]
[addRestToTimeline, timeline, disabled]
);

return (
Expand Down

1 comment on commit 61c816b

@vercel
Copy link

@vercel vercel bot commented on 61c816b Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.