Skip to content

Animate native views on the native driver with Animation and LayoutAnimation - #519

Merged
tim-smart merged 2 commits into
mainfrom
agent/nelson-effect-bot/6a1d0d711132
Nov 28, 2026
Merged

tim-smart merged 2 commits into
mainfrom
agent/nelson-effect-bot/6a1d0d711132

Conversation

@effect-bot

@effect-bot effect-bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes EFF-4574

Adds native animation to @effect/ui-native without porting React Native's Animated. Two kit modules talk to the native side directly:

  • Animation drives opacity and transform through NativeAnimatedModule. A number in the style is a target: when a render changes it, the driver animates from wherever the value is (timing, spring or immediate). An expression follows a scroll view's offset (scroll, interpolate, add, multiply, diffClamp), so a sticky header moves on the UI thread with no JS per frame. animationEnd fires once every target of the element is reached.
  • LayoutAnimation registers configs under view transition names, so $.viewTransition([LayoutAnimation.spring]) animates that step's layout changes.
const list = h.id("list")
pipe(h.view, Animation.style({
  transform: [{ translateY: Animation.interpolate(Animation.scroll(list, "y"), [0, 80], [0, -80], "clamp") }]
}))
pipe(h.scroll, Animation.source(list)).children(rows)

How the driver follows views

There's no Mount involved. Animation.style sets an animation prop that the module registers a setter for, plus a hook that runs after every commit. After each commit the hook:

  • builds or updates the node graph for each animated element,
  • connects it to the element's current view tag, and disconnects and reconnects when the host recreates the view with a new tag (a view name or parent change, such as a scroll view turning horizontal on Android),
  • disconnects the graph while a kept child is parked, and drops the nodes once the element leaves the tree,
  • binds a source's scroll events to the scroll view's current tag, with one value node per source and axis shared by every element that follows it. Sources and entries are kept per surface, so two surfaces mounting the same component don't share a source name.
  • when the graph changes shape, keeps the value nodes of numeric targets and their running animations, so changing a scroll expression doesn't restart a fade.

Every operation of a commit goes into one startOperationBatch/finishOperationBatch pair, so Android runs them in one frame. iOS ignores the batch and holds graph operations until the next mount, so on iOS the driver ends each batch with stopAnimation of an id that doesn't exist, which flushes the queue. Without it, a commit that changes only the graph (a header switching scroll axis) waits for an unrelated mount. The first frame shows from, or else the target, or else the expression at scroll offset 0, so nothing flashes before the driver connects. When an animation ends, the end values are committed so Fabric's shadow tree matches what's on screen, which keeps press hit tests right for views that moved. A change made with immediate ends at once, and animationEnd fires after the commit.

Native keeps showing the driver's last opacity/transform on a view after the driver disconnects (Android's tagToSynchronousMountProps, iOS's propKeysManagedByAnimated), until the view is deleted. So when a mounted element stops animating a key, the driver asks the host to recreate its view with a new tag (ViewHost.recreate). A key with no values, as in {} or { transform: [] }, is no animation for that key. Without a native driver, the view shows the targets as plain props, and clears them when the element stops animating.

Both platforms bind onScrollEnded along with onScroll, but only Android removes it along with onScroll (RCTNativeAnimatedNodesManager.mm), so on iOS the driver removes it explicitly.

On Android, a scroll with a RefreshControl renders inside AndroidSwipeRefreshLayout, and the host gives the wrapper the scroll view's outer props, transform included. Like React Native's AnimatedScrollView, the driver then builds one props node per view: keys the wrapper takes connect to the wrapper's view, the rest to the scroll view, and the graph is rebuilt when the wrapper comes or goes. Seeded and end values go through the host, so they land on the same views. Scroll events stay bound to the scroll view.

Only opacity and transform are animated. Those are the two props React Native keeps the driver's value for when a later commit resends a view's props (Android's getAnimatedPropsMap, iOS's propKeysManagedByAnimated). Colours, layout props and rotate are left out.

Host changes (paid by every app)

  • NativeHost looks up props it doesn't know in a registry that kit modules fill, and runs registered hooks after each commit.
  • viewTransition looks the step's types up in the LayoutAnimation registry. configureNextLayoutAnimation is called just before completeRoot, and only if the commit changes the tree, so a render with no Fabric diff leaves nothing pending in C++.
  • Native.ViewHost gains commit and recreate, and ViewEvents gains animationEnd. Commit hooks get the surface's ViewHost.

