Skip to content

Commit

Permalink
Improve useInstantTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
adamseckel committed Jun 22, 2023
1 parent 6114276 commit 0b2ca34
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/framer-motion/src/utils/use-instant-transition.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { frame } from "../frameloop"
import { useEffect } from "react"
import { useEffect, useRef } from "react"
import { useInstantLayoutTransition } from "../projection/use-instant-layout-transition"
import { useForceUpdate } from "./use-force-update"
import { instantAnimationState } from "./use-instant-transition-state"

export function useInstantTransition() {
const [forceUpdate, forcedRenderCount] = useForceUpdate()
const startInstantLayoutTransition = useInstantLayoutTransition()
const unlockOnFrameRef = useRef<number>()

useEffect(() => {
/**
* Unblock after two animation frames, otherwise this will unblock too soon.
*/
frame.postRender(() =>
frame.postRender(() => (instantAnimationState.current = false))
frame.postRender(() => {
/**
* If the callback has been called again after the effect
* triggered this 2 frame delay, don't unblock animations. This
* prevents the previous effect from unblocking the current
* instant transition too soon. This becomes more likely when
* used in conjunction with React.startTransition().
*/
if (forcedRenderCount !== unlockOnFrameRef.current) return
instantAnimationState.current = false
})
)
}, [forcedRenderCount])

Expand All @@ -22,6 +33,7 @@ export function useInstantTransition() {
instantAnimationState.current = true
forceUpdate()
callback()
unlockOnFrameRef.current = forcedRenderCount + 1
})
}
}

0 comments on commit 0b2ca34

Please sign in to comment.