This site runs best with JavaScript enabled.
Share

Spotify Login Animation With React Navigation V5

Youssouf El Azizi

β€”

April 12, 2020

elazizi, dev, react

If you are using Spotify mobile application to listen to your favorites music, for sure you noticed the great and smooth login animation.

As a Front-end and React Native developer I easily fell in love with such kind of micro-interaction and I always aim to create my own version.

Before jumping into our use case, I just want to highlight that having such kind of micro-interaction in your mobile application is very important for users, Using those animations would help to improve the user experience and will make your application special in front of all standard animation we have in Mobile Platform.

Last weekend, during the quarantine time πŸ™ƒ. I start thinking about Implementing the same behavior in one of the applications I am working on using react-navigation API.

This Article is my weekend work trying to implement Spotify login animation using react-navigation V5.

In case you are not familiar with Spotify and didn’t know how the Spotify login animation looks like. The login animation is a flip from the login screen to the home page. The flip Animation can be implemented easily using the react-native animation API, but the challenge here is to implement the animation using Navigation API.

Approach :

To Implement the Animation we should first prepare our login and home screens to make sure our example will reflect the exact Spotify app experience, then we should implement an AuthContext to handle our authentication flow.

Next, we are going to create our Navigators and set up the navigation experience with our authentication workflow.

Our last step is to create the custom flip animation using the new react-navigation API.

If you are Familiar with React-Navigation and you already woked with authentication flow before, you can skip all those sections to Create Spotify Transition.

Create Screens

To make it easy to follow, we are not going to create a Spotify clone, instead, we will only focus on creating the login and home screen with Albums.

Create the Login Screen using a Simple Form and a Login Button.

1// screens/Login.js
2
3import * as React from "react";
4import { Text, View, StyleSheet } from "react-native";
5import { Button, TextInput } from "react-native-paper";
6
7export const LogIn = ({ navigation }) => {
8 const [loading, setLoading] = React.useState(false);
9
10 const loginApi = () => {};
11 return (
12 <View style={styles.form}>
13 <TextInput label="Username" style={styles.textInput} />
14 <TextInput
15 label="Password"
16 style={styles.textInput}
17 secureTextEntry={true}
18 />
19 <Button mode="outlined" onPress={() => loginApi()} style={styles.button}>
20 {loading ? "Loading ...." : "LogIn"}
21 </Button>
22 </View>
23 );
24};

Make sure to read My previous post about the best way to handle forms in react native

Next step is to create the home screen with some albums cover images :

1import * as React from "react";
2import { StyleSheet, Image, ScrollView } from "react-native";
3import { Button, TextInput } from "react-native-paper";
4
5export const Home = ({ navigation }) => {
6 return (
7 <ScrollView contentContainerStyle={styles.albums}>
8 <Albums />
9 </ScrollView>
10 );
11};

Create Navigators and Auth Flow

To set-up our authentication flow we are going to use the same approach mentioned in react-navigation documentation with some changes on the navigation Part.

First, we are going to make an AuthContext & AuthProvider that will help us deal with all authentication flow.

For the navigation part, I think the best way for our case is to create two stacks, one for auth screens (login, signUp, …) and a second one for home screens ( albums, player …)

Finally, we use a root stack navigator that renders the correct stack based on user status ( loggedIn/Out).

1// navigation/index.js
2const AuthStack = createStackNavigator();
3const AppStack = createStackNavigator();
4const RootStack = createStackNavigator();
5
6const AuthNavigator = () => {
7 return (
8 <AuthStack.Navigator
9 headerMode="float"
10 screenOptions={{
11 cardOverlayEnabled: true,
12 gestureEnabled: true,
13 }}
14 >
15 <AuthStack.Screen name="Login" component={LogIn} />
16 </AuthStack.Navigator>
17 );
18};
19
20const HomeNavigator = () => {
21 return (
22 <AppStack.Navigator
23 headerMode="float"
24 screenOptions={{
25 cardOverlayEnabled: true,
26 gestureEnabled: true,
27 }}
28 >
29 <AppStack.Screen
30 name="Albums"
31 component={Home}
32 options={{
33 headerRight: () => <SignOutButton />,
34 }}
35 />
36 </AppStack.Navigator>
37 );
38};
39
40export const RootNavigator = () => {
41 const { status } = useAuth();
42
43 return (
44 <RootStack.Navigator headerMode="none">
45 {status === "signOut" ? (
46 <RootStack.Screen name="Auth" component={AuthNavigator} />
47 ) : (
48 <RootStack.Screen name="App" component={HomeNavigator} />
49 )}
50 </RootStack.Navigator>
51 );
52};

By the end, we should be able to login using the login screen.

Create Spotify Transition

React stack navigation v5 release introduced a new way to create custom Animation using declarative API and based on next and current screen progress.

Those transactions can be customized by using screen options prop for every screen, but in our case, we are going to implement the custom animation globally for our Root Navigator.

