Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

Commit

Permalink
refactor: rewrite based on reanimated
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 8, 2019
1 parent 269b097 commit 40a33b3
Show file tree
Hide file tree
Showing 48 changed files with 2,257 additions and 4,640 deletions.
1 change: 1 addition & 0 deletions example/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[
'react-navigation-stack',
'react-native-gesture-handler',
'react-native-reanimated',
'react-native-vector-icons',
],
},
Expand Down
7 changes: 1 addition & 6 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { useScreens } from 'react-native-screens';
I18nManager.forceRTL(false);

const data = [
{ component: SimpleStack, title: 'Simple', routeName: 'SimpleStack' },
{ component: HeaderPreset, title: 'UIKit Preset', routeName: 'UIKit' },
{ component: SimpleStack, title: 'Wipe Preset', routeName: 'SimpleStack' },
{ component: ImageStack, title: 'Image', routeName: 'ImageStack' },
{ component: ModalStack, title: 'Modal', routeName: 'ModalStack' },
{ component: FullScreen, title: 'Full Screen', routeName: 'FullScreen' },
Expand Down Expand Up @@ -76,11 +76,6 @@ const data = [
title: 'Header background (fade transition)',
routeName: 'HeaderBackgroundFade',
},
{
component: HeaderBackgroundTranslate,
title: 'Header background (translate transition)',
routeName: 'HeaderBackgroundTranslate',
},
];

// Cache images
Expand Down
19 changes: 12 additions & 7 deletions example/src/HeaderBackgrounds.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';
import Animated from 'react-native-reanimated';
import {
createStackNavigator,
TransitionPresets,
HeaderStyleInterpolators,
} from 'react-navigation-stack';

