React Nitro

Shared element transitions

Animate a view from its place on one stack screen to its counterpart on the next, then back again.

App.tsx
<SharedElement id="meadow">  <View style={coverStyle} /></SharedElement> <Stack.Screen name="Notebook"  component={Notebook}  options={({ route }) => ({    sharedElementId:      route.params.coverId,  })}/>
Shelf
Select a cover to open it, then go back to the shelf.

When a reader taps a notebook cover, the cover can expand into the detail screen. Going back shrinks it toward the cover they tapped. To connect the two views, give each SharedElement the same id and set the destination screen's sharedElementId option to that ID.

Mark a cover on both screens

The following three snippets make up src/App.tsx in a project with react-nitro-navigation installed. The covers use ordinary native views, so you can run the example without image assets.

import {
  createStackNavigator, NavigationContainer, SharedElement,
  useNavigation, type StackNavigationProp,
} from 'react-nitro-navigation'
import { TapGestureRecognizer, Text, View } from 'react-nitro-renderer'

const covers = {
  coast: { title: 'Coast', color: 0x265876ff },
  meadow: { title: 'Meadow', color: 0x46724aff },
}

type CoverId = keyof typeof covers
type Routes = { Shelf: undefined; Notebook: { coverId: CoverId } }
const Stack = createStackNavigator<Routes>()

function Cover({ id, size }: { id: CoverId; size: number }) {
  const cover = covers[id]
  return (
    <SharedElement id={id}>
      <View
        layout={{ width: size, height: size, justifyContent: 'center', alignItems: 'center' }}
        style={{ background: cover.color, cornerRadius: 8 }}
      >
        <Text color={0xffffffff}>{cover.title}</Text>
      </View>
    </SharedElement>
  )
}

Each wrapper takes one native child. Here that child is the colored View, so its label travels with it. For a photo transition, place the Image directly inside the wrapper.

Pass the selected ID through the route

Give each item an ID that stays with it if the shelf is reordered. When tapped, the thumbnail passes its own ID to push. The detail screen reads that parameter and draws the same cover at a larger size.

function Thumbnail({ id }: { id: CoverId }) {
  const navigation = useNavigation<StackNavigationProp<Routes, 'Shelf'>>()

  function attachTap() {
    const tap = new TapGestureRecognizer()
    tap.onTap = () => navigation.push('Notebook', { coverId: id })
    return [tap]
  }

  return (
    <View gestureRecognizers={attachTap} layout={{ margin: 8 }}>
      <Cover id={id} size={72} />
    </View>
  )
}

function Shelf() {
  return (
    <View layout={{ flexDirection: 'row', padding: 16 }}>
      <Thumbnail id="coast" />
      <Thumbnail id="meadow" />
    </View>
  )
}

function Notebook({ route }: { route: { params: Routes['Notebook'] } }) {
  return <Cover id={route.params.coverId} size={240} />
}

Configure the stack

Set sharedElementId on the Notebook screen. Reading it from route.params lets a single detail route handle either thumbnail.

export function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName="Shelf">
        <Stack.Screen name="Shelf" component={Shelf} />
        <Stack.Screen
          name="Notebook"
          component={Notebook}
          options={({ route }) => ({ sharedElementId: route.params.coverId })}
        />
      </Stack.Navigator>
    </NavigationContainer>
  )
}

Tap Meadow, go back, then try Coast. Each return should land on its own thumbnail. Keep the matching view mounted and visible when the stack needs it, and use each ID only once per screen.

What animates on each platform

  • iOS 18 and newer use UIKit's interactive zoom transition. The thumbnail supplies the zoom source and the detail marker supplies the rectangle UIKit aligns to. Earlier iOS versions use the standard stack transition.
  • Android uses a Fragment shared element transition to animate the matched views' bounds and transforms. When both children are native image views, Android also animates the image transform.

For a photo example, open Images in the repository's example app and tap a bundled thumbnail. Both screens receive the same loaded image so its pixels are available throughout the transition. Gallery example · SharedElement reference

On this page