@rific/drawer 0.1.1 → 0.2.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 +60 -4
- package/dist/index.d.mts +42 -10
- package/dist/index.d.ts +42 -10
- package/dist/index.js +198 -43
- package/dist/index.mjs +213 -51
- package/package.json +8 -1
- package/src/Drawer.tsx +242 -24
- package/src/DrawerEdgeSwipe.tsx +45 -18
- package/src/createDrawer.tsx +89 -11
- package/src/geometry.ts +16 -0
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @rific/drawer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sliding drawer/sheet 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. Slides in from any of the four edges — `left`/`right` for a nav/settings drawer, `top`/`bottom` for a sheet — same mechanism either way. One `createDrawer()` call per drawer instance, so a left nav drawer and a bottom sheet stay fully independent.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -16,6 +16,8 @@ npm install react-native-gesture-handler react-native-reanimated react-native-pa
|
|
|
16
16
|
|
|
17
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
|
+
Optionally install `@rific/auto-paper` as well to opt into blurred panels — see [Blur](#blur) below.
|
|
20
|
+
|
|
19
21
|
## Usage
|
|
20
22
|
|
|
21
23
|
```tsx
|
|
@@ -23,8 +25,11 @@ import { createDrawer } from '@rific/drawer'
|
|
|
23
25
|
|
|
24
26
|
const { DrawerProvider: NavDrawerProvider, useDrawer: useNavDrawer } = createDrawer({ side: 'left', width: 300 })
|
|
25
27
|
const { DrawerProvider: SettingsDrawerProvider, useDrawer: useSettingsDrawer } = createDrawer({ side: 'right', width: 320 })
|
|
28
|
+
const { DrawerProvider: SheetProvider, useDrawer: useSheet } = createDrawer({ side: 'bottom', height: 400 })
|
|
26
29
|
```
|
|
27
30
|
|
|
31
|
+
`side` accepts `'left'`, `'right'`, `'top'`, or `'bottom'` (default `'left'`). `left`/`right` drawers size themselves with `width` (default `300`); `top`/`bottom` sheets use `height` instead (same default). Only the prop matching the chosen axis matters — pass `width` for a horizontal drawer, `height` for a vertical one.
|
|
32
|
+
|
|
28
33
|
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
34
|
|
|
30
35
|
```tsx
|
|
@@ -50,17 +55,68 @@ Then anywhere else in the tree:
|
|
|
50
55
|
```tsx
|
|
51
56
|
import { useNavDrawer } from '@/drawers'
|
|
52
57
|
|
|
53
|
-
const { isOpen, open, close } = useNavDrawer()
|
|
58
|
+
const { isOpen, isVisible, open, close } = useNavDrawer()
|
|
54
59
|
|
|
55
60
|
<IconButton icon='menu' onPress={open} />
|
|
56
61
|
```
|
|
57
62
|
|
|
58
|
-
|
|
63
|
+
`isOpen` flips the instant `open()`/`close()` is called (or a swipe gesture commits). `isVisible` stays `true` for the full close animation too, only dropping to `false` once the panel has actually finished sliding away — useful for anything that needs to stay mounted/visible for as long as the panel is on screen (e.g. a FAB row portaled above it), rather than disappearing the moment closing starts.
|
|
64
|
+
|
|
65
|
+
The drawer opens by calling `open()`, by tapping the backdrop or an in-content close button (call `close()`), by swiping in from the screen edge, or — by default — by swiping the panel's own drag handle back toward its closed edge. No extra wiring is needed for either gesture; both are 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).
|
|
66
|
+
|
|
67
|
+
A few more options, settable at `createDrawer()` (a fixed default) and overridable per-mount on `DrawerProvider`, same pattern as `blur` below:
|
|
68
|
+
|
|
69
|
+
- **`dismissible`** (default `true`) — renders a small drag handle pinned to the panel's edge closest to closed, that can be swiped to dismiss the panel. It's a dedicated hit area, not the whole panel, so it won't conflict with interactive content (sliders, buttons) inside `content`. Pass `dismissible={false}` to remove it — `close()`, an in-content close button, or the backdrop remain as the other dismiss paths.
|
|
70
|
+
- **`showHandle`** (default `true`) — whether the drag handle's pill graphic renders. Only affects that visual; the strip's hit area and its drag-to-dismiss gesture (controlled by `dismissible` above) render exactly the same when this is `false` — the strip is still there and still grabbable, just without the pill drawn inside it.
|
|
71
|
+
- **`blockingBackdrop`** (default `true`) — whether the backdrop intercepts touches while the panel is open. The dimming/blur visual is unaffected either way; setting it to `false` only stops the backdrop from swallowing touches, so sibling content behind/around the panel stays reachable while it's open (tap-to-dismiss via the backdrop stops working in that mode — `dismissible`'s handle or your own close button take over).
|
|
72
|
+
- **`backdropOpacity`** (default `0.45`) — peak backdrop opacity once the panel is fully open, scaled down as it slides toward closed. `0` renders no dimming at all, leaving whatever's behind the panel fully visible. Independent of `blockingBackdrop` — a panel can stay undimmed and still block touches, or vice versa.
|
|
73
|
+
- **`contentSize`** (default `false`) — see [Content-driven sizing](#content-driven-sizing) below.
|
|
74
|
+
- **`closeOnBackPress`** (default `true`) — whether the Android hardware back button closes the drawer while it's open, instead of falling through to whatever's behind it (a screen, a navigator). No-op on iOS/web, where that back-press event never fires.
|
|
75
|
+
- **`zIndex`** (default `50`) — see [Stacking multiple drawers](#stacking-multiple-drawers) below.
|
|
76
|
+
|
|
77
|
+
## Stacking multiple drawers
|
|
78
|
+
|
|
79
|
+
If your app can have more than one drawer open at the same time (say, a nav drawer and a confirmation sheet both created via separate `createDrawer()` calls), which one renders on top is otherwise decided by plain View z-index ties, which break on render order — whatever order your `DrawerProvider`s happen to be nested/declared in, not necessarily the order that makes sense for that screen. Give the one that should stack on top a higher `zIndex`:
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
const { DrawerProvider: NavDrawerProvider } = createDrawer({ side: 'left', width: 300 }) // zIndex 50 (default)
|
|
83
|
+
const { DrawerProvider: ConfirmSheetProvider } = createDrawer({ side: 'bottom', height: 200, zIndex: 100 })
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`zIndex` sets the backdrop's stacking tier directly; the panel and its drag handle use `zIndex + 1` / `zIndex + 2`, so raising one drawer's `zIndex` moves its whole backdrop+panel+handle stack together, above any other drawer using a lower value.
|
|
87
|
+
|
|
88
|
+
## Content-driven sizing
|
|
89
|
+
|
|
90
|
+
By default the panel is a fixed `height`/`width`. Pass `contentSize` to size it to its content's natural size along the main axis instead, and it'll smoothly animate as that natural size changes (e.g. swapping to differently-sized content while open). `height`/`width` still matter as the pre-measurement placeholder — the size used for the very first render, before the content's natural size has been measured.
|
|
91
|
+
|
|
92
|
+
This isn't sheet-specific — the main axis is whichever one `side` uses, so it works the same way for a `left`/`right` drawer sizing itself to its content's natural *width* as it does for a `top`/`bottom` sheet sizing to height.
|
|
59
93
|
|
|
60
94
|
## Safe area
|
|
61
95
|
|
|
62
96
|
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.
|
|
63
97
|
|
|
98
|
+
## Blur
|
|
99
|
+
|
|
100
|
+
The panel can render with a blurred background instead of a solid one, via the sibling package [`@rific/auto-paper`](https://www.npmjs.com/package/@rific/auto-paper) (which ships a themed `BlurView` and a `useBlur()` hook backed by a global user preference). It's an optional peer — install it to opt in:
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
npm install @rific/auto-paper
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Pass `blur` wherever suits your app: to `createDrawer()` (a fixed default for that drawer), to `DrawerProvider` (overriding the `createDrawer()` default for that mount), or directly to `Drawer` if you're using it standalone. If you don't pass `blur` at all, the panel falls back to `@rific/auto-paper`'s own global `settings.blur` user preference via `useBlur()`.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
const { DrawerProvider: SettingsDrawerProvider } = createDrawer({ side: 'right', width: 320, blur: true })
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Only the panel surface blurs — the backdrop behind it stays solid, same as today. Without `@rific/auto-paper` installed, the panel always renders its solid `colors.surface` fallback, exactly as before.
|
|
113
|
+
|
|
64
114
|
## Lower-level pieces
|
|
65
115
|
|
|
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()`.
|
|
116
|
+
`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()`. Both take a `translateOffset` prop — a `SharedValue<number>` holding the panel's offset along its axis (`translateX` for `left`/`right`, `translateY` for `top`/`bottom`) — so `DrawerEdgeSwipe`'s gesture and `Drawer`'s spring animation can share the same value.
|
|
117
|
+
|
|
118
|
+
If you're driving `Drawer` standalone with `contentSize` and want `DrawerEdgeSwipe`'s own commit-threshold math to track the measured size too, pass `Drawer` an `onMeasure?: (size: number) => void` and feed the result into the `height`/`width` you pass `DrawerEdgeSwipe` — this is exactly what `createDrawer()` does internally.
|
|
119
|
+
|
|
120
|
+
`Drawer` also takes an `onClosed?: () => void`, fired once the close spring actually settles rather than the instant `open` flips to `false` — this is what powers `isVisible` above; drive it yourself the same way if you're managing `open` state by hand. `onOpened?: () => void` is the same idea for the open direction — fired once the panel has actually finished sliding in, e.g. to autofocus a field inside `content` only once it's actually on screen. `createDrawer()` doesn't need it internally (nothing downstream waits on the open side settling the way `isVisible` waits on the close side), so it's only available driving `Drawer` directly.
|
|
121
|
+
|
|
122
|
+
`Drawer` also takes the same `zIndex` prop described in [Stacking multiple drawers](#stacking-multiple-drawers) — useful if you're rendering more than one standalone `Drawer` (or mixing standalone `Drawer`s with `createDrawer()` ones) that might be open at the same time.
|
package/dist/index.d.mts
CHANGED
|
@@ -2,47 +2,79 @@ import * as react from 'react';
|
|
|
2
2
|
import { ReactNode, JSX } from 'react';
|
|
3
3
|
import { SharedValue } from 'react-native-reanimated';
|
|
4
4
|
|
|
5
|
+
type DrawerSide = 'left' | 'right' | 'top' | 'bottom';
|
|
6
|
+
|
|
5
7
|
type DrawerActions = {
|
|
6
8
|
close: () => void;
|
|
7
9
|
isOpen: boolean;
|
|
10
|
+
isVisible: boolean;
|
|
8
11
|
open: () => void;
|
|
9
12
|
};
|
|
10
13
|
type CreateDrawerOptions = {
|
|
11
|
-
|
|
14
|
+
backdropOpacity?: number;
|
|
15
|
+
blockingBackdrop?: boolean;
|
|
16
|
+
blur?: boolean;
|
|
17
|
+
closeOnBackPress?: boolean;
|
|
18
|
+
contentSize?: boolean;
|
|
19
|
+
dismissible?: boolean;
|
|
20
|
+
height?: number;
|
|
21
|
+
showHandle?: boolean;
|
|
22
|
+
side?: DrawerSide;
|
|
12
23
|
width?: number;
|
|
24
|
+
zIndex?: number;
|
|
13
25
|
};
|
|
14
26
|
type DrawerProviderProps = {
|
|
27
|
+
backdropOpacity?: number;
|
|
28
|
+
blockingBackdrop?: boolean;
|
|
29
|
+
blur?: boolean;
|
|
15
30
|
children?: ReactNode;
|
|
31
|
+
closeOnBackPress?: boolean;
|
|
16
32
|
content?: ReactNode;
|
|
33
|
+
contentSize?: boolean;
|
|
34
|
+
dismissible?: boolean;
|
|
17
35
|
enabled?: boolean;
|
|
36
|
+
showHandle?: boolean;
|
|
37
|
+
zIndex?: number;
|
|
18
38
|
};
|
|
19
39
|
type CreateDrawerResult = {
|
|
20
40
|
DrawerProvider: (props: DrawerProviderProps) => JSX.Element;
|
|
21
41
|
useDrawer: () => DrawerActions;
|
|
22
42
|
};
|
|
23
|
-
declare const createDrawer: ({ side, width }?: CreateDrawerOptions) => CreateDrawerResult;
|
|
43
|
+
declare const createDrawer: ({ backdropOpacity: factoryBackdropOpacity, blockingBackdrop: factoryBlockingBackdrop, blur: factoryBlur, closeOnBackPress: factoryCloseOnBackPress, contentSize: factoryContentSize, dismissible: factoryDismissible, height, showHandle: factoryShowHandle, side, width, zIndex: factoryZIndex }?: CreateDrawerOptions) => CreateDrawerResult;
|
|
24
44
|
type DrawerProviderComponent = (props: DrawerProviderProps) => JSX.Element;
|
|
25
45
|
declare const combineDrawerProviders: (...entries: [DrawerProviderComponent, Omit<DrawerProviderProps, "children">?][]) => ({ children }: {
|
|
26
46
|
children?: ReactNode;
|
|
27
47
|
}) => JSX.Element;
|
|
28
48
|
|
|
29
49
|
type DrawerProps = {
|
|
50
|
+
backdropOpacity?: number;
|
|
51
|
+
blockingBackdrop?: boolean;
|
|
52
|
+
blur?: boolean;
|
|
30
53
|
children?: ReactNode;
|
|
54
|
+
contentSize?: boolean;
|
|
55
|
+
dismissible?: boolean;
|
|
56
|
+
height?: number;
|
|
31
57
|
onClose: () => void;
|
|
58
|
+
onClosed?: () => void;
|
|
59
|
+
onMeasure?: (size: number) => void;
|
|
60
|
+
onOpened?: () => void;
|
|
32
61
|
open: boolean;
|
|
33
|
-
side?:
|
|
34
|
-
|
|
62
|
+
side?: DrawerSide;
|
|
63
|
+
showHandle?: boolean;
|
|
64
|
+
translateOffset?: SharedValue<number>;
|
|
35
65
|
width?: number;
|
|
66
|
+
zIndex?: number;
|
|
36
67
|
};
|
|
37
|
-
declare const Drawer: ({ children, onClose, open, side,
|
|
68
|
+
declare const Drawer: ({ backdropOpacity, blockingBackdrop, blur, children, contentSize, dismissible, height, onClose, onClosed, onMeasure, onOpened, open, side, showHandle, translateOffset: externalTranslateOffset, width, zIndex }: DrawerProps) => react.JSX.Element;
|
|
38
69
|
|
|
39
70
|
type DrawerEdgeSwipeProps = {
|
|
40
71
|
enabled?: boolean;
|
|
72
|
+
height?: number;
|
|
41
73
|
onOpen: () => void;
|
|
42
|
-
side?:
|
|
43
|
-
|
|
44
|
-
width
|
|
74
|
+
side?: DrawerSide;
|
|
75
|
+
translateOffset: SharedValue<number>;
|
|
76
|
+
width?: number;
|
|
45
77
|
};
|
|
46
|
-
declare const DrawerEdgeSwipe: ({ enabled, onOpen, side,
|
|
78
|
+
declare const DrawerEdgeSwipe: ({ enabled, height, onOpen, side, translateOffset, width }: DrawerEdgeSwipeProps) => react.JSX.Element;
|
|
47
79
|
|
|
48
|
-
export { type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerProps, type DrawerProviderComponent, type DrawerProviderProps, combineDrawerProviders, createDrawer };
|
|
80
|
+
export { type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerProps, type DrawerProviderComponent, type DrawerProviderProps, type DrawerSide, combineDrawerProviders, createDrawer };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,47 +2,79 @@ import * as react from 'react';
|
|
|
2
2
|
import { ReactNode, JSX } from 'react';
|
|
3
3
|
import { SharedValue } from 'react-native-reanimated';
|
|
4
4
|
|
|
5
|
+
type DrawerSide = 'left' | 'right' | 'top' | 'bottom';
|
|
6
|
+
|
|
5
7
|
type DrawerActions = {
|
|
6
8
|
close: () => void;
|
|
7
9
|
isOpen: boolean;
|
|
10
|
+
isVisible: boolean;
|
|
8
11
|
open: () => void;
|
|
9
12
|
};
|
|
10
13
|
type CreateDrawerOptions = {
|
|
11
|
-
|
|
14
|
+
backdropOpacity?: number;
|
|
15
|
+
blockingBackdrop?: boolean;
|
|
16
|
+
blur?: boolean;
|
|
17
|
+
closeOnBackPress?: boolean;
|
|
18
|
+
contentSize?: boolean;
|
|
19
|
+
dismissible?: boolean;
|
|
20
|
+
height?: number;
|
|
21
|
+
showHandle?: boolean;
|
|
22
|
+
side?: DrawerSide;
|
|
12
23
|
width?: number;
|
|
24
|
+
zIndex?: number;
|
|
13
25
|
};
|
|
14
26
|
type DrawerProviderProps = {
|
|
27
|
+
backdropOpacity?: number;
|
|
28
|
+
blockingBackdrop?: boolean;
|
|
29
|
+
blur?: boolean;
|
|
15
30
|
children?: ReactNode;
|
|
31
|
+
closeOnBackPress?: boolean;
|
|
16
32
|
content?: ReactNode;
|
|
33
|
+
contentSize?: boolean;
|
|
34
|
+
dismissible?: boolean;
|
|
17
35
|
enabled?: boolean;
|
|
36
|
+
showHandle?: boolean;
|
|
37
|
+
zIndex?: number;
|
|
18
38
|
};
|
|
19
39
|
type CreateDrawerResult = {
|
|
20
40
|
DrawerProvider: (props: DrawerProviderProps) => JSX.Element;
|
|
21
41
|
useDrawer: () => DrawerActions;
|
|
22
42
|
};
|
|
23
|
-
declare const createDrawer: ({ side, width }?: CreateDrawerOptions) => CreateDrawerResult;
|
|
43
|
+
declare const createDrawer: ({ backdropOpacity: factoryBackdropOpacity, blockingBackdrop: factoryBlockingBackdrop, blur: factoryBlur, closeOnBackPress: factoryCloseOnBackPress, contentSize: factoryContentSize, dismissible: factoryDismissible, height, showHandle: factoryShowHandle, side, width, zIndex: factoryZIndex }?: CreateDrawerOptions) => CreateDrawerResult;
|
|
24
44
|
type DrawerProviderComponent = (props: DrawerProviderProps) => JSX.Element;
|
|
25
45
|
declare const combineDrawerProviders: (...entries: [DrawerProviderComponent, Omit<DrawerProviderProps, "children">?][]) => ({ children }: {
|
|
26
46
|
children?: ReactNode;
|
|
27
47
|
}) => JSX.Element;
|
|
28
48
|
|
|
29
49
|
type DrawerProps = {
|
|
50
|
+
backdropOpacity?: number;
|
|
51
|
+
blockingBackdrop?: boolean;
|
|
52
|
+
blur?: boolean;
|
|
30
53
|
children?: ReactNode;
|
|
54
|
+
contentSize?: boolean;
|
|
55
|
+
dismissible?: boolean;
|
|
56
|
+
height?: number;
|
|
31
57
|
onClose: () => void;
|
|
58
|
+
onClosed?: () => void;
|
|
59
|
+
onMeasure?: (size: number) => void;
|
|
60
|
+
onOpened?: () => void;
|
|
32
61
|
open: boolean;
|
|
33
|
-
side?:
|
|
34
|
-
|
|
62
|
+
side?: DrawerSide;
|
|
63
|
+
showHandle?: boolean;
|
|
64
|
+
translateOffset?: SharedValue<number>;
|
|
35
65
|
width?: number;
|
|
66
|
+
zIndex?: number;
|
|
36
67
|
};
|
|
37
|
-
declare const Drawer: ({ children, onClose, open, side,
|
|
68
|
+
declare const Drawer: ({ backdropOpacity, blockingBackdrop, blur, children, contentSize, dismissible, height, onClose, onClosed, onMeasure, onOpened, open, side, showHandle, translateOffset: externalTranslateOffset, width, zIndex }: DrawerProps) => react.JSX.Element;
|
|
38
69
|
|
|
39
70
|
type DrawerEdgeSwipeProps = {
|
|
40
71
|
enabled?: boolean;
|
|
72
|
+
height?: number;
|
|
41
73
|
onOpen: () => void;
|
|
42
|
-
side?:
|
|
43
|
-
|
|
44
|
-
width
|
|
74
|
+
side?: DrawerSide;
|
|
75
|
+
translateOffset: SharedValue<number>;
|
|
76
|
+
width?: number;
|
|
45
77
|
};
|
|
46
|
-
declare const DrawerEdgeSwipe: ({ enabled, onOpen, side,
|
|
78
|
+
declare const DrawerEdgeSwipe: ({ enabled, height, onOpen, side, translateOffset, width }: DrawerEdgeSwipeProps) => react.JSX.Element;
|
|
47
79
|
|
|
48
|
-
export { type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerProps, type DrawerProviderComponent, type DrawerProviderProps, combineDrawerProviders, createDrawer };
|
|
80
|
+
export { type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerProps, type DrawerProviderComponent, type DrawerProviderProps, type DrawerSide, combineDrawerProviders, createDrawer };
|
package/dist/index.js
CHANGED
|
@@ -39,38 +39,141 @@ module.exports = __toCommonJS(index_exports);
|
|
|
39
39
|
|
|
40
40
|
// src/createDrawer.tsx
|
|
41
41
|
var import_react2 = require("react");
|
|
42
|
+
var import_react_native3 = require("react-native");
|
|
42
43
|
var import_react_native_reanimated3 = require("react-native-reanimated");
|
|
43
44
|
|
|
44
45
|
// src/Drawer.tsx
|
|
45
46
|
var import_haptic_press = require("@rific/haptic-press");
|
|
46
47
|
var import_react = require("react");
|
|
47
48
|
var import_react_native = require("react-native");
|
|
49
|
+
var import_react_native_gesture_handler = require("react-native-gesture-handler");
|
|
48
50
|
var import_react_native_paper = require("react-native-paper");
|
|
49
51
|
var import_react_native_reanimated = __toESM(require("react-native-reanimated"));
|
|
52
|
+
|
|
53
|
+
// src/geometry.ts
|
|
54
|
+
function isVerticalSide(side) {
|
|
55
|
+
return side === "top" || side === "bottom";
|
|
56
|
+
}
|
|
57
|
+
function getClosedOffset(side, size) {
|
|
58
|
+
return side === "left" || side === "top" ? -size : size;
|
|
59
|
+
}
|
|
60
|
+
function getOpenDirection(side) {
|
|
61
|
+
return side === "left" || side === "top" ? 1 : -1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/Drawer.tsx
|
|
50
65
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
66
|
var SPRING = { damping: 40, stiffness: 300, overshootClamping: true };
|
|
52
|
-
var
|
|
67
|
+
var VELOCITY_THRESHOLD = 500;
|
|
68
|
+
var AutoPaper = (() => {
|
|
69
|
+
try {
|
|
70
|
+
return require("@rific/auto-paper");
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
})();
|
|
75
|
+
var useBlurFallback = (override) => override ?? false;
|
|
76
|
+
var useBlurHook = AutoPaper?.useBlur ?? useBlurFallback;
|
|
77
|
+
var Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children, contentSize = false, dismissible = true, height = 300, onClose, onClosed, onMeasure, onOpened, open, side = "left", showHandle = true, translateOffset: externalTranslateOffset, width = 300, zIndex = 50 }) => {
|
|
53
78
|
const { colors } = (0, import_react_native_paper.useTheme)();
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
const
|
|
79
|
+
const vertical = isVerticalSide(side);
|
|
80
|
+
const size = vertical ? height : width;
|
|
81
|
+
const openDirection = getOpenDirection(side);
|
|
82
|
+
const closeDirection = -openDirection;
|
|
83
|
+
const [measuredSize, setMeasuredSize] = (0, import_react.useState)(null);
|
|
84
|
+
const effectiveSize = contentSize ? measuredSize ?? size : size;
|
|
85
|
+
const closedOffset = getClosedOffset(side, effectiveSize);
|
|
86
|
+
const internalTranslateOffset = (0, import_react_native_reanimated.useSharedValue)(closedOffset);
|
|
87
|
+
const translateOffset = externalTranslateOffset ?? internalTranslateOffset;
|
|
88
|
+
const resolvedBlur = useBlurHook(blur);
|
|
89
|
+
const animatedSize = (0, import_react_native_reanimated.useSharedValue)(effectiveSize);
|
|
57
90
|
(0, import_react.useEffect)(() => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
91
|
+
if (open) {
|
|
92
|
+
translateOffset.value = (0, import_react_native_reanimated.withSpring)(0, SPRING, (finished) => {
|
|
93
|
+
if (finished && onOpened) (0, import_react_native_reanimated.runOnJS)(onOpened)();
|
|
94
|
+
});
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
translateOffset.value = (0, import_react_native_reanimated.withSpring)(closedOffset, SPRING, (finished) => {
|
|
98
|
+
if (finished && onClosed) (0, import_react_native_reanimated.runOnJS)(onClosed)();
|
|
99
|
+
});
|
|
100
|
+
}, [open, closedOffset, translateOffset]);
|
|
101
|
+
(0, import_react.useEffect)(() => {
|
|
102
|
+
if (contentSize) {
|
|
103
|
+
animatedSize.value = (0, import_react_native_reanimated.withTiming)(effectiveSize);
|
|
104
|
+
}
|
|
105
|
+
}, [contentSize, effectiveSize, animatedSize]);
|
|
106
|
+
const drawerStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({ transform: [vertical ? { translateY: translateOffset.value } : { translateX: translateOffset.value }] }));
|
|
107
|
+
const backdropStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({ opacity: (1 - Math.abs(translateOffset.value) / effectiveSize) * backdropOpacity }));
|
|
108
|
+
const animatedSizeStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => vertical ? { height: animatedSize.value } : { width: animatedSize.value });
|
|
109
|
+
const positionStyle = side === "left" ? styles.anchorLeft : side === "right" ? styles.anchorRight : side === "top" ? styles.anchorTop : styles.anchorBottom;
|
|
110
|
+
const sizeStyle = vertical ? { height } : { width };
|
|
111
|
+
const handleLayout = (event) => {
|
|
112
|
+
const measured = vertical ? event.nativeEvent.layout.height : event.nativeEvent.layout.width;
|
|
113
|
+
setMeasuredSize(measured);
|
|
114
|
+
onMeasure?.(measured);
|
|
115
|
+
};
|
|
116
|
+
const fillStyle = import_react_native.StyleSheet.absoluteFill;
|
|
117
|
+
const fill = resolvedBlur && AutoPaper?.BlurView ? contentSize ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
118
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(AutoPaper.BlurView, { style: fillStyle }),
|
|
119
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { onLayout: handleLayout, children })
|
|
120
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AutoPaper.BlurView, { style: fillStyle, children }) : contentSize ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
121
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: [fillStyle, { backgroundColor: colors.surface }] }),
|
|
122
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { onLayout: handleLayout, children })
|
|
123
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: [fillStyle, { backgroundColor: colors.surface }], children });
|
|
124
|
+
const drawerOuterStyle = contentSize ? [styles.drawer, positionStyle, styles.clip, { flexDirection: vertical ? "column" : "row" }, drawerStyle, animatedSizeStyle, { zIndex: zIndex + 1 }] : [styles.drawer, positionStyle, sizeStyle, drawerStyle, { zIndex: zIndex + 1 }];
|
|
125
|
+
const clampMin = Math.min(0, closedOffset);
|
|
126
|
+
const clampMax = Math.max(0, closedOffset);
|
|
127
|
+
const handleAxisGesture = vertical ? import_react_native_gesture_handler.Gesture.Pan().activeOffsetY(closeDirection * 10).failOffsetX([-15, 15]) : import_react_native_gesture_handler.Gesture.Pan().activeOffsetX(closeDirection * 10).failOffsetY([-15, 15]);
|
|
128
|
+
const handleGesture = handleAxisGesture.enabled(open).onUpdate((event) => {
|
|
129
|
+
"worklet";
|
|
130
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
131
|
+
translateOffset.value = Math.min(clampMax, Math.max(clampMin, translation));
|
|
132
|
+
}).onEnd((event) => {
|
|
133
|
+
"worklet";
|
|
134
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
135
|
+
const velocity = vertical ? event.velocityY : event.velocityX;
|
|
136
|
+
const commit = translation * closeDirection > effectiveSize / 3 || velocity * closeDirection > VELOCITY_THRESHOLD;
|
|
137
|
+
if (commit) {
|
|
138
|
+
translateOffset.value = (0, import_react_native_reanimated.withSpring)(closedOffset, SPRING);
|
|
139
|
+
(0, import_react_native_reanimated.runOnJS)(onClose)();
|
|
140
|
+
} else {
|
|
141
|
+
translateOffset.value = (0, import_react_native_reanimated.withSpring)(0, SPRING);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
const HANDLE_STRIP_THICKNESS = 28;
|
|
145
|
+
const handleStripSizeStyle = vertical ? { height: HANDLE_STRIP_THICKNESS, left: 0, right: 0 } : { bottom: 0, top: 0, width: HANDLE_STRIP_THICKNESS };
|
|
146
|
+
const handleStripEdgeStyle = side === "bottom" ? { top: 0 } : side === "top" ? { bottom: 0 } : side === "left" ? { right: 0 } : { left: 0 };
|
|
147
|
+
const handlePillStyle = vertical ? { height: 6, width: 64 } : { height: 64, width: 6 };
|
|
148
|
+
const webCursorStyle = { cursor: import_react_native.Platform.OS === "web" ? "pointer" : void 0 };
|
|
63
149
|
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.
|
|
150
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { style: [styles.backdrop, { zIndex }, backdropStyle], pointerEvents: open && blockingBackdrop ? "auto" : "none", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_haptic_press.Pressable, { style: import_react_native.StyleSheet.absoluteFill, onPress: onClose }) }),
|
|
151
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native_reanimated.default.View, { style: drawerOuterStyle, children: [
|
|
152
|
+
fill,
|
|
153
|
+
dismissible && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_gesture_handler.GestureDetector, { gesture: handleGesture, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: [styles.handleStrip, handleStripSizeStyle, handleStripEdgeStyle, webCursorStyle, { zIndex: zIndex + 2 }], children: showHandle && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { style: [styles.handlePill, handlePillStyle, { backgroundColor: colors.onSurfaceVariant }] }) }) })
|
|
154
|
+
] })
|
|
66
155
|
] });
|
|
67
156
|
};
|
|
68
157
|
var styles = import_react_native.StyleSheet.create({
|
|
158
|
+
anchorBottom: {
|
|
159
|
+
bottom: 0,
|
|
160
|
+
left: 0,
|
|
161
|
+
right: 0
|
|
162
|
+
},
|
|
69
163
|
anchorLeft: {
|
|
70
|
-
|
|
164
|
+
bottom: 0,
|
|
165
|
+
left: 0,
|
|
166
|
+
top: 0
|
|
71
167
|
},
|
|
72
168
|
anchorRight: {
|
|
73
|
-
|
|
169
|
+
bottom: 0,
|
|
170
|
+
right: 0,
|
|
171
|
+
top: 0
|
|
172
|
+
},
|
|
173
|
+
anchorTop: {
|
|
174
|
+
left: 0,
|
|
175
|
+
right: 0,
|
|
176
|
+
top: 0
|
|
74
177
|
},
|
|
75
178
|
backdrop: {
|
|
76
179
|
backgroundColor: "#000",
|
|
@@ -78,45 +181,70 @@ var styles = import_react_native.StyleSheet.create({
|
|
|
78
181
|
left: 0,
|
|
79
182
|
position: "absolute",
|
|
80
183
|
right: 0,
|
|
81
|
-
top: 0
|
|
82
|
-
|
|
184
|
+
top: 0
|
|
185
|
+
},
|
|
186
|
+
clip: {
|
|
187
|
+
overflow: "hidden"
|
|
83
188
|
},
|
|
84
189
|
drawer: {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
190
|
+
position: "absolute"
|
|
191
|
+
},
|
|
192
|
+
handlePill: {
|
|
193
|
+
borderRadius: 3,
|
|
194
|
+
opacity: 0.5
|
|
195
|
+
},
|
|
196
|
+
handleStrip: {
|
|
197
|
+
alignItems: "center",
|
|
198
|
+
justifyContent: "center",
|
|
199
|
+
position: "absolute"
|
|
89
200
|
}
|
|
90
201
|
});
|
|
91
202
|
|
|
92
203
|
// src/DrawerEdgeSwipe.tsx
|
|
93
204
|
var import_react_native2 = require("react-native");
|
|
94
|
-
var
|
|
205
|
+
var import_react_native_gesture_handler2 = require("react-native-gesture-handler");
|
|
95
206
|
var import_react_native_reanimated2 = require("react-native-reanimated");
|
|
96
207
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
97
208
|
var EDGE_WIDTH = 24;
|
|
98
209
|
var SPRING2 = { damping: 40, overshootClamping: true, stiffness: 300 };
|
|
99
|
-
var
|
|
100
|
-
var DrawerEdgeSwipe = ({ enabled = true, onOpen, side = "left",
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
|
|
210
|
+
var VELOCITY_THRESHOLD2 = 500;
|
|
211
|
+
var DrawerEdgeSwipe = ({ enabled = true, height = 300, onOpen, side = "left", translateOffset, width = 300 }) => {
|
|
212
|
+
const vertical = isVerticalSide(side);
|
|
213
|
+
const size = vertical ? height : width;
|
|
214
|
+
const closedOffset = getClosedOffset(side, size);
|
|
215
|
+
const openDirection = getOpenDirection(side);
|
|
216
|
+
const clampMin = Math.min(0, closedOffset);
|
|
217
|
+
const clampMax = Math.max(0, closedOffset);
|
|
218
|
+
const axisGesture = vertical ? import_react_native_gesture_handler2.Gesture.Pan().activeOffsetY(side === "top" ? 10 : -10).failOffsetX([-15, 15]) : import_react_native_gesture_handler2.Gesture.Pan().activeOffsetX(side === "left" ? 10 : -10).failOffsetY([-15, 15]);
|
|
219
|
+
const gesture = axisGesture.enabled(enabled).onUpdate((event) => {
|
|
220
|
+
"worklet";
|
|
221
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
222
|
+
translateOffset.value = Math.min(clampMax, Math.max(clampMin, closedOffset + translation));
|
|
107
223
|
}).onEnd((event) => {
|
|
108
|
-
|
|
224
|
+
"worklet";
|
|
225
|
+
const translation = vertical ? event.translationY : event.translationX;
|
|
226
|
+
const velocity = vertical ? event.velocityY : event.velocityX;
|
|
227
|
+
const commit = translation * openDirection > size / 3 || velocity * openDirection > VELOCITY_THRESHOLD2;
|
|
109
228
|
if (commit) {
|
|
110
|
-
|
|
229
|
+
translateOffset.value = (0, import_react_native_reanimated2.withSpring)(0, SPRING2);
|
|
111
230
|
(0, import_react_native_reanimated2.runOnJS)(onOpen)();
|
|
112
231
|
} else {
|
|
113
|
-
|
|
232
|
+
translateOffset.value = (0, import_react_native_reanimated2.withSpring)(closedOffset, SPRING2);
|
|
114
233
|
}
|
|
115
234
|
});
|
|
116
|
-
|
|
235
|
+
const edgeStyle = vertical ? [styles2.edgeHorizontal, side === "top" ? styles2.top : styles2.bottom] : [styles2.edgeVertical, side === "left" ? styles2.left : styles2.right];
|
|
236
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native_gesture_handler2.GestureDetector, { gesture, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react_native2.View, { style: edgeStyle }) });
|
|
117
237
|
};
|
|
118
238
|
var styles2 = import_react_native2.StyleSheet.create({
|
|
119
|
-
|
|
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: {
|
|
120
248
|
bottom: 0,
|
|
121
249
|
position: "absolute",
|
|
122
250
|
top: 0,
|
|
@@ -124,31 +252,58 @@ var styles2 = import_react_native2.StyleSheet.create({
|
|
|
124
252
|
zIndex: 5
|
|
125
253
|
},
|
|
126
254
|
left: { left: 0 },
|
|
127
|
-
right: { right: 0 }
|
|
255
|
+
right: { right: 0 },
|
|
256
|
+
top: { top: 0 }
|
|
128
257
|
});
|
|
129
258
|
|
|
130
259
|
// src/createDrawer.tsx
|
|
131
260
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
132
261
|
var noop = () => {
|
|
133
262
|
};
|
|
134
|
-
var createDrawer = ({ side = "left", width = 300 } = {}) => {
|
|
135
|
-
const DrawerActionsContext = (0, import_react2.createContext)({ close: noop, isOpen: false, open: noop });
|
|
136
|
-
const
|
|
263
|
+
var createDrawer = ({ backdropOpacity: factoryBackdropOpacity, blockingBackdrop: factoryBlockingBackdrop, blur: factoryBlur, closeOnBackPress: factoryCloseOnBackPress, contentSize: factoryContentSize, dismissible: factoryDismissible, height = 300, showHandle: factoryShowHandle, side = "left", width = 300, zIndex: factoryZIndex } = {}) => {
|
|
264
|
+
const DrawerActionsContext = (0, import_react2.createContext)({ close: noop, isOpen: false, isVisible: false, open: noop });
|
|
265
|
+
const vertical = isVerticalSide(side);
|
|
266
|
+
const size = vertical ? height : width;
|
|
267
|
+
const closedOffset = getClosedOffset(side, size);
|
|
268
|
+
const DrawerProvider = ({ backdropOpacity: backdropOpacityOverride, blockingBackdrop: blockingBackdropOverride, blur: blurOverride, children, closeOnBackPress: closeOnBackPressOverride, content, contentSize: contentSizeOverride, dismissible: dismissibleOverride, enabled = true, showHandle: showHandleOverride, zIndex: zIndexOverride }) => {
|
|
137
269
|
const [isOpen, setIsOpen] = (0, import_react2.useState)(false);
|
|
138
|
-
const
|
|
139
|
-
const
|
|
270
|
+
const [isVisible, setIsVisible] = (0, import_react2.useState)(false);
|
|
271
|
+
const [measuredSize, setMeasuredSize] = (0, import_react2.useState)(null);
|
|
272
|
+
const translateOffset = (0, import_react_native_reanimated3.useSharedValue)(closedOffset);
|
|
273
|
+
const backdropOpacity = backdropOpacityOverride ?? factoryBackdropOpacity ?? 0.45;
|
|
274
|
+
const blur = blurOverride ?? factoryBlur;
|
|
275
|
+
const blockingBackdrop = blockingBackdropOverride ?? factoryBlockingBackdrop ?? true;
|
|
276
|
+
const closeOnBackPress = closeOnBackPressOverride ?? factoryCloseOnBackPress ?? true;
|
|
277
|
+
const contentSize = contentSizeOverride ?? factoryContentSize ?? false;
|
|
278
|
+
const dismissible = dismissibleOverride ?? factoryDismissible ?? true;
|
|
279
|
+
const showHandle = showHandleOverride ?? factoryShowHandle ?? true;
|
|
280
|
+
const zIndex = zIndexOverride ?? factoryZIndex ?? 50;
|
|
281
|
+
const effectiveSize = contentSize ? measuredSize ?? size : size;
|
|
282
|
+
const open = () => {
|
|
283
|
+
setIsOpen(true);
|
|
284
|
+
setIsVisible(true);
|
|
285
|
+
};
|
|
140
286
|
const actions = (0, import_react2.useMemo)(
|
|
141
287
|
() => ({
|
|
142
288
|
close: () => setIsOpen(false),
|
|
143
289
|
isOpen,
|
|
144
|
-
|
|
290
|
+
isVisible,
|
|
291
|
+
open
|
|
145
292
|
}),
|
|
146
|
-
[isOpen]
|
|
293
|
+
[isOpen, isVisible]
|
|
147
294
|
);
|
|
295
|
+
(0, import_react2.useEffect)(() => {
|
|
296
|
+
if (!isOpen || !closeOnBackPress) return;
|
|
297
|
+
const subscription = import_react_native3.BackHandler.addEventListener("hardwareBackPress", () => {
|
|
298
|
+
setIsOpen(false);
|
|
299
|
+
return true;
|
|
300
|
+
});
|
|
301
|
+
return () => subscription.remove();
|
|
302
|
+
}, [isOpen, closeOnBackPress]);
|
|
148
303
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(DrawerActionsContext.Provider, { value: actions, children: [
|
|
149
304
|
children,
|
|
150
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DrawerEdgeSwipe, { enabled: enabled && !isOpen,
|
|
151
|
-
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Drawer, { onClose: () => setIsOpen(false), open: isOpen, side,
|
|
305
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(DrawerEdgeSwipe, { enabled: enabled && !isOpen, height: vertical ? effectiveSize : height, onOpen: open, side, translateOffset, width: vertical ? width : effectiveSize }),
|
|
306
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(Drawer, { backdropOpacity, blockingBackdrop, blur, contentSize, dismissible, height: vertical ? effectiveSize : height, onClose: () => setIsOpen(false), onClosed: () => setIsVisible(false), onMeasure: setMeasuredSize, open: isOpen, showHandle, side, translateOffset, width: vertical ? width : effectiveSize, zIndex, children: content })
|
|
152
307
|
] });
|
|
153
308
|
};
|
|
154
309
|
const useDrawer = () => (0, import_react2.useContext)(DrawerActionsContext);
|