From 54c6a372aaadaab6719da4cc95915606a024d987 Mon Sep 17 00:00:00 2001 From: Bohdan Chornokondratenko Date: Mon, 26 Aug 2024 10:32:35 +0200 Subject: [PATCH] refactor(landing): AnimationsController change animationState to canRun and remove shouldRegisterAnimationAnd --- .../FeaturesSection/ResultsAnimation.tsx | 3 -- .../FeaturesSection/ScramblesAnimation.tsx | 1 - .../sections/FeaturesSection/animations.tsx | 44 ++++++++++--------- .../sections/FeaturesSection/index.tsx | 2 +- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/features/landing/sections/FeaturesSection/ResultsAnimation.tsx b/src/features/landing/sections/FeaturesSection/ResultsAnimation.tsx index 341657e..2cab73f 100644 --- a/src/features/landing/sections/FeaturesSection/ResultsAnimation.tsx +++ b/src/features/landing/sections/FeaturesSection/ResultsAnimation.tsx @@ -15,7 +15,6 @@ export function ResultsAnimation() { toRotation='11deg' toLeft='0%' toTop='52%' - shouldRegisterAnimationEnd > Average time @@ -114,13 +113,11 @@ function ResultsAnimationItem( | 'toRotation', string > & { - shouldRegisterAnimationEnd?: boolean children: string }, ) { return ( R' B U L' U' D2 R' U D2 B' U2 D2 L2 F' diff --git a/src/features/landing/sections/FeaturesSection/animations.tsx b/src/features/landing/sections/FeaturesSection/animations.tsx index e9babf3..f5f0dec 100644 --- a/src/features/landing/sections/FeaturesSection/animations.tsx +++ b/src/features/landing/sections/FeaturesSection/animations.tsx @@ -3,9 +3,9 @@ import { createContext, ReactNode, useRef, useState, useEffect, useContext, Comp import { type BlockType } from '.' type AnimationContextType = { - animationState: Record + canRun: Record blocksRef: BlocksRef - onAnimationEnd: () => void + onAnimationEnd: (block: BlockType) => void } const AnimationContext = createContext(null) @@ -13,11 +13,11 @@ const AnimationContext = createContext(null) type BlocksRef = Map export function AnimationsController({ children }: { children: ReactNode }) { const blocksRef = useRef(new Map()) - const [state, setState] = useState({ - results: 'waiting', - scrambles: 'waiting', - leaderboards: 'waiting', - sharing: 'waiting', + const [canRun, setCanRun] = useState({ + results: false, + scrambles: false, + leaderboards: false, + sharing: false, }) const [queue, setQueue] = useState(['results', 'scrambles', 'leaderboards', 'sharing']) @@ -29,7 +29,7 @@ export function AnimationsController({ children }: { children: ReactNode }) { const observer = new IntersectionObserver( ([{ isIntersecting }]) => { - if (isIntersecting) setState({ ...state, [currentBlock]: 'running' }) + if (isIntersecting) setCanRun({ ...canRun, [currentBlock]: true }) }, { rootMargin: '-72px 0px 0px', @@ -41,12 +41,15 @@ export function AnimationsController({ children }: { children: ReactNode }) { return () => observer.unobserve(currentBlockNode) }, [queue]) - function onAnimationEnd() { - setQueue((prev) => prev.slice(1)) + function onAnimationEnd(block: BlockType) { + setQueue((prev) => { + const currentBlock = prev[0] + return currentBlock === block ? prev.slice(1) : prev + }) } return ( - + {children} ) @@ -75,35 +78,34 @@ export function BlockIntersectionWrapper({ children, block }: { children: ReactN } export function AnimationItem({ - shouldRegisterAnimationEnd = false, className, block, children, ...props -}: ComponentPropsWithoutRef<'div'> & { block: BlockType; shouldRegisterAnimationEnd?: boolean }) { - const { ref } = useRegisterAnimationEnd(shouldRegisterAnimationEnd) - const { animationState } = useAnimationContext() +}: ComponentPropsWithoutRef<'div'> & { block: BlockType }) { + const { ref } = useRegisterAnimationEnd(block) + const { canRun } = useAnimationContext() return ( -
+
{children}
) } -function useRegisterAnimationEnd(enabled: boolean) { +function useRegisterAnimationEnd(block: BlockType) { const ref = useRef(null) - const { onAnimationEnd } = useContext(AnimationContext)! + const { onAnimationEnd } = useAnimationContext() useEffect(() => { const node = ref.current - if (!node || !enabled) { + if (!node) { return } - const callback = () => onAnimationEnd() + const callback = () => onAnimationEnd(block) node.addEventListener('animationend', callback) return () => node.removeEventListener('animationend', callback) - }, [ref, enabled]) + }, [ref, block]) return { ref } } diff --git a/src/features/landing/sections/FeaturesSection/index.tsx b/src/features/landing/sections/FeaturesSection/index.tsx index 86a7e96..fd34c7d 100644 --- a/src/features/landing/sections/FeaturesSection/index.tsx +++ b/src/features/landing/sections/FeaturesSection/index.tsx @@ -76,5 +76,5 @@ function Feature({ } function FakeAnimation({ block }: { block: BlockType }) { - return + return }