react-native-drawer-layout 2.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -35
  3. package/lib/commonjs/constants.js +15 -0
  4. package/lib/commonjs/constants.js.map +1 -0
  5. package/lib/commonjs/index.js +35 -0
  6. package/lib/commonjs/index.js.map +1 -0
  7. package/lib/commonjs/types.js +6 -0
  8. package/lib/commonjs/types.js.map +1 -0
  9. package/lib/commonjs/utils/DrawerGestureContext.js +12 -0
  10. package/lib/commonjs/utils/DrawerGestureContext.js.map +1 -0
  11. package/lib/commonjs/utils/DrawerProgressContext.js +12 -0
  12. package/lib/commonjs/utils/DrawerProgressContext.js.map +1 -0
  13. package/lib/commonjs/utils/useDrawerProgress.js +19 -0
  14. package/lib/commonjs/utils/useDrawerProgress.js.map +1 -0
  15. package/lib/commonjs/views/Drawer.js +89 -0
  16. package/lib/commonjs/views/Drawer.js.map +1 -0
  17. package/lib/commonjs/views/GestureHandler.android.js +17 -0
  18. package/lib/commonjs/views/GestureHandler.android.js.map +1 -0
  19. package/lib/commonjs/views/GestureHandler.ios.js +17 -0
  20. package/lib/commonjs/views/GestureHandler.ios.js.map +1 -0
  21. package/lib/commonjs/views/GestureHandler.js +33 -0
  22. package/lib/commonjs/views/GestureHandler.js.map +1 -0
  23. package/lib/commonjs/views/GestureHandlerNative.js +37 -0
  24. package/lib/commonjs/views/GestureHandlerNative.js.map +1 -0
  25. package/lib/commonjs/views/legacy/Drawer.js +419 -0
  26. package/lib/commonjs/views/legacy/Drawer.js.map +1 -0
  27. package/lib/commonjs/views/legacy/Overlay.js +74 -0
  28. package/lib/commonjs/views/legacy/Overlay.js.map +1 -0
  29. package/lib/commonjs/views/modern/Drawer.js +285 -0
  30. package/lib/commonjs/views/modern/Drawer.js.map +1 -0
  31. package/lib/commonjs/views/modern/Overlay.js +69 -0
  32. package/lib/commonjs/views/modern/Overlay.js.map +1 -0
  33. package/lib/module/constants.js +5 -0
  34. package/lib/module/constants.js.map +1 -0
  35. package/lib/module/index.js +5 -0
  36. package/lib/module/index.js.map +1 -0
  37. package/lib/module/types.js +2 -0
  38. package/lib/module/types.js.map +1 -0
  39. package/lib/module/utils/DrawerGestureContext.js +3 -0
  40. package/lib/module/utils/DrawerGestureContext.js.map +1 -0
  41. package/lib/module/utils/DrawerProgressContext.js +3 -0
  42. package/lib/module/utils/DrawerProgressContext.js.map +1 -0
  43. package/lib/module/utils/useDrawerProgress.js +10 -0
  44. package/lib/module/utils/useDrawerProgress.js.map +1 -0
  45. package/lib/module/views/Drawer.js +81 -0
  46. package/lib/module/views/Drawer.js.map +1 -0
  47. package/lib/module/views/GestureHandler.android.js +2 -0
  48. package/lib/module/views/GestureHandler.android.js.map +1 -0
  49. package/lib/module/views/GestureHandler.ios.js +2 -0
  50. package/lib/module/views/GestureHandler.ios.js.map +1 -0
  51. package/lib/module/views/GestureHandler.js +21 -0
  52. package/lib/module/views/GestureHandler.js.map +1 -0
  53. package/lib/module/views/GestureHandlerNative.js +11 -0
  54. package/lib/module/views/GestureHandlerNative.js.map +1 -0
  55. package/lib/module/views/legacy/Drawer.js +409 -0
  56. package/lib/module/views/legacy/Drawer.js.map +1 -0
  57. package/lib/module/views/legacy/Overlay.js +64 -0
  58. package/lib/module/views/legacy/Overlay.js.map +1 -0
  59. package/lib/module/views/modern/Drawer.js +276 -0
  60. package/lib/module/views/modern/Drawer.js.map +1 -0
  61. package/lib/module/views/modern/Overlay.js +60 -0
  62. package/lib/module/views/modern/Overlay.js.map +1 -0
  63. package/lib/typescript/src/constants.d.ts +5 -0
  64. package/lib/typescript/src/constants.d.ts.map +1 -0
  65. package/lib/typescript/src/index.d.ts +5 -0
  66. package/lib/typescript/src/index.d.ts.map +1 -0
  67. package/lib/typescript/src/types.d.ts +110 -0
  68. package/lib/typescript/src/types.d.ts.map +1 -0
  69. package/lib/typescript/src/utils/DrawerGestureContext.d.ts +4 -0
  70. package/lib/typescript/src/utils/DrawerGestureContext.d.ts.map +1 -0
  71. package/lib/typescript/src/utils/DrawerProgressContext.d.ts +5 -0
  72. package/lib/typescript/src/utils/DrawerProgressContext.d.ts.map +1 -0
  73. package/lib/typescript/src/utils/useDrawerProgress.d.ts +3 -0
  74. package/lib/typescript/src/utils/useDrawerProgress.d.ts.map +1 -0
  75. package/lib/typescript/src/views/Drawer.d.ts +20 -0
  76. package/lib/typescript/src/views/Drawer.d.ts.map +1 -0
  77. package/lib/typescript/src/views/GestureHandler.android.d.ts +2 -0
  78. package/lib/typescript/src/views/GestureHandler.android.d.ts.map +1 -0
  79. package/lib/typescript/src/views/GestureHandler.d.ts +15 -0
  80. package/lib/typescript/src/views/GestureHandler.d.ts.map +1 -0
  81. package/lib/typescript/src/views/GestureHandler.ios.d.ts +2 -0
  82. package/lib/typescript/src/views/GestureHandler.ios.d.ts.map +1 -0
  83. package/lib/typescript/src/views/GestureHandlerNative.d.ts +6 -0
  84. package/lib/typescript/src/views/GestureHandlerNative.d.ts.map +1 -0
  85. package/lib/typescript/src/views/legacy/Drawer.d.ts +51 -0
  86. package/lib/typescript/src/views/legacy/Drawer.d.ts.map +1 -0
  87. package/lib/typescript/src/views/legacy/Overlay.d.ts +89 -0
  88. package/lib/typescript/src/views/legacy/Overlay.d.ts.map +1 -0
  89. package/lib/typescript/src/views/modern/Drawer.d.ts +10 -0
  90. package/lib/typescript/src/views/modern/Drawer.d.ts.map +1 -0
  91. package/lib/typescript/src/views/modern/Overlay.d.ts +89 -0
  92. package/lib/typescript/src/views/modern/Overlay.d.ts.map +1 -0
  93. package/package.json +54 -94
  94. package/src/constants.tsx +4 -0
  95. package/src/index.tsx +4 -0
  96. package/src/types.tsx +122 -0
  97. package/src/utils/DrawerGestureContext.tsx +3 -0
  98. package/src/utils/DrawerProgressContext.tsx +6 -0
  99. package/src/utils/useDrawerProgress.tsx +18 -0
  100. package/src/views/Drawer.tsx +122 -0
  101. package/src/views/GestureHandler.android.tsx +1 -0
  102. package/src/views/GestureHandler.ios.tsx +1 -0
  103. package/src/views/GestureHandler.tsx +29 -0
  104. package/src/views/GestureHandlerNative.tsx +24 -0
  105. package/src/views/legacy/Drawer.tsx +682 -0
  106. package/src/views/legacy/Overlay.tsx +87 -0
  107. package/src/views/modern/Drawer.tsx +413 -0
  108. package/src/views/modern/Overlay.tsx +82 -0
  109. package/dist/DrawerLayout.js +0 -430
