Skip to content

Include animated props in layout reads with the shared animated backend - #60474

Open
fairysword wants to merge 2 commits into
react:mainfrom
fairysword:fix/pressable-stale-region-native-animation
Open

fairysword wants to merge 2 commits into
react:mainfrom
fairysword:fix/pressable-stale-region-native-animation

Conversation

@fairysword

@fairysword fairysword commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

  • Fixes #51621: with the shared animated backend, Pressability measures a view that a native-driven animation has already moved, but still sees the committed (stale) geometry, so the first MOVE cancels the press (onPressIn fires, onPress does not).
  • Reuses the existing AnimatedPropsRegistry and the same cloning path as AnimationBackendCommitHook: when JS reads layout (measure, measureInWindow, measureLayout, getBoundingClientRect, …), the revision it sees also has the registry values written into the node props.
  • Behind enableAnimatedPropsInLayoutReads (default off, experimental). Two commits: flag generation, then the fix.

Problem

With useNativeDriver: true and the shared animated backend:

  1. Each frame, non-layout props such as transform are written straight to the mounted views and recorded in AnimatedPropsRegistry.
  2. They are not committed to the shadow tree on every frame (that is the point of the fast path).
  3. When React commits, AnimationBackendCommitHook already writes the registry values into the new tree so React does not wipe the running animation.
  4. Layout reads from JS do not go through that hook. They read the revision held by LazyShadowTreeRevisionConsistencyManager, which still has the last committed props.

Pressability mixes the two worlds: it measure()s the responder on press-in (shadow tree), then compares that rect with native touch coordinates on every move (live view). On a moving / scaling view the measured rect is wrong, so the first finger drift looks like LEAVE_PRESS_RECT and onPress is dropped.

Reproducer: the App from #51621 (and the same pattern as RNTester / community demos linked from that issue).

Bug path

sequenceDiagram
  participant Anim as Shared Animation Backend
  participant View as Mounted View
  participant Reg as AnimatedPropsRegistry
  participant Tree as Committed Shadow Tree
  participant Press as Pressability
  participant Measure as measure / getBoundingClientRect

  loop Each animation frame
    Anim->>View: Write transform (synchronous)
    Anim->>Reg: Record current animated props
    Note over Tree: No per-frame commit
  end

  Press->>Measure: On press in
  Measure->>Tree: Read current revision
  Tree-->>Press: Stale transform / size
  Note over Press: Press rect is wrong
  Press->>Press: MOVE vs stale rect → LEAVE_PRESS_RECT
  Note over Press: onPressIn yes, onPress no
Loading

Approach in this PR

The commit hook already knows how to take a root and produce a tree whose animated nodes carry the registry values (cloneWithAnimatedProps). Layout reads need the same tree, without committing it and without retargeting React's runtime shadow node references at those clones.

  1. Extract the commit-hook clone body into cloneWithAnimatedProps.
  2. Add UIManagerAnimationBackend::getRootShadowNodeWithAnimatedProps.
  3. Let LazyShadowTreeRevisionConsistencyManager optionally transform the revision it exposes to JS; UIManager wires that to the backend when useSharedAnimatedBackend and enableAnimatedPropsInLayoutReads are on.
  4. Suspend RSNRU (Runtime Shadow Node Reference Updates) while cloning for reads, so React keeps pointing at committed nodes.
  5. In completeSurface, when the flag is on, only drop the revision captured for the surface instead of capturing it again right away (invalidateCurrentRevision). Capturing it there would call AnimatedPropsRegistry::getMap between the commit hook and clearRegistry: getMap moves the props that the UI thread wrote after the commit hook from pendingMap into map, and clearRegistry then clears them although they were never committed. If that was the last frame of an animation, the shadow tree keeps the previous value. The next read in the task captures the new commit with the props from the registry, so the tree is also cloned only when JS reads the shadow tree, not on every React commit. With the flag off, completeSurface is unchanged.

Fix path

sequenceDiagram
  participant Anim as Shared Animation Backend
  participant View as Mounted View
  participant Reg as AnimatedPropsRegistry
  participant Cons as LazyShadowTreeRevisionConsistencyManager
  participant Backend as getRootShadowNodeWithAnimatedProps
  participant Press as Pressability
  participant Measure as measure / getBoundingClientRect

  loop Each animation frame
    Anim->>View: Write transform (synchronous)
    Anim->>Reg: Record current animated props
  end

  Press->>Measure: On press in
  Measure->>Cons: getCurrentRevision
  Cons->>Backend: Transform revision for layout reads
  Backend->>Reg: getMap(surfaceId)
  Backend->>Backend: cloneWithAnimatedProps (RSNRU off)
  Backend-->>Cons: Tree with current animated props
  Cons-->>Measure: Live geometry
  Measure-->>Press: Correct press rect
  Note over Press: MOVE stays inside rect → onPress fires
