Skip to content

Commit

Permalink
Fix animation hooking not being robust enough locally
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaBobs committed May 19, 2024
1 parent cb42310 commit fd3cb84
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/common/anim/Animator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "react";

import "@src/common/anim/Animator.scss";
import { useEffectRefsPopulated } from "@src/common/hooks/useEffectRefsPopulated";
import { useRefsPopulatedEffect } from "@src/common/hooks/useRefsPopulatedEffect";
import { useCallback } from "react";

type AnimationContext = {
Expand Down Expand Up @@ -121,7 +121,7 @@ export function Anim(
const selfRef = useRef<HTMLDivElement>(null);
const animationState = useContext(animContext);

useEffectRefsPopulated(() => {
useRefsPopulatedEffect(() => {
const setups = props.setup(selfRef.current!);

const anims = setups
Expand Down Expand Up @@ -168,7 +168,7 @@ export function Anim(

anims.forEach((anim) => anim.cancel());
};
}, [selfRef.current]);
}, [selfRef]);

return (
<div ref={selfRef} className="anim">
Expand Down
20 changes: 0 additions & 20 deletions src/common/hooks/useEffectRefsPopulated.ts

This file was deleted.

32 changes: 32 additions & 0 deletions src/common/hooks/useRefsPopulatedEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useRef } from "react";
import { EffectCallback, useEffect } from "react";

export function useRefsPopulatedEffect(
callback: EffectCallback,
refs: React.MutableRefObject<any>[]
) {
const refData = useRef({
intervalIdx: 0,
effectRan: false,
cleanup: (() => {}) as ReturnType<EffectCallback>,
});

useEffect(() => {
if (!refData.current.effectRan) {
refData.current.intervalIdx = setInterval(() => {
if (!refData.current.effectRan && refs.every((ref) => ref.current)) {
refData.current.cleanup = callback();
refData.current.effectRan = true;
clearInterval(refData.current.intervalIdx);
}
}, 1);
}

return () => {
clearInterval(refData.current.intervalIdx);
// eslint-disable-next-line react-hooks/exhaustive-deps
refData.current.cleanup?.();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, refs);
}

0 comments on commit fd3cb84

Please sign in to comment.