@@ -0,0 +1,87 @@
1
+ import * as React from 'react';
2
+ import { Platform, Pressable, StyleSheet } from 'react-native';
3
+ import Animated from 'react-native-reanimated';
4
+
5
+ const {
6
+ // @ts-expect-error: this is to support reanimated 1
7
+ interpolate: interpolateDeprecated,
8
+ interpolateNode,
9
+ cond,
10
+ greaterThan,
11
+ } = Animated;
12
+
13
+ const interpolate: typeof interpolateNode =
14
+ interpolateNode ?? interpolateDeprecated;
15
+
16
+ const PROGRESS_EPSILON = 0.05;
17
+
18
+ type Props = React.ComponentProps<typeof Animated.View> & {
19
+ progress: Animated.Node<number>;
20
+ onPress: () => void;
21
+ accessibilityLabel?: string;
22
+ };
23
+
24
+ const Overlay = React.forwardRef(function Overlay(
25
+ {
26
+ progress,
27
+ onPress,
28
+ style,
29
+ accessibilityLabel = 'Close drawer',
30
+ ...props
31
+ }: Props,
32
+ ref: React.Ref<Animated.View>
33
+ ) {
34
+ const animatedStyle = {
35
+ opacity: interpolate(progress, {
36
+ // Default input range is [PROGRESS_EPSILON, 1]
37
+ // On Windows, the output value is 1 when input value is out of range for some reason
38
+ // The default value 0 will be interpolated to 1 in this case, which is not what we want.
39
+ // Therefore changing input range on Windows to [0,1] instead.
40
+ inputRange:
41
+ Platform.OS === 'windows' || Platform.OS === 'macos'
42
+ ? [0, 1]
43
+ : [PROGRESS_EPSILON, 1],
44
+ outputRange: [0, 1],
45
+ }),
46
+ // We don't want the user to be able to press through the overlay when drawer is open
47
+ // One approach is to adjust the pointerEvents based on the progress
48
+ // But we can also send the overlay behind the screen, which works, and is much less code
49
+ zIndex: cond(greaterThan(progress, PROGRESS_EPSILON), 0, -1),
50
+ };
51
+
52
+ return (
53
+ <Animated.View
54
+ {...props}
55
+ ref={ref}
56
+ style={[styles.overlay, overlayStyle, animatedStyle, style]}
57
+ >
58
+ <Pressable
59
+ onPress={onPress}
60
+ style={styles.pressable}
61
+ accessibilityRole="button"
62
+ accessibilityLabel={accessibilityLabel}
63
+ />
64
+ </Animated.View>
65
+ );
66
+ });
67
+
68
+ const overlayStyle = Platform.select<Record<string, string>>({
69
+ web: {
70
+ // Disable touch highlight on mobile Safari.
71
+ // WebkitTapHighlightColor must be used outside of StyleSheet.create because react-native-web will omit the property.
72
+ WebkitTapHighlightColor: 'transparent',
73
+ },
74
+ default: {},
75
+ });
76
+
77
+ const styles = StyleSheet.create({
78
+ overlay: {
79
+ ...StyleSheet.absoluteFillObject,
80
+ backgroundColor: 'rgba(0, 0, 0, 0.5)',
81
+ },
82
+ pressable: {
83
+ flex: 1,
84
+ },
85
+ });
86
+
87
+ export default Overlay;
@@ -0,0 +1,413 @@
1
+ import * as React from 'react';
2
+ import {
3
+ I18nManager,
4
+ InteractionManager,
5
+ Keyboard,
6
+ Platform,
7
+ StatusBar,
8
+ StyleSheet,
9
+ View,
10
+ } from 'react-native';
11
+ import Animated, {
12
+ interpolate,
13
+ runOnJS,
14
+ useAnimatedGestureHandler,
15
+ useAnimatedStyle,
16
+ useDerivedValue,
17
+ useSharedValue,
18
+ withSpring,
19
+ } from 'react-native-reanimated';
20
+
21
+ import {
22
+ DEFAULT_DRAWER_WIDTH,
23
+ SWIPE_MIN_DISTANCE,
24
+ SWIPE_MIN_OFFSET,
25
+ SWIPE_MIN_VELOCITY,
26
+ } from '../../constants';
27
+ import type { DrawerProps } from '../../types';
28
+ import DrawerProgressContext from '../../utils/DrawerProgressContext';
29
+ import {
30
+ GestureState,
31
+ PanGestureHandler,
32
+ PanGestureHandlerGestureEvent,
33
+ } from '../GestureHandler';
34
+ import Overlay from './Overlay';
35
+
36
+ const minmax = (value: number, start: number, end: number) => {
37
+ 'worklet';
38
+
39
+ return Math.min(Math.max(value, start), end);
40
+ };
41
+
42
+ type Props = DrawerProps & {
43
+ layout: { width: number };
44
+ };
45
+
46
+ export default function Drawer({
47
+ layout,
48
+ drawerPosition,
49
+ drawerStyle,
50
+ drawerType,
51
+ gestureHandlerProps,
52
+ hideStatusBarOnOpen,
53
+ keyboardDismissMode,
54
+ onClose,
55
+ onOpen,
56
+ open,
57
+ overlayStyle,
58
+ overlayAccessibilityLabel,
59
+ statusBarAnimation,
60
+ swipeEnabled,
61
+ swipeEdgeWidth,
62
+ swipeMinDistance = SWIPE_MIN_DISTANCE,
63
+ swipeMinVelocity = SWIPE_MIN_VELOCITY,
64
+ renderDrawerContent,
65
+ children,
66
+ }: Props) {
67
+ const getDrawerWidth = (): number => {
68
+ const { width = DEFAULT_DRAWER_WIDTH } =
69
+ StyleSheet.flatten(drawerStyle) || {};
70
+
71
+ if (typeof width === 'string' && width.endsWith('%')) {
72
+ // Try to calculate width if a percentage is given
73
+ const percentage = Number(width.replace(/%$/, ''));
74
+
75
+ if (Number.isFinite(percentage)) {
76
+ return layout.width * (percentage / 100);
77
+ }
78
+ }
79
+
80
+ return typeof width === 'number' ? width : 0;
81
+ };
82
+
83
+ const drawerWidth = getDrawerWidth();
84
+
85
+ const isOpen = drawerType === 'permanent' ? true : open;
86
+ const isRight = drawerPosition === 'right';
87
+
88
+ const getDrawerTranslationX = React.useCallback(
89
+ (open: boolean) => {
90
+ 'worklet';
91
+
92
+ if (drawerPosition === 'left') {
93
+ return open ? 0 : -drawerWidth;
94
+ }
95
+
96
+ return open ? 0 : drawerWidth;
97
+ },
98
+ [drawerPosition, drawerWidth]
99
+ );
100
+
101
+ const hideStatusBar = React.useCallback(
102
+ (hide: boolean) => {
103
+ if (hideStatusBarOnOpen) {
104
+ StatusBar.setHidden(hide, statusBarAnimation);
105
+ }
106
+ },
107
+ [hideStatusBarOnOpen, statusBarAnimation]
108
+ );
109
+
110
+ React.useEffect(() => {
111
+ hideStatusBar(isOpen);
112
+
113
+ return () => hideStatusBar(false);
114
+ }, [isOpen, hideStatusBarOnOpen, statusBarAnimation, hideStatusBar]);
115
+
116
+ const interactionHandleRef = React.useRef<number | null>(null);
117
+
118
+ const startInteraction = () => {
119
+ interactionHandleRef.current = InteractionManager.createInteractionHandle();
120
+ };
121
+
122
+ const endInteraction = () => {
123
+ if (interactionHandleRef.current != null) {
124
+ InteractionManager.clearInteractionHandle(interactionHandleRef.current);
125
+ interactionHandleRef.current = null;
126
+ }
127
+ };
128
+
129
+ const hideKeyboard = () => {
130
+ if (keyboardDismissMode === 'on-drag') {
131
+ Keyboard.dismiss();
132
+ }
133
+ };
134
+
135
+ const onGestureStart = () => {
136
+ startInteraction();
137
+ hideKeyboard();
138
+ hideStatusBar(true);
139
+ };
140
+
141
+ const onGestureFinish = () => {
142
+ endInteraction();
143
+ };
144
+
145
+ // FIXME: Currently hitSlop is broken when on Android when drawer is on right
146
+ // https://github.com/software-mansion/react-native-gesture-handler/issues/569
147
+ const hitSlop = isRight
148
+ ? // Extend hitSlop to the side of the screen when drawer is closed
149
+ // This lets the user drag the drawer from the side of the screen
150
+ { right: 0, width: isOpen ? undefined : swipeEdgeWidth }
151
+ : { left: 0, width: isOpen ? undefined : swipeEdgeWidth };
152
+
153
+ const touchStartX = useSharedValue(0);
154
+ const touchX = useSharedValue(0);
155
+ const translationX = useSharedValue(getDrawerTranslationX(open));
156
+ const gestureState = useSharedValue<GestureState>(GestureState.UNDETERMINED);
157
+
158
+ const toggleDrawer = React.useCallback(
159
+ (open: boolean, velocity?: number) => {
160
+ 'worklet';
161
+
162
+ const translateX = getDrawerTranslationX(open);
163
+
164
+ touchStartX.value = 0;
165
+ touchX.value = 0;
166
+ translationX.value = withSpring(translateX, {
167
+ velocity,
168
+ stiffness: 1000,
169
+ damping: 500,
170
+ mass: 3,
171
+ overshootClamping: true,
172
+ restDisplacementThreshold: 0.01,
173
+ restSpeedThreshold: 0.01,
174
+ });
175
+
176
+ if (open) {
177
+ runOnJS(onOpen)();
178
+ } else {
179
+ runOnJS(onClose)();
180
+ }
181
+ },
182
+ [getDrawerTranslationX, onClose, onOpen, touchStartX, touchX, translationX]
183
+ );
184
+
185
+ React.useEffect(() => toggleDrawer(open), [open, toggleDrawer]);
186
+
187
+ const onGestureEvent = useAnimatedGestureHandler<
188
+ PanGestureHandlerGestureEvent,
189
+ { startX: number; hasCalledOnStart: boolean }
190
+ >({
191
+ onStart: (event, ctx) => {
192
+ ctx.hasCalledOnStart = false;
193
+ ctx.startX = translationX.value;
194
+ gestureState.value = event.state;
195
+ touchStartX.value = event.x;
196
+ },
197
+ onActive: (event, ctx) => {
198
+ touchX.value = event.x;
199
+ translationX.value = ctx.startX + event.translationX;
200
+ gestureState.value = event.state;
201
+
202
+ // onStart will _always_ be called, even when the activation
203
+ // criteria isn't met yet. This makes sure onGestureStart is only
204
+ // called when the criteria is really met.
205
+ if (!ctx.hasCalledOnStart) {
206
+ ctx.hasCalledOnStart = true;
207
+ runOnJS(onGestureStart)();
208
+ }
209
+ },
210
+ onEnd: (event) => {
211
+ gestureState.value = event.state;
212
+
213
+ const nextOpen =
214
+ (Math.abs(event.translationX) > SWIPE_MIN_OFFSET &&
215
+ Math.abs(event.translationX) > swipeMinVelocity) ||
216
+ Math.abs(event.translationX) > swipeMinDistance
217
+ ? drawerPosition === 'left'
218
+ ? // If swiped to right, open the drawer, otherwise close it
219
+ (event.velocityX === 0 ? event.translationX : event.velocityX) > 0
220
+ : // If swiped to left, open the drawer, otherwise close it
221
+ (event.velocityX === 0 ? event.translationX : event.velocityX) < 0
222
+ : open;
223
+
224
+ toggleDrawer(nextOpen, event.velocityX);
225
+ },
226
+ onFinish: () => {
227
+ runOnJS(onGestureFinish)();
228
+ },
229
+ });
230
+
231
+ const translateX = useDerivedValue(() => {
232
+ // Comment stolen from react-native-gesture-handler/DrawerLayout
233
+ //
234
+ // While closing the drawer when user starts gesture outside of its area (in greyed
235
+ // out part of the window), we want the drawer to follow only once finger reaches the
236
+ // edge of the drawer.
237
+ // E.g. on the diagram below drawer is illustrate by X signs and the greyed out area by
238
+ // dots. The touch gesture starts at '*' and moves left, touch path is indicated by
239
+ // an arrow pointing left
240
+ // 1) +---------------+ 2) +---------------+ 3) +---------------+ 4) +---------------+
241
+ // |XXXXXXXX|......| |XXXXXXXX|......| |XXXXXXXX|......| |XXXXX|.........|
242
+ // |XXXXXXXX|......| |XXXXXXXX|......| |XXXXXXXX|......| |XXXXX|.........|
243
+ // |XXXXXXXX|......| |XXXXXXXX|......| |XXXXXXXX|......| |XXXXX|.........|
244
+ // |XXXXXXXX|......| |XXXXXXXX|.<-*..| |XXXXXXXX|<--*..| |XXXXX|<-----*..|
245
+ // |XXXXXXXX|......| |XXXXXXXX|......| |XXXXXXXX|......| |XXXXX|.........|
246
+ // |XXXXXXXX|......| |XXXXXXXX|......| |XXXXXXXX|......| |XXXXX|.........|
247
+ // |XXXXXXXX|......| |XXXXXXXX|......| |XXXXXXXX|......| |XXXXX|.........|
248
+ // +---------------+ +---------------+ +---------------+ +---------------+
249
+ //
250
+ // For the above to work properly we define animated value that will keep start position
251
+ // of the gesture. Then we use that value to calculate how much we need to subtract from
252
+ // the translationX. If the gesture started on the greyed out area we take the distance from the
253
+ // edge of the drawer to the start position. Otherwise we don't subtract at all and the
254
+ // drawer be pulled back as soon as you start the pan.
255
+ //
256
+ // This is used only when drawerType is "front"
257
+ const touchDistance =
258
+ drawerType === 'front' && gestureState.value === GestureState.ACTIVE
259
+ ? minmax(
260
+ drawerPosition === 'left'
261
+ ? touchStartX.value - drawerWidth
262
+ : layout.width - drawerWidth - touchStartX.value,
263
+ 0,
264
+ layout.width
265
+ )
266
+ : 0;
267
+
268
+ const translateX =
269
+ drawerPosition === 'left'
270
+ ? minmax(translationX.value + touchDistance, -drawerWidth, 0)
271
+ : minmax(translationX.value - touchDistance, 0, drawerWidth);
272
+
273
+ return translateX;
274
+ });
275
+
276
+ const isRTL = I18nManager.getConstants().isRTL;
277
+ const drawerAnimatedStyle = useAnimatedStyle(() => {
278
+ const distanceFromEdge = layout.width - drawerWidth;
279
+
280
+ return {
281
+ transform:
282
+ drawerType === 'permanent'
283
+ ? // Reanimated needs the property to be present, but it results in Browser bug
284
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=20574
285
+ []
286
+ : [
287
+ {
288
+ translateX:
289
+ // The drawer stays in place when `drawerType` is `back`
290
+ (drawerType === 'back' ? 0 : translateX.value) +
291
+ (drawerPosition === 'left'
292
+ ? isRTL
293
+ ? -distanceFromEdge
294
+ : 0
295
+ : isRTL
296
+ ? 0
297
+ : distanceFromEdge),
298
+ },
299
+ ],
300
+ };
301
+ });
302
+
303
+ const contentAnimatedStyle = useAnimatedStyle(() => {
304
+ return {
305
+ transform:
306
+ drawerType === 'permanent'
307
+ ? // Reanimated needs the property to be present, but it results in Browser bug
308
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=20574
309
+ []
310
+ : [
311
+ {
312
+ translateX:
313
+ // The screen content stays in place when `drawerType` is `front`
314
+ drawerType === 'front'
315
+ ? 0
316
+ : translateX.value +
317
+ drawerWidth * (drawerPosition === 'left' ? 1 : -1),
318
+ },
319
+ ],
320
+ };
321
+ });
322
+
323
+ const progress = useDerivedValue(() => {
324
+ return drawerType === 'permanent'
325
+ ? 1
326
+ : interpolate(
327
+ translateX.value,
328
+ [getDrawerTranslationX(false), getDrawerTranslationX(true)],
329
+ [0, 1]
330
+ );
331
+ });
332
+
333
+ return (
334
+ <DrawerProgressContext.Provider value={progress}>
335
+ <PanGestureHandler
336
+ activeOffsetX={[-SWIPE_MIN_OFFSET, SWIPE_MIN_OFFSET]}
337
+ failOffsetY={[-SWIPE_MIN_OFFSET, SWIPE_MIN_OFFSET]}
338
+ hitSlop={hitSlop}
339
+ enabled={drawerType !== 'permanent' && swipeEnabled}
340
+ onGestureEvent={onGestureEvent}
341
+ {...gestureHandlerProps}
342
+ >
343
+ {/* Immediate child of gesture handler needs to be an Animated.View */}
344
+ <Animated.View
345
+ style={[
346
+ styles.main,
347
+ {
348
+ flexDirection:
349
+ drawerType === 'permanent' && !isRight ? 'row-reverse' : 'row',
350
+ },
351
+ ]}
352
+ >
353
+ <Animated.View style={[styles.content, contentAnimatedStyle]}>
354
+ <View
355
+ accessibilityElementsHidden={isOpen && drawerType !== 'permanent'}
356
+ importantForAccessibility={
357
+ isOpen && drawerType !== 'permanent'
358
+ ? 'no-hide-descendants'
359
+ : 'auto'
360
+ }
361
+ style={styles.content}
362
+ >
363
+ {children}
364
+ </View>
365
+ {drawerType !== 'permanent' ? (
366
+ <Overlay
367
+ progress={progress}
368
+ onPress={() => toggleDrawer(false)}
369
+ style={overlayStyle}
370
+ accessibilityLabel={overlayAccessibilityLabel}
371
+ />
372
+ ) : null}
373
+ </Animated.View>
374
+ <Animated.View
375
+ removeClippedSubviews={Platform.OS !== 'ios'}
376
+ style={[
377
+ styles.container,
378
+ {
379
+ position: drawerType === 'permanent' ? 'relative' : 'absolute',
380
+ zIndex: drawerType === 'back' ? -1 : 0,
381
+ },
382
+ drawerAnimatedStyle,
383
+ drawerStyle as any,
384
+ ]}
385
+ >
386
+ {renderDrawerContent()}
387
+ </Animated.View>
388
+ </Animated.View>
389
+ </PanGestureHandler>
390
+ </DrawerProgressContext.Provider>
391
+ );
392
+ }
393
+
394
+ const styles = StyleSheet.create({
395
+ container: {
396
+ top: 0,
397
+ bottom: 0,
398
+ maxWidth: '100%',
399
+ width: DEFAULT_DRAWER_WIDTH,
400
+ },
401
+ content: {
402
+ flex: 1,
403
+ },
404
+ main: {
405
+ flex: 1,
406
+ ...Platform.select({
407
+ // FIXME: We need to hide `overflowX` on Web so the translated content doesn't show offscreen.
408
+ // But adding `overflowX: 'hidden'` prevents content from collapsing the URL bar.
409
+ web: null,
410
+ default: { overflow: 'hidden' },
411
+ }),
412
+ },
413
+ });
@@ -0,0 +1,82 @@
1
+ import * as React from 'react';
2
+ import { Platform, Pressable, StyleSheet } from 'react-native';
3
+ import Animated, {
4
+ useAnimatedProps,
5
+ useAnimatedStyle,
6
+ } from 'react-native-reanimated';
7
+
8
+ const PROGRESS_EPSILON = 0.05;
9
+
10
+ type Props = React.ComponentProps<typeof Animated.View> & {
11
+ progress: Animated.SharedValue<number>;
12
+ onPress: () => void;
13
+ accessibilityLabel?: string;
14
+ };
15
+
16
+ const Overlay = React.forwardRef(function Overlay(
17
+ {
18
+ progress,
19
+ onPress,
20
+ style,
21
+ accessibilityLabel = 'Close drawer',
22
+ ...props
23
+ }: Props,
24
+ ref: React.Ref<Animated.View>
25
+ ) {
26
+ const animatedStyle = useAnimatedStyle(() => {
27
+ return {
28
+ opacity: progress.value,
29
+ // We don't want the user to be able to press through the overlay when drawer is open
30
+ // We can send the overlay behind the screen to avoid it
31
+ zIndex: progress.value > PROGRESS_EPSILON ? 0 : -1,
32
+ };
33
+ });
34
+
35
+ const animatedProps = useAnimatedProps(() => {
36
+ const active = progress.value > PROGRESS_EPSILON;
37
+
38
+ return {
39
+ pointerEvents: active ? 'auto' : 'none',
40
+ accessibilityElementsHidden: !active,
41
+ importantForAccessibility: active ? 'auto' : 'no-hide-descendants',
42
+ } as const;
43
+ });
44
+
45
+ return (
46
+ <Animated.View
47
+ {...props}
48
+ ref={ref}
49
+ style={[styles.overlay, overlayStyle, animatedStyle, style]}
50
+ animatedProps={animatedProps}
51
+ >
52
+ <Pressable
53
+ onPress={onPress}
54
+ style={styles.pressable}
55
+ accessibilityRole="button"
56
+ accessibilityLabel={accessibilityLabel}
57
+ />
58
+ </Animated.View>
59
+ );
60
+ });
61
+
62
+ const overlayStyle = Platform.select<Record<string, string>>({
63
+ web: {
64
+ // Disable touch highlight on mobile Safari.
65
+ // WebkitTapHighlightColor must be used outside of StyleSheet.create because react-native-web will omit the property.
66
+ WebkitTapHighlightColor: 'transparent',
67
+ },
68
+ default: {},
69
+ });
70
+
71
+ const styles = StyleSheet.create({
72
+ overlay: {
73
+ ...StyleSheet.absoluteFillObject,
74
+ backgroundColor: 'rgba(0, 0, 0, 0.5)',
75
+ },
76
+ pressable: {
77
+ flex: 1,
78
+ pointerEvents: 'auto',
79
+ },
80
+ });
81
+
82
+ export default Overlay;