@rific/drawer 0.1.0 → 0.1.2
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.
- package/README.md +10 -6
- package/dist/index.js +2 -0
- package/dist/index.mjs +2 -0
- package/package.json +1 -1
- package/src/Drawer.tsx +3 -0
- package/src/DrawerEdgeSwipe.tsx +2 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @rific/drawer
|
|
2
2
|
|
|
3
|
-
Left/right sliding drawer for React Native
|
|
3
|
+
Left/right sliding drawer for React Native. Spring-animated, theme-aware (reads colors from `react-native-paper`), and opens either by calling an action or by swiping in from the screen edge. One `createDrawer()` call per drawer instance, so a left nav drawer and a right settings drawer stay fully independent.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -14,7 +14,7 @@ npm install @rific/drawer
|
|
|
14
14
|
npm install react-native-gesture-handler react-native-reanimated react-native-paper @rific/haptic-press
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Your app's root must already be wrapped in `GestureHandlerRootView` (from `react-native-gesture-handler`)
|
|
17
|
+
Your app's root must already be wrapped in `GestureHandlerRootView` (from `react-native-gesture-handler`). This package doesn't add its own, since only one should ever wrap the whole app.
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
@@ -25,7 +25,7 @@ const { DrawerProvider: NavDrawerProvider, useDrawer: useNavDrawer } = createDra
|
|
|
25
25
|
const { DrawerProvider: SettingsDrawerProvider, useDrawer: useSettingsDrawer } = createDrawer({ side: 'right', width: 320 })
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
Mount each `DrawerProvider` once, near your app root
|
|
28
|
+
Mount each `DrawerProvider` once, near your app root: `children` is the rest of the app (so `useDrawer()` is reachable from anywhere inside it), and `content` is what renders inside the sliding panel itself. With more than one drawer, `combineDrawerProviders` flattens the nesting into a single wrapper (first argument is outermost, matching how you'd nest them by hand):
|
|
29
29
|
|
|
30
30
|
```tsx
|
|
31
31
|
import { combineDrawerProviders } from '@rific/drawer'
|
|
@@ -43,7 +43,7 @@ export default function RootLayout() {
|
|
|
43
43
|
}
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
Each drawer's state stays fully independent under the hood
|
|
46
|
+
Each drawer's state stays fully independent under the hood. This is purely a way to avoid writing out the nesting by hand. With a single drawer, just render its `DrawerProvider` directly with a `content` prop; `combineDrawerProviders` only earns its keep once you have more than one.
|
|
47
47
|
|
|
48
48
|
Then anywhere else in the tree:
|
|
49
49
|
|
|
@@ -55,8 +55,12 @@ const { isOpen, open, close } = useNavDrawer()
|
|
|
55
55
|
<IconButton icon='menu' onPress={open} />
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
The drawer opens by calling `open()`, by tapping the backdrop or an in-content close button (call `close()`), or by swiping in from the screen edge
|
|
58
|
+
The drawer opens by calling `open()`, by tapping the backdrop or an in-content close button (call `close()`), or by swiping in from the screen edge. No extra wiring is needed for the swipe gesture; it's built into `DrawerProvider`. Pass `enabled={false}` to `DrawerProvider` to temporarily suppress the edge-swipe gesture (e.g. on a screen where it would conflict with another gesture).
|
|
59
|
+
|
|
60
|
+
## Safe area
|
|
61
|
+
|
|
62
|
+
The panel renders edge-to-edge, behind the status bar/notch and home indicator, on purpose. This package doesn't take a position on how you handle that. Pad your `content` yourself, however fits your app: `useSafeAreaInsets()` from `react-native-safe-area-context` directly, or a safe-area-aware header like `@rific/scroll-view`'s. Baking a fixed strategy into the panel itself would double up with whichever one you're already using for the rest of your app.
|
|
59
63
|
|
|
60
64
|
## Lower-level pieces
|
|
61
65
|
|
|
62
|
-
`Drawer` (the sliding panel
|
|
66
|
+
`Drawer` (the sliding panel and backdrop) and `DrawerEdgeSwipe` (the edge gesture zone) are also exported directly, for apps that want to manage the open/closed state and shared value themselves instead of using `createDrawer()`.
|
package/dist/index.js
CHANGED
|
@@ -103,8 +103,10 @@ var DrawerEdgeSwipe = ({ enabled = true, onOpen, side = "left", translateX, widt
|
|
|
103
103
|
const clampMin = Math.min(0, closedX);
|
|
104
104
|
const clampMax = Math.max(0, closedX);
|
|
105
105
|
const gesture = import_react_native_gesture_handler.Gesture.Pan().enabled(enabled).activeOffsetX(side === "left" ? 10 : -10).failOffsetY([-15, 15]).onUpdate((event) => {
|
|
106
|
+
"worklet";
|
|
106
107
|
translateX.value = Math.min(clampMax, Math.max(clampMin, closedX + event.translationX));
|
|
107
108
|
}).onEnd((event) => {
|
|
109
|
+
"worklet";
|
|
108
110
|
const commit = event.translationX * openDirection > width / 3 || event.velocityX * openDirection > VELOCITY_THRESHOLD;
|
|
109
111
|
if (commit) {
|
|
110
112
|
translateX.value = (0, import_react_native_reanimated2.withSpring)(0, SPRING2);
|
package/dist/index.mjs
CHANGED
|
@@ -64,8 +64,10 @@ var DrawerEdgeSwipe = ({ enabled = true, onOpen, side = "left", translateX, widt
|
|
|
64
64
|
const clampMin = Math.min(0, closedX);
|
|
65
65
|
const clampMax = Math.max(0, closedX);
|
|
66
66
|
const gesture = Gesture.Pan().enabled(enabled).activeOffsetX(side === "left" ? 10 : -10).failOffsetY([-15, 15]).onUpdate((event) => {
|
|
67
|
+
"worklet";
|
|
67
68
|
translateX.value = Math.min(clampMax, Math.max(clampMin, closedX + event.translationX));
|
|
68
69
|
}).onEnd((event) => {
|
|
70
|
+
"worklet";
|
|
69
71
|
const commit = event.translationX * openDirection > width / 3 || event.velocityX * openDirection > VELOCITY_THRESHOLD;
|
|
70
72
|
if (commit) {
|
|
71
73
|
translateX.value = withSpring2(0, SPRING2);
|
package/package.json
CHANGED
package/src/Drawer.tsx
CHANGED
|
@@ -7,6 +7,9 @@ import Animated, { SharedValue, useAnimatedStyle, useSharedValue, withSpring } f
|
|
|
7
7
|
const SPRING = { damping: 40, stiffness: 300, overshootClamping: true }
|
|
8
8
|
|
|
9
9
|
export type DrawerProps = {
|
|
10
|
+
// Renders edge-to-edge behind the status bar/notch/home indicator, deliberately — pad your own
|
|
11
|
+
// content with useSafeAreaInsets() (or a safe-area-aware header like @rific/scroll-view's) rather
|
|
12
|
+
// than have this component impose a strategy that might double up with one you're already using.
|
|
10
13
|
children?: ReactNode
|
|
11
14
|
onClose: () => void
|
|
12
15
|
open: boolean
|
package/src/DrawerEdgeSwipe.tsx
CHANGED
|
@@ -25,9 +25,11 @@ export const DrawerEdgeSwipe = ({ enabled = true, onOpen, side = 'left', transla
|
|
|
25
25
|
.activeOffsetX(side === 'left' ? 10 : -10)
|
|
26
26
|
.failOffsetY([-15, 15])
|
|
27
27
|
.onUpdate((event) => {
|
|
28
|
+
'worklet'
|
|
28
29
|
translateX.value = Math.min(clampMax, Math.max(clampMin, closedX + event.translationX))
|
|
29
30
|
})
|
|
30
31
|
.onEnd((event) => {
|
|
32
|
+
'worklet'
|
|
31
33
|
const commit = event.translationX * openDirection > width / 3 || event.velocityX * openDirection > VELOCITY_THRESHOLD
|
|
32
34
|
if (commit) {
|
|
33
35
|
translateX.value = withSpring(0, SPRING)
|