For stack Navigator There are 4 animation related options:

  • gestureDirection : to define your swipe gestures.
  • transitionSpec : An object which specifies the animation type (timing or spring) and their options (such as the duration for timing).
  • cardStyleInterpolator : This is a function that specifies interpolated styles for various parts of the card. Using this function we can interpolate styles for the container, the card itself, overlay and shadow, Our custom animation will be based on using this function.
  • headerStyleInterpolator : The same as cardStyleInterpolate but for header properties. To create a custom transition for our Root screens we need to create a custom transition object for our Transition and add it to screenOptions prop in RootNavigator
1// navigation/SpotifyTransition.js
2import { Easing } from "react-native";
3import {
4 TransitionSpecs,
5 HeaderStyleInterpolators,
6} from "@react-navigation/stack";
7
8const AnimationSpec = {
9 animation: "timing",
10 config: {
11 duration: 2000,
12 easing: Easing.ease,
13 },
14};
15
16export const SpotifyTransition = {
17 transitionSpec: {
18 open: AnimationSpec,
19 close: AnimationSpec,
20 },
21 cardStyleInterpolator: ({ current, next }) => {
22 return {};
23 },
24};
1// navigation/index.js
2export const RootNavigator = () => {
3 const { status } = useAuth();
4
5 return (
6 <RootStack.Navigator
7 headerMode="none"
8 screenOptions={{
9 cardOverlayEnabled: true,
10 gestureEnabled: true,
11 ...SpotifyTransition,
12 }}
13 >
14 {status === "signOut" ? (
15 <RootStack.Screen name="Auth" component={AuthNavigator} />
16 ) : (
17 <RootStack.Screen name="App" component={HomeNavigator} />
18 )}
19 </RootStack.Navigator>
20 );
21};

The first step in our transition is to rotate the focused screen(the screen we want to navigate to) according to Y-axis from 180deg to 0deg and at the same time, the next screen should rotate from 0deg to 180deg.

To Make it work, we need to update card Style and set rotateX as the following:

1export const SpotifyTransition = {
2 transitionSpec: {
3 open: AnimationSpec, //TransitionSpecs.TransitionIOSSpec,
4 close: AnimationSpec, //TransitionSpecs.TransitionIOSSpec,
5 },
6 cardStyleInterpolator: ({ current, next }: any) => {
7 return {
8 cardStyle: {
9 transform: [
10 {
11 rotateY: next
12 ? next.progress.interpolate({
13 inputRange: [0, 1],
14 outputRange: ["0deg", "180deg"],
15 })
16 : current.progress.interpolate({
17 inputRange: [0, 1],
18 outputRange: ["180deg", "0deg"],
19 }),
20 },
21 ],
22 },
23 };
24 },
25};

The highlighted lines mean that if we have the next value, we know that this card is behind the focused one, so we need to rotate from 0deg to 180deg.

As you can see the rotation is working as expected but the focused screen is always on the top and covers the old one immediately even if the Transition progress is still running.

To fix this issue, we are going to interpolate the navigation progress and use it for card opacity which will help us show the focused screen only when the progress is higher than 0.5 and show the old screen if it's not the case

1export const SpotifyTransition = {
2 transitionSpec: {
3 open: AnimationSpec,
4 close: AnimationSpec,
5 },
6 cardStyleInterpolator: ({ current, next }) => {
7 return {
8 cardStyle: {
9 opacity: next
10 ? next.progress.interpolate({
11 inputRange: [0, 0.5, 1],
12 outputRange: [1, 1, 0],
13 })
14 : current.progress.interpolate({
15 inputRange: [0, 0.5, 1],
16 outputRange: [0, 0, 1],
17 }),
18 transform: [
19 {
20 rotateY: next
21 ? next.progress.interpolate({
22 inputRange: [0, 1],
23 outputRange: ["0deg", "180deg"],
24 })
25 : current.progress.interpolate({
26 inputRange: [0, 1],
27 outputRange: ["180deg", "0deg"],
28 }),
29 },
30 ],
31 },
32 overlayStyle: {
33 opacity: current.progress.interpolate({
34 inputRange: [0, 1],
35 outputRange: [0, 1],
36 }),
37 },
38 };
39 },
40};

Now it's time to make the transition smooth using a spring IOS Transition from React Navigation instead of a simple timing config.

1export const SpotifyTransition = {
2 transitionSpec: {
3 open: TransitionSpecs.TransitionIOSSpec,
4 close: TransitionSpecs.TransitionIOSSpec,
5 },
6 ...
7 },
8};

Github repo with snack demo πŸ‘‰ https://github.com/yjose/spotify-login-animation-with-react-navigation-v5

I hope you found that interesting, informative, and entertaining. I would be more than happy to hear your remarks and thoughts.

If you think other people should read this post. Tweet, share and Follow me on twitter for the next articles.

Reac More πŸ‘‰ Forms in React Native, The right way

Share article πŸ‘‰

Get updates via email

JavaScript, React, React Native and web thoughts