Bundle size

Android release JS for examples/native (expo export --no-bytecode, as in its README):

Build Raw Gzip
main (e0b7cec) 267,404 B 83,923 B
This branch, app imports no animation API 267,746 B (+342) 84,056 B (+133)
This branch, app with the sticky header and LayoutAnimation demo 275,346 B (+7,942) 86,874 B (+2,951)

The no-import build contains none of NativeAnimated*, animationSource or startAnimatingNode. Its only new string is configureNextLayoutAnimation, which comes from the host. The last row also counts the demo's own view code.

Tests

test/utils/FakeAnimated.ts evaluates the node graph the way native does, and throws where native would fail or leak (a dropped node that is still connected or still feeds another, an edge to a missing node). Like native, a view keeps the driver's last values after it disconnects, and an ios mode holds graph operations until one that iOS flushes, and leaves onScrollEnded bound when onScroll is removed, as iOS does. Dropping a node that is still bound to an event throws. The tests check what each view shows:

  • a sticky header and a row inside the list follow scroll events, keep following after the scroll view and the row get new tags, and leave no nodes or event bindings once removed. A reused node gets a new view that shows nothing from the driver.
  • a fade mounts at from. An immediate target interrupts it and fires animationEnd once, and a later timing fires it again with the end value committed. An empty style creates nothing, and an empty transform clears the one shown before. Closing the surface drops every node.
  • on iOS, switching a header's scroll axis without a Fabric change takes effect, and a fade running at the time keeps its value. Closing leaves no nodes or event bindings.
  • without a native driver, the targets are set as props and cleared when the element stops animating.
  • two surfaces mounting the same component each follow their own scroll view, and closing one leaves the other working.
  • LayoutAnimation is configured for the commit of a step that asks for it, and not for a step that asks but changes nothing.

Not verified on a device

All of this comes from reading React Native 0.88.1's sources and running the fakes above. None of it has run on hardware.

  • Updates happening off the JS thread, and the first frame matching the seeded props.
  • Reconnecting after a tag change, on both platforms.
  • Scroll events reaching the driver with no onScroll prop set (Android dispatches them anyway; iOS posts them from RCTScrollViewComponentView with a default throttle of 0).
  • On iOS, the forced flush can run a connect before the mount that creates the view. The first update is then lost, and the view shows its seeded props until the next scroll or frame.
  • Module name: on iOS the module tries NativeAnimatedTurboModule first, then NativeAnimatedModule. That matches bridgeless with cxxNativeAnimatedEnabled off, which is the default.
  • Android LayoutAnimation needs enableLayoutAnimationsOnAndroid, which is off by default in 0.88.1. Without it the layout changes at once.
  • Committing the end values while the driver is still connected shouldn't make the view jump.
  • Recreating a view after a key is released clears what native kept on both platforms.

Notes

  • This overlaps Cover React Native scroll behaviour and add RefreshControl #508 in NativeHost.ts, internal/kinds.ts, kit/index.ts and FakeFabric.ts. Whichever PR lands second needs a small rebase.
  • Not in v1: decay, loops and keyframes, colours, layout props, and rotate (Fabric wants angle strings, which needs string-output interpolation). Gesture-driven animation is left for the responder work in EFF-4569.

@github-actions

Copy link
Copy Markdown

Bundle size

Production build, gzip level 9 per chunk. Effect-only client: 26780 B gzip. Full report

App Chunks Raw bytes Gzip bytes Added gzip B Δ raw bytes Δ gzip bytes
counter 1 175724 59927 33147 0 0
features 1 185206 63318 36538 0 0
routing 2 352744 120751 93971 0 0

Animation drives opacity and transform on React Native's native animated
driver, and LayoutAnimation animates a step's layout changes.
@effect-bot
effect-bot force-pushed the agent/nelson-effect-bot/6a1d0d711132 branch from 22f0f4d to 4272028 Compare November 28, 2026 06:32
@tim-smart
tim-smart merged commit d134e34 into main Nov 28, 2026
12 checks passed
@tim-smart
tim-smart deleted the agent/nelson-effect-bot/6a1d0d711132 branch November 28, 2026 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants