@rific/drawer 0.1.2 → 0.3.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.
- package/README.md +131 -8
- package/dist/index.d.mts +80 -17
- package/dist/index.d.ts +80 -17
- package/dist/index.js +265 -57
- package/dist/index.mjs +266 -61
- package/package.json +21 -11
- package/src/Drawer.tsx +373 -27
- package/src/DrawerConfig.tsx +48 -0
- package/src/DrawerEdgeSwipe.tsx +57 -19
- package/src/createDrawer.tsx +149 -21
- package/src/geometry.ts +52 -0
- package/src/index.ts +3 -1
- package/src/useDrawerSize.ts +55 -0
package/dist/index.mjs
CHANGED
|
@@ -1,37 +1,181 @@
|
|
|
1
1
|
// src/createDrawer.tsx
|
|
2
|
-
import { createContext, useContext, useMemo, useState } from "react";
|
|
3
|
-
import {
|
|
2
|
+
import { createContext, useContext, useEffect as useEffect2, useMemo, useState as useState2 } from "react";
|
|
3
|
+
import { BackHandler } from "react-native";
|
|
4
|
+
import { useDerivedValue, useSharedValue as useSharedValue2 } from "react-native-reanimated";
|
|
4
5
|
|
|
5
6
|
// src/Drawer.tsx
|
|
6
7
|
import { Pressable } from "@rific/haptic-press";
|
|
7
|
-
import { useEffect } from "react";
|
|
8
|
-
import { StyleSheet } from "react-native";
|
|
8
|
+
import { useEffect, useState } from "react";
|
|
9
|
+
import { Platform, StyleSheet, View } from "react-native";
|
|
10
|
+
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
9
11
|
import { useTheme } from "react-native-paper";
|
|
10
|
-
import Animated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
import Animated, { runOnJS, useAnimatedStyle, useSharedValue, withSpring, withTiming } from "react-native-reanimated";
|
|
13
|
+
|
|
14
|
+
// src/DrawerConfig.tsx
|
|
15
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
16
|
+
var config = {};
|
|
17
|
+
var configureDrawer = (next) => {
|
|
18
|
+
config = { ...config, ...next };
|
|
19
|
+
};
|
|
20
|
+
var getDrawerConfig = () => config;
|
|
21
|
+
var DrawerProvider = ({ autoPaper, children }) => {
|
|
22
|
+
configureDrawer({ autoPaper });
|
|
23
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/geometry.ts
|
|
27
|
+
function isVerticalSide(side) {
|
|
28
|
+
return side === "top" || side === "bottom";
|
|
29
|
+
}
|
|
30
|
+
function getClosedOffset(side, size) {
|
|
31
|
+
return size === 0 ? 0 : side === "left" || side === "top" ? -size : size;
|
|
32
|
+
}
|
|
33
|
+
function getOpenDirection(side) {
|
|
34
|
+
return side === "left" || side === "top" ? 1 : -1;
|
|
35
|
+
}
|
|
36
|
+
function resolveDimension(value, windowSize) {
|
|
37
|
+
return typeof value === "number" ? value : Number.parseFloat(value) / 100 * windowSize;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/useDrawerSize.ts
|
|
41
|
+
import { useWindowDimensions } from "react-native";
|
|
42
|
+
function useDrawerSize(side, size, maxSize, edgeInset = 0) {
|
|
43
|
+
const windowDimensions = useWindowDimensions();
|
|
44
|
+
const windowSize = isVerticalSide(side) ? windowDimensions.height : windowDimensions.width;
|
|
45
|
+
const percentBasis = windowSize - edgeInset;
|
|
46
|
+
const effectiveSize = resolveDimension(size, percentBasis);
|
|
47
|
+
const maxEffectiveSize = maxSize === void 0 ? effectiveSize : Math.max(resolveDimension(maxSize, percentBasis), effectiveSize);
|
|
48
|
+
const closedOffset = getClosedOffset(side, maxEffectiveSize);
|
|
49
|
+
const restOffset = getClosedOffset(side, maxEffectiveSize - effectiveSize);
|
|
50
|
+
return { closedOffset, effectiveSize, maxEffectiveSize, restOffset };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/Drawer.tsx
|
|
54
|
+
import { Fragment as Fragment2, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
55
|
+
var SPRING = { damping: 45, stiffness: 500, overshootClamping: true, restDisplacementThreshold: 0.5, restSpeedThreshold: 10 };
|
|
56
|
+
var VELOCITY_THRESHOLD = 500;
|
|
57
|
+
var useBlurFallback = (override) => override ?? false;
|
|
58
|
+
var Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children, contentSize = false, dismissible = true, edgeInset = 0, height = 300, maxHeight, maxWidth, onClose, onClosed, onMeasure, onOpened, open, side = "left", showHandle = true, translateOffset: externalTranslateOffset, width = 300, zIndex = 50 }) => {
|
|
14
59
|
const { colors } = useTheme();
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
60
|
+
const vertical = isVerticalSide(side);
|
|
61
|
+
const openDirection = getOpenDirection(side);
|
|
62
|
+
const closeDirection = -openDirection;
|
|
63
|
+
const [measuredSize, setMeasuredSize] = useState(null);
|
|
64
|
+
const sizeInput = contentSize ? measuredSize ?? (vertical ? height : width) : vertical ? height : width;
|
|
65
|
+
const maxSizeInput = contentSize ? void 0 : vertical ? maxHeight : maxWidth;
|
|
66
|
+
const { closedOffset, effectiveSize, maxEffectiveSize, restOffset } = useDrawerSize(side, sizeInput, maxSizeInput, edgeInset);
|
|
67
|
+
const internalTranslateOffset = useSharedValue(closedOffset);
|
|
68
|
+
const translateOffset = externalTranslateOffset ?? internalTranslateOffset;
|
|
69
|
+
const { autoPaper } = getDrawerConfig();
|
|
70
|
+
const resolvedBlur = (autoPaper?.useBlur ?? useBlurFallback)(blur);
|
|
71
|
+
const animatedSize = useSharedValue(effectiveSize);
|
|
18
72
|
useEffect(() => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
73
|
+
if (open) {
|
|
74
|
+
translateOffset.value = withSpring(restOffset, SPRING, (finished) => {
|
|
75
|
+
if (finished && onOpened) runOnJS(onOpened)();
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
translateOffset.value = withSpring(closedOffset, SPRING, (finished) => {
|
|
80
|
+
if (finished && onClosed) runOnJS(onClosed)();
|
|
81
|
+
});
|
|
82
|
+
}, [open, closedOffset, restOffset, translateOffset]);
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
if (contentSize) {
|
|
85
|
+
animatedSize.value = withTiming(effectiveSize);
|
|
86
|
+
}
|
|
87
|
+
}, [contentSize, effectiveSize, animatedSize]);
|
|
88
|
+
const drawerStyle = useAnimatedStyle(() => ({ transform: [vertical ? { translateY: translateOffset.value } : { translateX: translateOffset.value }] }));
|
|
89
|
+
const backdropStyle = useAnimatedStyle(() => ({ opacity: maxEffectiveSize > 0 ? (1 - Math.abs(translateOffset.value) / maxEffectiveSize) * backdropOpacity : 0 }));
|
|
90
|
+
const animatedSizeStyle = useAnimatedStyle(() => vertical ? { height: animatedSize.value } : { width: animatedSize.value });
|
|
91
|
+
const positionStyle = side === "left" ? styles.anchorLeft : side === "right" ? styles.anchorRight : side === "top" ? styles.anchorTop : styles.anchorBottom;
|
|
92
|
+
const sizeStyle = vertical ? { height: maxEffectiveSize } : { width: maxEffectiveSize };
|
|
93
|
+
const handleLayout = (event) => {
|
|
94
|
+
const measured = vertical ? event.nativeEvent.layout.height : event.nativeEvent.layout.width;
|
|
95
|
+
setMeasuredSize(measured);
|
|
96
|
+
onMeasure?.(measured);
|
|
97
|
+
};
|
|
98
|
+
const fillStyle = StyleSheet.absoluteFill;
|
|
99
|
+
const fill = resolvedBlur && autoPaper ? contentSize ? /* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
100
|
+
/* @__PURE__ */ jsx2(autoPaper.BlurView, { style: fillStyle }),
|
|
101
|
+
/* @__PURE__ */ jsx2(View, { onLayout: handleLayout, children })
|
|
102
|
+
] }) : /* @__PURE__ */ jsx2(autoPaper.BlurView, { style: fillStyle, children }) : contentSize ? /* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
103
|
+
/* @__PURE__ */ jsx2(View, { style: [fillStyle, { backgroundColor: colors.surface }] }),
|
|
104
|
+
/* @__PURE__ */ jsx2(View, { onLayout: handleLayout, children })
|
|
105
|
+
] }) : /* @__PURE__ */ jsx2(View, { style: [fillStyle, { backgroundColor: colors.surface }], children });
|
|
106
|
+
const drawerOuterStyle = contentSize ? [styles.drawer, positionStyle, drawerStyle, animatedSizeStyle, { zIndex: zIndex + 1 }] : [styles.drawer, positionStyle, sizeStyle, drawerStyle, { zIndex: zIndex + 1 }];
|
|
107
|
+
const contentClipStyle = [fillStyle, styles.clip, { flexDirection: vertical ? "column" : "row" }];
|
|
108
|
+
const clampMin = Math.min(0, closedOffset);
|
|
109
|
+
const clampMax = Math.max(0, closedOffset);
|
|
110
|
+
const handleAxisGesture = vertical ? Gesture.Pan().activeOffsetY(restOffset !== 0 ? [-10, 10] : closeDirection * 10).failOffsetX([-15, 15]) : Gesture.Pan().activeOffsetX(restOffset !== 0 ? [-10, 10] : closeDirection * 10).failOffsetY([-15, 15]);
|
|
111
|
+
const gestureStartOffset = useSharedValue(closedOffset);
|
|
112
|
+
const handleGesture = handleAxisGesture.enabled(open).onStart(() => {
|
|
113
|
+
"worklet";
|
|
114
|
+
gestureStartOffset.value = translateOffset.value;
|
|
115
|
+
}).onUpdate((event) => {
|
|
116
|
+
"worklet";
|
|
117
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
118
|
+
translateOffset.value = Math.min(clampMax, Math.max(clampMin, gestureStartOffset.value + translation));
|
|
119
|
+
}).onEnd((event) => {
|
|
120
|
+
"worklet";
|
|
121
|
+
const velocity = vertical ? event.velocityY : event.velocityX;
|
|
122
|
+
const velocityD = velocity * closeDirection;
|
|
123
|
+
const currentD = translateOffset.value * closeDirection;
|
|
124
|
+
const restD = restOffset * closeDirection;
|
|
125
|
+
let target;
|
|
126
|
+
let closing = false;
|
|
127
|
+
if (currentD <= restD) {
|
|
128
|
+
const progress = restD > 0 ? (restD - currentD) / restD : 1;
|
|
129
|
+
target = progress > 1 / 3 || velocityD < -VELOCITY_THRESHOLD ? 0 : restOffset;
|
|
130
|
+
} else {
|
|
131
|
+
const progress = effectiveSize > 0 ? (currentD - restD) / effectiveSize : 1;
|
|
132
|
+
closing = progress > 1 / 3 || velocityD > VELOCITY_THRESHOLD;
|
|
133
|
+
target = closing ? closedOffset : restOffset;
|
|
134
|
+
}
|
|
135
|
+
translateOffset.value = withSpring(target, SPRING);
|
|
136
|
+
if (closing) runOnJS(onClose)();
|
|
137
|
+
});
|
|
138
|
+
const HANDLE_STRIP_THICKNESS = 28;
|
|
139
|
+
const HANDLE_HIT_SLOP = 20;
|
|
140
|
+
const HANDLE_STRIP_TOTAL_THICKNESS = HANDLE_STRIP_THICKNESS + HANDLE_HIT_SLOP;
|
|
141
|
+
const handleStripSizeStyle = vertical ? { height: HANDLE_STRIP_TOTAL_THICKNESS, left: 0, right: 0 } : { bottom: 0, top: 0, width: HANDLE_STRIP_TOTAL_THICKNESS };
|
|
142
|
+
const oppositeSide = side === "left" ? "right" : side === "right" ? "left" : side === "top" ? "bottom" : "top";
|
|
143
|
+
const handleStripEdgeStyle = { [oppositeSide]: -HANDLE_STRIP_THICKNESS };
|
|
144
|
+
const handleStripPaddingStyle = side === "left" ? { paddingLeft: HANDLE_HIT_SLOP } : side === "right" ? { paddingRight: HANDLE_HIT_SLOP } : side === "top" ? { paddingTop: HANDLE_HIT_SLOP } : { paddingBottom: HANDLE_HIT_SLOP };
|
|
145
|
+
const handlePillStyle = vertical ? { height: 6, width: 64 } : { height: 64, width: 6 };
|
|
146
|
+
const webCursorStyle = { cursor: Platform.OS === "web" ? "pointer" : void 0 };
|
|
147
|
+
const handleVisibilityStyle = useAnimatedStyle(() => {
|
|
148
|
+
const visibleSize = maxEffectiveSize - translateOffset.value * closeDirection;
|
|
149
|
+
return { opacity: visibleSize >= HANDLE_STRIP_THICKNESS ? 1 : 0 };
|
|
150
|
+
});
|
|
151
|
+
return /* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
152
|
+
/* @__PURE__ */ jsx2(Animated.View, { style: [styles.backdrop, { zIndex }, backdropStyle], pointerEvents: open && blockingBackdrop ? "auto" : "none", children: /* @__PURE__ */ jsx2(Pressable, { style: StyleSheet.absoluteFill, onPress: onClose }) }),
|
|
153
|
+
/* @__PURE__ */ jsxs(Animated.View, { style: drawerOuterStyle, children: [
|
|
154
|
+
contentSize ? /* @__PURE__ */ jsx2(View, { style: contentClipStyle, children: fill }) : fill,
|
|
155
|
+
dismissible && /* @__PURE__ */ jsx2(GestureDetector, { gesture: handleGesture, children: /* @__PURE__ */ jsx2(Animated.View, { style: [styles.handleStrip, handleStripSizeStyle, handleStripEdgeStyle, handleStripPaddingStyle, webCursorStyle, handleVisibilityStyle, { zIndex: zIndex + 2 }], children: showHandle && /* @__PURE__ */ jsx2(View, { style: [styles.handlePill, handlePillStyle, { backgroundColor: colors.surface }] }) }) })
|
|
156
|
+
] })
|
|
27
157
|
] });
|
|
28
158
|
};
|
|
29
159
|
var styles = StyleSheet.create({
|
|
160
|
+
anchorBottom: {
|
|
161
|
+
bottom: 0,
|
|
162
|
+
left: 0,
|
|
163
|
+
right: 0
|
|
164
|
+
},
|
|
30
165
|
anchorLeft: {
|
|
31
|
-
|
|
166
|
+
bottom: 0,
|
|
167
|
+
left: 0,
|
|
168
|
+
top: 0
|
|
32
169
|
},
|
|
33
170
|
anchorRight: {
|
|
34
|
-
|
|
171
|
+
bottom: 0,
|
|
172
|
+
right: 0,
|
|
173
|
+
top: 0
|
|
174
|
+
},
|
|
175
|
+
anchorTop: {
|
|
176
|
+
left: 0,
|
|
177
|
+
right: 0,
|
|
178
|
+
top: 0
|
|
35
179
|
},
|
|
36
180
|
backdrop: {
|
|
37
181
|
backgroundColor: "#000",
|
|
@@ -39,47 +183,68 @@ var styles = StyleSheet.create({
|
|
|
39
183
|
left: 0,
|
|
40
184
|
position: "absolute",
|
|
41
185
|
right: 0,
|
|
42
|
-
top: 0
|
|
43
|
-
|
|
186
|
+
top: 0
|
|
187
|
+
},
|
|
188
|
+
clip: {
|
|
189
|
+
overflow: "hidden"
|
|
44
190
|
},
|
|
45
191
|
drawer: {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
192
|
+
position: "absolute"
|
|
193
|
+
},
|
|
194
|
+
handlePill: {
|
|
195
|
+
borderRadius: 3
|
|
196
|
+
},
|
|
197
|
+
handleStrip: {
|
|
198
|
+
alignItems: "center",
|
|
199
|
+
justifyContent: "center",
|
|
200
|
+
position: "absolute"
|
|
50
201
|
}
|
|
51
202
|
});
|
|
52
203
|
|
|
53
204
|
// src/DrawerEdgeSwipe.tsx
|
|
54
|
-
import { StyleSheet as StyleSheet2, View } from "react-native";
|
|
55
|
-
import { Gesture, GestureDetector } from "react-native-gesture-handler";
|
|
56
|
-
import { runOnJS, withSpring as withSpring2 } from "react-native-reanimated";
|
|
57
|
-
import { jsx as
|
|
205
|
+
import { StyleSheet as StyleSheet2, View as View2 } from "react-native";
|
|
206
|
+
import { Gesture as Gesture2, GestureDetector as GestureDetector2 } from "react-native-gesture-handler";
|
|
207
|
+
import { runOnJS as runOnJS2, withSpring as withSpring2 } from "react-native-reanimated";
|
|
208
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
58
209
|
var EDGE_WIDTH = 24;
|
|
59
|
-
var SPRING2 = { damping:
|
|
60
|
-
var
|
|
61
|
-
var DrawerEdgeSwipe = ({ enabled = true, onOpen, side = "left",
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
const
|
|
66
|
-
const
|
|
210
|
+
var SPRING2 = { damping: 45, overshootClamping: true, stiffness: 500, restDisplacementThreshold: 0.5, restSpeedThreshold: 10 };
|
|
211
|
+
var VELOCITY_THRESHOLD2 = 500;
|
|
212
|
+
var DrawerEdgeSwipe = ({ edgeInset = 0, enabled = true, height = 300, maxHeight, maxWidth, onOpen, side = "left", translateOffset, width = 300 }) => {
|
|
213
|
+
const vertical = isVerticalSide(side);
|
|
214
|
+
const { closedOffset, effectiveSize, restOffset } = useDrawerSize(side, vertical ? height : width, vertical ? maxHeight : maxWidth, edgeInset);
|
|
215
|
+
const openDirection = getOpenDirection(side);
|
|
216
|
+
const clampMin = Math.min(0, closedOffset);
|
|
217
|
+
const clampMax = Math.max(0, closedOffset);
|
|
218
|
+
const axisGesture = vertical ? Gesture2.Pan().activeOffsetY(side === "top" ? 10 : -10).failOffsetX([-15, 15]) : Gesture2.Pan().activeOffsetX(side === "left" ? 10 : -10).failOffsetY([-15, 15]);
|
|
219
|
+
const gesture = axisGesture.enabled(enabled).onUpdate((event) => {
|
|
67
220
|
"worklet";
|
|
68
|
-
|
|
221
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
222
|
+
translateOffset.value = Math.min(clampMax, Math.max(clampMin, closedOffset + translation));
|
|
69
223
|
}).onEnd((event) => {
|
|
70
224
|
"worklet";
|
|
71
|
-
const
|
|
225
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
226
|
+
const velocity = vertical ? event.velocityY : event.velocityX;
|
|
227
|
+
const commit = translation * openDirection > effectiveSize / 3 || velocity * openDirection > VELOCITY_THRESHOLD2;
|
|
72
228
|
if (commit) {
|
|
73
|
-
|
|
74
|
-
|
|
229
|
+
translateOffset.value = withSpring2(restOffset, SPRING2);
|
|
230
|
+
runOnJS2(onOpen)();
|
|
75
231
|
} else {
|
|
76
|
-
|
|
232
|
+
translateOffset.value = withSpring2(closedOffset, SPRING2);
|
|
77
233
|
}
|
|
78
234
|
});
|
|
79
|
-
|
|
235
|
+
const edgeStyle = vertical ? [styles2.edgeHorizontal, side === "top" ? styles2.top : styles2.bottom] : [styles2.edgeVertical, side === "left" ? styles2.left : styles2.right];
|
|
236
|
+
return /* @__PURE__ */ jsx3(GestureDetector2, { gesture, children: /* @__PURE__ */ jsx3(View2, { style: edgeStyle }) });
|
|
80
237
|
};
|
|
81
238
|
var styles2 = StyleSheet2.create({
|
|
82
|
-
|
|
239
|
+
bottom: { bottom: 0 },
|
|
240
|
+
edgeHorizontal: {
|
|
241
|
+
height: EDGE_WIDTH,
|
|
242
|
+
left: 0,
|
|
243
|
+
position: "absolute",
|
|
244
|
+
right: 0,
|
|
245
|
+
zIndex: 5
|
|
246
|
+
},
|
|
247
|
+
edgeVertical: {
|
|
83
248
|
bottom: 0,
|
|
84
249
|
position: "absolute",
|
|
85
250
|
top: 0,
|
|
@@ -87,43 +252,83 @@ var styles2 = StyleSheet2.create({
|
|
|
87
252
|
zIndex: 5
|
|
88
253
|
},
|
|
89
254
|
left: { left: 0 },
|
|
90
|
-
right: { right: 0 }
|
|
255
|
+
right: { right: 0 },
|
|
256
|
+
top: { top: 0 }
|
|
91
257
|
});
|
|
92
258
|
|
|
93
259
|
// src/createDrawer.tsx
|
|
94
|
-
import { jsx as
|
|
260
|
+
import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
95
261
|
var noop = () => {
|
|
96
262
|
};
|
|
97
|
-
var createDrawer = ({ side = "left", width = 300 } = {}) => {
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
263
|
+
var createDrawer = ({ backdropOpacity: factoryBackdropOpacity, blockingBackdrop: factoryBlockingBackdrop, blur: factoryBlur, closeOnBackPress: factoryCloseOnBackPress, contentSize: factoryContentSize, dismissible: factoryDismissible, edgeInset = 0, height = 300, maxHeight, maxWidth, showHandle: factoryShowHandle, side = "left", width = 300, zIndex: factoryZIndex } = {}) => {
|
|
264
|
+
const defaultExpandProgress = { value: 1 };
|
|
265
|
+
const DrawerActionsContext = createContext({ close: noop, expandProgress: defaultExpandProgress, isOpen: false, isVisible: false, open: noop });
|
|
266
|
+
const vertical = isVerticalSide(side);
|
|
267
|
+
const size = vertical ? height : width;
|
|
268
|
+
const maxSize = vertical ? maxHeight : maxWidth;
|
|
269
|
+
const closeDirection = -getOpenDirection(side);
|
|
270
|
+
const DrawerInstanceProvider = ({ backdropOpacity: backdropOpacityOverride, blockingBackdrop: blockingBackdropOverride, blur: blurOverride, children, closeOnBackPress: closeOnBackPressOverride, content, contentSize: contentSizeOverride, dismissible: dismissibleOverride, enabled = true, showHandle: showHandleOverride, zIndex: zIndexOverride }) => {
|
|
271
|
+
const [isOpen, setIsOpen] = useState2(false);
|
|
272
|
+
const [isVisible, setIsVisible] = useState2(false);
|
|
273
|
+
const [measuredSize, setMeasuredSize] = useState2(null);
|
|
274
|
+
const backdropOpacity = backdropOpacityOverride ?? factoryBackdropOpacity ?? 0.45;
|
|
275
|
+
const blur = blurOverride ?? factoryBlur;
|
|
276
|
+
const blockingBackdrop = blockingBackdropOverride ?? factoryBlockingBackdrop ?? true;
|
|
277
|
+
const closeOnBackPress = closeOnBackPressOverride ?? factoryCloseOnBackPress ?? true;
|
|
278
|
+
const contentSize = contentSizeOverride ?? factoryContentSize ?? false;
|
|
279
|
+
const dismissible = dismissibleOverride ?? factoryDismissible ?? true;
|
|
280
|
+
const showHandle = showHandleOverride ?? factoryShowHandle ?? true;
|
|
281
|
+
const zIndex = zIndexOverride ?? factoryZIndex ?? 50;
|
|
282
|
+
const effectiveSize = contentSize ? measuredSize ?? size : size;
|
|
283
|
+
const { closedOffset, restOffset } = useDrawerSize(side, effectiveSize, contentSize ? void 0 : maxSize, edgeInset);
|
|
284
|
+
const translateOffset = useSharedValue2(closedOffset);
|
|
285
|
+
const expandProgress = useDerivedValue(() => {
|
|
286
|
+
const currentD = translateOffset.value * closeDirection;
|
|
287
|
+
const restD = restOffset * closeDirection;
|
|
288
|
+
if (restD <= 0) return 1;
|
|
289
|
+
return Math.min(1, Math.max(0, (restD - currentD) / restD));
|
|
290
|
+
});
|
|
291
|
+
const open = () => {
|
|
292
|
+
setIsOpen(true);
|
|
293
|
+
setIsVisible(true);
|
|
294
|
+
};
|
|
103
295
|
const actions = useMemo(
|
|
104
296
|
() => ({
|
|
105
297
|
close: () => setIsOpen(false),
|
|
298
|
+
expandProgress,
|
|
106
299
|
isOpen,
|
|
107
|
-
|
|
300
|
+
isVisible,
|
|
301
|
+
open
|
|
108
302
|
}),
|
|
109
|
-
[isOpen]
|
|
303
|
+
[expandProgress, isOpen, isVisible]
|
|
110
304
|
);
|
|
305
|
+
useEffect2(() => {
|
|
306
|
+
if (!isOpen || !closeOnBackPress) return;
|
|
307
|
+
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
|
|
308
|
+
setIsOpen(false);
|
|
309
|
+
return true;
|
|
310
|
+
});
|
|
311
|
+
return () => subscription.remove();
|
|
312
|
+
}, [isOpen, closeOnBackPress]);
|
|
111
313
|
return /* @__PURE__ */ jsxs2(DrawerActionsContext.Provider, { value: actions, children: [
|
|
112
314
|
children,
|
|
113
|
-
/* @__PURE__ */
|
|
114
|
-
/* @__PURE__ */
|
|
315
|
+
/* @__PURE__ */ jsx4(DrawerEdgeSwipe, { edgeInset, enabled: enabled && !isOpen, height: vertical ? effectiveSize : height, maxHeight, maxWidth, onOpen: open, side, translateOffset, width: vertical ? width : effectiveSize }),
|
|
316
|
+
/* @__PURE__ */ jsx4(Drawer, { backdropOpacity, blockingBackdrop, blur, contentSize, dismissible, edgeInset, height: vertical ? effectiveSize : height, maxHeight, maxWidth, onClose: () => setIsOpen(false), onClosed: () => setIsVisible(false), onMeasure: setMeasuredSize, open: isOpen, showHandle, side, translateOffset, width: vertical ? width : effectiveSize, zIndex, children: content })
|
|
115
317
|
] });
|
|
116
318
|
};
|
|
117
319
|
const useDrawer = () => useContext(DrawerActionsContext);
|
|
118
|
-
return {
|
|
320
|
+
return { DrawerInstanceProvider, useDrawer };
|
|
119
321
|
};
|
|
120
322
|
var combineDrawerProviders = (...entries) => {
|
|
121
|
-
const CombinedDrawerProviders = ({ children }) => entries.reduceRight((acc, [Provider, props]) => /* @__PURE__ */
|
|
323
|
+
const CombinedDrawerProviders = ({ children }) => entries.reduceRight((acc, [Provider, props]) => /* @__PURE__ */ jsx4(Provider, { ...props, children: acc }), children);
|
|
122
324
|
return CombinedDrawerProviders;
|
|
123
325
|
};
|
|
124
326
|
export {
|
|
125
327
|
Drawer,
|
|
126
328
|
DrawerEdgeSwipe,
|
|
329
|
+
DrawerProvider,
|
|
127
330
|
combineDrawerProviders,
|
|
128
|
-
|
|
331
|
+
configureDrawer,
|
|
332
|
+
createDrawer,
|
|
333
|
+
getDrawerConfig
|
|
129
334
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rific/drawer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Left/right sliding drawer with edge-swipe gestures for React Native
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Left/right sliding drawer with edge-swipe gestures for React Native: spring-animated, theme-aware, one factory call per drawer instance",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
7
7
|
"expo",
|
|
@@ -28,14 +28,15 @@
|
|
|
28
28
|
"type": "commonjs",
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
|
-
"react-native": "./
|
|
31
|
+
"react-native": "./src/index.ts",
|
|
32
32
|
"types": "./dist/index.d.ts",
|
|
33
|
-
"import": "./dist/index.
|
|
33
|
+
"import": "./dist/index.mjs",
|
|
34
34
|
"require": "./dist/index.js"
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"main": "dist/index.js",
|
|
38
|
-
"module": "dist/index.
|
|
38
|
+
"module": "dist/index.mjs",
|
|
39
|
+
"react-native": "src/index.ts",
|
|
39
40
|
"types": "dist/index.d.ts",
|
|
40
41
|
"files": [
|
|
41
42
|
"dist",
|
|
@@ -59,6 +60,7 @@
|
|
|
59
60
|
"preversion": "npm run lint && npm test && npm run build"
|
|
60
61
|
},
|
|
61
62
|
"devDependencies": {
|
|
63
|
+
"@rific/auto-paper": "^0.7.0",
|
|
62
64
|
"@rific/haptic-press": "^0.4.3",
|
|
63
65
|
"@testing-library/dom": "^10.4.1",
|
|
64
66
|
"@testing-library/react": "^16.3.2",
|
|
@@ -76,10 +78,11 @@
|
|
|
76
78
|
"prettier": "^3.8.3",
|
|
77
79
|
"react": "^19.0.0",
|
|
78
80
|
"react-dom": "^19.0.0",
|
|
79
|
-
"react-native": "^0.
|
|
81
|
+
"react-native": "^0.85.3",
|
|
80
82
|
"react-native-gesture-handler": "^2.20.0",
|
|
81
83
|
"react-native-paper": "^5.12.0",
|
|
82
|
-
"react-native-reanimated": "^
|
|
84
|
+
"react-native-reanimated": "^4.5.1",
|
|
85
|
+
"react-native-worklets": "^0.10.3",
|
|
83
86
|
"ts-jest": "^29.4.9",
|
|
84
87
|
"ts-node": "^10.9.2",
|
|
85
88
|
"tsup": "^8.0.0",
|
|
@@ -87,12 +90,19 @@
|
|
|
87
90
|
"typescript-eslint": "^8.59.3"
|
|
88
91
|
},
|
|
89
92
|
"peerDependencies": {
|
|
93
|
+
"@rific/auto-paper": ">=0.7.0",
|
|
90
94
|
"@rific/haptic-press": ">=0.4.0",
|
|
91
|
-
"react": ">=
|
|
92
|
-
"react-native": ">=0.
|
|
93
|
-
"react-native-gesture-handler": ">=2.20.0",
|
|
95
|
+
"react": ">=19.0.0",
|
|
96
|
+
"react-native": ">=0.83.0",
|
|
97
|
+
"react-native-gesture-handler": ">=2.20.0 <3.0.0",
|
|
94
98
|
"react-native-paper": ">=5.0.0",
|
|
95
|
-
"react-native-reanimated": ">=
|
|
99
|
+
"react-native-reanimated": ">=4.0.0",
|
|
100
|
+
"react-native-worklets": "0.10.x"
|
|
101
|
+
},
|
|
102
|
+
"peerDependenciesMeta": {
|
|
103
|
+
"@rific/auto-paper": {
|
|
104
|
+
"optional": true
|
|
105
|
+
}
|
|
96
106
|
},
|
|
97
107
|
"publishConfig": {
|
|
98
108
|
"access": "public"
|