项目作者: Taymindis

项目描述 :
custom animatable stack design with react router native
高级语言: TypeScript
项目地址: git://github.com/Taymindis/react-router-native-animate-stack.git
创建时间: 2020-03-24T07:41:09Z
项目社区:https://github.com/Taymindis/react-router-native-animate-stack

开源协议:MIT License

下载


react-router-native-animate-stack

npm version
downloads

A latest React version react router native animate stack

purpose of this component

React Router Native v5 with your desired customization transition style! It’s design with Animated.View

This package only useable with React Router Native. Use it like a React router native’s Switch

@minikawoon/projects/rrn-animate-stack">Snack Demo

Installation

Install react-router-native and this package:

npm install react-router-native react-router-native-animate-stack --save

OR

yarn add react-router-native react-router-native-animate-stack

Usage

Here’s a simple working example of using it.

  1. // When no customization, it is just like a stack
  2. <AnimatedStack
  3. swipeCancelSpeed={50}
  4. swipeable={true}>
  5. <Route exact path='/'>
  6. <Home ></Home>
  7. </Route>
  8. <Route path='/about'>
  9. <About ></About>
  10. </Route>
  11. <Route path='/topics' component={Topics} ></Route>
  12. </AnimatedStack>

This is what the above code looks like running on iOS simulator:

Default Behaviour

The stack component will trigger gesture swipe forward and backward when passing swipeable true.

If you are familiar with Switch, you will know how to use this.

Customizing Animation your desire animation style

onMountAnimate

when the component mounted, you can defined how you going to animate

onTransitionAnimate

when the component swap between 2 screen, you can animating the view!

activedViewStyleHandler

Enter animation style is the ViewStyle for Enter Screen, which is acive screen

deactivedViewStyleHandler

Exit animation style is the ViewStyle for Exit Screen, which is deactive screen

Below example you will be more clear:

  1. const App = () => {
  2. const enterAnimKit = new Animated.Value(0);
  3. const exitAnimKit = new Animated.Value(0);
  4. const width = useWindowDimensions().width;
  5. return (
  6. <SafeAreaView>
  7. <NativeRouter>
  8. <View style={styles.container}>
  9. <Navigator ></Navigator>
  10. <AnimatedStack
  11. swipeCancelSpeed={50}
  12. swipeable={true}
  13. onMountAnimate={() => {
  14. Animated.timing(enterAnimKit, {
  15. toValue: 1,
  16. duration: 100
  17. }).start();
  18. }}
  19. onTransitionAnimate={({ location, action, isNestedRoute }) => {
  20. if (isNestedRoute) return;
  21. // Enter and exit or one only
  22. enterAnimKit.setValue(0);
  23. exitAnimKit.setValue(0);
  24. Animated.timing(enterAnimKit, {
  25. toValue: 1,
  26. duration: 500,
  27. delay: 200
  28. }).start();
  29. Animated.timing(exitAnimKit, {
  30. toValue: 1,
  31. duration: 500
  32. }).start();
  33. }}
  34. activedViewStyleHandler={({ location, action, isNestedRoute }) => {
  35. return {
  36. transform: [
  37. {
  38. translateX: enterAnimKit.interpolate({
  39. inputRange: [0, 1],
  40. outputRange: [width, 0]
  41. })
  42. }
  43. ]
  44. };
  45. }}
  46. deactivedViewStyleHandler={({ location, action, isNestedRoute }) => {
  47. return {
  48. transform: [
  49. {
  50. translateX: exitAnimKit.interpolate({
  51. inputRange: [0, 1],
  52. outputRange: [0, -width]
  53. })
  54. }
  55. ]
  56. };
  57. }}
  58. >
  59. <Route exact path='/'>
  60. <Home ></Home>
  61. </Route>
  62. <Route path='/about'>
  63. <About ></About>
  64. </Route>
  65. <Route path='/topics' component={Topics} ></Route>
  66. </AnimatedStack>
  67. </View>
  68. </NativeRouter>
  69. </SafeAreaView>
  70. );
  71. };

With this code given, the transition will be shown as below

Customize Transiction Animation

transition like butterfly

