@rific/drawer 0.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.
- package/LICENSE +21 -0
- package/README.md +62 -0
- package/dist/index.d.mts +48 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +167 -0
- package/dist/index.mjs +127 -0
- package/package.json +100 -0
- package/src/Drawer.tsx +65 -0
- package/src/DrawerEdgeSwipe.tsx +57 -0
- package/src/createDrawer.tsx +78 -0
- package/src/index.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jay Deaton
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @rific/drawer
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @rific/drawer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependencies:**
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install react-native-gesture-handler react-native-reanimated react-native-paper @rific/haptic-press
|
|
15
|
+
```
|
|
16
|
+
|
|
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
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { createDrawer } from '@rific/drawer'
|
|
23
|
+
|
|
24
|
+
const { DrawerProvider: NavDrawerProvider, useDrawer: useNavDrawer } = createDrawer({ side: 'left', width: 300 })
|
|
25
|
+
const { DrawerProvider: SettingsDrawerProvider, useDrawer: useSettingsDrawer } = createDrawer({ side: 'right', width: 320 })
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Mount each `DrawerProvider` once, near your app root — `children` is the rest of the app (so `useDrawer()` is reachable from anywhere inside it), `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
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import { combineDrawerProviders } from '@rific/drawer'
|
|
32
|
+
|
|
33
|
+
const AllDrawersProvider = combineDrawerProviders([NavDrawerProvider, { content: <AppDrawerContent /> }], [SettingsDrawerProvider, { content: <SettingsDrawerContent /> }])
|
|
34
|
+
|
|
35
|
+
export default function RootLayout() {
|
|
36
|
+
return (
|
|
37
|
+
<GestureHandlerRootView style={{ flex: 1 }}>
|
|
38
|
+
<AllDrawersProvider>
|
|
39
|
+
<RootNavigator />
|
|
40
|
+
</AllDrawersProvider>
|
|
41
|
+
</GestureHandlerRootView>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
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
|
+
|
|
48
|
+
Then anywhere else in the tree:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { useNavDrawer } from '@/drawers'
|
|
52
|
+
|
|
53
|
+
const { isOpen, open, close } = useNavDrawer()
|
|
54
|
+
|
|
55
|
+
<IconButton icon='menu' onPress={open} />
|
|
56
|
+
```
|
|
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 — no extra wiring 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
|
+
## Lower-level pieces
|
|
61
|
+
|
|
62
|
+
`Drawer` (the sliding panel + 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.d.mts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, JSX } from 'react';
|
|
3
|
+
import { SharedValue } from 'react-native-reanimated';
|
|
4
|
+
|
|
5
|
+
type DrawerActions = {
|
|
6
|
+
close: () => void;
|
|
7
|
+
isOpen: boolean;
|
|
8
|
+
open: () => void;
|
|
9
|
+
};
|
|
10
|
+
type CreateDrawerOptions = {
|
|
11
|
+
side?: 'left' | 'right';
|
|
12
|
+
width?: number;
|
|
13
|
+
};
|
|
14
|
+
type DrawerProviderProps = {
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
content?: ReactNode;
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
};
|
|
19
|
+
type CreateDrawerResult = {
|
|
20
|
+
DrawerProvider: (props: DrawerProviderProps) => JSX.Element;
|
|
21
|
+
useDrawer: () => DrawerActions;
|
|
22
|
+
};
|
|
23
|
+
declare const createDrawer: ({ side, width }?: CreateDrawerOptions) => CreateDrawerResult;
|
|
24
|
+
type DrawerProviderComponent = (props: DrawerProviderProps) => JSX.Element;
|
|
25
|
+
declare const combineDrawerProviders: (...entries: [DrawerProviderComponent, Omit<DrawerProviderProps, "children">?][]) => ({ children }: {
|
|
26
|
+
children?: ReactNode;
|
|
27
|
+
}) => JSX.Element;
|
|
28
|
+
|
|
29
|
+
type DrawerProps = {
|
|
30
|
+
children?: ReactNode;
|
|
31
|
+
onClose: () => void;
|
|
32
|
+
open: boolean;
|
|
33
|
+
side?: 'left' | 'right';
|
|
34
|
+
translateX?: SharedValue<number>;
|
|
35
|
+
width?: number;
|
|
36
|
+
};
|
|
37
|
+
declare const Drawer: ({ children, onClose, open, side, translateX: externalTranslateX, width }: DrawerProps) => react.JSX.Element;
|
|
38
|
+
|
|
39
|
+
type DrawerEdgeSwipeProps = {
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
onOpen: () => void;
|
|
42
|
+
side?: 'left' | 'right';
|
|
43
|
+
translateX: SharedValue<number>;
|
|
44
|
+
width: number;
|
|
45
|
+
};
|
|
46
|
+
declare const DrawerEdgeSwipe: ({ enabled, onOpen, side, translateX, width }: DrawerEdgeSwipeProps) => react.JSX.Element;
|
|
47
|
+
|
|
48
|
+
export { type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerProps, type DrawerProviderComponent, type DrawerProviderProps, combineDrawerProviders, createDrawer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { ReactNode, JSX } from 'react';
|
|
3
|
+
import { SharedValue } from 'react-native-reanimated';
|
|
4
|
+
|
|
5
|
+
type DrawerActions = {
|
|
6
|
+
close: () => void;
|
|
7
|
+
isOpen: boolean;
|
|
8
|
+
open: () => void;
|
|
9
|
+
};
|
|
10
|
+
type CreateDrawerOptions = {
|
|
11
|
+
side?: 'left' | 'right';
|
|
12
|
+
width?: number;
|
|
13
|
+
};
|
|
14
|
+
type DrawerProviderProps = {
|
|
15
|
+
children?: ReactNode;
|
|
16
|
+
content?: ReactNode;
|
|
17
|
+
enabled?: boolean;
|
|
18
|
+
};
|
|
19
|
+
type CreateDrawerResult = {
|
|
20
|
+
DrawerProvider: (props: DrawerProviderProps) => JSX.Element;
|
|
21
|
+
useDrawer: () => DrawerActions;
|
|
22
|
+
};
|
|
23
|
+
declare const createDrawer: ({ side, width }?: CreateDrawerOptions) => CreateDrawerResult;
|
|
24
|
+
type DrawerProviderComponent = (props: DrawerProviderProps) => JSX.Element;
|
|
25
|
+
declare const combineDrawerProviders: (...entries: [DrawerProviderComponent, Omit<DrawerProviderProps, "children">?][]) => ({ children }: {
|
|
26
|
+
children?: ReactNode;
|
|
27
|
+
}) => JSX.Element;
|
|
28
|
+
|
|
29
|
+
type DrawerProps = {
|
|
30
|
+
children?: ReactNode;
|
|
31
|
+
onClose: () => void;
|
|
32
|
+
open: boolean;
|
|
33
|
+
side?: 'left' | 'right';
|
|
34
|
+
translateX?: SharedValue<number>;
|
|
35
|
+
width?: number;
|
|
36
|
+
};
|
|
37
|
+
declare const Drawer: ({ children, onClose, open, side, translateX: externalTranslateX, width }: DrawerProps) => react.JSX.Element;
|
|
38
|
+
|
|
39
|
+
type DrawerEdgeSwipeProps = {
|
|
40
|
+
enabled?: boolean;
|
|
41
|
+
onOpen: () => void;
|
|
42
|
+
side?: 'left' | 'right';
|
|
43
|
+
translateX: SharedValue<number>;
|
|
44
|
+
width: number;
|
|
45
|
+
};
|
|
46
|
+
declare const DrawerEdgeSwipe: ({ enabled, onOpen, side, translateX, width }: DrawerEdgeSwipeProps) => react.JSX.Element;
|
|
47
|
+
|
|
48
|
+
export { type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerProps, type DrawerProviderComponent, type DrawerProviderProps, combineDrawerProviders, createDrawer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
Drawer: () => Drawer,
|
|
34
|
+
DrawerEdgeSwipe: () => DrawerEdgeSwipe,
|
|
35
|
+
combineDrawerProviders: () => combineDrawerProviders,
|
|
36
|
+
createDrawer: () => createDrawer
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
|
|
40
|
+
// src/createDrawer.tsx
|
|
41
|
+
var import_react2 = require("react");
|
|
42
|
+
var import_react_native_reanimated3 = require("react-native-reanimated");
|
|
43
|
+
|
|
44
|
+
// src/Drawer.tsx
|
|
45
|
+
var import_haptic_press = require("@rific/haptic-press");
|
|
46
|
+
var import_react = require("react");
|
|
47
|
+
var import_react_native = require("react-native");
|
|
48
|
+
var import_react_native_paper = require("react-native-paper");
|
|
49
|
+
var import_react_native_reanimated = __toESM(require("react-native-reanimated"));
|
|
50
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
|
+
var SPRING = { damping: 40, stiffness: 300, overshootClamping: true };
|
|
52
|
+
var Drawer = ({ children, onClose, open, side = "left", translateX: externalTranslateX, width = 300 }) => {
|
|
53
|
+
const { colors } = (0, import_react_native_paper.useTheme)();
|
|
54
|
+
const closedX = side === "left" ? -width : width;
|
|
55
|
+
const internalTranslateX = (0, import_react_native_reanimated.useSharedValue)(closedX);
|
|
56
|
+
const translateX = externalTranslateX ?? internalTranslateX;
|
|
57
|
+
(0, import_react.useEffect)(() => {
|
|
58
|
+
translateX.value = (0, import_react_native_reanimated.withSpring)(open ? 0 : closedX, SPRING);
|
|
59
|
+
}, [open, closedX, translateX]);
|
|
60
|
+
const drawerStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({ transform: [{ translateX: translateX.value }] }));
|
|
61
|
+
const backdropStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({ opacity: (1 - Math.abs(translateX.value) / width) * 0.45 }));
|
|
62
|
+
const positionStyle = side === "left" ? styles.anchorLeft : styles.anchorRight;
|
|
63
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
64
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { style: [styles.backdrop, backdropStyle], pointerEvents: open ? "auto" : "none", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_haptic_press.Pressable, { style: import_react_native.StyleSheet.absoluteFill, onPress: onClose }) }),
|
|
65
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { style: [styles.drawer, positionStyle, { backgroundColor: colors.surface, width }, drawerStyle], children })
|
|
66
|
+
] });
|
|
67
|
+
};
|
|
68
|
+
var styles = import_react_native.StyleSheet.create({
|
|
69
|
+
anchorLeft: {
|
|
70
|
+
left: 0
|
|
71
|
+
},
|
|
72
|
+
anchorRight: {
|
|
73
|
+
right: 0
|
|
74
|
+
},
|
|
75
|
+
backdrop: {
|
|
76
|
+
backgroundColor: "#000",
|
|
77
|
+
bottom: 0,
|
|
78
|
+
left: 0,
|
|
79
|
+
position: "absolute",
|
|
80
|
+
right: 0,
|
|
81
|
+
top: 0,
|
|
82
|
+
zIndex: 50
|
|
83
|
+
},
|
|
84
|
+
drawer: {
|
|
85
|
+
bottom: 0,
|
|
86
|
+
position: "absolute",
|
|
87
|
+
top: 0,
|
|
88
|
+
zIndex: 51
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// src/DrawerEdgeSwipe.tsx
|
|
93
|
+
var import_react_native2 = require("react-native");
|
|
94
|
+
var import_react_native_gesture_handler = require("react-native-gesture-handler");
|
|
95
|
+
var import_react_native_reanimated2 = require("react-native-reanimated");
|
|
96
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
97
|
+
var EDGE_WIDTH = 24;
|
|
98
|
+
var SPRING2 = { damping: 40, overshootClamping: true, stiffness: 300 };
|
|
99
|
+
var VELOCITY_THRESHOLD = 500;
|
|
100
|
+
var DrawerEdgeSwipe = ({ enabled = true, onOpen, side = "left", translateX, width }) => {
|
|
101
|
+
const closedX = side === "left" ? -width : width;
|
|
102
|
+
const openDirection = side === "left" ? 1 : -1;
|
|
103
|
+
const clampMin = Math.min(0, closedX);
|
|
104
|
+
const clampMax = Math.max(0, closedX);
|
|
105
|
+
const gesture = import_react_native_gesture_handler.Gesture.Pan().enabled(enabled).activeOffsetX(side === "left" ? 10 : -10).failOffsetY([-15, 15]).onUpdate((event) => {
|
|
106
|
+
translateX.value = Math.min(clampMax, Math.max(clampMin, closedX + event.translationX));
|
|
107
|
+
}).onEnd((event) => {
|
|
108
|
+
const commit = event.translationX * openDirection > width / 3 || event.velocityX * openDirection > VELOCITY_THRESHOLD;
|
|
109
|
+
if (commit) {
|
|
110
|
+
translateX.value = (0, import_react_native_reanimated2.withSpring)(0, SPRING2);
|
|
111
|
+
(0, import_react_native_reanimated2.runOnJS)(onOpen)();
|
|
112
|
+
} else {
|
|
113
|
+
translateX.value = (0, import_react_native_reanimated2.withSpring)(closedX, SPRING2);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native_gesture_handler.GestureDetector, { gesture, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native2.View, { style: [styles2.edge, side === "left" ? styles2.left : styles2.right] }) });
|
|
117
|
+
};
|
|
118
|
+
var styles2 = import_react_native2.StyleSheet.create({
|
|
119
|
+
edge: {
|
|
120
|
+
bottom: 0,
|
|
121
|
+
position: "absolute",
|
|
122
|
+
top: 0,
|
|
123
|
+
width: EDGE_WIDTH,
|
|
124
|
+
zIndex: 5
|
|
125
|
+
},
|
|
126
|
+
left: { left: 0 },
|
|
127
|
+
right: { right: 0 }
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// src/createDrawer.tsx
|
|
131
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
132
|
+
var noop = () => {
|
|
133
|
+
};
|
|
134
|
+
var createDrawer = ({ side = "left", width = 300 } = {}) => {
|
|
135
|
+
const DrawerActionsContext = (0, import_react2.createContext)({ close: noop, isOpen: false, open: noop });
|
|
136
|
+
const DrawerProvider = ({ children, content, enabled = true }) => {
|
|
137
|
+
const [isOpen, setIsOpen] = (0, import_react2.useState)(false);
|
|
138
|
+
const closedX = side === "left" ? -width : width;
|
|
139
|
+
const translateX = (0, import_react_native_reanimated3.useSharedValue)(closedX);
|
|
140
|
+
const actions = (0, import_react2.useMemo)(
|
|
141
|
+
() => ({
|
|
142
|
+
close: () => setIsOpen(false),
|
|
143
|
+
isOpen,
|
|
144
|
+
open: () => setIsOpen(true)
|
|
145
|
+
}),
|
|
146
|
+
[isOpen]
|
|
147
|
+
);
|
|
148
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(DrawerActionsContext.Provider, { value: actions, children: [
|
|
149
|
+
children,
|
|
150
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DrawerEdgeSwipe, { enabled: enabled && !isOpen, onOpen: () => setIsOpen(true), side, translateX, width }),
|
|
151
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Drawer, { onClose: () => setIsOpen(false), open: isOpen, side, translateX, width, children: content })
|
|
152
|
+
] });
|
|
153
|
+
};
|
|
154
|
+
const useDrawer = () => (0, import_react2.useContext)(DrawerActionsContext);
|
|
155
|
+
return { DrawerProvider, useDrawer };
|
|
156
|
+
};
|
|
157
|
+
var combineDrawerProviders = (...entries) => {
|
|
158
|
+
const CombinedDrawerProviders = ({ children }) => entries.reduceRight((acc, [Provider, props]) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Provider, { ...props, children: acc }), children);
|
|
159
|
+
return CombinedDrawerProviders;
|
|
160
|
+
};
|
|
161
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
162
|
+
0 && (module.exports = {
|
|
163
|
+
Drawer,
|
|
164
|
+
DrawerEdgeSwipe,
|
|
165
|
+
combineDrawerProviders,
|
|
166
|
+
createDrawer
|
|
167
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// src/createDrawer.tsx
|
|
2
|
+
import { createContext, useContext, useMemo, useState } from "react";
|
|
3
|
+
import { useSharedValue as useSharedValue2 } from "react-native-reanimated";
|
|
4
|
+
|
|
5
|
+
// src/Drawer.tsx
|
|
6
|
+
import { Pressable } from "@rific/haptic-press";
|
|
7
|
+
import { useEffect } from "react";
|
|
8
|
+
import { StyleSheet } from "react-native";
|
|
9
|
+
import { useTheme } from "react-native-paper";
|
|
10
|
+
import Animated, { useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated";
|
|
11
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
12
|
+
var SPRING = { damping: 40, stiffness: 300, overshootClamping: true };
|
|
13
|
+
var Drawer = ({ children, onClose, open, side = "left", translateX: externalTranslateX, width = 300 }) => {
|
|
14
|
+
const { colors } = useTheme();
|
|
15
|
+
const closedX = side === "left" ? -width : width;
|
|
16
|
+
const internalTranslateX = useSharedValue(closedX);
|
|
17
|
+
const translateX = externalTranslateX ?? internalTranslateX;
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
translateX.value = withSpring(open ? 0 : closedX, SPRING);
|
|
20
|
+
}, [open, closedX, translateX]);
|
|
21
|
+
const drawerStyle = useAnimatedStyle(() => ({ transform: [{ translateX: translateX.value }] }));
|
|
22
|
+
const backdropStyle = useAnimatedStyle(() => ({ opacity: (1 - Math.abs(translateX.value) / width) * 0.45 }));
|
|
23
|
+
const positionStyle = side === "left" ? styles.anchorLeft : styles.anchorRight;
|
|
24
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
25
|
+
/* @__PURE__ */ jsx(Animated.View, { style: [styles.backdrop, backdropStyle], pointerEvents: open ? "auto" : "none", children: /* @__PURE__ */ jsx(Pressable, { style: StyleSheet.absoluteFill, onPress: onClose }) }),
|
|
26
|
+
/* @__PURE__ */ jsx(Animated.View, { style: [styles.drawer, positionStyle, { backgroundColor: colors.surface, width }, drawerStyle], children })
|
|
27
|
+
] });
|
|
28
|
+
};
|
|
29
|
+
var styles = StyleSheet.create({
|
|
30
|
+
anchorLeft: {
|
|
31
|
+
left: 0
|
|
32
|
+
},
|
|
33
|
+
anchorRight: {
|
|
34
|
+
right: 0
|
|
35
|
+
},
|
|
36
|
+
backdrop: {
|
|
37
|
+
backgroundColor: "#000",
|
|
38
|
+
bottom: 0,
|
|
39
|
+
left: 0,
|
|
40
|
+
position: "absolute",
|
|
41
|
+
right: 0,
|
|
42
|
+
top: 0,
|
|
43
|
+
zIndex: 50
|
|
44
|
+
},
|
|
45
|
+
drawer: {
|
|
46
|
+
bottom: 0,
|
|
47
|
+
position: "absolute",
|
|
48
|
+
top: 0,
|
|
49
|
+
zIndex: 51
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// 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 jsx2 } from "react/jsx-runtime";
|
|
58
|
+
var EDGE_WIDTH = 24;
|
|
59
|
+
var SPRING2 = { damping: 40, overshootClamping: true, stiffness: 300 };
|
|
60
|
+
var VELOCITY_THRESHOLD = 500;
|
|
61
|
+
var DrawerEdgeSwipe = ({ enabled = true, onOpen, side = "left", translateX, width }) => {
|
|
62
|
+
const closedX = side === "left" ? -width : width;
|
|
63
|
+
const openDirection = side === "left" ? 1 : -1;
|
|
64
|
+
const clampMin = Math.min(0, closedX);
|
|
65
|
+
const clampMax = Math.max(0, closedX);
|
|
66
|
+
const gesture = Gesture.Pan().enabled(enabled).activeOffsetX(side === "left" ? 10 : -10).failOffsetY([-15, 15]).onUpdate((event) => {
|
|
67
|
+
translateX.value = Math.min(clampMax, Math.max(clampMin, closedX + event.translationX));
|
|
68
|
+
}).onEnd((event) => {
|
|
69
|
+
const commit = event.translationX * openDirection > width / 3 || event.velocityX * openDirection > VELOCITY_THRESHOLD;
|
|
70
|
+
if (commit) {
|
|
71
|
+
translateX.value = withSpring2(0, SPRING2);
|
|
72
|
+
runOnJS(onOpen)();
|
|
73
|
+
} else {
|
|
74
|
+
translateX.value = withSpring2(closedX, SPRING2);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return /* @__PURE__ */ jsx2(GestureDetector, { gesture, children: /* @__PURE__ */ jsx2(View, { style: [styles2.edge, side === "left" ? styles2.left : styles2.right] }) });
|
|
78
|
+
};
|
|
79
|
+
var styles2 = StyleSheet2.create({
|
|
80
|
+
edge: {
|
|
81
|
+
bottom: 0,
|
|
82
|
+
position: "absolute",
|
|
83
|
+
top: 0,
|
|
84
|
+
width: EDGE_WIDTH,
|
|
85
|
+
zIndex: 5
|
|
86
|
+
},
|
|
87
|
+
left: { left: 0 },
|
|
88
|
+
right: { right: 0 }
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// src/createDrawer.tsx
|
|
92
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
93
|
+
var noop = () => {
|
|
94
|
+
};
|
|
95
|
+
var createDrawer = ({ side = "left", width = 300 } = {}) => {
|
|
96
|
+
const DrawerActionsContext = createContext({ close: noop, isOpen: false, open: noop });
|
|
97
|
+
const DrawerProvider = ({ children, content, enabled = true }) => {
|
|
98
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
99
|
+
const closedX = side === "left" ? -width : width;
|
|
100
|
+
const translateX = useSharedValue2(closedX);
|
|
101
|
+
const actions = useMemo(
|
|
102
|
+
() => ({
|
|
103
|
+
close: () => setIsOpen(false),
|
|
104
|
+
isOpen,
|
|
105
|
+
open: () => setIsOpen(true)
|
|
106
|
+
}),
|
|
107
|
+
[isOpen]
|
|
108
|
+
);
|
|
109
|
+
return /* @__PURE__ */ jsxs2(DrawerActionsContext.Provider, { value: actions, children: [
|
|
110
|
+
children,
|
|
111
|
+
/* @__PURE__ */ jsx3(DrawerEdgeSwipe, { enabled: enabled && !isOpen, onOpen: () => setIsOpen(true), side, translateX, width }),
|
|
112
|
+
/* @__PURE__ */ jsx3(Drawer, { onClose: () => setIsOpen(false), open: isOpen, side, translateX, width, children: content })
|
|
113
|
+
] });
|
|
114
|
+
};
|
|
115
|
+
const useDrawer = () => useContext(DrawerActionsContext);
|
|
116
|
+
return { DrawerProvider, useDrawer };
|
|
117
|
+
};
|
|
118
|
+
var combineDrawerProviders = (...entries) => {
|
|
119
|
+
const CombinedDrawerProviders = ({ children }) => entries.reduceRight((acc, [Provider, props]) => /* @__PURE__ */ jsx3(Provider, { ...props, children: acc }), children);
|
|
120
|
+
return CombinedDrawerProviders;
|
|
121
|
+
};
|
|
122
|
+
export {
|
|
123
|
+
Drawer,
|
|
124
|
+
DrawerEdgeSwipe,
|
|
125
|
+
combineDrawerProviders,
|
|
126
|
+
createDrawer
|
|
127
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rific/drawer",
|
|
3
|
+
"version": "0.1.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
|
+
"keywords": [
|
|
6
|
+
"react-native",
|
|
7
|
+
"expo",
|
|
8
|
+
"drawer",
|
|
9
|
+
"navigation",
|
|
10
|
+
"gesture",
|
|
11
|
+
"swipe",
|
|
12
|
+
"edge-swipe",
|
|
13
|
+
"react-native-gesture-handler",
|
|
14
|
+
"react-native-reanimated",
|
|
15
|
+
"react-native-paper"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/jayrdeaton/react-native-drawer#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/jayrdeaton/react-native-drawer/issues"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/jayrdeaton/react-native-drawer.git"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "Jay Deaton",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"type": "commonjs",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"react-native": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js",
|
|
34
|
+
"require": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"main": "dist/index.js",
|
|
38
|
+
"module": "dist/index.js",
|
|
39
|
+
"types": "dist/index.d.ts",
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"src",
|
|
43
|
+
"!src/__tests__",
|
|
44
|
+
"!src/__mocks__"
|
|
45
|
+
],
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
48
|
+
"build:watch": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
49
|
+
"fix": "eslint --fix",
|
|
50
|
+
"lint": "eslint",
|
|
51
|
+
"prepublishOnly": "npm run build",
|
|
52
|
+
"release": "git push --follow-tags",
|
|
53
|
+
"release:major": "npm version major && git push --follow-tags",
|
|
54
|
+
"release:minor": "npm version minor && git push --follow-tags",
|
|
55
|
+
"release:patch": "npm version patch && git push --follow-tags",
|
|
56
|
+
"test": "jest",
|
|
57
|
+
"test:watch": "jest --watchAll",
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"preversion": "npm run lint && npm test && npm run build"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@rific/haptic-press": "^0.4.3",
|
|
63
|
+
"@testing-library/dom": "^10.4.1",
|
|
64
|
+
"@testing-library/react": "^16.3.2",
|
|
65
|
+
"@types/jest": "^30.0.0",
|
|
66
|
+
"@types/react": "^19.0.0",
|
|
67
|
+
"@typescript-eslint/parser": "^8.59.3",
|
|
68
|
+
"eslint": "^9.39.4",
|
|
69
|
+
"eslint-config-prettier": "^10.1.8",
|
|
70
|
+
"eslint-plugin-package-json": "^1.0.0",
|
|
71
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
72
|
+
"eslint-plugin-react-native": "^5.0.0",
|
|
73
|
+
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
74
|
+
"jest": "^30.4.2",
|
|
75
|
+
"jest-environment-jsdom": "^30.4.1",
|
|
76
|
+
"prettier": "^3.8.3",
|
|
77
|
+
"react": "^19.0.0",
|
|
78
|
+
"react-dom": "^19.0.0",
|
|
79
|
+
"react-native": "^0.76.0",
|
|
80
|
+
"react-native-gesture-handler": "^2.20.0",
|
|
81
|
+
"react-native-paper": "^5.12.0",
|
|
82
|
+
"react-native-reanimated": "^3.16.0",
|
|
83
|
+
"ts-jest": "^29.4.9",
|
|
84
|
+
"ts-node": "^10.9.2",
|
|
85
|
+
"tsup": "^8.0.0",
|
|
86
|
+
"typescript": "^6.0.3",
|
|
87
|
+
"typescript-eslint": "^8.59.3"
|
|
88
|
+
},
|
|
89
|
+
"peerDependencies": {
|
|
90
|
+
"@rific/haptic-press": ">=0.4.0",
|
|
91
|
+
"react": ">=18.0.0",
|
|
92
|
+
"react-native": ">=0.76.0",
|
|
93
|
+
"react-native-gesture-handler": ">=2.20.0",
|
|
94
|
+
"react-native-paper": ">=5.0.0",
|
|
95
|
+
"react-native-reanimated": ">=3.16.0"
|
|
96
|
+
},
|
|
97
|
+
"publishConfig": {
|
|
98
|
+
"access": "public"
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/Drawer.tsx
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Pressable } from '@rific/haptic-press'
|
|
2
|
+
import { ReactNode, useEffect } from 'react'
|
|
3
|
+
import { StyleSheet } from 'react-native'
|
|
4
|
+
import { useTheme } from 'react-native-paper'
|
|
5
|
+
import Animated, { SharedValue, useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated'
|
|
6
|
+
|
|
7
|
+
const SPRING = { damping: 40, stiffness: 300, overshootClamping: true }
|
|
8
|
+
|
|
9
|
+
export type DrawerProps = {
|
|
10
|
+
children?: ReactNode
|
|
11
|
+
onClose: () => void
|
|
12
|
+
open: boolean
|
|
13
|
+
side?: 'left' | 'right'
|
|
14
|
+
translateX?: SharedValue<number>
|
|
15
|
+
width?: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const Drawer = ({ children, onClose, open, side = 'left', translateX: externalTranslateX, width = 300 }: DrawerProps) => {
|
|
19
|
+
const { colors } = useTheme()
|
|
20
|
+
const closedX = side === 'left' ? -width : width
|
|
21
|
+
const internalTranslateX = useSharedValue(closedX)
|
|
22
|
+
const translateX = externalTranslateX ?? internalTranslateX
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
translateX.value = withSpring(open ? 0 : closedX, SPRING)
|
|
26
|
+
}, [open, closedX, translateX])
|
|
27
|
+
|
|
28
|
+
const drawerStyle = useAnimatedStyle(() => ({ transform: [{ translateX: translateX.value }] }))
|
|
29
|
+
const backdropStyle = useAnimatedStyle(() => ({ opacity: (1 - Math.abs(translateX.value) / width) * 0.45 }))
|
|
30
|
+
|
|
31
|
+
const positionStyle = side === 'left' ? styles.anchorLeft : styles.anchorRight
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<>
|
|
35
|
+
<Animated.View style={[styles.backdrop, backdropStyle]} pointerEvents={open ? 'auto' : 'none'}>
|
|
36
|
+
<Pressable style={StyleSheet.absoluteFill} onPress={onClose} />
|
|
37
|
+
</Animated.View>
|
|
38
|
+
<Animated.View style={[styles.drawer, positionStyle, { backgroundColor: colors.surface, width }, drawerStyle]}>{children}</Animated.View>
|
|
39
|
+
</>
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const styles = StyleSheet.create({
|
|
44
|
+
anchorLeft: {
|
|
45
|
+
left: 0
|
|
46
|
+
},
|
|
47
|
+
anchorRight: {
|
|
48
|
+
right: 0
|
|
49
|
+
},
|
|
50
|
+
backdrop: {
|
|
51
|
+
backgroundColor: '#000',
|
|
52
|
+
bottom: 0,
|
|
53
|
+
left: 0,
|
|
54
|
+
position: 'absolute',
|
|
55
|
+
right: 0,
|
|
56
|
+
top: 0,
|
|
57
|
+
zIndex: 50
|
|
58
|
+
},
|
|
59
|
+
drawer: {
|
|
60
|
+
bottom: 0,
|
|
61
|
+
position: 'absolute',
|
|
62
|
+
top: 0,
|
|
63
|
+
zIndex: 51
|
|
64
|
+
}
|
|
65
|
+
})
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { StyleSheet, View } from 'react-native'
|
|
2
|
+
import { Gesture, GestureDetector } from 'react-native-gesture-handler'
|
|
3
|
+
import { runOnJS, SharedValue, withSpring } from 'react-native-reanimated'
|
|
4
|
+
|
|
5
|
+
const EDGE_WIDTH = 24
|
|
6
|
+
const SPRING = { damping: 40, overshootClamping: true, stiffness: 300 }
|
|
7
|
+
const VELOCITY_THRESHOLD = 500
|
|
8
|
+
|
|
9
|
+
export type DrawerEdgeSwipeProps = {
|
|
10
|
+
enabled?: boolean
|
|
11
|
+
onOpen: () => void
|
|
12
|
+
side?: 'left' | 'right'
|
|
13
|
+
translateX: SharedValue<number>
|
|
14
|
+
width: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const DrawerEdgeSwipe = ({ enabled = true, onOpen, side = 'left', translateX, width }: DrawerEdgeSwipeProps) => {
|
|
18
|
+
const closedX = side === 'left' ? -width : width
|
|
19
|
+
const openDirection = side === 'left' ? 1 : -1
|
|
20
|
+
const clampMin = Math.min(0, closedX)
|
|
21
|
+
const clampMax = Math.max(0, closedX)
|
|
22
|
+
|
|
23
|
+
const gesture = Gesture.Pan()
|
|
24
|
+
.enabled(enabled)
|
|
25
|
+
.activeOffsetX(side === 'left' ? 10 : -10)
|
|
26
|
+
.failOffsetY([-15, 15])
|
|
27
|
+
.onUpdate((event) => {
|
|
28
|
+
translateX.value = Math.min(clampMax, Math.max(clampMin, closedX + event.translationX))
|
|
29
|
+
})
|
|
30
|
+
.onEnd((event) => {
|
|
31
|
+
const commit = event.translationX * openDirection > width / 3 || event.velocityX * openDirection > VELOCITY_THRESHOLD
|
|
32
|
+
if (commit) {
|
|
33
|
+
translateX.value = withSpring(0, SPRING)
|
|
34
|
+
runOnJS(onOpen)()
|
|
35
|
+
} else {
|
|
36
|
+
translateX.value = withSpring(closedX, SPRING)
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<GestureDetector gesture={gesture}>
|
|
42
|
+
<View style={[styles.edge, side === 'left' ? styles.left : styles.right]} />
|
|
43
|
+
</GestureDetector>
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const styles = StyleSheet.create({
|
|
48
|
+
edge: {
|
|
49
|
+
bottom: 0,
|
|
50
|
+
position: 'absolute',
|
|
51
|
+
top: 0,
|
|
52
|
+
width: EDGE_WIDTH,
|
|
53
|
+
zIndex: 5
|
|
54
|
+
},
|
|
55
|
+
left: { left: 0 },
|
|
56
|
+
right: { right: 0 }
|
|
57
|
+
})
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createContext, JSX, ReactNode, useContext, useMemo, useState } from 'react'
|
|
2
|
+
import { useSharedValue } from 'react-native-reanimated'
|
|
3
|
+
|
|
4
|
+
import { Drawer } from './Drawer'
|
|
5
|
+
import { DrawerEdgeSwipe } from './DrawerEdgeSwipe'
|
|
6
|
+
|
|
7
|
+
export type DrawerActions = {
|
|
8
|
+
close: () => void
|
|
9
|
+
isOpen: boolean
|
|
10
|
+
open: () => void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type CreateDrawerOptions = {
|
|
14
|
+
side?: 'left' | 'right'
|
|
15
|
+
width?: number
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type DrawerProviderProps = {
|
|
19
|
+
// Rest-of-app content the drawer's actions must be reachable from — a normal Provider children slot.
|
|
20
|
+
children?: ReactNode
|
|
21
|
+
// The drawer panel's own content, rendered inside the sliding panel rather than as `children`.
|
|
22
|
+
content?: ReactNode
|
|
23
|
+
// Lets a screen temporarily suppress the edge-swipe gesture (e.g. while another drawer is open).
|
|
24
|
+
enabled?: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type CreateDrawerResult = {
|
|
28
|
+
DrawerProvider: (props: DrawerProviderProps) => JSX.Element
|
|
29
|
+
useDrawer: () => DrawerActions
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const noop = () => {}
|
|
33
|
+
|
|
34
|
+
// One call per drawer instance — each gets its own Context, so a left nav drawer and a right
|
|
35
|
+
// settings drawer (say) stay fully independent without a stringly-typed id to keep them apart.
|
|
36
|
+
export const createDrawer = ({ side = 'left', width = 300 }: CreateDrawerOptions = {}): CreateDrawerResult => {
|
|
37
|
+
const DrawerActionsContext = createContext<DrawerActions>({ close: noop, isOpen: false, open: noop })
|
|
38
|
+
|
|
39
|
+
const DrawerProvider = ({ children, content, enabled = true }: DrawerProviderProps): JSX.Element => {
|
|
40
|
+
const [isOpen, setIsOpen] = useState(false)
|
|
41
|
+
const closedX = side === 'left' ? -width : width
|
|
42
|
+
const translateX = useSharedValue(closedX)
|
|
43
|
+
|
|
44
|
+
const actions = useMemo<DrawerActions>(
|
|
45
|
+
() => ({
|
|
46
|
+
close: () => setIsOpen(false),
|
|
47
|
+
isOpen,
|
|
48
|
+
open: () => setIsOpen(true)
|
|
49
|
+
}),
|
|
50
|
+
[isOpen]
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<DrawerActionsContext.Provider value={actions}>
|
|
55
|
+
{children}
|
|
56
|
+
<DrawerEdgeSwipe enabled={enabled && !isOpen} onOpen={() => setIsOpen(true)} side={side} translateX={translateX} width={width} />
|
|
57
|
+
<Drawer onClose={() => setIsOpen(false)} open={isOpen} side={side} translateX={translateX} width={width}>
|
|
58
|
+
{content}
|
|
59
|
+
</Drawer>
|
|
60
|
+
</DrawerActionsContext.Provider>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const useDrawer = (): DrawerActions => useContext(DrawerActionsContext)
|
|
65
|
+
|
|
66
|
+
return { DrawerProvider, useDrawer }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type DrawerProviderComponent = (props: DrawerProviderProps) => JSX.Element
|
|
70
|
+
|
|
71
|
+
// Flattens nesting N createDrawer() providers under the root into a single wrapper. Each entry is
|
|
72
|
+
// a [DrawerProvider, props] pair — props are that drawer's own content/enabled, forwarded as-is.
|
|
73
|
+
// Argument order is outer-to-inner, matching how you'd otherwise nest them by hand.
|
|
74
|
+
export const combineDrawerProviders = (...entries: [DrawerProviderComponent, Omit<DrawerProviderProps, 'children'>?][]) => {
|
|
75
|
+
const CombinedDrawerProviders = ({ children }: { children?: ReactNode }): JSX.Element => entries.reduceRight<ReactNode>((acc, [Provider, props]) => <Provider {...props}>{acc}</Provider>, children) as JSX.Element
|
|
76
|
+
|
|
77
|
+
return CombinedDrawerProviders
|
|
78
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { combineDrawerProviders, createDrawer, type CreateDrawerOptions, type CreateDrawerResult, type DrawerActions, type DrawerProviderComponent, type DrawerProviderProps } from './createDrawer'
|
|
2
|
+
export { Drawer, type DrawerProps } from './Drawer'
|
|
3
|
+
export { DrawerEdgeSwipe, type DrawerEdgeSwipeProps } from './DrawerEdgeSwipe'
|