@rific/drawer 0.6.1 → 0.7.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 +37 -12
- package/dist/index.d.mts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +13 -8
- package/dist/index.mjs +11 -7
- package/package.json +1 -1
- package/src/Drawer.tsx +69 -56
- package/src/DrawerEdgeSwipe.tsx +5 -5
- package/src/createDrawer.tsx +6 -6
- package/src/geometry.ts +15 -5
- package/src/index.ts +1 -0
- package/src/useDrawerSize.ts +17 -6
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ const { DrawerInstanceProvider: SettingsDrawerProvider, useDrawer: useSettingsDr
|
|
|
28
28
|
const { DrawerInstanceProvider: SheetProvider, useDrawer: useSheet } = createDrawer({ side: 'bottom', height: 400 })
|
|
29
29
|
```
|
|
30
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. Either accepts a plain pixel number or a percentage string (`height: '50%'`) resolved against the window's height/width, re-resolving automatically on rotation/resize.
|
|
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. Either accepts a plain pixel number or a percentage string (`height: '50%'`) resolved against the window's height/width, re-resolving automatically on rotation/resize. Either way, the resolved size is capped at the window's own size along that axis (the same goes for `maxHeight`/`maxWidth` below), so a fixed `width: 380` drawer fills a 360px-wide phone exactly instead of running its far edge off the screen, and stays 380 on anything wider.
|
|
32
32
|
|
|
33
33
|
Mount each `DrawerInstanceProvider` 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):
|
|
34
34
|
|
|
@@ -93,17 +93,17 @@ This isn't sheet-specific: the main axis is whichever one `side` uses, so it wor
|
|
|
93
93
|
|
|
94
94
|
## Expandable sheets
|
|
95
95
|
|
|
96
|
-
Pass `maxHeight`/`maxWidth` (same shape as `height`/`width`: a number, or a percentage string) to let the drag handle pull the panel open further than its resting size, up to this ceiling
|
|
96
|
+
Pass `maxHeight`/`maxWidth` (same shape as `height`/`width`: a number, or a percentage string) to let the drag handle pull the panel open further than its resting size, up to this ceiling - a sheet that opens partway but can be dragged the rest of the way open:
|
|
97
97
|
|
|
98
98
|
```tsx
|
|
99
99
|
const { DrawerInstanceProvider: SheetProvider } = createDrawer({ side: 'bottom', height: '50%', maxHeight: '100%' })
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
-
This sheet opens
|
|
102
|
+
This sheet opens - by calling `open()`, tapping, or swiping in from the edge - to half the screen, same as a plain `height: '50%'` would. From there, dragging the handle further up expands it the rest of the way to full screen; dragging back down closes it. Same commit rule either direction: past a third of the way toward the next state (or a fast enough flick) commits to it, otherwise it springs back to rest - just like the existing drag-to-dismiss behavior, now with a third resting point instead of two.
|
|
103
103
|
|
|
104
|
-
Without `maxHeight`/`maxWidth` (the default), nothing changes: `height`/`width` alone is both the resting size and the ceiling, so there's nothing to expand into, and the handle drag is a plain two-way open/closed toggle exactly as before. No effect when `contentSize` is set either, since that already sizes the panel to its content
|
|
104
|
+
Without `maxHeight`/`maxWidth` (the default), nothing changes: `height`/`width` alone is both the resting size and the ceiling, so there's nothing to expand into, and the handle drag is a plain two-way open/closed toggle exactly as before. No effect when `contentSize` is set either, since that already sizes the panel to its content - there's no separate rest-vs-max to speak of.
|
|
105
105
|
|
|
106
|
-
`useDrawer()` also returns `expandProgress`, a `SharedValue<number>`: 0 anywhere at or before resting (nothing to indicate
|
|
106
|
+
`useDrawer()` also returns `expandProgress`, a `SharedValue<number>`: 0 anywhere at or before resting (nothing to indicate - the panel isn't showing any more than its configured rest size), 1 once a drag has pulled it all the way open to `maxHeight`/`maxWidth`, smoothly interpolating in between as the handle is dragged or the spring animates. Content that only cares once the panel is genuinely at its full extent - see [Safe area](#safe-area) below for the motivating example - can read it directly in a `useAnimatedStyle`:
|
|
107
107
|
|
|
108
108
|
```tsx
|
|
109
109
|
const { expandProgress } = sheet.useDrawer()
|
|
@@ -117,9 +117,9 @@ Always a flat `1` while open when there's no `maxHeight`/`maxWidth` (rest alread
|
|
|
117
117
|
|
|
118
118
|
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.
|
|
119
119
|
|
|
120
|
-
For an expandable sheet specifically, a *top* inset is usually only relevant once the sheet is actually fully expanded (reaching the screen's own top edge/notch)
|
|
120
|
+
For an expandable sheet specifically, a *top* inset is usually only relevant once the sheet is actually fully expanded (reaching the screen's own top edge/notch) - wasted space while it's merely resting at a partial `height`. `expandProgress` (see [Expandable sheets](#expandable-sheets) above) is built for exactly this: scale the top inset by it instead of applying it unconditionally, and it smoothly animates in as the sheet expands rather than snapping. A bottom inset doesn't need this treatment - the sheet's bottom edge sits flush against the screen's bottom at any size, rest or expanded, so that inset stays relevant throughout and can be applied unconditionally as usual.
|
|
121
121
|
|
|
122
|
-
This padding approach keeps the panel itself full-bleed and compensates inside `content`, which is the right call if you want the panel's background to actually reach the screen edge (a full-screen sheet look once expanded). If instead you want the panel to visibly stop short of the status bar even fully expanded
|
|
122
|
+
This padding approach keeps the panel itself full-bleed and compensates inside `content`, which is the right call if you want the panel's background to actually reach the screen edge (a full-screen sheet look once expanded). If instead you want the panel to visibly stop short of the status bar even fully expanded - so an expanded sheet still reads as a *drawer*, not a full-screen modal - there's a second, opposite approach: `edgeInset`.
|
|
123
123
|
|
|
124
124
|
### Stopping the panel itself short of the inset
|
|
125
125
|
|
|
@@ -130,11 +130,11 @@ const insets = useSafeAreaInsets()
|
|
|
130
130
|
const { DrawerInstanceProvider: SheetProvider } = createDrawer({ side: 'bottom', height: '50%', maxHeight: '100%', edgeInset: insets.top })
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
Dragged all the way open, this sheet's top edge now stops at `insets.top` rather than sliding behind the status bar
|
|
133
|
+
Dragged all the way open, this sheet's top edge now stops at `insets.top` rather than sliding behind the status bar - there's nothing left for `content` to compensate for, so skip the `expandProgress` padding trick for this edge entirely (applying both would double the gap: the panel already stops at the inset, and the padding would push `content` down by another `insets.top` on top of that).
|
|
134
134
|
|
|
135
|
-
A plain pixel value is deliberately left untouched by `edgeInset`
|
|
135
|
+
A plain pixel value is deliberately left untouched by `edgeInset` - `resolveDimension` ignores its window-size argument entirely for numbers, so it's unaffected regardless. A percentage means "fill the available space" (which should already account for a known inset); a literal px value means "this exact size," typically one you already computed deliberately, and `edgeInset` silently overriding that would take away your only way to opt a specific drawer back out of it.
|
|
136
136
|
|
|
137
|
-
Both approaches are just different values passed to the same props, so nothing stops one drawer in your app from going full-bleed with `expandProgress`-scaled padding while another uses `edgeInset` to stay clear of the inset outright
|
|
137
|
+
Both approaches are just different values passed to the same props, so nothing stops one drawer in your app from going full-bleed with `expandProgress`-scaled padding while another uses `edgeInset` to stay clear of the inset outright - pick whichever fits each drawer, per drawer instance. The one thing to avoid is combining both for the *same* edge on the *same* drawer.
|
|
138
138
|
|
|
139
139
|
## Blur
|
|
140
140
|
|
|
@@ -180,11 +180,36 @@ Only the panel surface blurs; the backdrop behind it stays solid, same as today.
|
|
|
180
180
|
|
|
181
181
|
`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.
|
|
182
182
|
|
|
183
|
+
Seed that shared value at the drawer's closed offset with `useDrawerSize`, the same hook `Drawer`, `DrawerEdgeSwipe` and `createDrawer()` resolve their own geometry with, rather than negating the size by hand: a hand-negated `-380` disagrees with a panel that's been capped to a narrower window (see [Usage](#usage)). Pass it the same `side`, size, max size and `edgeInset` the `Drawer` gets. Its `effectiveSize` is also the panel's actual rest size in px, for laying out anything sized to the panel itself (a full-width carousel page, say):
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
import { Drawer, DrawerEdgeSwipe, useDrawerSize } from '@rific/drawer'
|
|
187
|
+
import { useSharedValue } from 'react-native-reanimated'
|
|
188
|
+
|
|
189
|
+
const WIDTH = 380
|
|
190
|
+
|
|
191
|
+
const MenuDrawer = ({ open, onOpen, onClose }: MenuDrawerProps) => {
|
|
192
|
+
const { closedOffset, effectiveSize } = useDrawerSize('left', WIDTH)
|
|
193
|
+
const translateOffset = useSharedValue(closedOffset)
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<>
|
|
197
|
+
<DrawerEdgeSwipe onOpen={onOpen} translateOffset={translateOffset} width={WIDTH} />
|
|
198
|
+
<Drawer onClose={onClose} open={open} translateOffset={translateOffset} width={WIDTH}>
|
|
199
|
+
<Carousel pageWidth={effectiveSize} />
|
|
200
|
+
</Drawer>
|
|
201
|
+
</>
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The seed only matters for the very first frame: after that, `Drawer` springs the value to its own closed offset whenever that changes (rotation, a resize across the cap).
|
|
207
|
+
|
|
183
208
|
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.
|
|
184
209
|
|
|
185
|
-
Both also take `maxHeight`/`maxWidth` (see [Expandable sheets](#expandable-sheets))
|
|
210
|
+
Both also take `maxHeight`/`maxWidth` (see [Expandable sheets](#expandable-sheets)) - pass the same values to both, same as `height`/`width`, so `DrawerEdgeSwipe`'s edge-swipe-to-open lands at the same rest position `Drawer`'s own tap-to-open does. Same for `edgeInset` (see [Safe area](#safe-area)): pass the same value to both, or their rest/max geometry will disagree.
|
|
186
211
|
|
|
187
|
-
`DrawerEdgeSwipe` alone also takes `insetTop`/`insetBottom`/`insetLeft`/`insetRight` (all default `0`), which shrink the edge-swipe hitbox's span along its long axis
|
|
212
|
+
`DrawerEdgeSwipe` alone also takes `insetTop`/`insetBottom`/`insetLeft`/`insetRight` (all default `0`), which shrink the edge-swipe hitbox's span along its long axis - only the pair perpendicular to `side`'s own strip thickness applies: `insetTop`/`insetBottom` for a `left`/`right` drawer's vertical strip, `insetLeft`/`insetRight` for a `top`/`bottom` drawer's horizontal one; the other pair is ignored. Use these to carve out chrome near the edge - a header or tab bar - that would otherwise have its own taps stolen by a hitbox spanning the full screen. Don't confuse this with `edgeInset` (see [Safe area](#safe-area)): that shrinks the *basis* a percentage size resolves against, on both `Drawer` and `DrawerEdgeSwipe`; these inset props only trim `DrawerEdgeSwipe`'s own hitbox span and have no equivalent on `Drawer` or `createDrawer()`.
|
|
188
213
|
|
|
189
214
|
`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.
|
|
190
215
|
|
package/dist/index.d.mts
CHANGED
|
@@ -120,4 +120,12 @@ type DrawerEdgeSwipeProps = {
|
|
|
120
120
|
};
|
|
121
121
|
declare const DrawerEdgeSwipe: ({ edgeInset, enabled, height, insetBottom, insetLeft, insetRight, insetTop, maxHeight, maxWidth, onOpen, side, translateOffset, width }: DrawerEdgeSwipeProps) => react.JSX.Element;
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
type DrawerSize = {
|
|
124
|
+
effectiveSize: number;
|
|
125
|
+
closedOffset: number;
|
|
126
|
+
maxEffectiveSize: number;
|
|
127
|
+
restOffset: number;
|
|
128
|
+
};
|
|
129
|
+
declare function useDrawerSize(side: DrawerSide, size: DrawerDimension, maxSize?: DrawerDimension, edgeInset?: number): DrawerSize;
|
|
130
|
+
|
|
131
|
+
export { type AutoPaperModule, type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, type DrawerConfig, type DrawerDimension, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerInstanceProviderComponent, type DrawerInstanceProviderProps, type DrawerProps, DrawerProvider, type DrawerProviderProps, type DrawerSide, type DrawerSize, combineDrawerProviders, configureDrawer, createDrawer, getDrawerConfig, useDrawerSize };
|
package/dist/index.d.ts
CHANGED
|
@@ -120,4 +120,12 @@ type DrawerEdgeSwipeProps = {
|
|
|
120
120
|
};
|
|
121
121
|
declare const DrawerEdgeSwipe: ({ edgeInset, enabled, height, insetBottom, insetLeft, insetRight, insetTop, maxHeight, maxWidth, onOpen, side, translateOffset, width }: DrawerEdgeSwipeProps) => react.JSX.Element;
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
type DrawerSize = {
|
|
124
|
+
effectiveSize: number;
|
|
125
|
+
closedOffset: number;
|
|
126
|
+
maxEffectiveSize: number;
|
|
127
|
+
restOffset: number;
|
|
128
|
+
};
|
|
129
|
+
declare function useDrawerSize(side: DrawerSide, size: DrawerDimension, maxSize?: DrawerDimension, edgeInset?: number): DrawerSize;
|
|
130
|
+
|
|
131
|
+
export { type AutoPaperModule, type CreateDrawerOptions, type CreateDrawerResult, Drawer, type DrawerActions, type DrawerConfig, type DrawerDimension, DrawerEdgeSwipe, type DrawerEdgeSwipeProps, type DrawerInstanceProviderComponent, type DrawerInstanceProviderProps, type DrawerProps, DrawerProvider, type DrawerProviderProps, type DrawerSide, type DrawerSize, combineDrawerProviders, configureDrawer, createDrawer, getDrawerConfig, useDrawerSize };
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,8 @@ __export(index_exports, {
|
|
|
36
36
|
combineDrawerProviders: () => combineDrawerProviders,
|
|
37
37
|
configureDrawer: () => configureDrawer,
|
|
38
38
|
createDrawer: () => createDrawer,
|
|
39
|
-
getDrawerConfig: () => getDrawerConfig
|
|
39
|
+
getDrawerConfig: () => getDrawerConfig,
|
|
40
|
+
useDrawerSize: () => useDrawerSize
|
|
40
41
|
});
|
|
41
42
|
module.exports = __toCommonJS(index_exports);
|
|
42
43
|
|
|
@@ -72,6 +73,9 @@ function getOpenDirection(side) {
|
|
|
72
73
|
function resolveDimension(value, windowSize) {
|
|
73
74
|
return typeof value === "number" ? value : Number.parseFloat(value) / 100 * windowSize;
|
|
74
75
|
}
|
|
76
|
+
function clampToWindow(size, windowSize) {
|
|
77
|
+
return windowSize > 0 ? Math.min(size, windowSize) : size;
|
|
78
|
+
}
|
|
75
79
|
|
|
76
80
|
// src/useDrawerSize.ts
|
|
77
81
|
var import_react_native = require("react-native");
|
|
@@ -79,8 +83,8 @@ function useDrawerSize(side, size, maxSize, edgeInset = 0) {
|
|
|
79
83
|
const windowDimensions = (0, import_react_native.useWindowDimensions)();
|
|
80
84
|
const windowSize = isVerticalSide(side) ? windowDimensions.height : windowDimensions.width;
|
|
81
85
|
const percentBasis = windowSize - edgeInset;
|
|
82
|
-
const effectiveSize = resolveDimension(size, percentBasis);
|
|
83
|
-
const maxEffectiveSize = maxSize === void 0 ? effectiveSize : Math.max(resolveDimension(maxSize, percentBasis), effectiveSize);
|
|
86
|
+
const effectiveSize = clampToWindow(resolveDimension(size, percentBasis), windowSize);
|
|
87
|
+
const maxEffectiveSize = maxSize === void 0 ? effectiveSize : Math.max(clampToWindow(resolveDimension(maxSize, percentBasis), windowSize), effectiveSize);
|
|
84
88
|
const closedOffset = getClosedOffset(side, maxEffectiveSize);
|
|
85
89
|
const restOffset = getClosedOffset(side, maxEffectiveSize - effectiveSize);
|
|
86
90
|
return { closedOffset, effectiveSize, maxEffectiveSize, restOffset };
|
|
@@ -132,7 +136,7 @@ var Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children,
|
|
|
132
136
|
const drawerStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({ transform: [vertical ? { translateY: translateOffset.value } : { translateX: translateOffset.value }] }));
|
|
133
137
|
const backdropStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => ({ opacity: maxEffectiveSize > 0 ? (1 - Math.abs(translateOffset.value) / maxEffectiveSize) * backdropOpacity : 0 }));
|
|
134
138
|
const animatedSizeStyle = (0, import_react_native_reanimated.useAnimatedStyle)(() => vertical ? { height: animatedSize.value } : { width: animatedSize.value });
|
|
135
|
-
const
|
|
139
|
+
const backdropPointerEvents = open && blockingBackdrop ? "box-none" : "none";
|
|
136
140
|
const backdropTapAreaStyle = side === "left" ? { left: maxEffectiveSize } : side === "right" ? { right: maxEffectiveSize } : side === "top" ? { top: maxEffectiveSize } : { bottom: maxEffectiveSize };
|
|
137
141
|
const positionStyle = side === "left" ? styles.anchorLeft : side === "right" ? styles.anchorRight : side === "top" ? styles.anchorTop : styles.anchorBottom;
|
|
138
142
|
const sizeStyle = vertical ? { height: maxEffectiveSize } : { width: maxEffectiveSize };
|
|
@@ -206,11 +210,11 @@ var Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children,
|
|
|
206
210
|
return { opacity: Math.min(1, Math.max(0, visibleSize / panelShadowFadeDistance)) };
|
|
207
211
|
});
|
|
208
212
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
209
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native_reanimated.default.View, { style: [styles.backdropPosition,
|
|
213
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native_reanimated.default.View, { pointerEvents: backdropPointerEvents, style: [styles.backdropPosition, { zIndex }], children: [
|
|
210
214
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { style: [styles.backdropTint, backdropStyle], pointerEvents: "none" }),
|
|
211
215
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_gesture_handler.GestureDetector, { gesture: backdropTapGesture, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { collapsable: false, style: [import_react_native2.StyleSheet.absoluteFill, backdropTapAreaStyle] }) })
|
|
212
216
|
] }),
|
|
213
|
-
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native2.View, { pointerEvents: "box-none", style: styles.panelClip, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native_reanimated.default.View, { style: drawerOuterStyle, children: [
|
|
217
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native2.View, { pointerEvents: "box-none", style: [styles.panelClip, { zIndex: zIndex + 1 }], children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react_native_reanimated.default.View, { style: drawerOuterStyle, children: [
|
|
214
218
|
resolvedPanelShadow && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native_reanimated.default.View, { pointerEvents: "none", style: [import_react_native2.StyleSheet.absoluteFill, panelShadowStyle, panelShadowVisibilityStyle] }),
|
|
215
219
|
contentSize ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native2.View, { style: contentClipStyle, children: fill }) : fill,
|
|
216
220
|
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_reanimated.default.View, { style: [styles.handleStrip, handleStripSizeStyle, handleStripEdgeStyle, handleStripPaddingStyle, webCursorStyle2, handleVisibilityStyle, handleStripInteractionStyle], children: showHandle && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native2.View, { style: [styles.handlePill, handlePillStyle, { backgroundColor: colors.surface }] }) }) })
|
|
@@ -267,7 +271,7 @@ var styles = import_react_native2.StyleSheet.create({
|
|
|
267
271
|
justifyContent: "center",
|
|
268
272
|
position: "absolute"
|
|
269
273
|
},
|
|
270
|
-
// See the comment above <View style={styles.panelClip}> in the render
|
|
274
|
+
// See the comment above <View style={styles.panelClip}> in the render - the unconditional clip
|
|
271
275
|
// ancestor that keeps a translated-off-screen "closed" panel from inflating page scroll extent.
|
|
272
276
|
panelClip: {
|
|
273
277
|
bottom: 0,
|
|
@@ -414,5 +418,6 @@ var combineDrawerProviders = (...entries) => {
|
|
|
414
418
|
combineDrawerProviders,
|
|
415
419
|
configureDrawer,
|
|
416
420
|
createDrawer,
|
|
417
|
-
getDrawerConfig
|
|
421
|
+
getDrawerConfig,
|
|
422
|
+
useDrawerSize
|
|
418
423
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -30,6 +30,9 @@ function getOpenDirection(side) {
|
|
|
30
30
|
function resolveDimension(value, windowSize) {
|
|
31
31
|
return typeof value === "number" ? value : Number.parseFloat(value) / 100 * windowSize;
|
|
32
32
|
}
|
|
33
|
+
function clampToWindow(size, windowSize) {
|
|
34
|
+
return windowSize > 0 ? Math.min(size, windowSize) : size;
|
|
35
|
+
}
|
|
33
36
|
|
|
34
37
|
// src/useDrawerSize.ts
|
|
35
38
|
import { useWindowDimensions } from "react-native";
|
|
@@ -37,8 +40,8 @@ function useDrawerSize(side, size, maxSize, edgeInset = 0) {
|
|
|
37
40
|
const windowDimensions = useWindowDimensions();
|
|
38
41
|
const windowSize = isVerticalSide(side) ? windowDimensions.height : windowDimensions.width;
|
|
39
42
|
const percentBasis = windowSize - edgeInset;
|
|
40
|
-
const effectiveSize = resolveDimension(size, percentBasis);
|
|
41
|
-
const maxEffectiveSize = maxSize === void 0 ? effectiveSize : Math.max(resolveDimension(maxSize, percentBasis), effectiveSize);
|
|
43
|
+
const effectiveSize = clampToWindow(resolveDimension(size, percentBasis), windowSize);
|
|
44
|
+
const maxEffectiveSize = maxSize === void 0 ? effectiveSize : Math.max(clampToWindow(resolveDimension(maxSize, percentBasis), windowSize), effectiveSize);
|
|
42
45
|
const closedOffset = getClosedOffset(side, maxEffectiveSize);
|
|
43
46
|
const restOffset = getClosedOffset(side, maxEffectiveSize - effectiveSize);
|
|
44
47
|
return { closedOffset, effectiveSize, maxEffectiveSize, restOffset };
|
|
@@ -90,7 +93,7 @@ var Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children,
|
|
|
90
93
|
const drawerStyle = useAnimatedStyle(() => ({ transform: [vertical ? { translateY: translateOffset.value } : { translateX: translateOffset.value }] }));
|
|
91
94
|
const backdropStyle = useAnimatedStyle(() => ({ opacity: maxEffectiveSize > 0 ? (1 - Math.abs(translateOffset.value) / maxEffectiveSize) * backdropOpacity : 0 }));
|
|
92
95
|
const animatedSizeStyle = useAnimatedStyle(() => vertical ? { height: animatedSize.value } : { width: animatedSize.value });
|
|
93
|
-
const
|
|
96
|
+
const backdropPointerEvents = open && blockingBackdrop ? "box-none" : "none";
|
|
94
97
|
const backdropTapAreaStyle = side === "left" ? { left: maxEffectiveSize } : side === "right" ? { right: maxEffectiveSize } : side === "top" ? { top: maxEffectiveSize } : { bottom: maxEffectiveSize };
|
|
95
98
|
const positionStyle = side === "left" ? styles.anchorLeft : side === "right" ? styles.anchorRight : side === "top" ? styles.anchorTop : styles.anchorBottom;
|
|
96
99
|
const sizeStyle = vertical ? { height: maxEffectiveSize } : { width: maxEffectiveSize };
|
|
@@ -164,11 +167,11 @@ var Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur, children,
|
|
|
164
167
|
return { opacity: Math.min(1, Math.max(0, visibleSize / panelShadowFadeDistance)) };
|
|
165
168
|
});
|
|
166
169
|
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
167
|
-
/* @__PURE__ */ jsxs(Animated.View, { style: [styles.backdropPosition,
|
|
170
|
+
/* @__PURE__ */ jsxs(Animated.View, { pointerEvents: backdropPointerEvents, style: [styles.backdropPosition, { zIndex }], children: [
|
|
168
171
|
/* @__PURE__ */ jsx(Animated.View, { style: [styles.backdropTint, backdropStyle], pointerEvents: "none" }),
|
|
169
172
|
/* @__PURE__ */ jsx(GestureDetector, { gesture: backdropTapGesture, children: /* @__PURE__ */ jsx(Animated.View, { collapsable: false, style: [StyleSheet.absoluteFill, backdropTapAreaStyle] }) })
|
|
170
173
|
] }),
|
|
171
|
-
/* @__PURE__ */ jsx(View, { pointerEvents: "box-none", style: styles.panelClip, children: /* @__PURE__ */ jsxs(Animated.View, { style: drawerOuterStyle, children: [
|
|
174
|
+
/* @__PURE__ */ jsx(View, { pointerEvents: "box-none", style: [styles.panelClip, { zIndex: zIndex + 1 }], children: /* @__PURE__ */ jsxs(Animated.View, { style: drawerOuterStyle, children: [
|
|
172
175
|
resolvedPanelShadow && /* @__PURE__ */ jsx(Animated.View, { pointerEvents: "none", style: [StyleSheet.absoluteFill, panelShadowStyle, panelShadowVisibilityStyle] }),
|
|
173
176
|
contentSize ? /* @__PURE__ */ jsx(View, { style: contentClipStyle, children: fill }) : fill,
|
|
174
177
|
dismissible && /* @__PURE__ */ jsx(GestureDetector, { gesture: handleGesture, children: /* @__PURE__ */ jsx(Animated.View, { style: [styles.handleStrip, handleStripSizeStyle, handleStripEdgeStyle, handleStripPaddingStyle, webCursorStyle2, handleVisibilityStyle, handleStripInteractionStyle], children: showHandle && /* @__PURE__ */ jsx(View, { style: [styles.handlePill, handlePillStyle, { backgroundColor: colors.surface }] }) }) })
|
|
@@ -225,7 +228,7 @@ var styles = StyleSheet.create({
|
|
|
225
228
|
justifyContent: "center",
|
|
226
229
|
position: "absolute"
|
|
227
230
|
},
|
|
228
|
-
// See the comment above <View style={styles.panelClip}> in the render
|
|
231
|
+
// See the comment above <View style={styles.panelClip}> in the render - the unconditional clip
|
|
229
232
|
// ancestor that keeps a translated-off-screen "closed" panel from inflating page scroll extent.
|
|
230
233
|
panelClip: {
|
|
231
234
|
bottom: 0,
|
|
@@ -371,5 +374,6 @@ export {
|
|
|
371
374
|
combineDrawerProviders,
|
|
372
375
|
configureDrawer,
|
|
373
376
|
createDrawer,
|
|
374
|
-
getDrawerConfig
|
|
377
|
+
getDrawerConfig,
|
|
378
|
+
useDrawerSize
|
|
375
379
|
};
|
package/package.json
CHANGED
package/src/Drawer.tsx
CHANGED
|
@@ -31,18 +31,18 @@ const useBlurFallback = (override?: boolean) => override ?? false
|
|
|
31
31
|
export type DrawerPanelShadow = {
|
|
32
32
|
blurRadius?: number
|
|
33
33
|
color?: string
|
|
34
|
-
// Magnitude only, in px
|
|
34
|
+
// Magnitude only, in px - always positive, regardless of `side`. The actual signed offset
|
|
35
35
|
// (which way the shadow falls) is derived from `side` internally, the same way DrawerEdgeSwipe
|
|
36
36
|
// derives its own opening direction from it: a consumer supplying the wrong sign for a
|
|
37
|
-
// right/bottom-anchored panel (an easy mistake
|
|
37
|
+
// right/bottom-anchored panel (an easy mistake - see this prop's own top-level doc comment) is
|
|
38
38
|
// exactly the bug this exists to make impossible.
|
|
39
39
|
distance?: number
|
|
40
|
-
// Android's own native shadow system, alongside (not instead of) the boxShadow above
|
|
40
|
+
// Android's own native shadow system, alongside (not instead of) the boxShadow above - the two
|
|
41
41
|
// aren't redundant: boxShadow is what actually paints on iOS/web, and while the New
|
|
42
42
|
// Architecture's Fabric renderer does translate boxShadow to a real shadow on Android too,
|
|
43
43
|
// elevation is the longer-established, better-tested path there and worth keeping as a belt-
|
|
44
44
|
// and-suspenders fallback rather than trusting boxShadow alone on every Android/RN version this
|
|
45
|
-
// could run on. Defaults to blurRadius itself
|
|
45
|
+
// could run on. Defaults to blurRadius itself - the same plain magnitude-to-elevation
|
|
46
46
|
// correspondence a consumer would otherwise hand-roll (this mirrors what this repo's own past
|
|
47
47
|
// panelContent styles, now replaced by this prop, always did: elevation set to the same number
|
|
48
48
|
// as blurRadius). Rendered on the SAME view panelShadowVisibilityStyle animates the opacity of,
|
|
@@ -72,11 +72,11 @@ export type DrawerProps = {
|
|
|
72
72
|
// content-facing edge) rather than claiming space inside it.
|
|
73
73
|
dismissible?: boolean
|
|
74
74
|
// Shrinks the basis a *percentage* height/width/maxHeight/maxWidth resolves against, so `'100%'`
|
|
75
|
-
// fills exactly up to `edgeInset` px short of the screen's edge instead of reaching it
|
|
75
|
+
// fills exactly up to `edgeInset` px short of the screen's edge instead of reaching it - e.g.
|
|
76
76
|
// `edgeInset={insets.top}` on a `bottom` sheet with `maxHeight: '100%'` stops it just short of the
|
|
77
77
|
// status bar/notch, rather than sliding behind it. The geometry-clamp alternative to padding
|
|
78
78
|
// `content` away from a safe-area inset (see the README's Safe area section): the panel itself
|
|
79
|
-
// never reaches the inset, so there's nothing inside `content` to compensate for
|
|
79
|
+
// never reaches the inset, so there's nothing inside `content` to compensate for - don't combine
|
|
80
80
|
// the two for the same edge, or the inset ends up applied twice. Has no effect on a plain px
|
|
81
81
|
// number: that's taken as the exact size you asked for, same as always, letting you opt back out
|
|
82
82
|
// of the inset for a specific drawer by passing a literal value instead of a percentage. Defaults
|
|
@@ -109,13 +109,13 @@ export type DrawerProps = {
|
|
|
109
109
|
// autofocusing a field inside `content` only once it's actually on screen.
|
|
110
110
|
onOpened?: () => void
|
|
111
111
|
open: boolean
|
|
112
|
-
// Off by default (this is a real, paid render
|
|
112
|
+
// Off by default (this is a real, paid render - an extra always-mounted Animated.View - not
|
|
113
113
|
// free to opt every consumer into). true uses sensible defaults (2px distance, 8px blur, 30%
|
|
114
114
|
// black); pass a DrawerPanelShadow to override any of those. Opacity fades in/out together with
|
|
115
115
|
// the handle strip's own pill (both driven by the same visibleSize), rather than being either
|
|
116
116
|
// permanently on or permanently off: a plain static shadow on the panel bleeds past its own
|
|
117
117
|
// blurRadius even while `open` is false and the panel is translated fully off-screen, since a
|
|
118
|
-
// closed panel sits exactly flush against the screen edge (translateOffset === closedOffset)
|
|
118
|
+
// closed panel sits exactly flush against the screen edge (translateOffset === closedOffset) -
|
|
119
119
|
// shadow rendering doesn't know or care that its box is off-screen, so a few px of blur reads as
|
|
120
120
|
// a permanent, unwanted line along that edge, closed or not.
|
|
121
121
|
panelShadow?: boolean | DrawerPanelShadow
|
|
@@ -150,12 +150,12 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
150
150
|
const panelShadowElevation = resolvedPanelShadow?.elevation ?? panelShadowBlurRadius
|
|
151
151
|
// Same sign convention as the "opens toward" direction below (openDirection: 1 for left/top, -1
|
|
152
152
|
// for right/bottom): a panel's shadow always falls AWAY from the panel, onto whatever content
|
|
153
|
-
// sits on its open side
|
|
153
|
+
// sits on its open side - which is exactly the side its own drag-to-open motion points toward.
|
|
154
154
|
const panelShadowMagnitude = panelShadowDistance * openDirection
|
|
155
155
|
const panelShadowStyle: ViewStyle = { boxShadow: [{ offsetX: vertical ? 0 : panelShadowMagnitude, offsetY: vertical ? panelShadowMagnitude : 0, blurRadius: panelShadowBlurRadius, color: panelShadowColor }], elevation: panelShadowElevation }
|
|
156
156
|
|
|
157
157
|
const [measuredSize, setMeasuredSize] = useState<number | null>(null)
|
|
158
|
-
// Tracks the last value actually committed to measuredSize, rounded
|
|
158
|
+
// Tracks the last value actually committed to measuredSize, rounded - see handleLayout below for
|
|
159
159
|
// why the comparison has to happen against this rather than just re-deriving it from measuredSize
|
|
160
160
|
// itself on every call.
|
|
161
161
|
const lastMeasuredRef = useRef<number | null>(null)
|
|
@@ -178,7 +178,7 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
178
178
|
|
|
179
179
|
useEffect(() => {
|
|
180
180
|
if (open) {
|
|
181
|
-
// Rests at restOffset (== 0, fully expanded, when there's no maxHeight/maxWidth
|
|
181
|
+
// Rests at restOffset (== 0, fully expanded, when there's no maxHeight/maxWidth - identical
|
|
182
182
|
// to before), not always 0: an expandable sheet opens to its rest size, not its max size.
|
|
183
183
|
translateOffset.value = withSpring(restOffset, SPRING, (finished) => {
|
|
184
184
|
if (finished && onOpened) runOnJS(onOpened)()
|
|
@@ -202,7 +202,7 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
202
202
|
|
|
203
203
|
const drawerStyle = useAnimatedStyle(() => ({ transform: [vertical ? { translateY: translateOffset.value } : { translateX: translateOffset.value }] }))
|
|
204
204
|
// Denominator is maxEffectiveSize, not effectiveSize/rest: dims proportionally to how much of the
|
|
205
|
-
// panel's full (possibly-expanded) size is currently covering the screen, not just its rest size
|
|
205
|
+
// panel's full (possibly-expanded) size is currently covering the screen, not just its rest size -
|
|
206
206
|
// otherwise a still-collapsing expandable sheet would hit full backdropOpacity well before it's
|
|
207
207
|
// actually fully open. Reduces to today's exact behavior when there's no maxHeight/maxWidth, since
|
|
208
208
|
// maxEffectiveSize === effectiveSize then.
|
|
@@ -213,24 +213,32 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
213
213
|
const backdropStyle = useAnimatedStyle(() => ({ opacity: maxEffectiveSize > 0 ? (1 - Math.abs(translateOffset.value) / maxEffectiveSize) * backdropOpacity : 0 }))
|
|
214
214
|
const animatedSizeStyle = useAnimatedStyle(() => (vertical ? { height: animatedSize.value } : { width: animatedSize.value }))
|
|
215
215
|
// 'box-none' (not 'auto'): this wrapper is just a positioning shell around the dimming tint and
|
|
216
|
-
// the actual tap-catching view below
|
|
217
|
-
// wins hit-testing over a plain-DOM-order sibling's
|
|
218
|
-
//
|
|
216
|
+
// the actual tap-catching view below - on react-native-web, an ancestor's own explicit zIndex
|
|
217
|
+
// wins hit-testing over a plain-DOM-order sibling's lower-tier descendants (the panel lives inside
|
|
218
|
+
// panelClip, which now carries the panel's tier itself - see its comment), so this wrapper being 'auto' made
|
|
219
219
|
// its own full-screen box outrank the panel for every click anywhere on screen, even where the
|
|
220
|
-
// panel visually covers it
|
|
220
|
+
// panel visually covers it - a tap on real panel content (e.g. a settings toggle) would land on
|
|
221
221
|
// this inert wrapper instead and silently do nothing. 'box-none' keeps the wrapper itself out of
|
|
222
222
|
// hit-testing while still letting its own children (the tap-catcher below) receive touches within
|
|
223
|
-
// their own (now-shrunk, see backdropTapAreaStyle) bounds
|
|
223
|
+
// their own (now-shrunk, see backdropTapAreaStyle) bounds - exactly the "auto" behavior this was
|
|
224
224
|
// meant to have, minus the container falsely claiming space it doesn't actually own.
|
|
225
|
-
|
|
225
|
+
//
|
|
226
|
+
//
|
|
227
|
+
// Passed as the pointerEvents PROP, not inside style. On react-native-web a style
|
|
228
|
+
// `pointerEvents: 'box-none'` is written inline as a plain CSS `pointer-events: none`, with nothing
|
|
229
|
+
// switching the children back on - and Animated.View flattens styles to inline, so even a
|
|
230
|
+
// StyleSheet one ends up that way. The tap-catcher below inherited `none`, so a tap beside an open
|
|
231
|
+
// drawer fell straight through to the screen under it. The prop becomes RNW's box-none class,
|
|
232
|
+
// which does re-enable the children; it's how the tint and panelClip already set theirs.
|
|
233
|
+
const backdropPointerEvents = open && blockingBackdrop ? 'box-none' : 'none'
|
|
226
234
|
// react-native-gesture-handler's web backend resolves a Gesture.Tap's hit region from this
|
|
227
235
|
// view's own measured bounds, not from real DOM occlusion/z-index the way native UIKit/Android
|
|
228
|
-
// hit-testing does
|
|
236
|
+
// hit-testing does - so on web, a tap landing anywhere on the (opaque, higher-zIndex) panel on
|
|
229
237
|
// top of this backdrop was still satisfying the backdrop's OWN Tap gesture underneath it,
|
|
230
238
|
// closing the drawer out from under a perfectly normal tap on its content (e.g. a toggle inside
|
|
231
239
|
// a settings panel). Native is unaffected (its real view hierarchy already occludes correctly),
|
|
232
240
|
// but shrinking this view's own bounds to exclude the panel's footprint is correct on every
|
|
233
|
-
// platform regardless
|
|
241
|
+
// platform regardless - that region was always hidden behind the opaque panel anyway, so this
|
|
234
242
|
// changes nothing observable, it just stops the gesture from being reachable there at all.
|
|
235
243
|
const backdropTapAreaStyle: ViewStyle = side === 'left' ? { left: maxEffectiveSize } : side === 'right' ? { right: maxEffectiveSize } : side === 'top' ? { top: maxEffectiveSize } : { bottom: maxEffectiveSize }
|
|
236
244
|
|
|
@@ -242,14 +250,14 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
242
250
|
// Rounded (not the raw sub-pixel float layout reports) and short-circuited against the last value
|
|
243
251
|
// actually committed: an animated ancestor (animatedSizeStyle below, mid-withTiming) can cause
|
|
244
252
|
// Yoga to re-measure this supposedly content-unconstrained wrapper on every frame it's still
|
|
245
|
-
// moving, and floating-point noise between passes (e.g. 283.3333740234375 vs 283.33331298828125
|
|
253
|
+
// moving, and floating-point noise between passes (e.g. 283.3333740234375 vs 283.33331298828125 -
|
|
246
254
|
// the same logical height, off by ~1/16000th of a pixel) is "different" by strict equality. Left
|
|
247
255
|
// unguarded, that noise commits a "new" measuredSize every frame, which restarts the very
|
|
248
|
-
// withTiming animation that's causing the re-measurement in the first place
|
|
256
|
+
// withTiming animation that's causing the re-measurement in the first place - a self-sustaining
|
|
249
257
|
// loop that never lets the height settle, seen as a stutter rather than a single smooth resize.
|
|
250
258
|
// Comparing against a ref (not measuredSize itself) matters here: measuredSize is the state that's
|
|
251
259
|
// deliberately being skipped on a no-op call, so re-deriving the comparison from it would still
|
|
252
|
-
// see the same (unchanged) value next time regardless
|
|
260
|
+
// see the same (unchanged) value next time regardless - the ref is what actually remembers what
|
|
253
261
|
// was last committed.
|
|
254
262
|
const handleLayout = (event: LayoutChangeEvent) => {
|
|
255
263
|
const measured = Math.round(vertical ? event.nativeEvent.layout.height : event.nativeEvent.layout.width)
|
|
@@ -292,10 +300,10 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
292
300
|
)
|
|
293
301
|
// contentSize's measuring wrapper above is deliberately given no style of its own (see the
|
|
294
302
|
// comment above `fillStyle`): it must report ITS OWN natural size, which only works if `children`
|
|
295
|
-
// itself has an intrinsic height Yoga can compute directly from its subtree
|
|
303
|
+
// itself has an intrinsic height Yoga can compute directly from its subtree - e.g. plain
|
|
296
304
|
// Views/Text/FABs, including a `flexWrap: 'wrap'` row like FabRow. A `ScrollView` breaks this: on
|
|
297
|
-
// native, ScrollView's outer box does NOT size itself to its content the way a plain View does
|
|
298
|
-
// it needs a bounded height from an ANCESTOR instead (see ScrollView's own docs)
|
|
305
|
+
// native, ScrollView's outer box does NOT size itself to its content the way a plain View does -
|
|
306
|
+
// it needs a bounded height from an ANCESTOR instead (see ScrollView's own docs) - so measuring
|
|
299
307
|
// one via onLayout here reports some small, real-but-wrong box height (whatever Yoga happens to
|
|
300
308
|
// resolve it to absent an explicit bound) rather than the content's true extent, and clips
|
|
301
309
|
// anything beyond that. contentSize's `children` must render its own natural-height content
|
|
@@ -318,17 +326,17 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
318
326
|
// the panel slides back toward), gesture-matched to `side` the same way DrawerEdgeSwipe is. With no
|
|
319
327
|
// maxHeight/maxWidth it's a simple two-point toggle, same as before: starts from the OPEN/rest
|
|
320
328
|
// position (restOffset, which equals 0 in that case) and commits toward `closedOffset` or springs
|
|
321
|
-
// back. With one, it's three points along the same axis
|
|
322
|
-
// expanded (0)
|
|
329
|
+
// back. With one, it's three points along the same axis - closedOffset, restOffset, and fully
|
|
330
|
+
// expanded (0) - and the drag can commit toward whichever of those neighbors it's currently
|
|
323
331
|
// nearest, in either direction. Deliberately its own hit area rather than the whole panel, so it
|
|
324
332
|
// can't steal touches from arbitrary interactive content inside `children`.
|
|
325
333
|
const clampMin = Math.min(0, closedOffset)
|
|
326
334
|
const clampMax = Math.max(0, closedOffset)
|
|
327
335
|
|
|
328
336
|
// A single signed offset (e.g. closeDirection * 10) only arms the gesture for drags past that
|
|
329
|
-
// threshold in ONE direction
|
|
337
|
+
// threshold in ONE direction - exactly what the plain close-only case wants (dragging the "wrong"
|
|
330
338
|
// way, further into "already open," should fall through rather than eat the touch), but it means a
|
|
331
|
-
// drag toward expanding
|
|
339
|
+
// drag toward expanding - the opposite direction - never activates the gesture at all. With no
|
|
332
340
|
// maxHeight/maxWidth (restOffset always 0) that's fine, since there's nothing to expand into: keep
|
|
333
341
|
// the single-direction threshold, unchanged from before. Once expansion is possible, the gesture
|
|
334
342
|
// needs to recognize BOTH directions, so it switches to a symmetric range instead.
|
|
@@ -372,7 +380,7 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
372
380
|
const velocity = vertical ? event.velocityY : event.velocityX
|
|
373
381
|
// Everything below works in "closing-direction" units (closeDirection-normalized), where 0 is
|
|
374
382
|
// always fully expanded and larger is always closer to closed, regardless of `side`'s sign
|
|
375
|
-
// convention
|
|
383
|
+
// convention - the same trick the old single-segment formula used, just reused per segment.
|
|
376
384
|
const velocityD = velocity * closeDirection
|
|
377
385
|
const currentD = translateOffset.value * closeDirection
|
|
378
386
|
const restD = restOffset * closeDirection // 0 when there's no maxHeight/maxWidth
|
|
@@ -383,7 +391,7 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
383
391
|
// progress 1): traveled = restD - currentD, since currentD counts down from restD to 0 as
|
|
384
392
|
// the drag moves toward expanding. Same 1/3-traveled threshold as the rest/closed segment
|
|
385
393
|
// below, just scoped to this segment's own length and direction; a fast fling toward
|
|
386
|
-
// expanding (velocityD very negative
|
|
394
|
+
// expanding (velocityD very negative - the opposite sign from the closing fling below) also
|
|
387
395
|
// commits, regardless of distance covered.
|
|
388
396
|
const progress = restD > 0 ? (restD - currentD) / restD : 1
|
|
389
397
|
target = progress > 1 / 3 || velocityD < -VELOCITY_THRESHOLD ? 0 : restOffset
|
|
@@ -411,26 +419,26 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
411
419
|
// minimum touch target) without the pill itself growing or the strip visually eating panel space.
|
|
412
420
|
// Deliberately not done via Gesture.Pan().hitSlop(): react-native-gesture-handler's web backend
|
|
413
421
|
// binds its pointer listeners directly to the strip's own DOM node, so a pointer genuinely outside
|
|
414
|
-
// that node's rendered box never reaches the handler there at all
|
|
422
|
+
// that node's rendered box never reaches the handler there at all - hitSlop only helps on native,
|
|
415
423
|
// where the OS extends the touch region a different way. Growing the real box, below, works
|
|
416
424
|
// everywhere a pointer/touch can actually land.
|
|
417
425
|
const HANDLE_HIT_SLOP = 20
|
|
418
426
|
const HANDLE_STRIP_TOTAL_THICKNESS = HANDLE_STRIP_THICKNESS + HANDLE_HIT_SLOP
|
|
419
427
|
const handleStripSizeStyle: ViewStyle = vertical ? { height: HANDLE_STRIP_TOTAL_THICKNESS, left: 0, right: 0 } : { bottom: 0, top: 0, width: HANDLE_STRIP_TOTAL_THICKNESS }
|
|
420
428
|
// Sits OUTSIDE the panel's own box rather than claiming interior visual space. Anchored from the
|
|
421
|
-
// FAR (backdrop-facing) edge
|
|
429
|
+
// FAR (backdrop-facing) edge - the opposite side from `side` - since that edge's distance from the
|
|
422
430
|
// panel is the one constant here (always exactly HANDLE_STRIP_THICKNESS past the panel, same as
|
|
423
431
|
// before this box grew): [oppositeSide]: -HANDLE_STRIP_THICKNESS fixes it there, and the box's full
|
|
424
432
|
// HANDLE_STRIP_TOTAL_THICKNESS width/height extends back from that fixed point, covering the
|
|
425
433
|
// original strip's footprint plus HANDLE_HIT_SLOP more, into the panel. `right`/`left`/`top`/
|
|
426
|
-
// `bottom` values are always relative to the parent's actual current edge, so
|
|
427
|
-
// percentage-based version
|
|
434
|
+
// `bottom` values are always relative to the parent's actual current edge, so - like the old
|
|
435
|
+
// percentage-based version - this still tracks correctly while contentSize is animating the
|
|
428
436
|
// panel's size. Requires drawerOuterStyle to stay unclipped (see its comment above) or this would
|
|
429
437
|
// be cut off.
|
|
430
438
|
const oppositeSide = side === 'left' ? 'right' : side === 'right' ? 'left' : side === 'top' ? 'bottom' : 'top'
|
|
431
439
|
const handleStripEdgeStyle: ViewStyle = { [oppositeSide]: -HANDLE_STRIP_THICKNESS }
|
|
432
440
|
// React Native sizes width/height as the outer box (unlike default web CSS box-sizing), so this
|
|
433
|
-
// padding
|
|
441
|
+
// padding - on the panel-facing side only - shrinks just the flex CONTENT area back down to the
|
|
434
442
|
// original 28px, without changing the box's own outer bounds. alignItems/justifyContent below then
|
|
435
443
|
// center the pill within that content area, landing it at exactly its pre-hit-slop spot: only the
|
|
436
444
|
// invisible margin around it grew.
|
|
@@ -445,7 +453,7 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
445
453
|
// panel's own content, it isn't carried off-screen by the panel's close translation: that
|
|
446
454
|
// translation is only ever calibrated to hide the panel's own maxEffectiveSize, not the strip's
|
|
447
455
|
// extra thickness beyond it. Once less than the strip's own thickness of panel remains visible,
|
|
448
|
-
// there's no panel edge left for it to be flush against, so it's hidden
|
|
456
|
+
// there's no panel edge left for it to be flush against, so it's hidden - otherwise it wells up
|
|
449
457
|
// sitting right at the screen edge looking like the drawer is still (a little) open, even at rest
|
|
450
458
|
// fully closed. visibleSize mirrors the gesture's own D-space trick above: 0 at fully closed,
|
|
451
459
|
// maxEffectiveSize at fully expanded, regardless of `side`'s sign convention.
|
|
@@ -458,7 +466,7 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
458
466
|
// the panel's true edge) rather than snapping like the handle strip's own binary visibleSize
|
|
459
467
|
// check above: by the time visibleSize passes that point, the panel edge itself is already
|
|
460
468
|
// further onto the screen than the shadow ever bled, so there's no longer a "fake" shadow to
|
|
461
|
-
// distinguish from a real one
|
|
469
|
+
// distinguish from a real one - full opacity is correct from there on. Always computed (hooks
|
|
462
470
|
// can't be called conditionally) even when panelShadow is falsy; harmless, since the style is
|
|
463
471
|
// never applied to anything in that case.
|
|
464
472
|
const panelShadowFadeDistance = panelShadowBlurRadius + panelShadowDistance
|
|
@@ -469,48 +477,53 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
469
477
|
|
|
470
478
|
return (
|
|
471
479
|
<>
|
|
472
|
-
<Animated.View style={[styles.backdropPosition,
|
|
480
|
+
<Animated.View pointerEvents={backdropPointerEvents} style={[styles.backdropPosition, { zIndex }]}>
|
|
473
481
|
{/* The dimming tint is its own child, carrying the actual black backgroundColor and
|
|
474
482
|
backdropStyle's animated opacity, separate from the tap-catching view below and from the
|
|
475
483
|
OUTER container above (which must stay paint-free, not just visually transparent at
|
|
476
484
|
opacity 0): on iOS, a view at opacity 0 (e.g. backdropOpacity={0}, an intentional no-dim
|
|
477
|
-
backdrop
|
|
485
|
+
backdrop - see controlGroups.tsx) is excluded from hit-testing entirely by UIKit,
|
|
478
486
|
regardless of pointerEvents. That's fine for a purely decorative tint, but it would
|
|
479
|
-
silently make the whole backdrop untouchable too if the SAME view carried both
|
|
487
|
+
silently make the whole backdrop untouchable too if the SAME view carried both - and if
|
|
480
488
|
the outer container carried the background color instead, it would paint solid black at
|
|
481
489
|
full opacity at all times (no animated opacity of its own), open or not. */}
|
|
482
490
|
<Animated.View style={[styles.backdropTint, backdropStyle]} pointerEvents='none' />
|
|
483
491
|
<GestureDetector gesture={backdropTapGesture}>
|
|
484
492
|
{/* collapsable={false}: this view has no paint properties of its own (no background,
|
|
485
493
|
no animated style), exactly the shape React Native's view-flattening optimizer removes
|
|
486
|
-
from the native tree on native platforms
|
|
494
|
+
from the native tree on native platforms - leaving the GestureDetector above with no real
|
|
487
495
|
view left to attach its tap recognizer to, so it would silently never receive a touch.
|
|
488
|
-
backdropTapAreaStyle shrinks this view's own bounds to exclude the panel's footprint
|
|
496
|
+
backdropTapAreaStyle shrinks this view's own bounds to exclude the panel's footprint -
|
|
489
497
|
see its own comment above for why that matters specifically on web. */}
|
|
490
498
|
<Animated.View collapsable={false} style={[StyleSheet.absoluteFill, backdropTapAreaStyle]} />
|
|
491
499
|
</GestureDetector>
|
|
492
500
|
</Animated.View>
|
|
493
|
-
{/* panelClip is a NEW, unconditional clipping ancestor around the panel
|
|
501
|
+
{/* panelClip is a NEW, unconditional clipping ancestor around the panel - NOT the same box as
|
|
494
502
|
drawerOuterStyle/styles.drawer, and deliberately not itself transformed. drawerOuterStyle
|
|
495
503
|
carries the open/closed translate (drawerStyle) and is anchored flush to its edge at its
|
|
496
|
-
FULL maxEffectiveSize (sizeStyle)
|
|
504
|
+
FULL maxEffectiveSize (sizeStyle) - so a "closed" panel is never actually removed from the
|
|
497
505
|
tree, only slid to closedOffset, which for side='right'/'bottom' lands at a POSITIVE
|
|
498
|
-
coordinate just past the visible edge (see geometry.ts's getClosedOffset
|
|
506
|
+
coordinate just past the visible edge (see geometry.ts's getClosedOffset - left/top land at
|
|
499
507
|
NEGATIVE coordinates instead, which ordinary page scroll can't reach, which is why this only
|
|
500
508
|
ever showed up on right/bottom drawers). With nothing upstream constraining that positive
|
|
501
509
|
offset, react-native-web's host page treats the translated-but-still-full-size panel as real
|
|
502
|
-
document content sitting just outside the viewport, which grows the page's scrollable area
|
|
510
|
+
document content sitting just outside the viewport, which grows the page's scrollable area -
|
|
503
511
|
scrolling into it reveals a fully rendered "closed" panel. Adding overflow:'hidden' directly
|
|
504
512
|
to styles.drawer itself is NOT the fix: that's the same box the transform moves, so its own
|
|
505
513
|
clip region would travel with it and cut off the handle strip below, which is deliberately
|
|
506
514
|
positioned outside drawerOuterStyle's bounds (see its own comment). panelClip instead fills
|
|
507
515
|
the drawer's whole mount area (never itself transformed) and clips ITS bounds, which stay
|
|
508
|
-
fixed regardless of where the translated panel inside it currently sits
|
|
516
|
+
fixed regardless of where the translated panel inside it currently sits - so the handle strip
|
|
509
517
|
(well within panelClip's much larger box) is unaffected, while a closed panel translated past
|
|
510
518
|
panelClip's edge is clipped from paint and stops contributing to page scroll extent.
|
|
511
519
|
pointerEvents='box-none': this wrapper spans the full mount area purely to establish the clip
|
|
512
|
-
boundary
|
|
513
|
-
|
|
520
|
+
boundary - it must never itself catch a touch, only its real content underneath should.
|
|
521
|
+
zIndex + 1 (the panel's own tier) ON panelClip itself: react-native-web gives every View
|
|
522
|
+
z-index 0, which makes panelClip a stacking context of its own, so the panel's zIndex + 1
|
|
523
|
+
only ranks it inside panelClip. Without a tier here, the backdrop (zIndex, a sibling of
|
|
524
|
+
panelClip) painted over the whole panel, tint included, and an open drawer's own content
|
|
525
|
+
read as dimmed and disabled. */}
|
|
526
|
+
<View pointerEvents='box-none' style={[styles.panelClip, { zIndex: zIndex + 1 }]}>
|
|
514
527
|
<Animated.View style={drawerOuterStyle}>
|
|
515
528
|
{/* pointerEvents='none': purely decorative, drawn outside the panel's own box (a directional
|
|
516
529
|
box-shadow never paints over the box's own content area), so it has no business
|
|
@@ -522,13 +535,13 @@ export const Drawer = ({ backdropOpacity = 0.45, blockingBackdrop = true, blur,
|
|
|
522
535
|
<GestureDetector gesture={handleGesture}>
|
|
523
536
|
{/* pointerEvents mirrors handleGesture's own .enabled(open) above, the same way the
|
|
524
537
|
backdrop's tap-catcher mirrors backdropTapGesture's .enabled(open && blockingBackdrop)
|
|
525
|
-
just above
|
|
538
|
+
just above - handleVisibilityStyle's opacity already fades this strip out once the
|
|
526
539
|
panel's closed, but opacity alone doesn't stop hit-testing on web/Android (unlike iOS,
|
|
527
|
-
which excludes opacity-0 views from hit-testing regardless of pointerEvents
|
|
540
|
+
which excludes opacity-0 views from hit-testing regardless of pointerEvents - see the
|
|
528
541
|
backdrop tint's own comment on that asymmetry). Left at 'auto' while closed, this
|
|
529
|
-
48px-wide strip sits at zIndex+2 flush against the screen edge closest to `side`
|
|
542
|
+
48px-wide strip sits at zIndex+2 flush against the screen edge closest to `side` -
|
|
530
543
|
easy to land a consumer's own edge-anchored chrome (an app bar icon, say) in that same
|
|
531
|
-
band
|
|
544
|
+
band - and swallows taps meant for whatever's underneath it despite being invisible
|
|
532
545
|
AND, since the gesture itself is disabled here too, unable to do anything with them
|
|
533
546
|
either. */}
|
|
534
547
|
<Animated.View style={[styles.handleStrip, handleStripSizeStyle, handleStripEdgeStyle, handleStripPaddingStyle, webCursorStyle, handleVisibilityStyle, handleStripInteractionStyle]}>{showHandle && <View style={[styles.handlePill, handlePillStyle, { backgroundColor: colors.surface }]} />}</Animated.View>
|
|
@@ -590,7 +603,7 @@ const styles = StyleSheet.create({
|
|
|
590
603
|
justifyContent: 'center',
|
|
591
604
|
position: 'absolute'
|
|
592
605
|
},
|
|
593
|
-
// See the comment above <View style={styles.panelClip}> in the render
|
|
606
|
+
// See the comment above <View style={styles.panelClip}> in the render - the unconditional clip
|
|
594
607
|
// ancestor that keeps a translated-off-screen "closed" panel from inflating page scroll extent.
|
|
595
608
|
panelClip: {
|
|
596
609
|
bottom: 0,
|
package/src/DrawerEdgeSwipe.tsx
CHANGED
|
@@ -20,7 +20,7 @@ export type DrawerEdgeSwipeProps = {
|
|
|
20
20
|
enabled?: boolean
|
|
21
21
|
height?: DrawerDimension
|
|
22
22
|
// Shrink the strip's span along its long axis, to clear chrome (a header, a tab bar, ...) that
|
|
23
|
-
// would otherwise have its own touches stolen by this gesture
|
|
23
|
+
// would otherwise have its own touches stolen by this gesture - only the pair perpendicular to
|
|
24
24
|
// `side`'s own strip thickness applies: insetTop/insetBottom for a left/right drawer's vertical
|
|
25
25
|
// strip, insetLeft/insetRight for a top/bottom drawer's horizontal one. The other pair is
|
|
26
26
|
// ignored. All default to 0 (the strip's prior, full-length behavior).
|
|
@@ -29,7 +29,7 @@ export type DrawerEdgeSwipeProps = {
|
|
|
29
29
|
insetRight?: number
|
|
30
30
|
insetTop?: number
|
|
31
31
|
// Mirrors Drawer's own maxHeight: when set, an edge swipe still only opens to `height` (the rest
|
|
32
|
-
// size), not all the way to this ceiling
|
|
32
|
+
// size), not all the way to this ceiling - matching Drawer's tap-to-open behavior, which also
|
|
33
33
|
// lands at rest. Reaching past rest, up to maxHeight, is only ever done via Drawer's own handle.
|
|
34
34
|
maxHeight?: DrawerDimension
|
|
35
35
|
// The maxWidth mirror of maxHeight, for `left`/`right` drawers.
|
|
@@ -61,7 +61,7 @@ export const DrawerEdgeSwipe = ({ edgeInset = 0, enabled = true, height = 300, i
|
|
|
61
61
|
'worklet'
|
|
62
62
|
const translation = vertical ? event.translationY : event.translationX
|
|
63
63
|
// react-hooks/immutability flags this because translateOffset is a prop, but mutating a
|
|
64
|
-
// Reanimated SharedValue's .value is the correct way to drive it
|
|
64
|
+
// Reanimated SharedValue's .value is the correct way to drive it - not a prop reassignment.
|
|
65
65
|
// eslint-disable-next-line react-hooks/immutability
|
|
66
66
|
translateOffset.value = Math.min(clampMax, Math.max(clampMin, closedOffset + translation))
|
|
67
67
|
})
|
|
@@ -85,7 +85,7 @@ export const DrawerEdgeSwipe = ({ edgeInset = 0, enabled = true, height = 300, i
|
|
|
85
85
|
return (
|
|
86
86
|
<GestureDetector gesture={gesture}>
|
|
87
87
|
{/* Same web-only affordance Drawer.tsx's own handle strip gives its drag grip (see its
|
|
88
|
-
webCursorStyle)
|
|
88
|
+
webCursorStyle) - without it, nothing on screen hints that this edge is draggable at
|
|
89
89
|
all on a platform with a mouse cursor to change in the first place.
|
|
90
90
|
|
|
91
91
|
pointerEvents here (not just gesture.enabled(enabled) above) matters on web specifically:
|
|
@@ -93,7 +93,7 @@ export const DrawerEdgeSwipe = ({ edgeInset = 0, enabled = true, height = 300, i
|
|
|
93
93
|
doesn't stop this View's underlying DOM node from being hit-tested. Without this, a caller
|
|
94
94
|
passing enabled={false} to shed the edge-swipe-to-open behavior (e.g. because their own
|
|
95
95
|
screen never uses it) still ends up with this zone as an invisible, unconditionally opaque
|
|
96
|
-
strip along the edge
|
|
96
|
+
strip along the edge - silently swallowing presses meant for anything else placed there,
|
|
97
97
|
even though the gesture it exists for no longer does anything. 'none' when disabled lets
|
|
98
98
|
that click/tap fall straight through to whatever's actually underneath. */}
|
|
99
99
|
<View style={[...edgeStyle, webCursorStyle, enabled ? styles.interactive : styles.nonInteractive]} />
|
package/src/createDrawer.tsx
CHANGED
|
@@ -9,14 +9,14 @@ import { useDrawerSize } from './useDrawerSize'
|
|
|
9
9
|
|
|
10
10
|
export type DrawerActions = {
|
|
11
11
|
close: () => void
|
|
12
|
-
// 0 anywhere at or before `height`/`width` (closed, or resting at its default open size
|
|
12
|
+
// 0 anywhere at or before `height`/`width` (closed, or resting at its default open size -
|
|
13
13
|
// nothing to indicate, since the panel isn't showing any more than that), 1 once a drag has
|
|
14
14
|
// pulled it all the way open to `maxHeight`/`maxWidth`, smoothly interpolating in between as the
|
|
15
15
|
// handle is dragged or the spring animates. For content that only needs to react once the panel
|
|
16
|
-
// is genuinely at its full extent
|
|
16
|
+
// is genuinely at its full extent - e.g. fading in a top safe-area inset that's wasted space while
|
|
17
17
|
// merely at rest, but genuinely needed once expansion reaches the screen edge/notch. This is the
|
|
18
18
|
// content-padding approach to a safe-area inset; `edgeInset` (see CreateDrawerOptions) is the
|
|
19
|
-
// alternative geometry-clamp approach, which keeps the panel itself short of the inset instead
|
|
19
|
+
// alternative geometry-clamp approach, which keeps the panel itself short of the inset instead -
|
|
20
20
|
// don't combine both for the same edge on the same drawer, or the inset gets applied twice. Always
|
|
21
21
|
// a flat 1 while open (matching "already at full extent") when there's no maxHeight/maxWidth
|
|
22
22
|
// configured.
|
|
@@ -99,7 +99,7 @@ const noop = () => {}
|
|
|
99
99
|
// settings drawer (say) stay fully independent without a stringly-typed id to keep them apart.
|
|
100
100
|
export const 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 }: CreateDrawerOptions = {}): CreateDrawerResult => {
|
|
101
101
|
// Static, non-reactive stand-in for the default context value only (before any real Provider has
|
|
102
|
-
// mounted)
|
|
102
|
+
// mounted) - same reasoning as the plain `noop` functions alongside it.
|
|
103
103
|
const defaultExpandProgress = { value: 1 } as SharedValue<number>
|
|
104
104
|
const DrawerActionsContext = createContext<DrawerActions>({ close: noop, expandProgress: defaultExpandProgress, isOpen: false, isVisible: false, open: noop })
|
|
105
105
|
const vertical = isVerticalSide(side)
|
|
@@ -129,7 +129,7 @@ export const createDrawer = ({ backdropOpacity: factoryBackdropOpacity, blocking
|
|
|
129
129
|
|
|
130
130
|
// Percentages can't resolve outside a component (no window dimensions to hand), so unlike
|
|
131
131
|
// Drawer/DrawerEdgeSwipe (which each resolve their own copy internally), this is the one place
|
|
132
|
-
// createDrawer needs it directly
|
|
132
|
+
// createDrawer needs it directly - both for translateOffset's own initial seed, and restOffset
|
|
133
133
|
// for expandProgress below. contentSize never gets a maxSize (mirrors Drawer's own
|
|
134
134
|
// contentSize-has-no-expand-ceiling rule), so expandProgress is always a flat 1 there too.
|
|
135
135
|
const { closedOffset, restOffset } = useDrawerSize(side, effectiveSize, contentSize ? undefined : maxSize, edgeInset)
|
|
@@ -138,7 +138,7 @@ export const createDrawer = ({ backdropOpacity: factoryBackdropOpacity, blocking
|
|
|
138
138
|
// (kept in sync, tested there directly) for the same formula explained in more depth. Inlined
|
|
139
139
|
// rather than calling that shared helper: react-native-worklets (Reanimated 4+) only forwards a
|
|
140
140
|
// worklet's *relative* imports for files matching an explicit allowlist, which doesn't include
|
|
141
|
-
// published node_modules packages by default
|
|
141
|
+
// published node_modules packages by default - importing and calling it here would fail at
|
|
142
142
|
// runtime as an unrecognized cross-module "remote function" call from the UI thread. currentD/
|
|
143
143
|
// restD are in the same closeDirection-normalized "D-space" Drawer.tsx's own gesture math uses,
|
|
144
144
|
// so this stays agnostic of `side`'s sign convention.
|
package/src/geometry.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type DrawerSide = 'left' | 'right' | 'top' | 'bottom'
|
|
2
2
|
|
|
3
3
|
// A plain pixel value, or a percentage of the relevant screen dimension (window height for
|
|
4
|
-
// top/bottom, window width for left/right)
|
|
4
|
+
// top/bottom, window width for left/right) - see resolveDimension.
|
|
5
5
|
export type DrawerDimension = number | `${number}%`
|
|
6
6
|
|
|
7
7
|
export function isVerticalSide(side: DrawerSide): boolean {
|
|
@@ -28,24 +28,34 @@ export function resolveDimension(value: DrawerDimension, windowSize: number): nu
|
|
|
28
28
|
return typeof value === 'number' ? value : (Number.parseFloat(value) / 100) * windowSize
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
// Caps a resolved size at the window's own size along the drawer's axis. A panel anchored flush to
|
|
32
|
+
// one edge can never show anything past the opposite one, so a fixed `width: 380` on a 360px-wide
|
|
33
|
+
// phone would otherwise just cut off its own far edge (and everything laid out against it) rather
|
|
34
|
+
// than fill the screen. A windowSize of 0 means the window hasn't been measured yet (web, briefly,
|
|
35
|
+
// before hydration), not a 0px screen: the size passes through untouched then, or a px-sized
|
|
36
|
+
// drawer would seed its closed offset at 0 and visibly slide shut once the real size arrives.
|
|
37
|
+
export function clampToWindow(size: number, windowSize: number): number {
|
|
38
|
+
return windowSize > 0 ? Math.min(size, windowSize) : size
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
// How far past `height`/`width` (its rest size) an expandable sheet currently is, as a 0-1
|
|
32
|
-
// fraction: 0 anywhere at or before rest (closed, or resting
|
|
42
|
+
// fraction: 0 anywhere at or before rest (closed, or resting - nothing to indicate, since the
|
|
33
43
|
// panel isn't showing any more than its configured rest size), 1 once fully expanded to
|
|
34
44
|
// `maxHeight`/`maxWidth`, ramping smoothly in between. For content that only needs to react once
|
|
35
|
-
// the panel is actually at its full extent
|
|
45
|
+
// the panel is actually at its full extent - e.g. a top safe-area inset that's wasted space while
|
|
36
46
|
// merely at rest, but genuinely needed once expansion reaches the screen edge/notch.
|
|
37
47
|
// currentD/restD are both expected in the same closeDirection-normalized "D-space" the gesture
|
|
38
48
|
// math elsewhere uses (0 = fully expanded, larger = closer to closed), so this stays agnostic of
|
|
39
49
|
// `side`'s sign convention. restD <= 0 (no maxHeight/maxWidth configured, or a degenerate config)
|
|
40
50
|
// means there's no expand range to speak of: rest already IS the full extent, so this is always 1.
|
|
41
51
|
// NOT actually called from createDrawer.tsx's useDerivedValue at runtime, despite computing the
|
|
42
|
-
// exact same thing
|
|
52
|
+
// exact same thing - deliberately duplicated inline there instead (see its own comment) rather
|
|
43
53
|
// than imported, since react-native-worklets (Reanimated 4+) only forwards a worklet's *relative*
|
|
44
54
|
// imports for files matching an explicit allowlist, which a published node_modules package like
|
|
45
55
|
// this one isn't part of by default; an imported (not inlined) function called from inside a
|
|
46
56
|
// worklet then fails at runtime as an unrecognized "remote function" call. This copy exists so the
|
|
47
57
|
// interesting interpolation math (which createDrawer's own open/close-only test API can't reach)
|
|
48
|
-
// still has a direct, isolated test
|
|
58
|
+
// still has a direct, isolated test - keep both in sync if this formula ever changes.
|
|
49
59
|
export function getExpandProgress(currentD: number, restD: number): number {
|
|
50
60
|
if (restD <= 0) return 1
|
|
51
61
|
return Math.min(1, Math.max(0, (restD - currentD) / restD))
|
package/src/index.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { Drawer, type DrawerProps } from './Drawer'
|
|
|
3
3
|
export { type AutoPaperModule, configureDrawer, type DrawerConfig, DrawerProvider, type DrawerProviderProps, getDrawerConfig } from './DrawerConfig'
|
|
4
4
|
export { DrawerEdgeSwipe, type DrawerEdgeSwipeProps } from './DrawerEdgeSwipe'
|
|
5
5
|
export { type DrawerDimension, type DrawerSide } from './geometry'
|
|
6
|
+
export { type DrawerSize, useDrawerSize } from './useDrawerSize'
|
package/src/useDrawerSize.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useWindowDimensions } from 'react-native'
|
|
2
2
|
|
|
3
|
-
import { type DrawerDimension, type DrawerSide, getClosedOffset, isVerticalSide, resolveDimension } from './geometry'
|
|
3
|
+
import { clampToWindow, type DrawerDimension, type DrawerSide, getClosedOffset, isVerticalSide, resolveDimension } from './geometry'
|
|
4
4
|
|
|
5
5
|
export type DrawerSize = {
|
|
6
6
|
// The panel's rest size, in px: what it opens to by default, and what a drag-to-close gesture
|
|
@@ -23,30 +23,41 @@ export type DrawerSize = {
|
|
|
23
23
|
// current window size into concrete offsets, shared by Drawer, DrawerEdgeSwipe, and createDrawer
|
|
24
24
|
// so all three always agree on where "closed"/"rest"/"expanded" actually are.
|
|
25
25
|
//
|
|
26
|
+
// Also exported, for apps driving Drawer/DrawerEdgeSwipe standalone: pass the same side/size/
|
|
27
|
+
// maxSize/edgeInset the Drawer gets, then seed its translateOffset shared value with closedOffset
|
|
28
|
+
// (not a hand-negated size, which disagrees with the panel wherever the clamp below kicks in), and
|
|
29
|
+
// lay out anything sized to the panel itself (e.g. a full-width carousel page) by effectiveSize.
|
|
30
|
+
//
|
|
31
|
+
// Both sizes are capped at the window's own size along the drawer's axis (see clampToWindow): a
|
|
32
|
+
// fixed `width: 380` fills a 360px-wide phone exactly instead of running off its far edge, and
|
|
33
|
+
// every offset below follows from the capped size, so the closed panel still sits flush against
|
|
34
|
+
// its edge. Capped against the raw window, not the edgeInset-shrunk percentBasis below, so a plain
|
|
35
|
+
// px size stays clear of edgeInset exactly as described next.
|
|
36
|
+
//
|
|
26
37
|
// edgeInset (px, default 0) shrinks the basis a *percentage* height/width/maxHeight/maxWidth
|
|
27
|
-
// resolves against
|
|
38
|
+
// resolves against - windowSize - edgeInset instead of the raw windowSize - so `'100%'` fills
|
|
28
39
|
// exactly up to that boundary (e.g. `edgeInset={insets.top}` on a `bottom` sheet's `maxHeight:
|
|
29
40
|
// '100%'` stops it just short of the status bar/notch instead of sliding behind it) rather than
|
|
30
41
|
// the literal screen edge. This is the geometry-clamp alternative to padding `content` away from a
|
|
31
42
|
// safe-area inset (see the README's Safe area section): the panel itself never reaches the inset,
|
|
32
|
-
// so there's nothing inside `content` to compensate for
|
|
43
|
+
// so there's nothing inside `content` to compensate for - don't combine the two for the same edge,
|
|
33
44
|
// or the inset ends up applied twice.
|
|
34
45
|
//
|
|
35
46
|
// A plain pixel number is deliberately left untouched by edgeInset (resolveDimension ignores its
|
|
36
47
|
// windowSize argument entirely for numbers, so passing the shrunk basis to it is a no-op there):
|
|
37
48
|
// a percentage means "fill the available space," which should already account for a known inset,
|
|
38
49
|
// while a literal px value means "this exact size," typically one the caller already computed
|
|
39
|
-
// deliberately (including, if they want, against an inset themselves)
|
|
50
|
+
// deliberately (including, if they want, against an inset themselves) - edgeInset silently
|
|
40
51
|
// overriding that would take away the one way to opt back out of it for a specific drawer.
|
|
41
52
|
export function useDrawerSize(side: DrawerSide, size: DrawerDimension, maxSize?: DrawerDimension, edgeInset = 0): DrawerSize {
|
|
42
53
|
const windowDimensions = useWindowDimensions()
|
|
43
54
|
const windowSize = isVerticalSide(side) ? windowDimensions.height : windowDimensions.width
|
|
44
55
|
const percentBasis = windowSize - edgeInset
|
|
45
56
|
|
|
46
|
-
const effectiveSize = resolveDimension(size, percentBasis)
|
|
57
|
+
const effectiveSize = clampToWindow(resolveDimension(size, percentBasis), windowSize)
|
|
47
58
|
// Guards a misconfigured maxSize smaller than size, which would otherwise produce a negative
|
|
48
59
|
// "hidden" range and invert the rest/closed math below.
|
|
49
|
-
const maxEffectiveSize = maxSize === undefined ? effectiveSize : Math.max(resolveDimension(maxSize, percentBasis), effectiveSize)
|
|
60
|
+
const maxEffectiveSize = maxSize === undefined ? effectiveSize : Math.max(clampToWindow(resolveDimension(maxSize, percentBasis), windowSize), effectiveSize)
|
|
50
61
|
|
|
51
62
|
const closedOffset = getClosedOffset(side, maxEffectiveSize)
|
|
52
63
|
const restOffset = getClosedOffset(side, maxEffectiveSize - effectiveSize)
|