Loading

React commit path (commit hook unchanged, shared helper)

sequenceDiagram
  participant React as React commit (completeSurface)
  participant Hook as AnimationBackendCommitHook
  participant Reg as AnimatedPropsRegistry
  participant Tree as New Shadow Tree
  participant Cons as LazyShadowTreeRevisionConsistencyManager

  React->>Hook: shadowTreeWillCommit
  Hook->>Reg: getMap(surfaceId)
  Hook->>Hook: cloneWithAnimatedProps
  Hook-->>Tree: New tree with current animated props
  Note over Tree: Prevents React from wiping the running animation
  React->>Cons: invalidateCurrentRevision (flag on)
  Note over Cons: Drops the captured revision without calling getMap
  React->>Reg: clearRegistry
  Note over Cons: The next read in the task captures the new commit with the props from the registry
Loading

Other approaches

Community proposals for the same symptom, listed here for reviewers' reference.

Measure the mounted native view — #51835, #55025

Make Pressability (or Fabric measure itself) measure the UI-thread view hierarchy instead of the shadow tree, like Paper's NativeViewHierarchyManager. #51835 adds a measureAsyncOnUI element method that Pressability calls; #55025 makes measure / measureInWindow in UIManagerBinding ask the native mounting layer through SchedulerDelegate.

Record synchronous transforms at the write funnel — #59392

Whenever props are written synchronously to a view, record transform / transformOrigin in a small map on UIManager. Layout metrics (computeRelativeLayoutMetrics) consult that map before falling back to committed props.

Why this PR

On current main, OSS stable already enables C++ Native Animated and the shared animated backend. The registry is already the source of truth for “what the backend last applied.” React commits already trust that registry via the commit hook. Layout reads use the same source for the same reason, with the same clone helper.

Scope note: This only applies to animation frontends that write through the shared backend into AnimatedPropsRegistry.

Commits

  1. Add enableAnimatedPropsInLayoutReads feature flag — config + yarn featureflags --update only.
  2. Include animated props in layout reads with the shared animated backend — C++ wiring, docs, Fantom and C++ unit tests.

Changelog:

[General][Fixed] - Fix layout reads such as measure and getBoundingClientRect not seeing the props set by running native-driven animations with the shared animated backend, which made Pressable drop onPress on views moved or scaled by them, behind the experimental enableAnimatedPropsInLayoutReads flag

Test Plan:

Commands

Run on this PR, all locally; there was nothing I needed to skip. The last change, which only added the new method to the C++ API snapshots, came after the Fantom and gtest runs.

  • yarn fantom packages/react-native/Libraries/Animated/__tests__/AnimatedBackendLayoutReads-itest.js: 6/6. Mid-animation measure / getBoundingClientRect match the running transform; still correct after a re-render that changes layout; a read after a commit in the same task sees the new commit, while a read before it in the same task doesn't; fails when the flag is off.
  • yarn fantom Animated: 382 passed, 3 skipped.
  • gtest LazyShadowTreeRevisionConsistencyManagerTest: 11/11. After invalidateCurrentRevision, the next read captures the new revision and the revision is captured again; the revision transformer only runs on the read, once per lock. Built and run locally through the Fantom CMake build (target react_renderer_uimanager_consistency_tests).
  • yarn jest packages/react-native/src/private/featureflags: 4/4.
  • yarn featureflags --verify-unchanged, node scripts/clang-format.js --check on the changed C++ files, yarn flow-check (no errors in tracked files).
  • yarn cxx-api-validate with doxygen 1.16.1, as on CI: all 12 snapshots match.

RNTester repro

repro/pressable-stale-region-native-animation adds two commits on top of this PR, neither of which is part of it: an RNTester example and a switch for the flag. The example is under Pressable → Pressable with a native-driven animation. It is the #51621 reproducer: an Animated.createAnimatedComponent(Pressable) whose scale loops 0 → 1 → 0, 1 s each way, with useNativeDriver: true. The page shows the flags and the onPressIn / onPress counts.

Each run is 20 presses, each held for 300 ms while moving 5 dp (5 pt on iOS) to the right, starting 60 dp left of the center of the box (adb shell input swipe on Android, idb ui swipe on iOS). Presses that come while the box is too small to cover that point don't hit it and aren't counted. With the flag off, layout reads and completeSurface run the same code as on main.

RNTester has no switch for native feature flags, so the second commit adds one, read at app start: a system property on Android and a launch argument on iOS. React Native already sets the overrides for the release level (Stable in RNTester) at startup, so when the switch is on they are replaced with the same Stable overrides plus this flag through dangerouslyForceOverride.