function createHeaderBackgroundExample(options = {}) {
return createStackNavigator(
Expand All @@ -19,7 +24,7 @@ function createHeaderBackgroundExample(options = {}) {
navigationOptions: {
headerTitle: 'Login Screen',
headerTintColor: '#fff',
headerBackground: (
headerBackground: () => (
<View style={{ flex: 1, backgroundColor: '#FF0066' }} />
),
},
Expand All @@ -38,7 +43,7 @@ function createHeaderBackgroundExample(options = {}) {
navigationOptions: {
headerTitle: 'Games Screen',
headerTintColor: '#fff',
headerBackground: (
headerBackground: () => (
<View style={{ flex: 1, backgroundColor: '#3388FF' }} />
),
},
Expand Down Expand Up @@ -90,11 +95,11 @@ function createHeaderBackgroundExample(options = {}) {
);
}
export const HeaderBackgroundDefault = createHeaderBackgroundExample();
export const HeaderBackgroundTranslate = createHeaderBackgroundExample({
headerBackgroundTransitionPreset: 'translate',
});

export const HeaderBackgroundFade = createHeaderBackgroundExample({
headerBackgroundTransitionPreset: 'fade',
...TransitionPresets.SlideFromRightIOS,
headerStyleInterpolator: HeaderStyleInterpolators.forFade,
headerMode: 'float',
});

const styles = StyleSheet.create({
Expand Down
9 changes: 5 additions & 4 deletions example/src/HeaderPreset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as React from 'react';
import { Button, StatusBar } from 'react-native';
import { SafeAreaView } from '@react-navigation/native';
import { createStackNavigator } from 'react-navigation-stack';
import {
createStackNavigator,
TransitionPresets,
} from 'react-navigation-stack';

class HomeScreen extends React.Component {
static navigationOptions = {
Expand Down Expand Up @@ -100,9 +103,7 @@ const StackWithHeaderPreset = createStackNavigator(
ScreenWithNoHeader: ScreenWithNoHeader,
ScreenWithLongTitle: ScreenWithLongTitle,
},
{
headerTransitionPreset: 'uikit',
}
TransitionPresets.SlideFromRightIOS
);

export default StackWithHeaderPreset;
2 changes: 1 addition & 1 deletion example/src/ImageStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ListScreen extends React.Component {
static navigationOptions = ({ navigation }) => ({
title: 'Image list',
headerBackTitle: 'Back',
headerLeft: (
headerLeft: () => (
<Button title="Back" onPress={() => navigation.navigate('Home')} />
),
});
Expand Down
13 changes: 6 additions & 7 deletions example/src/SimpleStack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as React from 'react';
import { Dimensions, Button, View, Text } from 'react-native';
import { withNavigation } from '@react-navigation/core';
import { createStackNavigator } from 'react-navigation-stack';
import {
createStackNavigator,
TransitionPresets,
} from 'react-navigation-stack';

const Buttons = withNavigation(props => (
<React.Fragment>
Expand Down Expand Up @@ -148,11 +151,7 @@ export default createStackNavigator(
},
{
initialRouteName: 'List',

// these are the defaults
cardShadowEnabled: true,
cardOverlayEnabled: false,

// headerTransitionPreset: 'uikit',
headerMode: 'screen',
...TransitionPresets.WipeFromBottomAndroid,
}
);
37 changes: 19 additions & 18 deletions example/src/TransparentStack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as React from 'react';
import { Animated, Button, Easing, View, Text } from 'react-native';
import { Button, View, Text } from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';
import Animated from 'react-native-reanimated';

const { interpolate, multiply, cond } = Animated;

class ListScreen extends React.Component {
render() {
Expand Down Expand Up @@ -78,23 +81,21 @@ export default createStackNavigator(
navigationOptions: {
gesturesEnabled: false,
},
transitionConfig: () => ({
transitionSpec: {
duration: 300,
easing: Easing.inOut(Easing.ease),
timing: Animated.timing,
},
screenInterpolator: sceneProps => {
const { position, scene } = sceneProps;
const { index } = scene;

const opacity = position.interpolate({
inputRange: [index - 1, index],
outputRange: [0, 1],
});
cardStyleInterpolator: ({ progress: { current }, closing }) => {
const opacity = cond(
closing,
current,
interpolate(current, {
inputRange: [0, 0.5, 0.9, 1],
outputRange: [0, 0.25, 0.7, 1],
})
);

return { opacity };
},
}),
return {
cardStyle: {
opacity,
},
};
},
}
);
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"url": "https://github.com/react-navigation/react-navigation-stack/issues"
},
"homepage": "https://github.com/react-navigation/react-navigation-stack#readme",
"dependencies": {
"react-native-safe-area-view": "^0.14.4"
},
"devDependencies": {
"@babel/core": "^7.4.4",
"@commitlint/config-conventional": "^7.5.0",
Expand All @@ -63,7 +66,8 @@
"react": "16.5.0",
"react-dom": "16.5.0",
"react-native": "~0.57.7",
"react-native-gesture-handler": "^1.1.0",
"react-native-gesture-handler": "^1.2.1",
"react-native-reanimated": "^1.0.1",
"react-native-screens": "^1.0.0-alpha.22",
"react-test-renderer": "16.5.0",
"release-it": "^11.0.0",
Expand All @@ -76,6 +80,7 @@
"react": "*",
"react-native": "*",
"react-native-gesture-handler": "^1.0.0",
"react-native-reanimated": "^1.0.0",
"react-native-screens": "^1.0.0 || ^1.0.0-alpha"
},
"jest": {
Expand Down
140 changes: 140 additions & 0 deletions src/TransitionConfigs/CardStyleInterpolators.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import Animated from 'react-native-reanimated';
import { CardInterpolationProps, CardInterpolatedStyle } from '../types';

const { cond, multiply, interpolate } = Animated;

/**
* Standard iOS-style slide in from the right.
*/
export function forHorizontalIOS({
progress: { current, next },
layouts: { screen },
}: CardInterpolationProps): CardInterpolatedStyle {
const translateFocused = interpolate(current, {
inputRange: [0, 1],
outputRange: [screen.width, 0],
});
const translateUnfocused = next
? interpolate(next, {
inputRange: [0, 1],
outputRange: [0, multiply(screen.width, -0.3)],
})
: 0;

const opacity = interpolate(current, {
inputRange: [0, 1],
outputRange: [0, 0.07],
});

const shadowOpacity = interpolate(current, {
inputRange: [0, 1],
outputRange: [0, 0.3],
});

return {
cardStyle: {
backgroundColor: '#eee',
transform: [
// Translation for the animation of the current card
{ translateX: translateFocused },
// Translation for the animation of the card on top of this
{ translateX: translateUnfocused },
],
shadowOpacity,
},
overlayStyle: { opacity },
};
}

/**
* Standard iOS-style slide in from the bottom (used for modals).
*/
export function forVerticalIOS({
progress: { current },
layouts: { screen },
}: CardInterpolationProps): CardInterpolatedStyle {
const translateY = interpolate(current, {
inputRange: [0, 1],
outputRange: [screen.height, 0],
});

return {
cardStyle: {
backgroundColor: '#eee',
transform: [
// Translation for the animation of the current card
{ translateY },
],
},
};
}

/**
* Standard Android-style fade in from the bottom for Android Oreo.
*/
export function forFadeFromBottomAndroid({
progress: { current },
layouts: { screen },
closing,
}: CardInterpolationProps): CardInterpolatedStyle {
const translateY = interpolate(current, {
inputRange: [0, 1],
outputRange: [multiply(screen.height, 0.08), 0],
});

const opacity = cond(
closing,
current,
interpolate(current, {
inputRange: [0, 0.5, 0.9, 1],
outputRange: [0, 0.25, 0.7, 1],
})
);

return {
cardStyle: {
opacity,
transform: [{ translateY }],
},
};
}

/**
* Standard Android-style wipe from the bottom for Android Pie.
*/
export function forWipeFromBottomAndroid({
progress: { current, next },
layouts: { screen },
}: CardInterpolationProps): CardInterpolatedStyle {
const containerTranslateY = interpolate(current, {
inputRange: [0, 1],
outputRange: [screen.height, 0],
});
const cardTranslateYFocused = interpolate(current, {
inputRange: [0, 1],
outputRange: [multiply(screen.height, 95.9 / 100, -1), 0],
});
const cardTranslateYUnfocused = next
? interpolate(next, {
inputRange: [0, 1],
outputRange: [0, multiply(screen.height, 2 / 100, -1)],
})
: 0;
const overlayOpacity = interpolate(current, {
inputRange: [0, 0.36, 1],
outputRange: [0, 0.1, 0.1],
});

return {
containerStyle: {
transform: [{ translateY: containerTranslateY }],
},
cardStyle: {
transform: [
{ translateY: cardTranslateYFocused },
{ translateY: cardTranslateYUnfocused },
],
},
overlayStyle: { opacity: overlayOpacity },
};
}
Loading

0 comments on commit 40a33b3

Please sign in to comment.