Files
supabase/apps/studio/hooks/misc/useSynchronizedAnimation.ts
Alaister Young b04dbf9876 fix: use-isomorphic-layout-effect (#29762)
* fix: use-isomorphic-layout-effect

* stop messing with kevin
2024-10-09 12:33:12 +08:00

46 lines
1.1 KiB
TypeScript

import { useIsomorphicLayoutEffect } from 'common'
import { useRef } from 'react'
// Source: https://youtu.be/3kDVachh-BM
let stashedTime: number | null = null
export function useSynchronizedAnimation<T>(name: string) {
const ref = useRef<T>(null)
useIsomorphicLayoutEffect(() => {
const animations = document
.getAnimations()
.filter(
(animation) => animation instanceof CSSAnimation && animation.animationName === 'shimmer'
)
const myAnimation = animations.find(
(animation) =>
animation.effect instanceof KeyframeEffect && animation.effect.target === ref.current
)
if (myAnimation === undefined) {
return
}
const leadAnimation = animations[0]
if (myAnimation === leadAnimation && stashedTime) {
myAnimation.currentTime = stashedTime
}
if (myAnimation !== leadAnimation) {
myAnimation.currentTime = leadAnimation.currentTime
}
return () => {
if (myAnimation === leadAnimation && myAnimation.currentTime) {
stashedTime = Number(myAnimation.currentTime)
}
}
}, [])
return ref
}