react-native-screen-transitions

repository·main·Indexed 23 days ago

https://github.com/eds2002/react-native-screen-transitions

A library for creating customizable, gesture-driven screen transitions in React Native. It features shared element transitions via the Bounds API, snap-point sheets with gesture-aware ScrollView and FlatList coordination, and built-in presets for modal and card animations. The library integrates with Blank stack, native stack, and Expo Router, and is compatible with Reanimated v3/v4 and React Native Gesture Handler v2.

Tokens
60K
Snippets
159
Records
277
Agent score
81%

What's inside react-native-screen-transitions

  1. Overview of react-native-screen-transitions features

    main

    react-native-screen-transitions provides a customizable API for building advanced screen animations in React Native. Key capabilities include:

    • Animation Control: Full control over screen enter, exit, and gesture-driven states.
    • Shared Elements: Shared element and fullscreen navigation zoom transitions using the Bounds API.
    • Sheets: Snap-point sheets with coordination for gesture-aware ScrollView and FlatList.
    • Transition Slots: Dedicated slots for content, backdrop, surface, and custom tagged elements.
    • Presets: Built-in patterns for common modal, card, and shared-transition animations.
    • Navigation Integration: Supports Blank stack, native stack, and Expo Router.
  2. What is react-native-screen-transitions

    main

    react-native-screen-transitions is a React Navigation transition toolkit designed for high-fidelity motion in React Native. It allows developers to move beyond platform defaults to build:

    • Custom push and dismiss animations
    • Gesture-driven cards and sheets
    • Snap-point flows
    • Floating overlays
    • Bounds-driven navigation zoom and shared-transition style motion

    The library leverages React Navigation's JS stack, react-native-reanimated, and react-native-gesture-handler to achieve smooth, 60 fps transitions on both Android and iOS.

  3. Access the react-native-screen-transitions API surface

    main

    The library provides four primary ways to interact with its features:

    1. Navigator Creators: Specialized stack navigators available via subpaths (e.g., react-native-screen-transitions/react-navigation).
    2. Adapter Helpers: Functions like withScreenTransitions() to adapt existing native-stack navigators.
    3. The Transition Export: The main entry point for transition-aware primitives, boundary components, presets, and specs.
    4. Named Exports: Hooks, helpers, constants, and types exported from the main package.

    Use the blank-stack subpath for the recommended navigator setup and Transition for UI components and configuration.

    import Transition, {
      withScreenTransitions,
      snapTo,
      type ScreenStyleInterpolator,
      type ScreenTransitionConfig,
    } from "react-native-screen-transitions";
    
    import { createBlankStackNavigator } from "react-native-screen-transitions/react-navigation";
  4. Use Blank Stack for custom animations and gestures

    main

    A Blank Stack is a highly customizable navigator designed for complex animation and gesture systems. It provides the transition runtime but does not impose a visual language, headers, or chrome, allowing you to define your own design system and motion.

    Use Blank Stack when you need:

    • Custom screen interpolators
    • Gestures
    • Snap sheets
    • Bounds transitions
    • Navigation zoom
    • Overlays

    It is the preferred path for new embedded and independent flows.

    const Stack = createBlankStackNavigator();
    
    <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen} />
      <Stack.Screen
        name="Detail"
        component={DetailScreen}
        options={{
          ...Transition.Presets.SlideFromBottom(),
          gestureEnabled: true,
        }}
      />
    </Stack.Navigator>
  5. How v4 Screen Transitions architecture works

    main

    The v4 experimental architecture uses a 'Standard Navigator' approach called Blank Stack. This allows a single transition navigator to integrate with both React Navigation and Expo Router via host-specific entry points.

    There are two primary ways to use Screen Transitions:

    1. Blank Stack: The default choice when you want Screen Transitions to drive the entire stack. It replaces the standard stack navigator.
    2. withScreenTransitions adapter: Used when you want to wrap an existing native stack. This allows you to opt individual screens into Screen Transitions animations while retaining native-stack behavior for the rest of the stack.
  6. How Shadowing works in Nested Navigators

    main

    Shadowing occurs when a child screen and an ancestor both claim the same direction. The child screen wins the claim for that direction, preventing the ancestor from reacting to the same drag.

    Key behaviors:

    • Only the topmost route in a child navigator registers a shadowing claim.
    • Once a child screen starts dismissing, the ancestor is allowed to react to the gesture again.
  7. How Transition.Boundary works

    main

    The Transition.Boundary component is the primary way to define boundaries for transitions. It replaces the deprecated Transition.Boundary.View and Transition.Boundary.Trigger aliases.

    • Passive Boundary: Omit the onPress prop. Use this for elements that only need to measure and participate in the transition (e.g., a placeholder or a destination target).
    • Pressable Boundary: Include the onPress prop. Use this for elements that should trigger a navigation event (e.g., a button or a card).
    // Passive boundary
    <Transition.Boundary id="avatar">
      <Image source={avatar} style={styles.avatar} />
    </Transition.Boundary>
    
    // Pressable boundary
    <Transition.Boundary
      id="avatar"
      onPress={() => navigation.push("Profile")}
    >
      <Image source={avatar} style={styles.avatar} />
    </Transition.Boundary>
  8. Choose a stack: Blank Stack vs Native Stack

    main

    The library provides two different integration paths depending on your needs for control versus existing infrastructure.

    Use createBlankStackNavigator() for maximum control. This is the best fit for:

    • Custom interpolators
    • Snap sheets
    • Overlays
    • Bounds transitions
    • Embedded and independent flows

    Native Stack

    Use withScreenTransitions(createNativeStackNavigator()) if you already have a stack built around @react-navigation/native-stack and want to opt a specific flow into this library's transition system.

    Important considerations for Native Stack:

    • You must enable enableTransitions on the screens or navigator where you want the library to take over.
    • It acts as a compatibility layer: the transitioned native-stack screen is switched into a transparent modal-style presentation, native animation is disabled, and the library drives visuals using Reanimated.
    • While it preserves native-stack features like existing headers, it offers less control over custom animations compared to the Blank Stack.
  9. How Boundary measurement works in v3.11

    main

    Boundary measurement no longer relies on the screen origin provider, which prevents issues where transformed parent layouts (e.g., using translateY) would cause destination measurements to be offset.

    Destinations now initially render without transition styles so Bounds can measure their actual layout. Once source and destination measurements are ready, the library applies transition styles and starts the animation. This requires no configuration changes.

  10. Choose between Native Stack and Blank Stack

    main

    The library offers two navigator types with different trade-offs. Choose based on your need for integration versus control.

    Native Stack

    Use this only when you require native-stack integration. It acts as a compatibility layer where transitioned screens are switched into containedTransparentModal with native animation disabled. Trade-offs:

    • Background screens may appear tappable while being blocked by a transparent modal layer.
    • Dismissal may feel less fluid because native removal and JS state synchronization are coordinated separately.
    • Higher likelihood of platform-specific edge cases.

    Blank Stack

    This is the default recommendation for most use cases. It provides the most features and control. Best for:

    • Custom interpolators and gestures.
    • Snap sheets and overlays.
    • Bounds transitions and navigation zoom.
    • Embedded flows.

    Trade-offs:

    • It is a JavaScript navigator. While simple transitions can be competitive with the baseline React Navigation JS stack, it may not always win timing benchmarks due to how transition completion is coordinated.