Skip to content

Commit 8636893

Browse files
committed
Document useDrawerProgress
1 parent 9fe0d5a commit 8636893

File tree

1 file changed

+127
-38
lines changed

1 file changed

+127
-38
lines changed

versioned_docs/version-7.x/drawer-navigator.md

+127-38
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,6 @@ When this is set to `open`, the drawer will be open from the initial render. It
220220

221221
Boolean used to indicate whether inactive screens should be detached from the view hierarchy to save memory. This enables integration with [react-native-screens](https://github.com/software-mansion/react-native-screens). Defaults to `true`.
222222

223-
#### `useLegacyImplementation`
224-
225-
Whether to use the legacy implementation based on Reanimated 1. The new implementation based on Reanimated 2 will perform better, but you need additional configuration and need to use Hermes with Flipper to debug.
226-
227-
This defaults to `true` in the following cases:
228-
229-
- Reanimated 2 is not configured
230-
- App is connected to Chrome debugger (Reanimated 2 cannot be used with Chrome debugger)
231-
- App is running on Web
232-
233-
Otherwise, it defaults to `false`
234-
235223
#### `drawerContent`
236224

237225
Function that returns React element to render as the content of the drawer, for example, navigation items
@@ -294,30 +282,6 @@ The `DrawerItem` component accepts the following props:
294282
- `labelStyle`: Style object for the label `Text`.
295283
- `style`: Style object for the wrapper `View`.
296284

297-
The `progress` object can be used to do interesting animations in your `drawerContent`, such as parallax motion of the drawer contents:
298-
299-
<samp id="animated-drawer-content" />
300-
301-
```js
302-
function CustomDrawerContent(props) {
303-
const progress = useDrawerProgress();
304-
305-
// If you are on react-native-reanimated 1.x, use `Animated.interpolate` instead of `Animated.interpolateNode`
306-
const translateX = Animated.interpolateNode(progress, {
307-
inputRange: [0, 1],
308-
outputRange: [-100, 0],
309-
});
310-
311-
return (
312-
<Animated.View style={{ transform: [{ translateX }] }}>
313-
{/* ... drawer contents */}
314-
</Animated.View>
315-
);
316-
}
317-
```
318-
319-
The `progress` object is a Reanimated `Node` if you're using Reanimated 1 (see [`useLegacyImplementation`](#uselegacyimplementation)), otherwise a `SharedValue`. It represents the animated position of the drawer (0 is closed; 1 is open).
320-
321285
Note that you **cannot** use the `useNavigation` hook inside the `drawerContent` since `useNavigation` is only available inside screens. You get a `navigation` prop for your `drawerContent` which you can use instead:
322286

323287
```js
@@ -671,7 +635,132 @@ Navigates to an existing screen in the drawer navigator. The method accepts the
671635
navigation.jumpTo('Profile', { owner: 'Satya' });
672636
```
673637

674-
## Checking if the drawer is open
638+
### Hooks
639+
640+
The drawer navigator exports the following hooks:
641+
642+
#### `useDrawerProgress`
643+
644+
This hook returns the progress of the drawer. It is available in the screen components rendered by the drawer navigator as well as in the [custom drawer content](#drawercontent).
645+
646+
The `progress` object is a `SharedValue` that represents the animated position of the drawer (`0` is closed; `1` is open). It can be used to animate elements based on the animation of the drawer with [Reanimated](https://docs.swmansion.com/react-native-reanimated/):
647+
648+
<Tabs groupId="config" queryString="config">
649+
<TabItem value="static" label="Static" default>
650+
651+
```js name="Drawer animation progress" snack version=7
652+
import * as React from 'react';
653+
import { Text, View, Button } from 'react-native';
654+
import { createStaticNavigation } from '@react-navigation/native';
655+
// codeblock-focus-start
656+
import {
657+
createDrawerNavigator,
658+
useDrawerProgress,
659+
} from '@react-navigation/drawer';
660+
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
661+
662+
function HomeScreen() {
663+
// highlight-next-line
664+
const progress = useDrawerProgress();
665+
666+
const animatedStyle = useAnimatedStyle(() => ({
667+
transform: [{ translateX: progress.value * -100 }],
668+
}));
669+
670+
return (
671+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
672+
<Animated.View
673+
style={[
674+
{
675+
height: 100,
676+
aspectRatio: 1,
677+
backgroundColor: 'tomato',
678+
},
679+
animatedStyle,
680+
]}
681+
/>
682+
</View>
683+
);
684+
}
685+
// codeblock-focus-end
686+
687+
const MyDrawer = createDrawerNavigator({
688+
screens: {
689+
Home: HomeScreen,
690+
},
691+
});
692+
693+
const Navigation = createStaticNavigation(MyDrawer);
694+
695+
export default function App() {
696+
return <Navigation />;
697+
}
698+
```
699+
700+
</TabItem>
701+
<TabItem value="dynamic" label="Dynamic">
702+
703+
```js name="Drawer animation progress" snack version=7
704+
import * as React from 'react';
705+
import { Text, View, Button } from 'react-native';
706+
import { NavigationContainer } from '@react-navigation/native';
707+
// codeblock-focus-start
708+
import {
709+
createDrawerNavigator,
710+
useDrawerProgress,
711+
} from '@react-navigation/drawer';
712+
import Animated, { useAnimatedStyle } from 'react-native-reanimated';
713+
714+
function HomeScreen() {
715+
// highlight-next-line
716+
const progress = useDrawerProgress();
717+
718+
const animatedStyle = useAnimatedStyle(() => ({
719+
transform: [{ translateX: progress.value * -100 }],
720+
}));
721+
722+
return (
723+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
724+
<Animated.View
725+
style={[
726+
{
727+
height: 100,
728+
aspectRatio: 1,
729+
backgroundColor: 'tomato',
730+
},
731+
animatedStyle,
732+
]}
733+
/>
734+
</View>
735+
);
736+
}
737+
// codeblock-focus-end
738+
739+
const Drawer = createDrawerNavigator();
740+
741+
function MyDrawer() {
742+
return (
743+
<Drawer.Navigator>
744+
<Drawer.Screen name="Home" component={HomeScreen} />
745+
</Drawer.Navigator>
746+
);
747+
}
748+
749+
export default function App() {
750+
return (
751+
<NavigationContainer>
752+
<MyDrawer />
753+
</NavigationContainer>
754+
);
755+
}
756+
```
757+
758+
</TabItem>
759+
</Tabs>
760+
761+
This hook is not supported on Web.
762+
763+
#### `useDrawerStatus`
675764

676765
You can check if the drawer is open by using the `useDrawerStatus` hook.
677766

@@ -693,7 +782,7 @@ import { getDrawerStatusFromState } from '@react-navigation/drawer';
693782
const isDrawerOpen = getDrawerStatusFromState(navigation.getState()) === 'open';
694783
```
695784

696-
For class components, you can listen to the `state` event to check if drawer was opened or closed:
785+
For class components, you can listen to the `state` event to check if the drawer was opened or closed:
697786

698787
```js
699788
class Profile extends React.Component {

0 commit comments

Comments
 (0)