@react-navigation/drawer 7.0.0-alpha.2 → 7.0.0-alpha.21

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 (94) hide show
  1. package/lib/commonjs/index.js +0 -6
  2. package/lib/commonjs/index.js.map +1 -1
  3. package/lib/commonjs/navigators/createDrawerNavigator.js +13 -6
  4. package/lib/commonjs/navigators/createDrawerNavigator.js.map +1 -1
  5. package/lib/commonjs/types.js.map +1 -1
  6. package/lib/commonjs/utils/DrawerPositionContext.js +3 -4
  7. package/lib/commonjs/utils/DrawerPositionContext.js.map +1 -1
  8. package/lib/commonjs/utils/DrawerStatusContext.js +3 -4
  9. package/lib/commonjs/utils/DrawerStatusContext.js.map +1 -1
  10. package/lib/commonjs/utils/addCancelListener.js +19 -0
  11. package/lib/commonjs/utils/addCancelListener.js.map +1 -0
  12. package/lib/commonjs/utils/addCancelListener.native.js +15 -0
  13. package/lib/commonjs/utils/addCancelListener.native.js.map +1 -0
  14. package/lib/commonjs/utils/getDrawerStatusFromState.js +1 -1
  15. package/lib/commonjs/utils/getDrawerStatusFromState.js.map +1 -1
  16. package/lib/commonjs/utils/useDrawerStatus.js +2 -2
  17. package/lib/commonjs/utils/useDrawerStatus.js.map +1 -1
  18. package/lib/commonjs/views/DrawerContent.js +2 -2
  19. package/lib/commonjs/views/DrawerContent.js.map +1 -1
  20. package/lib/commonjs/views/DrawerContentScrollView.js +10 -6
  21. package/lib/commonjs/views/DrawerContentScrollView.js.map +1 -1
  22. package/lib/commonjs/views/DrawerItem.js +22 -66
  23. package/lib/commonjs/views/DrawerItem.js.map +1 -1
  24. package/lib/commonjs/views/DrawerItemList.js +3 -3
  25. package/lib/commonjs/views/DrawerItemList.js.map +1 -1
  26. package/lib/commonjs/views/DrawerToggleButton.js +4 -6
  27. package/lib/commonjs/views/DrawerToggleButton.js.map +1 -1
  28. package/lib/commonjs/views/DrawerView.js +29 -30
  29. package/lib/commonjs/views/DrawerView.js.map +1 -1
  30. package/lib/commonjs/views/ScreenFallback.js +4 -6
  31. package/lib/commonjs/views/ScreenFallback.js.map +1 -1
  32. package/lib/module/index.js +1 -1
  33. package/lib/module/index.js.map +1 -1
  34. package/lib/module/navigators/createDrawerNavigator.js +10 -2
  35. package/lib/module/navigators/createDrawerNavigator.js.map +1 -1
  36. package/lib/module/types.js.map +1 -1
  37. package/lib/module/utils/DrawerPositionContext.js.map +1 -1
  38. package/lib/module/utils/DrawerStatusContext.js.map +1 -1
  39. package/lib/module/utils/addCancelListener.js +12 -0
  40. package/lib/module/utils/addCancelListener.js.map +1 -0
  41. package/lib/module/utils/addCancelListener.native.js +8 -0
  42. package/lib/module/utils/addCancelListener.native.js.map +1 -0
  43. package/lib/module/utils/getDrawerStatusFromState.js +1 -1
  44. package/lib/module/utils/getDrawerStatusFromState.js.map +1 -1
  45. package/lib/module/utils/useDrawerStatus.js.map +1 -1
  46. package/lib/module/views/DrawerContent.js.map +1 -1
  47. package/lib/module/views/DrawerContentScrollView.js +8 -3
  48. package/lib/module/views/DrawerContentScrollView.js.map +1 -1
  49. package/lib/module/views/DrawerItem.js +22 -66
  50. package/lib/module/views/DrawerItem.js.map +1 -1
  51. package/lib/module/views/DrawerItemList.js +2 -2
  52. package/lib/module/views/DrawerItemList.js.map +1 -1
  53. package/lib/module/views/DrawerToggleButton.js +2 -4
  54. package/lib/module/views/DrawerToggleButton.js.map +1 -1
  55. package/lib/module/views/DrawerView.js +30 -31
  56. package/lib/module/views/DrawerView.js.map +1 -1
  57. package/lib/module/views/ScreenFallback.js +2 -4
  58. package/lib/module/views/ScreenFallback.js.map +1 -1
  59. package/lib/typescript/src/index.d.ts +2 -2
  60. package/lib/typescript/src/index.d.ts.map +1 -1
  61. package/lib/typescript/src/navigators/createDrawerNavigator.d.ts +15 -9
  62. package/lib/typescript/src/navigators/createDrawerNavigator.d.ts.map +1 -1
  63. package/lib/typescript/src/types.d.ts +8 -16
  64. package/lib/typescript/src/types.d.ts.map +1 -1
  65. package/lib/typescript/src/utils/addCancelListener.d.ts +2 -0
  66. package/lib/typescript/src/utils/addCancelListener.d.ts.map +1 -0
  67. package/lib/typescript/src/utils/addCancelListener.native.d.ts +2 -0
  68. package/lib/typescript/src/utils/addCancelListener.native.d.ts.map +1 -0
  69. package/lib/typescript/src/views/DrawerContent.d.ts +2 -1
  70. package/lib/typescript/src/views/DrawerContent.d.ts.map +1 -1
  71. package/lib/typescript/src/views/DrawerContentScrollView.d.ts +1 -1
  72. package/lib/typescript/src/views/DrawerContentScrollView.d.ts.map +1 -1
  73. package/lib/typescript/src/views/DrawerItem.d.ts +3 -3
  74. package/lib/typescript/src/views/DrawerItem.d.ts.map +1 -1
  75. package/lib/typescript/src/views/DrawerItemList.d.ts +1 -1
  76. package/lib/typescript/src/views/DrawerItemList.d.ts.map +1 -1
  77. package/lib/typescript/src/views/DrawerToggleButton.d.ts +2 -1
  78. package/lib/typescript/src/views/DrawerToggleButton.d.ts.map +1 -1
  79. package/lib/typescript/src/views/DrawerView.d.ts +3 -2
  80. package/lib/typescript/src/views/DrawerView.d.ts.map +1 -1
  81. package/lib/typescript/src/views/ScreenFallback.d.ts +3 -3
  82. package/lib/typescript/src/views/ScreenFallback.d.ts.map +1 -1
  83. package/package.json +22 -23
  84. package/src/index.tsx +1 -1
  85. package/src/navigators/createDrawerNavigator.tsx +42 -12
  86. package/src/types.tsx +15 -20
  87. package/src/utils/addCancelListener.native.tsx +12 -0
  88. package/src/utils/addCancelListener.tsx +13 -0
  89. package/src/views/DrawerContentScrollView.tsx +9 -10
  90. package/src/views/DrawerItem.tsx +22 -87
  91. package/src/views/DrawerItemList.tsx +6 -6
  92. package/src/views/DrawerToggleButton.tsx +2 -4
  93. package/src/views/DrawerView.tsx +42 -37
  94. package/src/views/ScreenFallback.tsx +6 -1
