Skip to content

Add screen transition animation - #5274

Merged
piaskowyk merged 58 commits into
mainfrom
@piaskowyk/screen-animations-swipe-back
Mar 25, 2024
Merged

piaskowyk merged 58 commits into
mainfrom
@piaskowyk/screen-animations-swipe-back

Conversation

@piaskowyk

@piaskowyk piaskowyk commented Oct 19, 2023 •

Copy link
Copy Markdown
Member

Summary

This pull request introduces a new feature which adds screen "go back" transition animations based on gestures. The feature is a collaboration between three libraries: react-native-reanimated, react-native-screens, and react-native-gesture-handler. Implementing this feature required changes in both Reanimated and RNScreens. You can find the related pull request in the react-native-screens repository: PR 1913.

Dependency requirements:

  • react-native-reanimated@3.6+
  • react-native-screens@3.28+
  • react-native-gesture-handler@2+

A demo of this feature was presented at the RNCK meetup: RNCK Meetup Demo

API Overview

Basic usage

To use this feature, you need to import the GestureDetectorProvider from react-native-screens/gesture-handler and wrap your navigation container with it. Here's an example:

import { GestureDetectorProvider } from 'react-native-screens/gesture-handler';

<GestureHandlerRootView>
  <NavigationContainer>
    <GestureDetectorProvider>
      <Stack.Navigator
        screenOptions={{
          stackAnimation: 'none',
        }}>
        <Stack.Screen
          name="ScreenA"
          component={ScreenA}
        />
        <Stack.Screen
          name="ScreenB"
          component={ScreenB}
          options={{
            goBackGesture: 'swipeRight', // gestures that trigger the screen transition
          }}
        />
      </Stack.Navigator>
    </GestureDetectorProvider>
  </NavigationContainer>
</GestureHandlerRootView>

Available gestures:

  • swipeRight
  • swipeLeft
  • swipeUp
  • swipeDown
  • verticalSwipe
  • horizontalSwipe
  • twoDimensionalSwipe

Preset usage

You can also use preset animations for the transition. Import ScreenTransition from react-native-reanimated and specify the desired preset in the transitionAnimation option. Example:

import { ScreenTransition } from 'react-native-reanimated';

<Stack.Screen
  name="ScreenB"
  component={ScreenB}
  options={{
    goBackGesture: 'swipeRight',
    transitionAnimation: ScreenTransition.SwipeRightFade, // transition preset
  }}
/>

Available presets:

  • SwipeRight
  • SwipeLeft
  • SwipeDown
  • SwipeUp
  • Horizontal
  • Vertical
  • TwoDimensional
  • SwipeRightFade

You don't need to worry about the preset because each gesture is associated with a default transition preset.

Custom Animation

If you want to create a custom animation, you can define your own AnimatedScreenTransition object. Here's an example:

const customTransition: AnimatedScreenTransition = {
  topScreenFrame: (event, screenSize) => {
    'worklet';
    const progress = event.translationX / screenSize.width;
    return {
      transform: [
        { translateX: 1.3 * event.translationX },
        { rotate: 20 * progress + 'deg' }
      ]
    };
  },
  belowTopScreenFrame: (event, screenSize) => {
    'worklet';
    const progress = event.translationX / screenSize.width;
    return {
      transform: [
        { scale: 0.7 + 0.3 * progress }
      ]
    };
  },
}

<Stack.Screen
  name="ScreenB"
  component={ScreenB}
  options={{
    goBackGesture: 'swipeRight',
    transitionAnimation: customTransition
  }}
/>

screenEdgeGesture

To trigger a gesture that starts from the edge of the screen, you can use the screenEdgeGesture option. Example:

<Stack.Screen
  name="ScreenB"
  component={ScreenB}
  options={{
    goBackGesture: 'swipeRight',
    screenEdgeGesture: true
  }}
/>

This means you can trigger a transition with a gesture that starts from the edge of the screen.

Test plan

Test example from Example App

@piaskowyk
piaskowyk requested a review from tomekzaw March 7, 2024 14:12

@tomekzaw tomekzaw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫡

Comment thread app/src/examples/index.ts Resolved
@piaskowyk
piaskowyk enabled auto-merge March 25, 2024 09:39
@piaskowyk
piaskowyk added this pull request to the merge queue Mar 25, 2024
Merged via the queue into main with commit 6d93a90 Mar 25, 2024
@piaskowyk
piaskowyk deleted the @piaskowyk/screen-animations-swipe-back branch March 25, 2024 09:40
@Bayramito

Copy link
Copy Markdown
Contributor

Is this feature working with the current versions of RNGH and Reanimated ?

@anisurrahman072

Copy link
Copy Markdown

Is this feature working with the current versions of RNGH and Reanimated ?

Nope @Bayramito. It is currently only available in "react-native-reanimated": "3.9.0-rc.0"

@Bayramito

Copy link
Copy Markdown
Contributor

Could not make it work with the latest versions of Scresns, Reanimated and GH

@anisurrahman072

Copy link
Copy Markdown

@Bayramito

Bayramito commented Apr 30, 2024 •

Copy link
Copy Markdown
Contributor

Hey, yes it's 3.9.0. I was waiting for official release to test this feature...

@anisurrahman072

Copy link
Copy Markdown

I didn't try with 3.9.0 but I tried with 3.9.0-rc.0 (pre release of 3.9). It worked pretty smoothly 🚀

Let's try it on 3.9.0-rc.0. If it works then you can move to 3.9.0 (Latest).

✅ If you want then you can follow my guide on this here (Link: https://github.com/anisurrahman072/React-Native-SDK-Research/blob/master/ReactNativeScreen3.30.md)

Full output is here too: https://x.com/anis_RNCore/status/1778968599597899891

@Bayramito

Copy link
Copy Markdown
Contributor

Well I just spot my issue. I was importing the createNativeStackNavigator from @react-navigation instead of react-native-screens.
But now i got this error

simulator_screenshot_932DD1AE-12DC-49BA-94CC-66D7BC0D78C1

@anisurrahman072

anisurrahman072 commented Apr 30, 2024 •

Copy link
Copy Markdown

Amazing 🚀. But it would be great if you create a separate discussion or ISSUE mate, as it seems a good discussion 💯

ja1ns pushed a commit to WiseOwlTech/react-native-screens that referenced this pull request Oct 9, 2024
## Summary

This pull request introduces a new feature which adds screen "go back"
transition animations based on gestures. The feature is a collaboration
between three libraries: `react-native-reanimated`,
`react-native-screens`, and `react-native-gesture-handler`. Implementing
this feature required changes in both Reanimated and RNScreens. You can
find the related pull request in the `react-native-reanimated`
repository: [PR
1913](software-mansion/react-native-reanimated#5274).

## Test plan

Test example from Example App

---------

Co-authored-by: Krzysztof Piaskowy <krzysztof.piaskowy@swmansion.com>
jamesdev-king added a commit to jamesdev-king/flutter_ui_challenge_flight_search that referenced this pull request Aug 1, 2025
## Summary

This pull request introduces a new feature which adds screen "go back"
transition animations based on gestures. The feature is a collaboration
between three libraries: `react-native-reanimated`,
`react-native-screens`, and `react-native-gesture-handler`. Implementing
this feature required changes in both Reanimated and RNScreens. You can
find the related pull request in the `react-native-reanimated`
repository: [PR
1913](software-mansion/react-native-reanimated#5274).

## Test plan

Test example from Example App

---------

Co-authored-by: Krzysztof Piaskowy <krzysztof.piaskowy@swmansion.com>
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.

6 participants