Skip to content

Commit 5fcdc6c

Browse files
committed
Drawer
1 parent e9e88a5 commit 5fcdc6c

2 files changed

Lines changed: 199 additions & 96 deletions

File tree

packages/docs-gesture-handler/docs/components/reanimated-drawer-layout.mdx

Lines changed: 199 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -6,141 +6,240 @@ sidebar_label: Reanimated Drawer Layout
66

77
import useBaseUrl from '@docusaurus/useBaseUrl';
88

9-
Cross-platform replacement for the React Native's [DrawerLayoutAndroid](http://reactnative.dev/docs/drawerlayoutandroid.html) component.
10-
For detailed usage of standard parameters, please refer to the [React Native docs](http://reactnative.dev/docs/drawerlayoutandroid.html).
9+
:::info
10+
This component acts as a cross-platform replacement for React Native's [`DrawerLayoutAndroid`](http://reactnative.dev/docs/drawerlayoutandroid.html) component, written using [Reanimated](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/getting-started). For detailed information on standard parameters, please refer to the [React Native documentation](http://reactnative.dev/docs/drawerlayoutandroid.html).
11+
:::
1112

12-
### Usage:
13+
To use `ReanimatedDrawerLayout`, first ensure that Reanimated is installed and that your app is wrapped in `GestureHandlerRootView`. You can then import it as follows:
1314

14-
To use it, import it in the following way:
15-
16-
```js
15+
```ts
1716
import ReanimatedDrawerLayout from 'react-native-gesture-handler/ReanimatedDrawerLayout';
1817
```
1918

20-
## Properties:
19+
## Properties
20+
21+
### drawerType
2122

22-
### `drawerType`
23+
<CollapsibleCode
24+
label="Show composed types definitions"
25+
expandedLabel="Hide composed types definitions"
26+
lineBounds={[0, 1]}
27+
src={`
28+
drawerType?: DrawerType;
2329
24-
specifies the way the drawer will be displayed.
25-
Accepts values of the `DrawerPosition` enum. Defaults to `FRONT`.
30+
export enum DrawerType {
31+
FRONT,
32+
BACK,
33+
SLIDE,
34+
}
35+
`}/>
2636

27-
- `FRONT` the drawer will be displayed above the content view.
28-
- `BACK` the drawer will be displayed below the content view, revealed by sliding away the content view.
29-
- `SLIDE` the drawer will appear attached to the content view, opening it slides both the drawer and the content view.
37+
Specifies the way the drawer will be displayed.
38+
Accepts values of the `DrawerType` enum. Defaults to `FRONT`.
39+
40+
| Type | Description |
41+
| -- | -- |
42+
|`FRONT` | The drawer will be displayed above the content view. |
43+
|`BACK` | The drawer will be displayed below the content view, revealed by sliding away the content view. |
44+
|`SLIDE` | The drawer will appear attached to the content view, opening it slides both the drawer and the content view. |
3045

3146
| `FRONT` | `BACK` | `SLIDE` |
3247
| ----------------------------------------------------- | ---------------------------------------------------- | ----------------------------------------------------- |
3348
| <img src={useBaseUrl('gifs/new-drawer-front.gif')} /> | <img src={useBaseUrl('gifs/new-drawer-back.gif')} /> | <img src={useBaseUrl('gifs/new-drawer-slide.gif')} /> |
3449

35-
### `edgeWidth`
50+
### edgeWidth
51+
52+
```ts
53+
edgeWidth?: number;
54+
```
55+
56+
Width of the invisible, draggable area on the edge of the content view, which can be dragged to open the drawer.
57+
58+
### hideStatusBar
59+
60+
```ts
61+
hideStatusBar?: boolean;
62+
```
63+
64+
When set to `true`, drawer component will use [StatusBar API](http://reactnative.dev/docs/statusbar.html) to hide the OS status bar when the drawer is dragged or idle in the `open` position.
3665

37-
width of the invisible, draggable area on the edge of the content view, which can be dragged to open the drawer.
66+
### statusBarAnimation
3867

39-
### `hideStatusBar`
68+
<CollapsibleCode
69+
label="Show composed types definitions"
70+
expandedLabel="Hide composed types definitions"
71+
lineBounds={[0, 1]}
72+
collapsed={false}
73+
src={`
74+
statusBarAnimation?: StatusBarAnimation;
4075
41-
a boolean value. When set to `true`, drawer component will use [StatusBar API](http://reactnative.dev/docs/statusbar.html) to hide the OS status bar when the drawer is dragged or idle in the `open` position.
76+
export type StatusBarAnimation = 'none' | 'fade' | 'slide';
77+
`}/>
4278

43-
### `statusBarAnimation`
79+
May be used in combination with [`hideStatusBar`](#hidestatusbar) to select the animation used for hiding the status bar.
80+
See [StatusBar API](http://reactnative.dev/docs/statusbar.html#statusbaranimation) docs. Defaults to `slide`.
4481

45-
a string with possible values: `slide`, `none` or `fade`. Defaults to `slide`.
46-
May be used in combination with `hideStatusBar` to select the animation used for hiding the status bar.
47-
See [StatusBar API](http://reactnative.dev/docs/statusbar.html#statusbaranimation) docs.
82+
### overlayColor
4883

49-
### `overlayColor`
84+
```ts
85+
overlayColor?: string;
86+
```
5087

51-
color of the background overlay on top of the content window when the drawer is `open`.
88+
Color of the background overlay on top of the content window when the drawer is `open`.
5289
This color's opacity animates from 0% to 100% as the drawer transitions from closed to open. Defaults to `rgba(0, 0, 0, 0.7)`.
5390

54-
### `renderNavigationView`
91+
### renderNavigationView
5592

56-
a renderer function for the drawer component, provided with a `progress` parameter.
93+
```ts
94+
renderNavigationView: (
95+
progressAnimatedValue: SharedValue<number>
96+
) => ReactNode;
97+
```
98+
A renderer function for the drawer component is provided with a `progress` parameter called `progressAnimatedValue`, which is a [`SharedValue`](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#shared-value) indicating the progress of the drawer's opening or closing animation. This value is `0` when the drawer is fully closed and `1` when it is fully opened. The drawer component can use this value to animate its children during the opening or closing process. This function must return a [`ReactNode`](https://reactnative.dev/docs/react-node).
5799

58-
- `progress` - `SharedValue` that indicates the progress of drawer opening/closing animation.
59-
- equals `0` when the `drawer` is closed and `1` when the `drawer` is opened
60-
- can be used by the `drawer` component to animated its children while the `drawer` is opening or closing
100+
### onDrawerClose
61101

62-
### `onDrawerClose`
102+
```ts
103+
onDrawerClose?: () => void;
104+
```
63105

64-
a function which is called when the drawer has been closed.
106+
A function which is called when the drawer has been closed.
65107

66-
### `onDrawerOpen`
108+
### onDrawerOpen
67109

68-
a function which is called when the drawer has been opened.
110+
```ts
111+
onDrawerOpen?: () => void;
112+
```
69113

70-
### `onDrawerSlide`
114+
A function which is called when the drawer has been opened.
71115

72-
a function which is called when drawer is moving or animating, provided with a `progress` parameter.
116+
### onDrawerSlide
73117

74-
- `progress` - `SharedValue` that indicates the progress of drawer opening/closing animation.
75-
- equals `0` when the `drawer` is closed and `1` when the `drawer` is opened
76-
- can be used by the `drawer` component to animated its children while the `drawer` is opening or closing
118+
```ts
119+
onDrawerSlide?: (position: number) => void;
120+
```
77121

78-
### `onDrawerStateChanged`
122+
A function is called when the drawer is moving or animating, provided with a `position` parameter. This `position` value indicates the progress of the drawer's opening or closing animation. It equals `0` when the drawer is closed and `1` when the drawer is fully opened. This value can be utilized by the drawer component to animate its children as the drawer opens or closes.
79123

80-
a function which is called when the status of the drawer changes. It takes two arguments:
124+
### onDrawerStateChanged
81125

82-
- `newState` - interaction state of the drawer. It can be one of the following:
83-
- `DrawerState.IDLE`
84-
- `DrawerState.DRAGGING`
85-
- `DrawerState.SETTLING`
86-
- `drawerWillShow` - `true` when `drawer` started animating to `open` position, `false` otherwise.
126+
<CollapsibleCode
127+
label="Show composed types definitions"
128+
expandedLabel="Hide composed types definitions"
129+
lineBounds={[0, 4]}
130+
src={`
131+
onDrawerStateChanged?: (
132+
newState: DrawerState,
133+
drawerWillShow: boolean
134+
) => void;
87135
88-
### `enableTrackpadTwoFingerGesture` (iOS only)
136+
export enum DrawerState {
137+
IDLE,
138+
DRAGGING,
139+
SETTLING,
140+
}
141+
`}/>
89142

90-
enables two-finger gestures on supported devices, for example iPads with trackpads.
91-
If not enabled, the gesture will require click + drag, with `enableTrackpadTwoFingerGesture` swiping with two fingers will also trigger the gesture.
143+
A function is called when the status of the drawer changes, taking `newState` to represent the drawer's interaction state and `drawerWillShow`, which is `true` when the drawer starts animating towards the open position and `false` otherwise.
92144

93-
### `children`
145+
### enableTrackpadTwoFingerGesture (iOS only)
94146

95-
either a component that's rendered in the content view or a function.
96-
If `children` is a function, it is provided with a `progress` parameter.
147+
```ts
148+
enableTrackpadTwoFingerGesture?: boolean;
149+
```
97150

98-
- `progress` - `SharedValue` that indicates the progress of drawer opening/closing animation.
99-
- equals `0` when the `drawer` is closed and `1` when the `drawer` is opened
100-
- can be used by the `drawer` component to animated its children while the `drawer` is opening or closing
151+
Enables two-finger gestures on supported devices, for example iPads with trackpads. If not enabled the gesture will require click + drag, with `enableTrackpadTwoFingerGesture` swiping with two fingers will also trigger the gesture.
101152

102-
### `mouseButton(value: MouseButton)` (Web & Android only)
103153

104-
allows users to choose which mouse button should handler respond to.
105-
The enum `MouseButton` consists of the following predefined fields:
154+
### children
106155

107-
- `LEFT`
108-
- `RIGHT`
109-
- `MIDDLE`
110-
- `BUTTON_4`
111-
- `BUTTON_5`
112-
- `ALL`
156+
```ts
157+
children?: ReactNode | ((openValue?: SharedValue<number>) => ReactNode);
158+
```
113159

114-
Arguments can be combined using `|` operator, e.g. `mouseButton(MouseButton.LEFT | MouseButton.RIGHT)`. Defaults to `MouseButton.LEFT`.
160+
Either a component rendered in the content view or a function. If `children` is a function, it receives an `openValue` parameter - [`SharedValue`](https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/glossary#shared-value) that indicates the progress of the drawer's opening or closing animation. This value equals `0` when the drawer is closed and `1` when it is fully opened. The drawer component can use this value to animate its children during the opening or closing process. This function must return a [`ReactNode`](https://reactnative.dev/docs/react-node).
115161

116-
### `enableContextMenu(value: boolean)` (Web only)
162+
### mouseButton (Web & Android only)
163+
164+
<CollapsibleCode
165+
label="Show composed types definitions"
166+
expandedLabel="Hide composed types definitions"
167+
lineBounds={[0, 1]}
168+
src={`
169+
mouseButton: MouseButton | SharedValue<MouseButton>;
170+
171+
enum MouseButton {
172+
LEFT,
173+
RIGHT,
174+
MIDDLE,
175+
BUTTON_4,
176+
BUTTON_5,
177+
ALL,
178+
}
179+
`}/>
180+
181+
Allows users to choose which mouse button should handler respond to. Arguments can be combined using `|` operator, e.g. `mouseButton(MouseButton.LEFT | MouseButton.RIGHT)`. Default value is set to `MouseButton.LEFT`.
182+
183+
184+
### enableContextMenu (Web only)
185+
186+
```ts
187+
enableContextMenu: boolean;
188+
```
189+
190+
Specifies whether context menu should be enabled after clicking on underlying view with right mouse button. Default value is set to `false` if [`MouseButton.RIGHT`](#mousebutton-web--android-only) is specified.
117191

118-
specifies whether context menu should be enabled after clicking on underlying view with right mouse button. Defaults to `false`.
119192

120193
## Methods
121194

122-
### `openDrawer(options)`
195+
Using a reference to `ReanimatedDrawerLayout` allows you to manually trigger the opening and closing of the component.
123196

124-
`openDrawer` accepts an optional `options` parameter, which is an object with the following optional properties:
197+
```ts
198+
const drawerRef = useRef<DrawerLayoutMethods>(null);
199+
```
125200

126-
- `initialVelocity` - the initial velocity of the object attached to the spring. Defaults to `0`.
127-
- `animationSpeed` - controls speed of the animation. Defaults to `1`.
201+
Both methods accept an optional `options` parameter, which allows you to customize the animation of the drawer movement.
128202

129-
### `closeDrawer(options)`
203+
```ts
204+
export type DrawerMovementOption = {
205+
initialVelocity?: number;
206+
animationSpeed?: number;
207+
};
208+
```
209+
210+
### openDrawer
130211

131-
`closeDrawer` accepts an optional `options` parameter, which is an object with the following optional properties:
212+
```ts
213+
openDrawer: (options?: DrawerMovementOption) => void;
214+
```
132215

133-
- `initialVelocity` - initial velocity of the object attached to the spring. Defaults to `0`.
134-
- `animationSpeed` - controls speed of the animation. Defaults to `1`.
216+
Allows to manually open the drawer.
135217

136-
## Example:
218+
### closeDrawer
219+
220+
```ts
221+
closeDrawer: (options?: DrawerMovementOption) => void;
222+
```
137223

138-
See the [reanimated drawer layout example](https://github.com/software-mansion/react-native-gesture-handler/blob/main/apps/common-app/src/release_tests/reanimatedDrawerLayout/index.tsx) from GestureHandler example app.
224+
Allows to manually close the drawer.
139225

140-
```js
226+
## Example
227+
228+
Example of a `ReanimatedDrawerLayout` component can be found in [Gesture Handler repository](https://github.com/software-mansion/react-native-gesture-handler/blob/main/apps/common-app/src/new_api/components/drawer/index.tsx).
229+
230+
231+
<CollapsibleCode
232+
label="Show full example"
233+
expandedLabel="Hide full example"
234+
lineBounds={[14, 49]}
235+
src={`
141236
import React, { useRef } from 'react';
142237
import { StyleSheet, Text, View } from 'react-native';
143-
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
238+
import {
239+
GestureDetector,
240+
GestureHandlerRootView,
241+
useTapGesture,
242+
} from 'react-native-gesture-handler';
144243
145244
import ReanimatedDrawerLayout, {
146245
DrawerType,
@@ -157,25 +256,30 @@ const DrawerPage = () => {
157256
};
158257
159258
export default function ReanimatedDrawerExample() {
160-
const drawerRef = useRef < DrawerLayoutMethods > null;
161-
const tapGesture = Gesture.Tap()
162-
.runOnJS(true)
163-
.onStart(() => drawerRef.current?.openDrawer());
259+
const drawerRef = useRef<DrawerLayoutMethods>(null);
260+
const tapGesture = useTapGesture({
261+
onDeactivate: () => {
262+
drawerRef.current?.openDrawer();
263+
},
264+
runOnJS: true,
265+
});
164266
165267
return (
166-
<ReanimatedDrawerLayout
167-
ref={drawerRef}
168-
renderNavigationView={() => <DrawerPage />}
169-
drawerPosition={DrawerPosition.LEFT}
170-
drawerType={DrawerType.FRONT}>
171-
<View style={styles.innerContainer}>
172-
<GestureDetector gesture={tapGesture}>
173-
<View style={styles.box}>
174-
<Text>Open drawer</Text>
175-
</View>
176-
</GestureDetector>
177-
</View>
178-
</ReanimatedDrawerLayout>
268+
<GestureHandlerRootView>
269+
<ReanimatedDrawerLayout
270+
ref={drawerRef}
271+
renderNavigationView={() => <DrawerPage />}
272+
drawerPosition={DrawerPosition.LEFT}
273+
drawerType={DrawerType.FRONT}>
274+
<View style={styles.innerContainer}>
275+
<GestureDetector gesture={tapGesture}>
276+
<View style={styles.box}>
277+
<Text>Open drawer</Text>
278+
</View>
279+
</GestureDetector>
280+
</View>
281+
</ReanimatedDrawerLayout>
282+
</GestureHandlerRootView>
179283
);
180284
}
181285
@@ -198,4 +302,4 @@ const styles = StyleSheet.create({
198302
backgroundColor: 'pink',
199303
},
200304
});
201-
```
305+
`}/>

packages/docs-gesture-handler/docs/components/reanimated_swipeable.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ Enables two-finger gestures on supported devices, for example iPads with trackpa
254254
label="Show composed types definitions"
255255
expandedLabel="Hide composed types definitions"
256256
lineBounds={[0, 1]}
257-
collapsed={false}
258257
src={`
259258
mouseButton: MouseButton | SharedValue<MouseButton>;
260259

0 commit comments

Comments
 (0)