@@ -1,15 +1,13 @@
1
- import { PlatformPressable } from '@react-navigation/elements';
2
- import { CommonActions, Link, Route, useTheme } from '@react-navigation/native';
1
+ import { PlatformPressable, Text } from '@react-navigation/elements';
2
+ import { type Route, useTheme } from '@react-navigation/native';
3
3
  import Color from 'color';
4
4
  import * as React from 'react';
5
5
  import {
6
- Platform,
7
- StyleProp,
6
+ type StyleProp,
8
7
  StyleSheet,
9
- Text,
10
- TextStyle,
8
+ type TextStyle,
11
9
  View,
12
- ViewStyle,
10
+ type ViewStyle,
13
11
  } from 'react-native';
14
12
 
15
13
  type Props = {
@@ -96,65 +94,6 @@ type Props = {
96
94
  testID?: string;
97
95
  };
98
96
 
99
- const LinkPressable = ({
100
- route,
101
- href,
102
- children,
103
- style,
104
- onPress,
105
- onLongPress,
106
- onPressIn,
107
- onPressOut,
108
- accessibilityRole,
109
- ...rest
110
- }: Omit<React.ComponentProps<typeof PlatformPressable>, 'style'> & {
111
- style: StyleProp<ViewStyle>;
112
- } & {
113
- route: Route<string>;
114
- href?: string;
115
- children: React.ReactNode;
116
- onPress?: () => void;
117
- }) => {
118
- if (Platform.OS === 'web') {
119
- // React Native Web doesn't forward `onClick` if we use `TouchableWithoutFeedback`.
120
- // We need to use `onClick` to be able to prevent default browser handling of links.
121
- return (
122
- <Link
123
- {...rest}
124
- href={href}
125
- action={CommonActions.navigate(route.name, route.params)}
126
- style={[styles.button, style]}
127
- onPress={(e: any) => {
128
- if (
129
- !(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey) && // ignore clicks with modifier keys
130
- (e.button == null || e.button === 0) // ignore everything but left clicks
131
- ) {
132
- e.preventDefault();
133
- onPress?.(e);
134
- }
135
- }}
136
- // types for PressableProps and TextProps are incompatible with each other by `null` so we
137
- // can't use {...rest} for these 3 props
138
- onLongPress={onLongPress ?? undefined}
139
- onPressIn={onPressIn ?? undefined}
140
- onPressOut={onPressOut ?? undefined}
141
- >
142
- {children}
143
- </Link>
144
- );
145
- } else {
146
- return (
147
- <PlatformPressable
148
- {...rest}
149
- accessibilityRole={accessibilityRole}
150
- onPress={onPress}
151
- >
152
- <View style={style}>{children}</View>
153
- </PlatformPressable>
154
- );
155
- }
156
- };
157
-
158
97
  /**
159
98
  * A component used to show an action item with an icon and a label in a navigation drawer.
160
99
  */
@@ -162,7 +101,6 @@ export function DrawerItem(props: Props) {
162
101
  const { colors, fonts } = useTheme();
163
102
 
164
103
  const {
165
- route,
166
104
  href,
167
105
  icon,
168
106
  label,
@@ -182,7 +120,7 @@ export function DrawerItem(props: Props) {
182
120
  ...rest
183
121
  } = props;
184
122
 
185
- const { borderRadius = 4 } = StyleSheet.flatten(style || {});
123
+ const { borderRadius = 56 } = StyleSheet.flatten(style || {});
186
124
  const color = focused ? activeTintColor : inactiveTintColor;
187
125
  const backgroundColor = focused
188
126
  ? activeBackgroundColor
@@ -196,31 +134,24 @@ export function DrawerItem(props: Props) {
196
134
  {...rest}
197
135
  style={[styles.container, { borderRadius, backgroundColor }, style]}
198
136
  >
199
- <LinkPressable
137
+ <PlatformPressable
200
138
  testID={testID}
201
139
  onPress={onPress}
202
- style={[styles.wrapper, { borderRadius }]}
203
140
  accessibilityLabel={accessibilityLabel}
204
141
  accessibilityRole="button"
205
142
  accessibilityState={{ selected: focused }}
206
143
  pressColor={pressColor}
207
144
  pressOpacity={pressOpacity}
208
- route={route}
209
145
  href={href}
210
146
  >
211
- <React.Fragment>
147
+ <View style={[styles.wrapper, { borderRadius }]}>
212
148
  {iconNode}
213
- <View
214
- style={[
215
- styles.label,
216
- { marginLeft: iconNode ? 32 : 0, marginVertical: 5 },
217
- ]}
218
- >
149
+ <View style={[styles.label, { marginStart: iconNode ? 16 : 0 }]}>
219
150
  {typeof label === 'string' ? (
220
151
  <Text
221
152
  numberOfLines={1}
222
153
  allowFontScaling={allowFontScaling}
223
- style={[{ color }, fonts.medium, labelStyle]}
154
+ style={[styles.labelText, { color }, fonts.medium, labelStyle]}
224
155
  >
225
156
  {label}
226
157
  </Text>
@@ -228,28 +159,32 @@ export function DrawerItem(props: Props) {
228
159
  label({ color, focused })
229
160
  )}
230
161
  </View>
231
- </React.Fragment>
232
- </LinkPressable>
162
+ </View>
163
+ </PlatformPressable>
233
164
  </View>
234
165
  );
235
166
  }
236
167
 
237
168
  const styles = StyleSheet.create({
238
169
  container: {
239
- marginHorizontal: 10,
240
- marginVertical: 4,
170
+ marginHorizontal: 12,
171
+ marginVertical: 2,
241
172
  overflow: 'hidden',
242
173
  },
243
174
  wrapper: {
244
175
  flexDirection: 'row',
245
176
  alignItems: 'center',
246
- padding: 8,
177
+ paddingVertical: 12,
178
+ paddingStart: 16,
179
+ paddingEnd: 24,
247
180
  },
248
181
  label: {
249
- marginRight: 32,
182
+ marginEnd: 12,
183
+ marginVertical: 4,
250
184
  flex: 1,
251
185
  },
252
- button: {
253
- display: 'flex',
186
+ labelText: {
187
+ lineHeight: 24,
188
+ textAlignVertical: 'center',
254
189
  },
255
190
  });
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  CommonActions,
3
3
  DrawerActions,
4
- DrawerNavigationState,
5
- ParamListBase,
6
- useLinkTools,
4
+ type DrawerNavigationState,
5
+ type ParamListBase,
6
+ useLinkBuilder,
7
7
  } from '@react-navigation/native';
8
8
  import * as React from 'react';
9
9
 
@@ -20,7 +20,7 @@ type Props = {
20
20
  * Component that renders the navigation list in the drawer.
21
21
  */
22
22
  export function DrawerItemList({ state, navigation, descriptors }: Props) {
23
- const { buildHref } = useLinkTools();
23
+ const { buildHref } = useLinkBuilder();
24
24
 
25
25
  const focusedRoute = state.routes[state.index];
26
26
  const focusedDescriptor = descriptors[focusedRoute.key];
@@ -71,8 +71,8 @@ export function DrawerItemList({ state, navigation, descriptors }: Props) {
71
71
  drawerLabel !== undefined
72
72
  ? drawerLabel
73
73
  : title !== undefined
74
- ? title
75
- : route.name
74
+ ? title
75
+ : route.name
76
76
  }
77
77
  icon={drawerIcon}
78
78
  focused={focused}
@@ -1,7 +1,7 @@
1
1
  import { PlatformPressable } from '@react-navigation/elements';
2
2
  import {
3
3
  DrawerActions,
4
- ParamListBase,
4
+ type ParamListBase,
5
5
  useNavigation,
6
6
  } from '@react-navigation/native';
7
7
  import * as React from 'react';
@@ -22,8 +22,6 @@ export function DrawerToggleButton({ tintColor, ...rest }: Props) {
22
22
  return (
23
23
  <PlatformPressable
24
24
  {...rest}
25
- accessible
26
- accessibilityRole="button"
27
25
  android_ripple={{ borderless: true }}
28
26
  onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
29
27
  style={styles.touchable}
@@ -34,6 +32,7 @@ export function DrawerToggleButton({ tintColor, ...rest }: Props) {
34
32
  >
35
33
  <Image
36
34
  style={[styles.icon, tintColor ? { tintColor } : null]}
35
+ resizeMode="contain"
37
36
  source={require('./assets/toggle-drawer-icon.png')}
38
37
  fadeDuration={0}
39
38
  />
@@ -46,7 +45,6 @@ const styles = StyleSheet.create({
46
45
  height: 24,
47
46
  width: 24,
48
47
  margin: 3,
49
- resizeMode: 'contain',
50
48
  },
51
49
  touchable: {
52
50
  marginHorizontal: 11,
@@ -1,4 +1,5 @@
1
1
  import {
2
+ getDefaultSidebarWidth,
2
3
  getHeaderTitle,
3
4
  Header,
4
5
  SafeAreaProviderCompat,
@@ -6,13 +7,14 @@ import {
6
7
  } from '@react-navigation/elements';
7
8
  import {
8
9
  DrawerActions,
9
- DrawerNavigationState,
10
- DrawerStatus,
11
- ParamListBase,
10
+ type DrawerNavigationState,
11
+ type DrawerStatus,
12
+ type ParamListBase,
13
+ useLocale,
12
14
  useTheme,
13
15
  } from '@react-navigation/native';
14
16
  import * as React from 'react';
15
- import { BackHandler, I18nManager, Platform, StyleSheet } from 'react-native';
17
+ import { Platform, StyleSheet } from 'react-native';
16
18
  import { Drawer } from 'react-native-drawer-layout';
17
19
  import { useSafeAreaFrame } from 'react-native-safe-area-context';
18
20
  import useLatestCallback from 'use-latest-callback';
@@ -25,6 +27,7 @@ import type {
25
27
  DrawerNavigationHelpers,
26
28
  DrawerNavigationProp,
27
29
  } from '../types';
30
+ import { addCancelListener } from '../utils/addCancelListener';
28
31
  import { DrawerPositionContext } from '../utils/DrawerPositionContext';
29
32
  import { DrawerStatusContext } from '../utils/DrawerStatusContext';
30
33
  import { getDrawerStatusFromState } from '../utils/getDrawerStatusFromState';
@@ -39,6 +42,8 @@ type Props = DrawerNavigationConfig & {
39
42
  descriptors: DrawerDescriptorMap;
40
43
  };
41
44
 
45
+ const DRAWER_BORDER_RADIUS = 16;
46
+
42
47
  function DrawerViewBase({
43
48
  state,
44
49
  navigation,
@@ -51,14 +56,16 @@ function DrawerViewBase({
51
56
  Platform.OS === 'android' ||
52
57
  Platform.OS === 'ios',
53
58
  }: Props) {
59
+ const { direction } = useLocale();
60
+
54
61
  const focusedRouteKey = state.routes[state.index].key;
55
62
  const {
56
63
  drawerHideStatusBarOnOpen,
57
- drawerPosition = I18nManager.getConstants().isRTL ? 'right' : 'left',
64
+ drawerPosition = direction === 'rtl' ? 'right' : 'left',
58
65
  drawerStatusBarAnimation,
59
66
  drawerStyle,
60
- drawerType,
61
- gestureHandlerProps,
67
+ drawerType = Platform.select({ ios: 'slide', default: 'front' }),
68
+ configureGestureHandler,
62
69
  keyboardDismissMode,
63
70
  overlayColor = 'rgba(0, 0, 0, 0.5)',
64
71
  swipeEdgeWidth,
@@ -153,31 +160,10 @@ function DrawerViewBase({
153
160
  return true;
154
161
  };
155
162
 
156
- const handleEscape = (e: KeyboardEvent) => {
157
- if (e.key === 'Escape') {
158
- handleHardwareBack();
159
- }
160
- };
161
-
162
163
  // We only add the listeners when drawer opens
163
164
  // This way we can make sure that the listener is added as late as possible
164
165
  // This will make sure that our handler will run first when back button is pressed
165
- const subscription = BackHandler.addEventListener(
166
- 'hardwareBackPress',
167
- handleHardwareBack
168
- );
169
-
170
- if (Platform.OS === 'web') {
171
- document?.body?.addEventListener?.('keyup', handleEscape);
172
- }
173
-
174
- return () => {
175
- subscription.remove();
176
-
177
- if (Platform.OS === 'web') {
178
- document?.body?.removeEventListener?.('keyup', handleEscape);
179
- }
180
- };
166
+ return addCancelListener(handleHardwareBack);
181
167
  }, [
182
168
  defaultStatus,
183
169
  drawerStatus,
@@ -215,8 +201,13 @@ function DrawerViewBase({
215
201
  return null;
216
202
  }
217
203
 
218
- if (lazy && !loaded.includes(route.key) && !isFocused) {
219
- // Don't render a lazy screen if we've never navigated to it
204
+ if (
205
+ lazy &&
206
+ !loaded.includes(route.key) &&
207
+ !isFocused &&
208
+ !state.preloadedRouteKeys.includes(route.key)
209
+ ) {
210
+ // Don't render a lazy screen if we've never navigated to it or it wasn't preloaded
220
211
  return null;
221
212
  }
222
213
 
@@ -284,7 +275,7 @@ function DrawerViewBase({
284
275
  onTransitionStart={handleTransitionStart}
285
276
  onTransitionEnd={handleTransitionEnd}
286
277
  layout={dimensions}
287
- gestureHandlerProps={gestureHandlerProps}
278
+ configureGestureHandler={configureGestureHandler}
288
279
  swipeEnabled={swipeEnabled}
289
280
  swipeEdgeWidth={swipeEdgeWidth}
290
281
  swipeMinDistance={swipeMinDistance}
@@ -295,16 +286,30 @@ function DrawerViewBase({
295
286
  overlayAccessibilityLabel={overlayAccessibilityLabel}
296
287
  drawerPosition={drawerPosition}
297
288
  drawerStyle={[
298
- { backgroundColor: colors.card },
289
+ {
290
+ backgroundColor: colors.card,
291
+ width: getDefaultSidebarWidth(dimensions),
292
+ },
299
293
  drawerType === 'permanent' &&
300
294
  (drawerPosition === 'left'
301
295
  ? {
302
- borderRightColor: colors.border,
303
- borderRightWidth: StyleSheet.hairlineWidth,
296
+ borderEndColor: colors.border,
297
+ borderEndWidth: StyleSheet.hairlineWidth,
298
+ }
299
+ : {
300
+ borderStartColor: colors.border,
301
+ borderStartWidth: StyleSheet.hairlineWidth,
302
+ }),
303
+
304
+ drawerType === 'front' &&
305
+ (drawerPosition === 'left'
306
+ ? {
307
+ borderTopRightRadius: DRAWER_BORDER_RADIUS,
308
+ borderBottomRightRadius: DRAWER_BORDER_RADIUS,
304
309
  }
305
310
  : {
306
- borderLeftColor: colors.border,
307
- borderLeftWidth: StyleSheet.hairlineWidth,
311
+ borderTopLeftRadius: DRAWER_BORDER_RADIUS,
312
+ borderBottomLeftRadius: DRAWER_BORDER_RADIUS,
308
313
  }),
309
314
  drawerStyle,
310
315
  ]}
@@ -1,6 +1,11 @@
1
1
  import { ResourceSavingView } from '@react-navigation/elements';
2
2
  import * as React from 'react';
3
- import { StyleProp, View, ViewProps, ViewStyle } from 'react-native';
3
+ import {
4
+ type StyleProp,
5
+ View,
6
+ type ViewProps,
7
+ type ViewStyle,
8
+ } from 'react-native';
4
9
 
5
10
  type Props = {
6
11
  visible: boolean;