|
2 | 2 |
|
3 | 3 | React Navigation bindings for [react-native-shared-element](https://github.com/IjzerenHein/react-native-shared-element) 💫 |
4 | 4 |
|
| 5 | +## Compatibility <!-- omit in toc --> |
5 | 6 |
|
6 | | -## Documentation |
| 7 | +*This library is currently undergoing development to support the latest react-navigation & stack versions.* |
7 | 8 |
|
8 | | -- [Shared element for React Navigation 5.x](./docs/Navigation5.md) |
9 | | -- [Shared element for React Navigation 4.x](./docs/Navigation4.md) |
10 | | -- [Migration guide](./docs/Migration.md) |
| 9 | +Supported are: |
11 | 10 |
|
12 | | -## Compatibility <!-- omit in toc --> |
| 11 | +- [X] react-navigation 4 & 3 |
| 12 | +- [X] react-navigation-stack 2 |
| 13 | +- [X] react-navigation-stack 1 [(use react-navigation-shared-element@1)](https://github.com/IjzerenHein/react-navigation-shared-element/tree/v1) |
13 | 14 |
|
14 | | -The following versions or react-navigation and the stack navigator are supported. |
| 15 | +Under development: |
15 | 16 |
|
16 | | -| Version | React-Navigation | Comments | |
17 | | -| ----------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
18 | | -| 3.x | 4 & 5 | Use `createSharedElementStackNavigator4` to use it with 4.x. In this version the `sharedElements` function was changed to use `route` instead of `navigator`. | |
19 | | -| [2.x](https://github.com/IjzerenHein/react-navigation-shared-element/tree/v2) | 3 & 4 | This version is compatible with `react-navigation-stack@2`. | |
20 | | -| [1.x](https://github.com/IjzerenHein/react-navigation-shared-element/tree/v1) | 3 & 4 | This version is compatible with `react-navigation-stack@1`. | |
| 17 | + 🚧 [react-navigation 5 (see navigation-v5 branch if you want to test)](https://github.com/IjzerenHein/react-navigation-shared-element/tree/navigation-v5) |
21 | 18 |
|
22 | 19 | Unlikely to be supported: |
23 | 20 |
|
24 | 21 | - [ ] [react-native-screens/createNativeStackNavigator](https://github.com/IjzerenHein/react-navigation-shared-element/issues/14) |
25 | 22 |
|
26 | 23 |
|
| 24 | +## Index <!-- omit in toc --> |
| 25 | + |
| 26 | +- [Installation](#installation) |
| 27 | +- [Usage](#usage) |
| 28 | +- [Documentation](#documentation) |
| 29 | + - [createSharedElementStackNavigator](#createsharedelementstacknavigator) |
| 30 | + - [Debugging shared element transitions](#debugging-shared-element-transitions) |
| 31 | + - [SharedElement](#sharedelement) |
| 32 | + - [sharedElements config](#sharedelements-config) |
| 33 | +- [Demo App](#demo-app) |
| 34 | +- [Videos](#videos) |
| 35 | +- [License](#license) |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +Open a Terminal in your project's folder and run, |
| 40 | + |
| 41 | +```sh |
| 42 | +$ yarn add react-navigation-shared-element react-native-shared-element |
| 43 | +``` |
| 44 | + |
| 45 | +Enure that [react-native-shared-element](https://github.com/IjzerenHein/react-native-shared-element) is also linked into your project. |
| 46 | + |
| 47 | +Finally, make sure that the compatible react-navigation dependencies are installed: |
| 48 | + |
| 49 | +```sh |
| 50 | +$ yarn add react-navigation@4 react-navigation-stack@2 |
| 51 | +``` |
| 52 | + |
| 53 | +> react-navigation@5 is not supported yet, so don't bother.. |
| 54 | +
|
| 55 | +## Usage |
| 56 | + |
| 57 | +In order to enable shared element transitions, the following steps need to be performed |
| 58 | + |
| 59 | +- Create a stack-navigator using `createSharedElementStackNavigator` |
| 60 | +- Wrap your component with `<SharedElement>` and provide a unique `id` |
| 61 | +- Define a static `sharedElements` config on the Screen that you want to animate |
| 62 | + |
| 63 | +```jsx |
| 64 | +import { createSharedElementStackNavigator } from 'react-navigation-shared-element'; |
| 65 | + |
| 66 | +const stackNavigator = createSharedElementStackNavigator( |
| 67 | + { |
| 68 | + List: ListScreen, |
| 69 | + Detail: DetailScreen, |
| 70 | + }, |
| 71 | + { |
| 72 | + initialRouteName: 'List', |
| 73 | + } |
| 74 | +); |
| 75 | + |
| 76 | +const AppContainer = createAppContainer(stackNavigator); |
| 77 | +``` |
| 78 | + |
| 79 | +```jsx |
| 80 | +// ListScreen.js |
| 81 | +import { SharedElement } from 'react-navigation-shared-element'; |
| 82 | + |
| 83 | +class ListScreen extends React.Component { |
| 84 | + renderItem(item) { |
| 85 | + const { navigation } = this.props; |
| 86 | + return ( |
| 87 | + <TouchableOpacity onPress={() => navigation.push('Detail', { item })}> |
| 88 | + <SharedElement id={`item.${item.id}.photo`}> |
| 89 | + <Image source={item.photoUrl} /> |
| 90 | + </SharedElement> |
| 91 | + </TouchableOpacity> |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +```jsx |
| 98 | +// DetailScreen.js |
| 99 | +const DetailScreen = (props) => { |
| 100 | + const item = props.navigation.getParam('item'); |
| 101 | + return ( |
| 102 | + <SharedElement id={`item.${item.id}.photo`}> |
| 103 | + <Image source={item.photoUrl} /> |
| 104 | + </SharedElement> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +DetailScreen.sharedElements = (navigation, otherNavigation, showing) => { |
| 109 | + const item = navigation.getParam('item'); |
| 110 | + return [`item.${item.id}.photo`]; |
| 111 | +}; |
| 112 | +``` |
| 113 | + |
| 114 | +## Documentation |
| 115 | + |
| 116 | +### createSharedElementStackNavigator |
| 117 | + |
| 118 | +The `createSharedElementStackNavigator` function wraps an existing stack-navigator and enables shared element transitions for it. |
| 119 | + |
| 120 | +It performs the following actions |
| 121 | + |
| 122 | +- Creates a top-level renderer to host the shared element transitions |
| 123 | +- Wraps each route with a shared element scene |
| 124 | +- Detect route changes and trigger shared element transitions |
| 125 | + |
| 126 | +**Arguments** |
| 127 | + |
| 128 | +| Argument | Type | Description | |
| 129 | +| --------------------- | -------- | ----------------------------------------------------------------------- | |
| 130 | +| `routeConfig` | `object` | Routes-config | |
| 131 | +| `stackConfig` | `object` | Optional stack navigator config | |
| 132 | +| `sharedElementConfig` | `object` | Optional [shared element config](#debugging-shared-element-transitions) | |
| 133 | + |
| 134 | +#### Debugging shared element transitions |
| 135 | + |
| 136 | +When transitions aren't working as expected, you can enable debug-mode |
| 137 | +to log scene transitions and shared-element id's to the console. The |
| 138 | +log output is useful for understanding scene changes and for reporting issues. |
| 139 | + |
| 140 | +```jsx |
| 141 | +const stackNavigator1 = createSharedElementStackNavigator( |
| 142 | + { ... }, // routeConfig |
| 143 | + { ... } // stackConfig |
| 144 | + { |
| 145 | + name: 'MyStackNav', |
| 146 | + debug: true |
| 147 | + } |
| 148 | +); |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +### SharedElement |
| 153 | + |
| 154 | +The `<SharedElement>` component accepts a single child and a _"shared"_ id. The child is the element that is made available for doing shared element transitions. It can be any component, like a `<View>`, `<Text>` or `<Image>`. In case the wrapped view is an `<Image>`, special handling is performed to deal with image loading and resizing. |
| 155 | + |
| 156 | +This component is synonymous for the `<SharedElement>` component as defined in `react-native-shared-element`. Instead of a `node` it uses an `id` to create a higher lever API which automatically ties in with the scenes created by `createSharedElementStackNavigator`. |
| 157 | + |
| 158 | +**Props** |
| 159 | + |
| 160 | +| Property | Type | Description | |
| 161 | +| --------------- | --------- | ------------------------------------------------------------------------------------ | |
| 162 | +| `children` | `element` | A single child component, which must map to a real view in the native view hierarchy | |
| 163 | +| `id` | `string` | Unique id of the shared element | |
| 164 | +| `View props...` | | Other props supported by View | |
| 165 | + |
| 166 | +### sharedElements config |
| 167 | + |
| 168 | +In order to trigger shared element transitions between screens, a static `sharedElements` config needs to be defined on one of the two screens. For |
| 169 | +each screen transition, both screens are evaluated and checked whether they have a `sharedElements` config. The screen that is being shown is **evaluated first**, followed by the screen that is being hidden. If `undefined` is returned, evaluation continues at the other screen. |
| 170 | + |
| 171 | +The `sharedElements` function receives 3 arguments |
| 172 | + |
| 173 | +| Argument | Type | Description | |
| 174 | +| ----------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ | |
| 175 | +| `navigation` | `NavigationProp` | Navigation prop of the current screen. You can use this to get the params of the screen using `getParam`, or the route-name using `state.routeName` | |
| 176 | +| `otherNavigation` | `NavigationProp` | The navigation-prop of the other screen. You can use this to get the params of that screen using `getParam`, or the route-name using `state.routeName` | |
| 177 | +| `showing` | `boolean` | `true` when this screen is being shown, and `false` when this screen is being hidden. | | |
| 178 | + |
| 179 | +The return value should be either `undefined` or an array of shared-element configs or identifiers. Specifying a string-identifier is shorthand for `{id: 'myid'}`. |
| 180 | + |
| 181 | +**Basic example** |
| 182 | + |
| 183 | +```js |
| 184 | +class DetailScreen extends Component { |
| 185 | + static sharedElements = (navigation, otherNavigation, showing) => { |
| 186 | + // Transition element `item.${item.id}.photo` when either |
| 187 | + // showing or hiding this screen (coming from any route) |
| 188 | + const item = navigation.getParam('item'); |
| 189 | + return [`item.${item.id}.photo`]; |
| 190 | + } |
| 191 | + |
| 192 | + render() {...} |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +**Only transition when coming from a specific route** |
| 197 | + |
| 198 | +If you only want to show a transition when pushing from a particular screen, then use the route-name and `showing` argument. |
| 199 | + |
| 200 | +```js |
| 201 | +class DetailScreen extends Component { |
| 202 | + static sharedElements = (navigation, otherNavigation, showing) => { |
| 203 | + if ((otherNavigation.state.routeName === 'List') && showing) { |
| 204 | + const item = navigation.getParam('item'); |
| 205 | + return [`item.${item.id}.photo`]; |
| 206 | + } |
| 207 | + } |
| 208 | + ... |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +**Customize the animation** |
| 213 | + |
| 214 | +If the source- and target elements are visually distinct, the consider using a cross-fade animation. |
| 215 | + |
| 216 | +```js |
| 217 | +class DetailScreen extends Component { |
| 218 | + static sharedElements = (navigation, otherNavigation, showing) => { |
| 219 | + const item = navigation.getParam('item'); |
| 220 | + return [{ |
| 221 | + id: `item.${item.id}.photo`, |
| 222 | + animation: 'fade' |
| 223 | + // resize: 'clip' |
| 224 | + // align: ''left-top' |
| 225 | + }]; |
| 226 | + } |
| 227 | + ... |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +The following fields can be specified in a config item |
| 232 | + |
| 233 | +| Field | Type | Description | |
| 234 | +| ----------- | --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 235 | +| `id` | `string` | Id that corresponds to the `id` specified in the `<SharedElement>` component | |
| 236 | +| `otherId` | `string` | Optional id that corresponds to the `id` specified in the other screen. | |
| 237 | +| `animation` | `move` \| `fade` | Type of animation to perform (default = `move`), [see SharedElementAnimation](https://github.com/IjzerenHein/react-native-shared-element#sharedelementanimation) | |
| 238 | +| `resize` | `auto` \| `stretch` \| `clip` \| `none` | Resize behavior of the transition (default = `auto`), [see SharedElementResize](https://github.com/IjzerenHein/react-native-shared-element#sharedelementresize) | |
| 239 | +| `align` | `auto` \| `top-left` \| `...` | Align behavior of the transition (default = `auto`), [see SharedElementAlign](https://github.com/IjzerenHein/react-native-shared-element#sharedelementalign) | | |
27 | 240 |
|
28 | 241 | ## Demo App |
29 | 242 |
|
|
0 commit comments