|
| 1 | +[](https://codecov.io/gh/Taymindis/react-router-native-animate-stack/) |
| 2 | + |
1 | 3 | # react-router-native-animate-stack
|
2 |
| -custom animatable stack design with react router native |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/react-router-native-animate-stack) |
| 6 | +[](https://www.npmjs.com/package/react-router-native-animate-stack) |
| 7 | + |
| 8 | +A latest React version react router native animate stack |
| 9 | + |
| 10 | +## purpose of this component |
| 11 | + |
| 12 | +React Router Native v5 with your desired customization transition style! It's design with Animated.View |
| 13 | + |
| 14 | +This package only useable with React Router Native. Use it like a React router native's **Switch** |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Install `react-router-native` and this package: |
| 19 | + |
| 20 | +`npm install react-router-native react-router-native-animate-stack --save` |
| 21 | + |
| 22 | +OR |
| 23 | + |
| 24 | +`yarn add react-router-native react-router-native-animate-stack` |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +Here's a simple working example of using it. |
| 29 | + |
| 30 | +```jsx |
| 31 | +// When no customization, it is just like a stack |
| 32 | +<AnimatedStack |
| 33 | + swipeCancelSpeed={50} |
| 34 | + swipeable={true}> |
| 35 | + <Route exact path='/'> |
| 36 | + <Home /> |
| 37 | + </Route> |
| 38 | + <Route path='/about'> |
| 39 | + <About /> |
| 40 | + </Route> |
| 41 | + <Route path='/topics' component={Topics} /> |
| 42 | +</AnimatedStack> |
| 43 | +``` |
| 44 | + |
| 45 | +This is what the above code looks like running on iOS simulator: |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +The stack component will trigger gesture swipe forward and backward when passing swipeable true. |
| 50 | + |
| 51 | +If you are familiar with [Switch](https://reacttraining.com/react-router/native/api/Switch), you will know how to use this. |
| 52 | + |
| 53 | +## Customizing Animation your desire animation style |
| 54 | + |
| 55 | +###### onMountAnimate |
| 56 | +when the component mounted, you can defined how you going to animate |
| 57 | + |
| 58 | +##### onTransitionAnimate |
| 59 | +when the component swap between 2 screen, you can animating the view! |
| 60 | + |
| 61 | +##### activedViewStyleHandler |
| 62 | +Enter animation style is the ViewStyle for Enter Screen, which is acive screen |
| 63 | + |
| 64 | +##### deactivedViewStyleHandler |
| 65 | +Exit animation style is the ViewStyle for Exit Screen, which is deactive screen |
| 66 | + |
| 67 | +Below example you will be more clear: |
| 68 | + |
| 69 | +```jsx |
| 70 | + |
| 71 | +const App = () => { |
| 72 | + const enterAnimKit = new Animated.Value(0); |
| 73 | + const exitAnimKit = new Animated.Value(0); |
| 74 | + const width = useWindowDimensions().width; |
| 75 | + return ( |
| 76 | + <SafeAreaView> |
| 77 | + <NativeRouter> |
| 78 | + <View style={styles.container}> |
| 79 | + <Navigator /> |
| 80 | + <AnimatedStack |
| 81 | + swipeCancelSpeed={50} |
| 82 | + swipeable={true} |
| 83 | + onMountAnimate={() => { |
| 84 | + Animated.timing(enterAnimKit, { |
| 85 | + toValue: 1, |
| 86 | + duration: 100 |
| 87 | + }).start(); |
| 88 | + }} |
| 89 | + onTransitionAnimate={({ location, action, isNestedRoute }) => { |
| 90 | + if (isNestedRoute) return; |
| 91 | + // Enter and exit or one only |
| 92 | + enterAnimKit.setValue(0); |
| 93 | + exitAnimKit.setValue(0); |
| 94 | + |
| 95 | + Animated.timing(enterAnimKit, { |
| 96 | + toValue: 1, |
| 97 | + duration: 500, |
| 98 | + delay: 200 |
| 99 | + }).start(); |
| 100 | + |
| 101 | + Animated.timing(exitAnimKit, { |
| 102 | + toValue: 1, |
| 103 | + duration: 500 |
| 104 | + }).start(); |
| 105 | + }} |
| 106 | + activedViewStyleHandler={({ location, action, isNestedRoute }) => { |
| 107 | + return { |
| 108 | + transform: [ |
| 109 | + { |
| 110 | + translateX: enterAnimKit.interpolate({ |
| 111 | + inputRange: [0, 1], |
| 112 | + outputRange: [width, 0] |
| 113 | + }) |
| 114 | + } |
| 115 | + ] |
| 116 | + }; |
| 117 | + }} |
| 118 | + deactivedViewStyleHandler={({ location, action, isNestedRoute }) => { |
| 119 | + return { |
| 120 | + transform: [ |
| 121 | + { |
| 122 | + translateX: exitAnimKit.interpolate({ |
| 123 | + inputRange: [0, 1], |
| 124 | + outputRange: [0, -width] |
| 125 | + }) |
| 126 | + } |
| 127 | + ] |
| 128 | + }; |
| 129 | + }} |
| 130 | + > |
| 131 | + <Route exact path='/'> |
| 132 | + <Home /> |
| 133 | + </Route> |
| 134 | + <Route path='/about'> |
| 135 | + <About /> |
| 136 | + </Route> |
| 137 | + <Route path='/topics' component={Topics} /> |
| 138 | + </AnimatedStack> |
| 139 | + </View> |
| 140 | + </NativeRouter> |
| 141 | + </SafeAreaView> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +``` |
| 146 | + |
| 147 | +With this code given, the transition will be shown as below |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +## Reason of created this |
| 153 | + |
| 154 | +React Router Native still a popular routing engine which native, clean(no other UI kit dependencies) and powerful for routing but no animation. |
| 155 | + |
| 156 | +Now added on animate stack, you can animate the view on run time, changing the animation style on runtime! |
| 157 | + |
| 158 | +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. |
| 159 | + |
| 160 | + |
| 161 | +## Design for latest React Version. |
| 162 | + |
| 163 | +This package is using getDerivedStateFromProps function which going to replace componentWillReceiveProps |
| 164 | + |
0 commit comments