Android. Coordinates are in px (dp × density / 160, density from adb shell wm density). The box is centered horizontally; take its vertical center from a screenshot.

adb shell setprop debug.rn.layout_reads_fix 1   # 0 for flag off
adb shell am force-stop com.facebook.react.uiapp
adb shell "am start -a android.intent.action.VIEW -d 'rntester://example/PressableExample/native-driven-animation'"
sleep 25   # wait for the bundle to load and the example to open
# X1 = center x - 60 dp, X2 = X1 + 5 dp, Y = center y of the box.
# Venus e5 (density 320): 240 546 250 546. vivo V2338A (density 560): 420 998 438 998.
for i in $(seq 1 20); do adb shell input swipe X1 Y X2 Y 300; sleep 1.3; done

iOS. Coordinates are in pt; the values are for the iPhone 17 Pro.

xcrun simctl launch --terminate-running-process booted com.meta.RNTester.localDevelopment -RNLayoutReadsFix YES   # NO for flag off
sleep 20   # wait for the bundle to load
xcrun simctl openurl booted 'rntester://example/PressableExample/native-driven-animation'
# Confirm the "Open in RNTester?" dialog, then:
for i in $(seq 1 20); do idb ui swipe --udid <UDID> 141 289 146 289 --duration 0.3 --delta 1; sleep 1.3; done

Before/after: with the flag off, as on main, about half of the presses with finger drift only fire onPressIn; with the flag on, every press fires onPressIn and onPress.

Device OS enableAnimatedPropsInLayoutReads onPressIn onPress
Venus e5 (MT6763) Android 9 off 13 6
Venus e5 (MT6763) Android 9 on 12 12
vivo V2338A Android 16 off 11 6
vivo V2338A Android 16 on 12 12
iPhone 17 Pro simulator iOS 26.5 off 12 6
iPhone 17 Pro simulator iOS 26.5 on 12 12

The presses aren't synced with the animation, so which half of it each press lands in, and how many presses hit the box, changes from run to run. Across the runs I did on each device the flag-off counts varied (e.g. 12 / 7 and 13 / 6 on the Venus e5, 11 / 5 to 14 / 7 on iOS), but every flag-off run dropped about half and every flag-on run dropped none.

The native builds for the table are from this PR before its last rebase onto main, with an earlier version of the switch that also logged the flag; since then only a method name and comments changed in this PR. The JS is from the PR head. I also built both apps from the branch as pushed. iOS, run twice more, the second time with the commands above: 11 / 5 and 14 / 7 with the flag off, 12 / 12 and 15 / 15 with it on. Android on the vivo V2338A, with the commands above: 12 / 6 with the flag off, 12 / 12 with it on.

Dropped presses

The example only shows the last measure(), so the breakdown is from an earlier run of the same reproducer that logged measure() on every press-in, with the same presses. The box there is 340 x 250 on Android and 382 x 250 on iOS.

Device Flag Landed onPress Dropped, measured 0 x 0 Dropped, measured full size Fired, measured full size
vivo V2338A off 12 6 6 0 6
Venus e5 off 9 4 5 0 4
iPhone 17 Pro simulator off 14 6 8 0 6

All 19 drops measured 0 x 0 and none measured the full size. The shadow tree only gets the animated scale when each Animated.timing segment ends, so at press-in it has the value from the end of the previous segment. While the box grows, that is scale: 0: the press rect is a point at the center of the box plus the default press rect offsets (20 on each side, 30 at the bottom), so a press 60 dp left of the center leaves it on the first move. While the box shrinks, that is scale: 1: the press rect is the full box, larger than the box on screen, so the move stays inside and onPress fires. That is why about half of the presses get through with the flag off; they also use a stale rect, it is just too large instead of too small.

With the flag on, every press that landed fired onPress (vivo 12/12, Venus 8/8, iOS 15/15), and measure() returned sizes between the two, following the scale at the time of the press: 177.1 x 130.2 to 329.4 x 242.2 on vivo, 166.5 x 122.4 to 339.6 x 249.7 on Venus, e.g. 276.0 x 180.6 and 341.0 x 223.1 on iOS.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 2, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 2, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 2, 2026
@fairysword
fairysword force-pushed the fix/pressable-stale-region-native-animation branch from ddb271c to 29a5b3e Compare December 2, 2026 13:49