Butterfly Transiction Animation

  1. activedViewStyleHandler={({ location, action, isNestedRoute }) => {
  2. return {
  3. transform: [
  4. {
  5. translateX: enterAnimKit.interpolate({
  6. inputRange: [0, 1],
  7. outputRange: [width, 0]
  8. })
  9. },
  10. {
  11. rotateX: enterAnimKit.interpolate({
  12. inputRange: [0, 0.5, 1],
  13. outputRange: ['0deg', '180deg', '0deg']
  14. })
  15. }
  16. ]
  17. };
  18. }}
  19. deactivedViewStyleHandler={({ location, action, isNestedRoute }) => {
  20. return {
  21. transform: [
  22. {
  23. translateX: exitAnimKit.interpolate({
  24. inputRange: [0, 1],
  25. outputRange: [0, -width]
  26. })
  27. },
  28. {
  29. rotateX: exitAnimKit.interpolate({
  30. inputRange: [0, 0.5, 1],
  31. outputRange: ['0deg', '180deg', '0deg']
  32. })
  33. }
  34. ]
  35. };
  36. }}

transition like Scaling

Scaling Transiction Animation

  1. activedViewStyleHandler={({ location, action, isNestedRoute }) => {
  2. return {
  3. transform: [
  4. {
  5. translateX: enterAnimKit.interpolate({
  6. inputRange: [0, 1],
  7. outputRange: [width, 0]
  8. })
  9. },
  10. {
  11. scale: enterAnimKit.interpolate({
  12. inputRange: [0, 0.5, 1],
  13. outputRange: [1, 0.2, 1]
  14. })
  15. }
  16. ]
  17. };
  18. }}
  19. deactivedViewStyleHandler={({ location, action, isNestedRoute }) => {
  20. return {
  21. transform: [
  22. {
  23. translateX: exitAnimKit.interpolate({
  24. inputRange: [0, 1],
  25. outputRange: [0, -width]
  26. })
  27. },
  28. {
  29. scale: exitAnimKit.interpolate({
  30. inputRange: [0, 0.5, 1],
  31. outputRange: [1, 0.2, 1]
  32. })
  33. }
  34. ]
  35. };
  36. }}

Vertical Swipe and Navigation

Scaling Transiction Up Animation

  1. <NativeRouter>
  2. <View style={styles.container}>
  3. <View style={{ flex: 9, height: '100%' }}>
  4. <AnimatedStack
  5. swipeMethod={SwipeMethod.SWIPE_VERTICAL}
  6. onMountAnimate={() => {
  7. Animated.timing(enterAnimKit, {
  8. toValue: 1,
  9. duration: 100,
  10. }).start();
  11. }}
  12. onTransitionAnimate={({ location, action, isNestedRoute }) => {
  13. if (isNestedRoute) return;
  14. // Enter and exit or one only
  15. enterAnimKit.setValue(0);
  16. exitAnimKit.setValue(0);
  17. Animated.timing(enterAnimKit, {
  18. toValue: 1,
  19. duration: 500,
  20. delay: 200,
  21. }).start();
  22. Animated.timing(exitAnimKit, {
  23. toValue: 1,
  24. duration: 500,
  25. }).start();
  26. }}
  27. activedViewStyleHandler={({ location, action, isNestedRoute }) => {
  28. return {
  29. paddingLeft: 40,
  30. transform: [
  31. {
  32. translateY: enterAnimKit.interpolate({
  33. inputRange: [0, 1],
  34. outputRange: [height, 0],
  35. }),
  36. },
  37. {
  38. scale: enterAnimKit.interpolate({
  39. inputRange: [0, 0.5, 1],
  40. outputRange: [1, 0.2, 1],
  41. }),
  42. },
  43. ],
  44. };
  45. }}
  46. deactivedViewStyleHandler={({ location, action, isNestedRoute }) => {
  47. return {
  48. transform: [
  49. {
  50. translateY: exitAnimKit.interpolate({
  51. inputRange: [0, 1],
  52. outputRange: [0, -height],
  53. }),
  54. },
  55. {
  56. scale: exitAnimKit.interpolate({
  57. inputRange: [0, 0.5, 1],
  58. outputRange: [1, 0.2, 1],
  59. }),
  60. },
  61. ],
  62. };
  63. }}
  64. >
  65. <Route exact path="/">
  66. <Home ></Home>
  67. </Route>
  68. <Route path="/about">
  69. <About ></About>
  70. </Route>
  71. <Route path="/topics" component={Topics} ></Route>
  72. </AnimatedStack>
  73. </View>
  74. <View style={{ flex: 1 }}>
  75. <Navigator ></Navigator>
  76. </View>
  77. </View>
  78. </NativeRouter>

Reason of created this

React Router Native still a popular routing engine which native, clean(no other UI kit dependencies) and powerful for routing but no animation.

Now added on animate stack, you can animate the view on run time, changing the animation style on runtime!

React router for routing, let the drawer, menu bar, tab bar and other fancy UI kits bar for other Ui Library control without any breakage.

Design for latest React Version.

This package is using getDerivedStateFromProps function which going to replace componentWillReceiveProps