Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(frontend): repair enter animation in useAnimatedPresence (#73960)

Juraj Majerik7fd84f021c23
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index e8a5d7a8ce48..a032b5e961ba 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -6735,13 +6735,13 @@ snapshots:
scenes-app-posthog-ai--thread-with-multiple-context-objects--light:
hash: v1.k794b7964.37f894dbe521fe73a27b410291a78f0923b47724e7a39cbf7db15c276f438508.-6CML5IWBfVRwqAaunxC77tcqM-10BAvLhGbhXTvemU
scenes-app-posthog-ai--thread-with-opened-suggestions--dark:
- hash: v1.k794b7964.8c73cdf7bf2dace2fb229f3020df37ab6f448ee744c389888c8ce0b487a2dc07.tMZr_nR6fXzfhdZP7hKMYvOFCLy5JvmSY-BkmY7Bs10
+ hash: v1.k794b7964.c79ac281f0b66d67344b562edc6c2cbff16e8f682696111a553aef7b8884ddb9.C9NmPxKsTOkt8iaHcVzRKEtzXRZcZkLFRU_MyjJcx2k
scenes-app-posthog-ai--thread-with-opened-suggestions--light:
- hash: v1.k794b7964.9e5a59a2bd14ff1971710ebbf7c1c937fd9fc38dc18ac5301187f6ae3461f4bf.jjRrZLaE7on6A_kc-HQc0jX-SXVDBIaIf9tLtt43GGo
+ hash: v1.k794b7964.9c6105576943a59057f2e9928df3e26ac5e7f80ec98f774a0ba3288a4b3ac568.5BQzurf66egTBupaf5vDBYFxSUbKQLMuRTGXmjIImhw
scenes-app-posthog-ai--thread-with-opened-suggestions-mobile--dark:
- hash: v1.k794b7964.8c73cdf7bf2dace2fb229f3020df37ab6f448ee744c389888c8ce0b487a2dc07.1sNRAgSPDu6G3QQPRdrd29G5EZ2riv_tiArl3mfMRME
+ hash: v1.k794b7964.c79ac281f0b66d67344b562edc6c2cbff16e8f682696111a553aef7b8884ddb9.6WI7SnQGqgSKvP4EfLvSQIIPchaSG_tL5hMQ8K80PRg
scenes-app-posthog-ai--thread-with-opened-suggestions-mobile--light:
- hash: v1.k794b7964.9e5a59a2bd14ff1971710ebbf7c1c937fd9fc38dc18ac5301187f6ae3461f4bf.AeODbRDxzTWJNhaTxgiu9vFiTn-PSoDrQgQC4psGQ7A
+ hash: v1.k794b7964.9c6105576943a59057f2e9928df3e26ac5e7f80ec98f774a0ba3288a4b3ac568.RUUG3hSKh3sZTu4XM2XKEmhGj0mkLlup_7AlK2v0pIg
scenes-app-posthog-ai--thread-with-quick-replies--dark:
hash: v1.k794b7964.bf2e3ec6f1256a7e1a41120510001d6eb825d70c85f0858a184f028c00a4752d.ttC4L7QLV8_5xjTvtSnk2TQEemTWuumQUT4__5_s3_w
scenes-app-posthog-ai--thread-with-quick-replies--light:
diff --git a/frontend/src/lib/hooks/useAnimatedPresence.test.ts b/frontend/src/lib/hooks/useAnimatedPresence.test.ts
index faba7e148e67..72433be9ba77 100644
--- a/frontend/src/lib/hooks/useAnimatedPresence.test.ts
+++ b/frontend/src/lib/hooks/useAnimatedPresence.test.ts
@@ -28,12 +28,20 @@ describe('useAnimatedPresence', () => {
window.cancelAnimationFrame = originalCancelRaf
})
- function flushRaf(): void {
+ function runFrame(): void {
const pending = rafCallbacks
rafCallbacks = []
pending.forEach((cb) => cb(0))
}
+ // Drains nested RAFs too, so callers that just want the enter to settle
+ // don't need to know how many frames the flip takes.
+ function flushRaf(): void {
+ while (rafCallbacks.length) {
+ runFrame()
+ }
+ }
+
it.each([
['initially in', true],
['initially out', false],
@@ -42,7 +50,7 @@ describe('useAnimatedPresence', () => {
expect(result.current).toEqual({ rendered: initial, shown: initial })
})
- it('renders immediately on enter and flips shown on the next frame', () => {
+ it('renders immediately on enter but holds shown for a frame so the hidden state paints first', () => {
const { result, rerender } = renderHook(({ isIn }) => useAnimatedPresence(isIn, 200), {
initialProps: { isIn: false },
})
@@ -51,7 +59,15 @@ describe('useAnimatedPresence', () => {
rerender({ isIn: true })
expect(result.current).toEqual({ rendered: true, shown: false })
- act(() => flushRaf())
+ // First frame paints the mounted-but-hidden state; shown must not flip
+ // yet, otherwise the browser coalesces both into one paint and the
+ // enter transition never runs.
+ act(() => runFrame())
+ expect(result.current).toEqual({ rendered: true, shown: false })
+
+ // Second frame flips shown, so the transition animates from the
+ // already-painted hidden state.
+ act(() => runFrame())
expect(result.current).toEqual({ rendered: true, shown: true })
})
diff --git a/frontend/src/lib/hooks/useAnimatedPresence.ts b/frontend/src/lib/hooks/useAnimatedPresence.ts
index 7faee2850268..47d9fd08ad0b 100644
--- a/frontend/src/lib/hooks/useAnimatedPresence.ts
+++ b/frontend/src/lib/hooks/useAnimatedPresence.ts
@@ -27,12 +27,20 @@ export function useAnimatedPresence(isIn: boolean, durationMs: number): Animated
useEffect(() => {
if (isIn) {
setRendered(true)
- // Defer the visibility flip a frame so the browser paints the
- // pre-transition (hidden) styles first; without this RAF the
- // browser coalesces both states into one paint and the CSS
- // transition never runs.
- const raf = window.requestAnimationFrame(() => setShown(true))
- return () => window.cancelAnimationFrame(raf)
+ // Defer the visibility flip two frames so the browser paints the
+ // pre-transition (hidden) styles first. `setRendered` runs from a
+ // passive effect (after paint), so a single RAF fires in the same
+ // frame as the mount's paint — the browser coalesces both states
+ // into one paint and the enter transition never runs. The extra
+ // frame guarantees the hidden state paints before `shown` flips.
+ let innerRaf = 0
+ const outerRaf = window.requestAnimationFrame(() => {
+ innerRaf = window.requestAnimationFrame(() => setShown(true))
+ })
+ return () => {
+ window.cancelAnimationFrame(outerRaf)
+ window.cancelAnimationFrame(innerRaf)
+ }
}
if (!renderedRef.current) {
return