|
| 1 | +--- |
| 2 | +id: drawer-navigator |
| 3 | +title: Drawer-based navigation |
| 4 | +sidebar_label: Drawer-based navigation |
| 5 | +--- |
| 6 | + |
| 7 | +Used to easily set up a screen with a drawer navigation. For a live example please see [our expo demo](https://exp.host/@react-navigation/NavigationPlayground). |
| 8 | + |
| 9 | +```js |
| 10 | +class MyHomeScreen extends React.Component { |
| 11 | + static navigationOptions = { |
| 12 | + drawerLabel: 'Home', |
| 13 | + drawerIcon: ({ tintColor }) => ( |
| 14 | + <Image |
| 15 | + source={require('./chats-icon.png')} |
| 16 | + style={[styles.icon, {tintColor: tintColor}]} |
| 17 | + /> |
| 18 | + ), |
| 19 | + }; |
| 20 | + |
| 21 | + render() { |
| 22 | + return ( |
| 23 | + <Button |
| 24 | + onPress={() => this.props.navigation.navigate('Notifications')} |
| 25 | + title="Go to notifications" |
| 26 | + /> |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class MyNotificationsScreen extends React.Component { |
| 32 | + static navigationOptions = { |
| 33 | + drawerLabel: 'Notifications', |
| 34 | + drawerIcon: ({ tintColor }) => ( |
| 35 | + <Image |
| 36 | + source={require('./notif-icon.png')} |
| 37 | + style={[styles.icon, {tintColor: tintColor}]} |
| 38 | + /> |
| 39 | + ), |
| 40 | + }; |
| 41 | + |
| 42 | + render() { |
| 43 | + return ( |
| 44 | + <Button |
| 45 | + onPress={() => this.props.navigation.goBack()} |
| 46 | + title="Go back home" |
| 47 | + /> |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const styles = StyleSheet.create({ |
| 53 | + icon: { |
| 54 | + width: 24, |
| 55 | + height: 24, |
| 56 | + }, |
| 57 | +}); |
| 58 | + |
| 59 | +const MyApp = DrawerNavigator({ |
| 60 | + Home: { |
| 61 | + screen: MyHomeScreen, |
| 62 | + }, |
| 63 | + Notifications: { |
| 64 | + screen: MyNotificationsScreen, |
| 65 | + }, |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +To open and close drawer, navigate to `'DrawerOpen'` and `'DrawerClose'` respectively. |
| 70 | + |
| 71 | +```js |
| 72 | +this.props.navigation.navigate('DrawerOpen'); // open drawer |
| 73 | +this.props.navigation.navigate('DrawerClose'); // close drawer |
| 74 | +``` |
| 75 | +If you would like to toggle the drawer you can navigate to `'DrawerToggle'`, and this will choose which navigation is appropriate for you given the drawers current state. |
| 76 | + |
| 77 | +```js |
| 78 | +// fires 'DrawerOpen'/'DrawerClose' accordingly |
| 79 | +this.props.navigation.navigate('DrawerToggle'); |
| 80 | +``` |
| 81 | + |
| 82 | +## API Definition |
| 83 | + |
| 84 | +```js |
| 85 | +DrawerNavigator(RouteConfigs, DrawerNavigatorConfig) |
| 86 | +``` |
| 87 | + |
| 88 | +### RouteConfigs |
| 89 | + |
| 90 | +The route configs object is a mapping from route name to a route config, which tells the navigator what to present for that route, see [example](/docs/api/navigators/StackNavigator.md#routeconfigs) from `StackNavigator`. |
| 91 | + |
| 92 | + |
| 93 | +### DrawerNavigatorConfig |
| 94 | +- `drawerWidth` - Width of the drawer or a function returning it. |
| 95 | +- `drawerPosition` - Options are `left` or `right`. Default is `left` position. |
| 96 | +- `contentComponent` - Component used to render the content of the drawer, for example, navigation items. Receives the `navigation` prop for the drawer. Defaults to `DrawerItems`. For more information, see below. |
| 97 | +- `contentOptions` - Configure the drawer content, see below. |
| 98 | +- `useNativeAnimations` - Enable native animations. Default is `true`. |
| 99 | +- `drawerBackgroundColor` - Use the Drawer background for some color. The Default is `white`. |
| 100 | + |
| 101 | +Several options get passed to the underlying router to modify navigation logic: |
| 102 | + |
| 103 | +- `initialRouteName` - The routeName for the initial route. |
| 104 | +- `order` - Array of routeNames which defines the order of the drawer items. |
| 105 | +- `paths` - Provide a mapping of routeName to path config, which overrides the paths set in the routeConfigs. |
| 106 | +- `backBehavior` - Should the back button cause switch to the initial route? If yes, set to `initialRoute`, otherwise `none`. Defaults to `initialRoute` behavior. |
| 107 | + |
| 108 | +### Providing a custom `contentComponent` |
| 109 | + |
| 110 | +The default component for the drawer is scrollable and only contains links for the routes in the RouteConfig. You can easily override the default component to add a header, footer, or other content to the drawer. By default the drawer is scrollable and supports iPhone X safe area. If you customize the content, be sure to wrap the content in a SafeAreaView: |
| 111 | + |
| 112 | +```js |
| 113 | +import { DrawerItems, SafeAreaView } from 'react-navigation'; |
| 114 | + |
| 115 | +const CustomDrawerContentComponent = (props) => ( |
| 116 | + <ScrollView> |
| 117 | + <SafeAreaView style={styles.container} forceInset={{ top: 'always', horizontal: 'never' }}> |
| 118 | + <DrawerItems {...props} /> |
| 119 | + </SafeAreaView> |
| 120 | + </ScrollView> |
| 121 | +); |
| 122 | + |
| 123 | +const styles = StyleSheet.create({ |
| 124 | + container: { |
| 125 | + flex: 1, |
| 126 | + }, |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +### `contentOptions` for `DrawerItems` |
| 131 | + |
| 132 | +- `items` - the array of routes, can be modified or overridden |
| 133 | +- `activeItemKey` - key identifying the active route |
| 134 | +- `activeTintColor` - label and icon color of the active label |
| 135 | +- `activeBackgroundColor` - background color of the active label |
| 136 | +- `inactiveTintColor` - label and icon color of the inactive label |
| 137 | +- `inactiveBackgroundColor` - background color of the inactive label |
| 138 | +- `onItemPress(route)` - function to be invoked when an item is pressed |
| 139 | +- `itemsContainerStyle` - style object for the content section |
| 140 | +- `itemStyle` - style object for the single item, which can contain an Icon and/or a Label |
| 141 | +- `labelStyle` - style object to overwrite `Text` style inside content section, when your label is a string |
| 142 | +- `iconContainerStyle` - style object to overwrite `View` icon container styles. |
| 143 | + |
| 144 | +#### Example: |
| 145 | + |
| 146 | +```js |
| 147 | +contentOptions: { |
| 148 | + activeTintColor: '#e91e63', |
| 149 | + itemsContainerStyle: { |
| 150 | + marginVertical: 0, |
| 151 | + }, |
| 152 | + iconContainerStyle: { |
| 153 | + opacity: 1 |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### Screen Navigation Options |
| 159 | + |
| 160 | +#### `title` |
| 161 | + |
| 162 | +Generic title that can be used as a fallback for `headerTitle` and `drawerLabel` |
| 163 | + |
| 164 | +#### `drawerLabel` |
| 165 | + |
| 166 | +String, React Element or a function that given `{ focused: boolean, tintColor: string }` returns a React.Node, to display in drawer sidebar. When undefined, scene `title` is used |
| 167 | + |
| 168 | +#### `drawerIcon` |
| 169 | + |
| 170 | +React Element or a function, that given `{ focused: boolean, tintColor: string }` returns a React.Node, to display in drawer sidebar |
| 171 | + |
| 172 | +#### `drawerLockMode` |
| 173 | + |
| 174 | +Specifies the [lock mode](https://facebook.github.io/react-native/docs/drawerlayoutandroid.html#drawerlockmode) of the drawer. This can also update dynamically by using screenProps.drawerLockMode on your top level router. |
| 175 | + |
| 176 | +### Navigator Props |
| 177 | + |
| 178 | +The navigator component created by `DrawerNavigator(...)` takes the following props: |
| 179 | + |
| 180 | +- `screenProps` - Pass down extra options to child screens, for example: |
| 181 | + |
| 182 | + |
| 183 | + ```jsx |
| 184 | + const DrawerNav = DrawerNavigator({ |
| 185 | + // config |
| 186 | + }); |
| 187 | + |
| 188 | + <DrawerNav |
| 189 | + screenProps={/* this prop will get passed to the screen components and nav options as props.screenProps */} |
| 190 | + /> |
| 191 | + ``` |
| 192 | + |
| 193 | + ### Nesting `DrawerNavigation` |
| 194 | + |
| 195 | +Please bear in mind that if you nest the DrawerNavigation, the drawer will show below the parent navigation. |
0 commit comments