Skip to content

Commit 3e21b6c

Browse files
authored
Add animationSpeed to ReanimatedDrawerLayout (#3401)
## Description This PR adds `animationSpeed` to `ReanimatedDrawerLayout`. I was conflicted between adding `animationSpeed: number`, `animationOptions: DrawerMovementOption`, and `animationConfig: Record<string, unknown>`. - `animationConfig` is the most flexible one, but `ReanimatedDrawerLayout` already opinionates animation configuration, and doesn't allow for any configuration beyond `DrawerMovementOption` - `DrawerMovementOption` is already in place, but we don't need `initialVelocity` value for the default animation. - since we only need animation speed, I think `animationSpeed: number` makes the most sense Suggested by #3392 Closes #3392 ## Test plan - see how both opening and dismissal animations are modified: <details> <summary> Collapsed repro </summary> ```tsx import React, { useRef } from 'react'; import { StyleSheet, Text, View } from 'react-native'; import { Gesture, GestureDetector, GestureHandlerRootView, } from 'react-native-gesture-handler'; import ReanimatedDrawerLayout, { DrawerType, DrawerPosition, DrawerLayoutMethods, DrawerLockMode, } from 'react-native-gesture-handler/ReanimatedDrawerLayout'; const DrawerPage = () => { return ( <View style={styles.drawerContainer}> <Text>Lorem ipsum</Text> </View> ); }; export default function ReanimatedDrawerExample() { const drawerRef = useRef<DrawerLayoutMethods>(null); const tapGesture = Gesture.Tap() .runOnJS(true) .onStart(() => drawerRef.current?.openDrawer({ animationSpeed: 5000 })); return ( <GestureHandlerRootView> <ReanimatedDrawerLayout ref={drawerRef} animationSpeed={5000} renderNavigationView={() => <DrawerPage />} drawerLockMode={DrawerLockMode.UNLOCKED} drawerPosition={DrawerPosition.LEFT} drawerType={DrawerType.FRONT}> <View style={styles.innerContainer}> <GestureDetector gesture={tapGesture}> <View style={styles.box}> <Text>Open drawer</Text> </View> </GestureDetector> </View> </ReanimatedDrawerLayout> </GestureHandlerRootView> ); } const styles = StyleSheet.create({ drawerContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'pink', }, innerContainer: { flex: 1, backgroundColor: 'white', alignItems: 'center', justifyContent: 'center', gap: 20, }, box: { padding: 20, backgroundColor: 'pink', }, }); ``` </details>
1 parent bb248de commit 3e21b6c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/components/ReanimatedDrawerLayout.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ export interface DrawerLayoutProps {
143143
*/
144144
drawerType?: DrawerType;
145145

146+
/**
147+
* Speed of animation that will play when letting go, or dismissing the drawer.
148+
* This will also be the default animation speed for programatic controlls.
149+
*/
150+
animationSpeed?: number;
151+
146152
/**
147153
* Defines how far from the edge of the content view the gesture should
148154
* activate.
@@ -292,6 +298,7 @@ const DrawerLayout = forwardRef<DrawerLayoutMethods, DrawerLayoutProps>(
292298
onDrawerClose,
293299
onDrawerOpen,
294300
onDrawerStateChanged,
301+
animationSpeed: animationSpeedProp,
295302
} = props;
296303

297304
const isFromLeft = drawerPosition === DrawerPosition.LEFT;
@@ -392,7 +399,9 @@ const DrawerLayout = forwardRef<DrawerLayoutMethods, DrawerLayoutProps>(
392399
{
393400
overshootClamping: true,
394401
velocity: normalizedInitialVelocity,
395-
mass: animationSpeed ? 1 / animationSpeed : 1,
402+
mass: animationSpeed
403+
? 1 / animationSpeed
404+
: 1 / (animationSpeedProp ?? 1),
396405
damping: 40,
397406
stiffness: 500,
398407
},

0 commit comments

Comments
 (0)