Reanimated 4.8.0 brings Shared Element Transitions and Animated Text
A post by Bartłomiej Błoniarz on swmansion.com covers Shared Element Transitions in Reanimated 4.8.0, along with other additions in the release.
Shared Element Transitions
Shared Element Transitions let developers associate two Views so that when one becomes active, Reanimated runs an animation that moves the element between them. Components are marked as connected with sharedTransitionTag and placed in different SharedTransitionBoundary components; flipping the isActive state on the boundaries makes the View travel visually between them. The post notes this can be combined with useIsFocused from react-navigation or expo-router to run the animation on screen change, and the animation can be customized with sharedTransitionStyle.
When a transition is triggered, Reanimated hides the target view using opacity, removes the source view from its parent, places it under the root so it appears above the whole app, and animates it to the target position. It then, in one atomic transaction, moves the source view back to its original parent and unhides the target view.
The feature had been in the experimental channel, and the release introduces new features based on that period:
- SharedTransitionBoundary: transitions were previously triggered implicitly by stack push and pop. The new component unties the transition lifecycle from navigation, allows transitions between two containers on the same screen, and requires screens to explicitly opt in. This made it simpler to support transitions between tabs in a Tab Navigator and enables custom navigation flows within a single screen.
- fadeContent: a modifier on the
SharedTransitionbuilder that fades out differing children on the source View and fades them in on the target View. Shared children run their own Shared Element Transition. - Continuity: the implementation animates the real view rather than a snapshot, so if source and target share native state, they can continue operating during the transition.
The post explains that an earlier version built on the Old React Native Architecture had separate Android and iOS implementations that were hard to maintain. The move to the New Architecture allowed the work to happen in a shared C++ layer, and the isActive prop lets Reanimated read navigator state from the current render.
Animated Text
In 4.8, a shared value can be passed as a child of Animated.Text:
const progress = useSharedValue(0);
<Animated.Text>Downloading: {progress}%</Animated.Text>;
The shared value is rendered as nested text updated directly by the UI thread without a React rerender. It can be mixed with static text, and formatting can be done by wrapping it in useDerivedValue.
CSS animations and internals
CSS animations and transitions can now animate backgroundImage, so gradients interpolate colors, color stop positions, angle, and radial size and position. Support was also added for borderCurve, writingDirection, verticalAlign and objectFit, and PlatformColor values no longer crash animations. The release also cuts unnecessary per-frame work on both the UI and JS threads.
Links
Source: swmansion.com