Skip to content

Commit 60928ae

Browse files
authored
Add screen transitions blog post (#1488)
Add a community blog post covering react-native-screen-transitions, including setup, an iOS-style card transition, navigation.zoom(), and boundary groups. Add the accompanying demo videos and author metadata. Enable the existing static2dynamic rehype transform for blog posts so the article can use the same Static/Dynamic code example pattern as the docs.
1 parent 57cf63e commit 60928ae

8 files changed

Lines changed: 357 additions & 1 deletion

File tree

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
---
2+
title: Building custom transitions with react-native-screen-transitions
3+
authors: ed
4+
tags: [tutorial, screen-transitions]
5+
---
6+
7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
10+
There are a few ways to make an app feel more alive, and I'm a big believer that motion is one of them.
11+
12+
Most people already know their OS animations by muscle memory. That's why a custom transition can land so well: used in the right place, it breaks the routine just enough to make a flow feel intentional.
13+
14+
[`react-native-screen-transitions`](https://screen-transitions.esjr.org) is a React Navigation transition toolkit for flows that need more control over navigation motion. In this article, we'll recreate an iOS-style page transition, then build up to a bounds-driven `navigation.zoom()` flow.
15+
16+
<!--truncate-->
17+
18+
One caveat before we start: this is not a blanket replacement for [`@react-navigation/native-stack`](/docs/native-stack-navigator) or [`@react-navigation/stack`](/docs/stack-navigator). If `native-stack` already does the job, keep using it. If the JS stack already gives you enough control, keep using that too. `react-native-screen-transitions` fits when a specific flow needs more freedom: custom gesture choreography, snap points, bounds-driven motion, or a Reanimated-first transition model.
19+
20+
## Setup
21+
22+
The [`react-native-screen-transitions`](https://screen-transitions.esjr.org/installation) package contains the transition primitives. In your project directory, run:
23+
24+
```bash npm2yarn
25+
npm install react-native-screen-transitions
26+
```
27+
28+
### Installing peer dependencies
29+
30+
Next, install the necessary peer dependencies used by `react-native-screen-transitions`, and `@react-native-masked-view/masked-view` for the example later in the article:
31+
32+
<Tabs groupId='framework' queryString="framework">
33+
<TabItem value='expo' label='Expo' default>
34+
35+
In your project directory, run:
36+
37+
```bash
38+
npx expo install react-native-reanimated react-native-gesture-handler \
39+
@react-navigation/native @react-navigation/native-stack \
40+
@react-navigation/elements react-native-screens \
41+
react-native-safe-area-context \
42+
@react-native-masked-view/masked-view
43+
```
44+
45+
This will install versions of these libraries that are compatible with your Expo SDK version.
46+
47+
</TabItem>
48+
<TabItem value='community-cli' label='Community CLI'>
49+
50+
In your project directory, run:
51+
52+
```bash npm2yarn
53+
npm install react-native-reanimated react-native-gesture-handler \
54+
@react-navigation/native @react-navigation/native-stack \
55+
@react-navigation/elements react-native-screens \
56+
react-native-safe-area-context \
57+
@react-native-masked-view/masked-view
58+
```
59+
60+
If you're on a Mac and developing for iOS, install the pods via [CocoaPods](https://cocoapods.org/) to complete the linking:
61+
62+
```bash
63+
npx pod-install ios
64+
```
65+
66+
</TabItem>
67+
</Tabs>
68+
69+
## Recreating the iOS page transition
70+
71+
<div className="device-frame">
72+
<video playsInline autoPlay muted loop>
73+
<source src="/assets/blog/screen-transitions/ios-reference.mp4" />
74+
</video>
75+
</div>
76+
77+
Let's dissect the native iOS page animation and mimic it closely:
78+
79+
- the incoming screen slides in from the right
80+
- the screen underneath shifts slightly left
81+
- optionally, we can round the corners of the page, and on newer iOS versions that can move closer to a squircle look
82+
83+
## Start with a Blank Stack
84+
85+
[Blank Stack](https://screen-transitions.esjr.org/stack-types) is the navigator that ships with `react-native-screen-transitions`. It comes with no built-in animations, so every transition is yours to define. That's exactly what we want here.
86+
87+
```tsx static2dynamic
88+
import { createBlankStackNavigator } from 'react-native-screen-transitions/blank-stack';
89+
90+
const RootStack = createBlankStackNavigator({
91+
screens: {
92+
Home: HomeScreen,
93+
Detail: DetailScreen,
94+
},
95+
});
96+
```
97+
98+
## Define the transition
99+
100+
To define a transition, we configure two things: how the gesture behaves, and how the screen animates.
101+
102+
`transitionSpec` controls the spring configuration for opening and closing. [`screenStyleInterpolator`](https://screen-transitions.esjr.org/custom-animations) is the function that returns the animated styles for the transition based on values like progress and screen layout. For this example, we'll keep it simple and drive everything from the root-level progress helper.
103+
104+
```tsx
105+
import { interpolate } from 'react-native-reanimated';
106+
import Transition, {
107+
type ScreenTransitionConfig,
108+
} from 'react-native-screen-transitions';
109+
110+
const iosCardStackTransition: ScreenTransitionConfig = {
111+
gestureEnabled: true,
112+
gestureDirection: 'horizontal',
113+
transitionSpec: {
114+
open: Transition.Specs.DefaultSpec,
115+
close: Transition.Specs.DefaultSpec,
116+
},
117+
screenStyleInterpolator: ({ active, current, progress }) => {
118+
'worklet';
119+
120+
const width = current.layouts.screen.width;
121+
const translateX = interpolate(
122+
progress,
123+
[0, 1, 2],
124+
[width, 0, -width * 0.3],
125+
'clamp'
126+
);
127+
128+
return {
129+
content: {
130+
borderRadius: active.settled ? 0 : DEVICE_CORNER_RADIUS,
131+
borderCurve: active.settled ? 'continuous' : 'circular',
132+
overflow: 'hidden',
133+
transform: [{ translateX }],
134+
},
135+
backdrop: {
136+
backgroundColor: 'rgba(0,0,0,1)',
137+
opacity: interpolate(active.progress, [0, 1], [0, 0.1], 'clamp'),
138+
},
139+
};
140+
},
141+
};
142+
```
143+
144+
Then apply that config to the stack:
145+
146+
```tsx static2dynamic
147+
const RootStack = createBlankStackNavigator({
148+
screens: {
149+
Home: HomeScreen,
150+
Detail: {
151+
screen: DetailScreen,
152+
options: iosCardStackTransition,
153+
},
154+
},
155+
});
156+
```
157+
158+
<div className="device-frame">
159+
<video playsInline autoPlay muted loop>
160+
<source src="/assets/blog/screen-transitions/ios-card-transition.mp4" />
161+
</video>
162+
</div>
163+
164+
And we're set: a close replica of the iOS page animation.
165+
166+
The interesting bit is `screenStyleInterpolator`. We're using one root-level `progress` value to describe both sides of the transition:
167+
168+
- When `progress` goes from `0 -> 1`: the incoming screen moves from `width` to `0`
169+
- When `progress` goes from `1 -> 2`: the previous screen continues from `0` to `-width * 0.3`
170+
- `current.layouts.screen.width` gives us the full distance to work with
171+
- `transform: [{ translateX }]` is applied on `content`, so the whole screen moves as one unit
172+
- `borderRadius: active.settled ? 0 : DEVICE_CORNER_RADIUS` only rounds the screen while it is moving, then lets it settle back to full-bleed
173+
- `borderCurve` helps the corners read closer to the system look while the card is in motion
174+
- the `backdrop` fade adds just a little depth under the active screen
175+
176+
## Why not just use JS stack?
177+
178+
JS stack already does this, so what's the point?
179+
180+
For the example above, nothing. JS stack does this well, and if that's all you need, use it.
181+
182+
Where `react-native-screen-transitions` starts to pay off is when the transition needs to know about geometry: the position and size of a specific component on one screen, animating to a specific component on another. That's not something JS stack expresses cleanly, and it's what makes the next example possible.
183+
184+
## navigation.zoom()
185+
186+
<div className="device-frame">
187+
<video playsInline autoPlay muted loop>
188+
<source src="/assets/blog/screen-transitions/navigation-zoom.mp4" />
189+
</video>
190+
</div>
191+
192+
One thing I'm really proud to announce with v3.4 is [`navigation.zoom()`](https://screen-transitions.esjr.org/navigation-zoom).
193+
194+
`navigation.zoom()` is a bounds-driven helper for recreating that navigation zoom handoff between a source element and a destination screen. It works by measuring component A and component B with the Bounds API, then animating between them. This isn't a traditional shared-element system, so if that's what you need, I'd wait for Reanimated's version to mature.
195+
196+
Let's start with the source screen. In a realistic flow, the card already knows which item it represents, so use that item's id as the boundary id and pass it through navigation.
197+
198+
```tsx
199+
function FeedCard({ item, navigation }) {
200+
return (
201+
<Transition.Boundary.Trigger
202+
id={item.id}
203+
onPress={() => {
204+
navigation.navigate('Detail', { id: item.id });
205+
}}
206+
>
207+
<Image source={item.image} style={styles.card} />
208+
</Transition.Boundary.Trigger>
209+
);
210+
}
211+
```
212+
213+
On the destination screen, use the same `id` from `route.params`. You don't have to define a `Transition.Boundary.View` on the destination, but if you want the destination to resize itself to match component A's bounds, you should.
214+
215+
```tsx
216+
function DetailScreen({ route }) {
217+
const item = getItem(route.params.id);
218+
219+
return (
220+
<View style={styles.screen}>
221+
<Transition.Boundary.View id={item.id} style={styles.hero}>
222+
<Image source={item.image} style={styles.hero} />
223+
</Transition.Boundary.View>
224+
</View>
225+
);
226+
}
227+
```
228+
229+
Now orchestrate the animation. `options` receives `route`, so we can derive the same `id` there and pass it into the bounds helper:
230+
231+
```tsx static2dynamic
232+
const RootStack = createBlankStackNavigator({
233+
screens: {
234+
Feed: FeedScreen,
235+
Detail: {
236+
screen: DetailScreen,
237+
options: ({ route }) => {
238+
const zoomId = route.params.id;
239+
240+
return {
241+
navigationMaskEnabled: Platform.OS === 'ios',
242+
gestureEnabled: true,
243+
gestureDirection: ['vertical', 'vertical-inverted', 'horizontal'],
244+
gestureDrivesProgress: false,
245+
transitionSpec: {
246+
open: Transition.Specs.DefaultSpec,
247+
close: Transition.Specs.FlingSpec,
248+
},
249+
screenStyleInterpolator: ({ bounds }) => {
250+
'worklet';
251+
252+
return bounds({ id: zoomId }).navigation.zoom({
253+
target: 'bound',
254+
});
255+
},
256+
};
257+
},
258+
},
259+
},
260+
});
261+
```
262+
263+
A few choices here are worth calling out.
264+
265+
`navigationMaskEnabled` requires `@react-native-masked-view/masked-view`. I keep the platform guard because animating layout properties on the mask element tends to hold up much better on iOS than on Android.
266+
267+
`gestureDrivesProgress: false` means the drag does not directly scrub the stack's main transition progress. The gesture still updates live drag values and still participates in the dismiss decision on release, but the zoom helper stays in control of the interaction instead of behaving like a normal interactive pop.
268+
269+
`close: Transition.Specs.FlingSpec` turns `overshootClamping` off and uses a looser spring, so a release or fling feels more natural on the way out.
270+
271+
### Taking this further with boundary groups
272+
273+
The example above works well when you have one obvious source and one obvious destination. A gallery is more interesting. You might have a masonry grid on the index screen, then a paged detail screen where the user can swipe between images before closing.
274+
275+
<div className="device-frame">
276+
<video playsInline autoPlay muted loop>
277+
<source src="/assets/blog/screen-transitions/boundary-groups.mp4" />
278+
</video>
279+
</div>
280+
281+
This is where the boundary `group` prop becomes useful. Think of `group` as a namespace for a family of related bounds. The `id` still chooses the specific item, but the `group` tells the system which collection that item belongs to.
282+
283+
Start by defining a stable group and a mutable value for the active item:
284+
285+
```tsx
286+
export const GALLERY_GROUP = 'gallery';
287+
export const activeGalleryId = makeMutable(GALLERY_ITEMS[0].id);
288+
```
289+
290+
On the source screen, every thumbnail uses its own `id`, but they all share the same `group`:
291+
292+
```tsx
293+
<Transition.Boundary.Trigger
294+
id={item.id}
295+
group={GALLERY_GROUP}
296+
onPress={() => {
297+
activeGalleryId.set(item.id);
298+
navigation.navigate('Detail', { id: item.id });
299+
}}
300+
>
301+
<Image source={{ uri: item.uri }} style={styles.image} />
302+
</Transition.Boundary.Trigger>
303+
```
304+
305+
On the destination screen, the matching image uses the same `id` and the same `group`:
306+
307+
```tsx
308+
<Transition.Boundary.View
309+
id={item.id}
310+
group={GALLERY_GROUP}
311+
style={{ width: imageWidth, height: imageHeight }}
312+
>
313+
<Image source={{ uri: item.uri }} style={styles.image} />
314+
</Transition.Boundary.View>
315+
```
316+
317+
Then the transition asks bounds for both values:
318+
319+
```tsx
320+
const id = activeGalleryId.get();
321+
322+
return bounds({
323+
id,
324+
group: GALLERY_GROUP,
325+
}).navigation.zoom({ target: 'bound' });
326+
```
327+
328+
Groups are useful when the active item can change while the destination stays mounted. The mutable `activeGalleryId` keeps track of the current active id, so when bounds need a fresh measurement, for example before a drag or dismiss, the system knows which element to remeasure.
329+
330+
The gallery example also updates `activeGalleryId` when the horizontal detail list settles on a new page. That way, if the user opens one image, swipes to another, and then closes the screen, the transition returns to the image they are actually looking at instead of the one they originally opened.
331+
332+
And that's the whole thing: SwiftUI's `navigation.zoom()` look in pure JS.
333+
334+
The full source for both examples lives in the [react-native-screen-transitions GitHub repo](https://github.com/eds2002/react-native-screen-transitions) if you want to poke at it.
335+
336+
## What's next for Screen Transitions
337+
338+
I've been quietly working on the next wave of improvements: moving the architecture over to Reanimated 4, Gesture Handler v3, and React 19's new Activity component. Pinch-in and pinch-out transitions are also in progress, so there should be a few exciting changes landing soon.
339+
340+
Thanks for all the support on Screen Transitions. It's honestly a dream package for me, and I'm excited that other people seem to share the excitement. If you build something with it, I'd love to see it!

blog/authors.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ oskar:
4747
socials:
4848
x: o_kwasniewski
4949
github: okwasniewsk
50+
51+
ed:
52+
name: Ed
53+
url: https://github.com/eds2002
54+
image_url: https://github.com/eds2002.png
55+
title: React Native Screen Transitions
56+
socials:
57+
x: trpfsu
58+
github: eds2002

docusaurus.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ const config: Config = {
186186
},
187187
blog: {
188188
remarkPlugins: [[remarkNpm2Yarn, { sync: true }]],
189+
rehypePlugins: [
190+
[
191+
rehypeCodeblockMeta,
192+
{ match: { snack: true, lang: true, tabs: true } },
193+
],
194+
rehypeStaticToDynamic,
195+
],
189196
},
190197
pages: {
191198
remarkPlugins: [[remarkNpm2Yarn, { sync: true }]],

src/css/custom.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ code {
14691469

14701470
.device-frame {
14711471
--device-frame-bezel: 10px;
1472-
--device-frame-radius: 52px;
1472+
--device-frame-radius: 67px;
14731473
--device-frame-screen-radius: calc(
14741474
var(--device-frame-radius) - var(--device-frame-bezel) - 2px
14751475
);
3.27 MB
Binary file not shown.
52.4 KB
Binary file not shown.
42.8 KB
Binary file not shown.
1.46 MB
Binary file not shown.

0 commit comments

Comments
 (0)