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

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:

  • Fantom: AnimatedBackendLayoutReads-itest.js — 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.
  • C++: LazyShadowTreeRevisionConsistencyManagerTest — 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.
  • Fantom Animated suites (flag on as in the new test).
  • Manual: enable enableAnimatedPropsInLayoutReads (with shared backend on), run the #51621 reproducer; press the moving/scaling target with slight finger movement — onPress should fire and measure() on press-in should track the live geometry. Done with 20 presses moving 5dp (5pt on iOS) per run: on Android with the same build on a vivo V2338A and a low-end MT6763 device (input swipe), and on an iPhone 17 Pro simulator with iOS 26.5 (idb ui swipe). All 35 presses that landed on the box fired onPress (20 on Android, 15 on iOS), and measure() returned the current size (e.g. 213.7 x 157.1).
  • Confirm flag off preserves today's behavior. Same builds and presses: 19 of the 35 presses that landed on the box didn't fire onPress (11 of 21 on Android, 8 of 14 on iOS). measure() only returned what was committed at the end of the previous animation (0 x 0 or the full size of the box), and every press measured at 0 x 0 was dropped.

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`.
@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
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.
@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.

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