Skip to content

Commit 84ab2e8

Browse files
jeremywhojgonet
authored andcommitted
Add optional speed parameter to open/closeDrawer (software-mansion#1133)
## Description I want to control the speed with which the drawer opens/closes. Velocity does not seem to provide this functionality. Adjusting the Animation.spring `speed` parameter does. From the RN Animation docs here: https://reactnative.dev/docs/animated#spring > speed: Controls speed of the animation. Default 12. > ... > velocity: The initial velocity of the object attached to the spring. Default 0 (object is at rest). ## Test plan I manually tested the change my using different values for speed and comparing the results on device. Co-authored-by: Jakub <[email protected]>
1 parent 85af9ea commit 84ab2e8

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

DrawerLayout.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ declare module 'react-native-gesture-handler/DrawerLayout' {
4141

4242
interface DrawerMovementOptionType {
4343
velocity?: number;
44+
speed?: number;
4445
}
4546

4647
export default class DrawerLayout extends React.Component<DrawerLayoutProperties> {

DrawerLayout.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type EventType = {
6767

6868
export type DrawerMovementOptionType = {
6969
velocity?: number,
70+
speed?: number,
7071
};
7172

7273
export default class DrawerLayout extends Component<PropType, StateType> {
@@ -314,7 +315,12 @@ export default class DrawerLayout extends Component<PropType, StateType> {
314315
});
315316
};
316317

317-
_animateDrawer = (fromValue: ?number, toValue: number, velocity: number) => {
318+
_animateDrawer = (
319+
fromValue: ?number,
320+
toValue: number,
321+
velocity: number,
322+
speed: ?number
323+
) => {
318324
this.state.dragX.setValue(0);
319325
this.state.touchX.setValue(
320326
this.props.drawerPosition === 'left' ? 0 : this.state.containerWidth
@@ -347,6 +353,7 @@ export default class DrawerLayout extends Component<PropType, StateType> {
347353
bounciness: 0,
348354
toValue,
349355
useNativeDriver: this.props.useNativeAnimations,
356+
speed: speed ?? undefined,
350357
}).start(({ finished }) => {
351358
if (finished) {
352359
this._emitStateChanged(IDLE, willShow);

Example/horizontalDrawer/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default class Example extends Component {
124124
flipSide={() => this.setState({ fromLeft: !this.state.fromLeft })}
125125
nextType={() =>
126126
this.setState({ type: (this.state.type + 1) % TYPES.length })}
127-
openDrawer={() => this.drawer.openDrawer()}
127+
openDrawer={() => this.drawer.openDrawer({speed: 14})}
128128
/>
129129
</DrawerLayout>
130130
</View>

docs/docs/component-drawer-layout.mdx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar_label: DrawerLayout
55
---
66

77
import useBaseUrl from '@docusaurus/useBaseUrl';
8-
import GifGallery from '@site/components/GifGallery'
8+
import GifGallery from '@site/components/GifGallery';
99

1010
This is a cross-platform replacement for React Native's [DrawerLayoutAndroid](http://facebook.github.io/react-native/docs/drawerlayoutandroid.html) component. It provides a compatible API but allows for the component to be used on both Android and iOS. Please refer to [React Native docs](http://facebook.github.io/react-native/docs/drawerlayoutandroid.html) for the detailed usage for standard parameters.
1111

@@ -26,18 +26,21 @@ On top of the standard list of parameters DrawerLayout has an additional set of
2626
possible values are: `front`, `back` or `slide` (default is `front`). It specifies the way the drawer will be displayed. When set to `front` the drawer will slide in and out along with the gesture and will display on top of the content view. When `back` is used the drawer displays behind the content view and can be revealed with gesture of pulling the content view to the side. Finally `slide` option makes the drawer appear like it is attached to the side of the content view; when you pull both content view and drawer will follow the gesture.
2727

2828
Type `slide`:
29+
2930
<GifGallery>
30-
<img src={useBaseUrl("gifs/drawer-slide.gif")} width="280" />
31+
<img src={useBaseUrl('gifs/drawer-slide.gif')} width="280" />
3132
</GifGallery>
3233

3334
Type `front`:
35+
3436
<GifGallery>
35-
<img src={useBaseUrl("gifs/drawer-front.gif")} width="280" />
37+
<img src={useBaseUrl('gifs/drawer-front.gif')} width="280" />
3638
</GifGallery>
3739

3840
Type `back`:
41+
3942
<GifGallery>
40-
<img src={useBaseUrl("gifs/drawer-back.gif")} width="280" />
43+
<img src={useBaseUrl('gifs/drawer-back.gif')} width="280" />
4144
</GifGallery>
4245

4346
### `edgeWidth`
@@ -80,6 +83,27 @@ Enables two-finger gestures on supported devices, for example iPads with trackpa
8083

8184
component or function. Children is a component which is rendered by default and is wrapped by drawer. However, it could be also a render function which takes an Animated value as a parameter that indicates the progress of drawer opening/closing animation (progress value is 0 when closed and 1 when opened) is the same way like `renderNavigationView` prop.
8285

86+
## Methods
87+
88+
89+
### `openDrawer(options)`
90+
91+
`openDrawer` can take an optional `options` parameter which is an object, enabling further customization of the open animation.
92+
93+
`options` has two optional properties:
94+
95+
`velocity`: number, the initial velocity of the object attached to the spring. Default 0 (object is at rest).
96+
`speed`: number, controls speed of the animation. Default 12.
97+
98+
### `closeDrawer(options)`
99+
100+
`closeDrawer` can take an optional `options` parameter which is an object, enabling further customization of the close animation.
101+
102+
`options` has two optional properties:
103+
104+
`velocity`: number, the initial velocity of the object attached to the spring. Default 0 (object is at rest).
105+
`speed`: number, controls speed of the animation. Default 12.
106+
83107
## Example:
84108

85109
See the [drawer example](https://github.com/software-mansion/react-native-gesture-handler/blob/master/Example/horizontalDrawer/index.js) from [GestureHandler Example App](example.md) or view it directly on your phone by visiting [our expo demo](https://snack.expo.io/@adamgrzybowski/react-native-gesture-handler-demo).

0 commit comments

Comments
 (0)