From 83d666ad086975ca695a0ca9611d58b73ea1e349 Mon Sep 17 00:00:00 2001 From: Jay Varner Date: Tue, 6 Aug 2024 12:23:32 -0400 Subject: [PATCH] Better Scrollama setup --- app/components/ScrollytellWrapper.tsx | 24 ++++++++--- app/components/layout/ChapterBody.tsx | 15 ++++--- .../shanawdithit/DrawingScrollytell.tsx | 43 +++++++++---------- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/app/components/ScrollytellWrapper.tsx b/app/components/ScrollytellWrapper.tsx index 53652597..71895c59 100644 --- a/app/components/ScrollytellWrapper.tsx +++ b/app/components/ScrollytellWrapper.tsx @@ -1,4 +1,4 @@ -import { useContext, useEffect, useRef } from "react"; +import { useContext, useEffect, useRef, useState } from "react"; import scrollama from "scrollama"; import { ChapterContext } from "~/chapterContext"; import { useResizeObserver } from "~/hooks"; @@ -48,16 +48,22 @@ export default function ScrollytellWrapper({ const scrollerRef = useRef(undefined); const scrollerElementRef = useRef(null); const { documentSize } = useResizeObserver(); + const [setupFailed, setSetupFailed] = useState(false); + const [shouldRetry, setShouldRetry] = useState(false); useEffect(() => { - if (steps?.current?.children.length !== triggers.length) return; + if ( + steps?.current?.children.length !== triggers.length || + scrollerRef.current + ) + return; try { scrollerRef.current = scrollama(); scrollerRef.current .setup({ // @ts-ignore may be a Scrollama bug. offset does allow strings. - offset: scrollOffset ?? "60px", - step: stepClassName ?? ".step", + offset: "60px", + step: ".step", progress: true, debug, parent, @@ -68,8 +74,10 @@ export default function ScrollytellWrapper({ if (setCurrentStep) setCurrentStep(index); setScrollProgress(index + progress); }); + setSetupFailed(false); } catch (error) { - console.error(error); + scrollerRef.current = undefined; + setSetupFailed(true); } return () => { @@ -87,8 +95,14 @@ export default function ScrollytellWrapper({ steps, threshold, triggers, + shouldRetry, ]); + useEffect(() => { + // Mostly a bug when navigating from error page. + setShouldRetry(setupFailed); + }, [setupFailed]); + useEffect(() => { try { scrollerRef.current?.resize(); diff --git a/app/components/layout/ChapterBody.tsx b/app/components/layout/ChapterBody.tsx index 0e371014..4a72d55f 100644 --- a/app/components/layout/ChapterBody.tsx +++ b/app/components/layout/ChapterBody.tsx @@ -20,6 +20,8 @@ export default function ChapterBody({ children, className }: Props) { const [fixedNav, setFixedNav] = useState(false); const { mainContentSize, windowSize } = useResizeObserver(); const { backgroundColor } = useContext(ChapterContext); + const [setupFailed, setSetupFailed] = useState(false); + const [shouldRetry, setShouldRetry] = useState(false); useEffect(() => { if (!windowSize.height) return; @@ -30,10 +32,7 @@ export default function ChapterBody({ children, className }: Props) { // the image containers the size of the image. Maybe later... // https://github.com/russellsamora/scrollama/issues/145 scrollerRef.current.resize(); - } else if ( - containerRef.current && - document.body.contains(containerRef.current) - ) { + } else if (!scrollerRef.current && containerRef.current) { scrollerRef.current = scrollama(); try { scrollerRef.current @@ -47,6 +46,7 @@ export default function ChapterBody({ children, className }: Props) { .onStepProgress(({ progress }) => setChapterProgressState(progress)); } catch { scrollerRef.current = undefined; + setSetupFailed(true); } } @@ -54,7 +54,12 @@ export default function ChapterBody({ children, className }: Props) { scrollerRef.current?.destroy(); scrollerRef.current = undefined; }; - }, [mainContentSize, windowSize]); + }, [mainContentSize, windowSize, shouldRetry]); + + useEffect(() => { + // Mostly a bug when navigating from error page. + setShouldRetry(setupFailed); + }, [setupFailed]); useEffect(() => { setFixedNav(chapterProgressState > 0.98); diff --git a/app/components/shanawdithit/DrawingScrollytell.tsx b/app/components/shanawdithit/DrawingScrollytell.tsx index f4cfeb8c..e3596582 100644 --- a/app/components/shanawdithit/DrawingScrollytell.tsx +++ b/app/components/shanawdithit/DrawingScrollytell.tsx @@ -121,7 +121,24 @@ function SketchScrollytell({ figure, triggers }: Props) { viewBox={`0 0 ${width} ${height}`} className="max-h-[60vh] max-w-[90%] md:mb-16 mx-auto drop-shadow-2xl" > + + + + - - - - - - - - */} + {/* + /> */}