@zeyap zeyap left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting up the fix. A few things would make this more convincing on the testing side. #59392's test plan is a good model:

  1. Flag-on run with the same setup. The same 35 presses on the same builds with enableAnimatedPropsInLayoutReads on, reported per platform: how many fired onPress, and what measure() returned at press-in. Ideally 0 dropped, and measure() matching the on-screen size or position.

  2. A breakdown of the 19 dropped presses. How many measured 0 x 0 and how many measured the full box? If some drops measured the full size, the stale read probably isn't the cause, and the flag may not fix those.

  3. An RNTester repro. A "Pressable inside a natively animated view" example (or reuse the one from #59392), with the device or simulator and OS you tested on, plus a before/after line, e.g. "on main a press with finger drift only fires onPressIn; with the flag it fires onPressIn and onPress."

  4. Test Plan and Changelog sections in the PR description, listing the commands you ran (yarn fantom …, the gtests) and anything you couldn't run locally.

@fairysword

Copy link
Copy Markdown
Contributor Author

@zeyap Thanks for the detailed review. I've rewritten the Test Plan along these lines:

  1. Flag-on run. Same builds and presses, per platform: vivo V2338A 12/12, Venus e5 8/8, iOS simulator 15/15 fired onPress. measure() returned in-between sizes that follow the scale at the time of the press (ranges are in the Test Plan), instead of 0 x 0 or the full box.
  2. Breakdown of the 19 drops. All 19 measured 0 x 0; none measured the full box. The presses that measured the full size all fired: during the shrinking half the shadow tree has the full size, which is larger than the box on screen, so the finger drift stays inside the press rect. That's why about half of the presses get through with the flag off.
  3. RNTester repro. I added "Pressable with a native-driven animation" (the onPress not working while animating views #51621 reproducer) and a switch for the flag in a separate branch on top of this PR, and reran it on Android 9 (Venus e5), Android 16 (vivo V2338A) and iOS 26.5 (iPhone 17 Pro simulator). With the flag off, as on main, about half of the presses with finger drift only fire onPressIn; with the flag on, every press fires onPressIn and onPress. Happy to add the example to this PR or send it separately if you'd like it in RNTester.
  4. Commands. The Test Plan now lists what I ran (yarn fantom …, the consistency gtest through the Fantom CMake build, the feature flags Jest test, featureflags, clang-format, flow, and yarn cxx-api-validate with doxygen 1.16.1 as on CI) with results. All of them ran locally; there was nothing I needed to skip.

@fairysword
fairysword requested a review from zeyap December 3, 2026 06:48
@fairysword
fairysword force-pushed the fix/pressable-stale-region-native-animation branch from 29a5b3e to 7bf826b Compare December 4, 2026 02:41
Experimental flag, off by default, for including the animated props of the
shared animated backend in layout reads from JS (measure, measureInWindow,
measureLayout, getBoundingClientRect, ...). Nothing reads it yet.

Generated with `yarn featureflags --update`.

Signed-off-by: jh.xiong <uabearbest@gmail.com>
With the shared animated backend, non-layout props such as transform are
written to the views and to AnimatedPropsRegistry on every frame, but not
committed to the shadow tree. AnimationBackendCommitHook applies the registry
when React commits, so renders don't revert running animations. Layout reads
from JS (measure, measureInWindow, measureLayout, getBoundingClientRect, ...)
read the revision captured by LazyShadowTreeRevisionConsistencyManager though,
which doesn't include these props. Pressability measures the pressable on
press in, so a view moved or scaled by a native-driven animation is measured
at its old position and the first move cancels the press (react#51621).

Behind the enableAnimatedPropsInLayoutReads flag, the revision that JS
reads is cloned with the props from the registry, with the same cloning as
the commit hook (moved into cloneWithAnimatedProps). The clones are only used
for reads: RSNRU is suspended while cloning, so the references held by React
keep pointing to committed nodes.

The tree is cloned when JS reads the shadow tree (layout reads, but also DOM
traversal like parentNode), not when React commits. Behind the flag,
completeSurface only drops the revision captured for the surface, and the next
read captures it again with the props from the registry. Capturing it in
completeSurface would call AnimatedPropsRegistry::getMap before clearRegistry,
which would then clear the props that the UI thread wrote after the commit
hook, although they were never committed. This way, commits that are not
followed by such a read also don't clone the tree.

This covers C++ Animated with the shared backend only. The platform Animated
modules don't write to the registry and are unchanged.

One of the Fantom tests reads the layout in render and in a layout effect of
the same task, so the read after the commit has to see the new commit rather
than the revision captured before it. The consistency manager tests cover that
invalidating makes the next read capture and transform the revision again.

Signed-off-by: jh.xiong <uabearbest@gmail.com>
@fairysword
fairysword force-pushed the fix/pressable-stale-region-native-animation branch from 7bf826b to c6ec88c Compare December 4, 2026 05:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

onPress not working while animating views

2 participants