@seed-design/react-drawer 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Drawer-12s-COJG4T4l.js +1421 -0
- package/lib/Drawer-12s-a4zbhVrK.cjs +1454 -0
- package/lib/index.cjs +27 -0
- package/lib/index.d.ts +212 -0
- package/lib/index.js +17 -0
- package/package.json +1 -1
- package/src/Drawer.tsx +4 -5
- package/src/index.ts +1 -1
- package/src/useDrawer.ts +15 -8
|
@@ -0,0 +1,1454 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
3
|
+
var reactComposeRefs = require('@radix-ui/react-compose-refs');
|
|
4
|
+
var DialogPrimitive = require('@radix-ui/react-dialog');
|
|
5
|
+
var reactUseCallbackRef = require('@radix-ui/react-use-callback-ref');
|
|
6
|
+
var domUtils = require('@seed-design/dom-utils');
|
|
7
|
+
var reactPrimitive = require('@seed-design/react-primitive');
|
|
8
|
+
var React = require('react');
|
|
9
|
+
var reactUseControllableState = require('@radix-ui/react-use-controllable-state');
|
|
10
|
+
|
|
11
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
12
|
+
|
|
13
|
+
function _interopNamespace(e) {
|
|
14
|
+
if (e && e.__esModule) return e;
|
|
15
|
+
var n = Object.create(null);
|
|
16
|
+
if (e) {
|
|
17
|
+
Object.keys(e).forEach(function (k) {
|
|
18
|
+
if (k !== 'default') {
|
|
19
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return e[k]; }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
n.default = e;
|
|
28
|
+
return n;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var DialogPrimitive__namespace = /*#__PURE__*/_interopNamespace(DialogPrimitive);
|
|
32
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
33
|
+
|
|
34
|
+
function isMobileFirefox() {
|
|
35
|
+
if (typeof window === "undefined" || typeof navigator === "undefined") return false;
|
|
36
|
+
return /Firefox/.test(navigator.userAgent) && /Mobile/.test(navigator.userAgent) || /FxiOS/.test(navigator.userAgent);
|
|
37
|
+
}
|
|
38
|
+
function isMac() {
|
|
39
|
+
return testPlatform(/^Mac/);
|
|
40
|
+
}
|
|
41
|
+
function isIPhone() {
|
|
42
|
+
return testPlatform(/^iPhone/);
|
|
43
|
+
}
|
|
44
|
+
function isSafari() {
|
|
45
|
+
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
46
|
+
}
|
|
47
|
+
function isIPad() {
|
|
48
|
+
return testPlatform(/^iPad/) || // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
|
|
49
|
+
isMac() && navigator.maxTouchPoints > 1;
|
|
50
|
+
}
|
|
51
|
+
function isIOS() {
|
|
52
|
+
return isIPhone() || isIPad();
|
|
53
|
+
}
|
|
54
|
+
function testPlatform(re) {
|
|
55
|
+
return typeof window !== "undefined" && window.navigator != null ? re.test(window.navigator.platform) : undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* TODO: move to recipe
|
|
60
|
+
*/ const TRANSITIONS = {
|
|
61
|
+
ENTER_DURATION: 0.3,
|
|
62
|
+
EXIT_DURATION: 0.2,
|
|
63
|
+
OVERLAY_ENTER_TIMING_FUNCTION: "cubic-bezier(0, 0, 0.15, 1)",
|
|
64
|
+
OVERLAY_EXIT_TIMING_FUNCTION: "cubic-bezier(0.35, 0, 1, 1)",
|
|
65
|
+
CONTENT_ENTER_TIMING_FUNCTION: "cubic-bezier(0.03, 0.4, 0.1, 1)",
|
|
66
|
+
CONTENT_EXIT_TIMING_FUNCTION: "cubic-bezier(0.35, 0, 1, 1)"
|
|
67
|
+
};
|
|
68
|
+
const VELOCITY_THRESHOLD = 0.4;
|
|
69
|
+
const CLOSE_THRESHOLD = 0.25;
|
|
70
|
+
const SCROLL_LOCK_TIMEOUT = 100;
|
|
71
|
+
const WINDOW_TOP_OFFSET = 26;
|
|
72
|
+
const DRAG_CLASS = "seed-dragging";
|
|
73
|
+
|
|
74
|
+
const cache = new WeakMap();
|
|
75
|
+
function set(el, styles, ignoreCache = false) {
|
|
76
|
+
if (!el || !(el instanceof HTMLElement)) return;
|
|
77
|
+
let originalStyles = {};
|
|
78
|
+
Object.entries(styles).forEach(([key, value])=>{
|
|
79
|
+
if (key.startsWith("--")) {
|
|
80
|
+
el.style.setProperty(key, value);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
originalStyles[key] = el.style[key];
|
|
84
|
+
el.style[key] = value;
|
|
85
|
+
});
|
|
86
|
+
if (ignoreCache) return;
|
|
87
|
+
cache.set(el, originalStyles);
|
|
88
|
+
}
|
|
89
|
+
function reset(el, prop) {
|
|
90
|
+
if (!el || !(el instanceof HTMLElement)) return;
|
|
91
|
+
let originalStyles = cache.get(el);
|
|
92
|
+
if (!originalStyles) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
{
|
|
96
|
+
el.style[prop] = originalStyles[prop];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const isVertical = (direction)=>{
|
|
100
|
+
switch(direction){
|
|
101
|
+
case "top":
|
|
102
|
+
case "bottom":
|
|
103
|
+
return true;
|
|
104
|
+
case "left":
|
|
105
|
+
case "right":
|
|
106
|
+
return false;
|
|
107
|
+
default:
|
|
108
|
+
return direction;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function getTranslate(element, direction) {
|
|
112
|
+
if (!element) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
const style = window.getComputedStyle(element);
|
|
116
|
+
const transform = // @ts-ignore
|
|
117
|
+
style.transform || style.webkitTransform || style.mozTransform;
|
|
118
|
+
let mat = transform.match(/^matrix3d\((.+)\)$/);
|
|
119
|
+
if (mat) {
|
|
120
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix3d
|
|
121
|
+
return Number.parseFloat(mat[1].split(", ")[isVertical(direction) ? 13 : 12]);
|
|
122
|
+
}
|
|
123
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix
|
|
124
|
+
mat = transform.match(/^matrix\((.+)\)$/);
|
|
125
|
+
return mat ? Number.parseFloat(mat[1].split(", ")[isVertical(direction) ? 5 : 4]) : null;
|
|
126
|
+
}
|
|
127
|
+
function dampenValue(v) {
|
|
128
|
+
return 8 * (Math.log(v + 1) - 2);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// This code comes from https://github.com/emilkowalski/vaul/blob/main/src/use-position-fixed.ts
|
|
132
|
+
let previousBodyPosition = null;
|
|
133
|
+
/**
|
|
134
|
+
* This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android).
|
|
135
|
+
* I won't get into too much detail about what bugs it solves, but so far I've found that setting the body to `position: fixed` is the most reliable way to prevent those bugs.
|
|
136
|
+
* Issues that this hook solves:
|
|
137
|
+
* https://github.com/emilkowalski/vaul/issues/435
|
|
138
|
+
* https://github.com/emilkowalski/vaul/issues/433
|
|
139
|
+
* And more that I discovered, but were just not reported.
|
|
140
|
+
*/ function usePositionFixed({ isOpen, modal, nested, hasBeenOpened, preventScrollRestoration, noBodyStyles }) {
|
|
141
|
+
const [activeUrl, setActiveUrl] = React__default.default.useState(()=>typeof window !== "undefined" ? window.location.href : "");
|
|
142
|
+
const scrollPos = React__default.default.useRef(0);
|
|
143
|
+
const setPositionFixed = React__default.default.useCallback(()=>{
|
|
144
|
+
// All browsers on iOS will return true here.
|
|
145
|
+
if (!isSafari()) return;
|
|
146
|
+
// If previousBodyPosition is already set, don't set it again.
|
|
147
|
+
if (previousBodyPosition === null && isOpen && !noBodyStyles) {
|
|
148
|
+
previousBodyPosition = {
|
|
149
|
+
position: document.body.style.position,
|
|
150
|
+
top: document.body.style.top,
|
|
151
|
+
left: document.body.style.left,
|
|
152
|
+
height: document.body.style.height,
|
|
153
|
+
right: "unset"
|
|
154
|
+
};
|
|
155
|
+
// Update the dom inside an animation frame
|
|
156
|
+
const { scrollX, innerHeight } = window;
|
|
157
|
+
document.body.style.setProperty("position", "fixed", "important");
|
|
158
|
+
Object.assign(document.body.style, {
|
|
159
|
+
top: `${-scrollPos.current}px`,
|
|
160
|
+
left: `${-scrollX}px`,
|
|
161
|
+
right: "0px",
|
|
162
|
+
height: "auto"
|
|
163
|
+
});
|
|
164
|
+
window.setTimeout(()=>window.requestAnimationFrame(()=>{
|
|
165
|
+
// Attempt to check if the bottom bar appeared due to the position change
|
|
166
|
+
const bottomBarHeight = innerHeight - window.innerHeight;
|
|
167
|
+
if (bottomBarHeight && scrollPos.current >= innerHeight) {
|
|
168
|
+
// Move the content further up so that the bottom bar doesn't hide it
|
|
169
|
+
document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`;
|
|
170
|
+
}
|
|
171
|
+
}), 300);
|
|
172
|
+
}
|
|
173
|
+
}, [
|
|
174
|
+
isOpen
|
|
175
|
+
]);
|
|
176
|
+
const restorePositionSetting = React__default.default.useCallback(()=>{
|
|
177
|
+
// All browsers on iOS will return true here.
|
|
178
|
+
if (!isSafari()) return;
|
|
179
|
+
if (previousBodyPosition !== null && !noBodyStyles) {
|
|
180
|
+
// Convert the position from "px" to Int
|
|
181
|
+
const y = -parseInt(document.body.style.top, 10);
|
|
182
|
+
const x = -parseInt(document.body.style.left, 10);
|
|
183
|
+
// Restore styles
|
|
184
|
+
Object.assign(document.body.style, previousBodyPosition);
|
|
185
|
+
window.requestAnimationFrame(()=>{
|
|
186
|
+
if (preventScrollRestoration && activeUrl !== window.location.href) {
|
|
187
|
+
setActiveUrl(window.location.href);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
window.scrollTo(x, y);
|
|
191
|
+
});
|
|
192
|
+
previousBodyPosition = null;
|
|
193
|
+
}
|
|
194
|
+
}, [
|
|
195
|
+
activeUrl
|
|
196
|
+
]);
|
|
197
|
+
React__default.default.useEffect(()=>{
|
|
198
|
+
function onScroll() {
|
|
199
|
+
scrollPos.current = window.scrollY;
|
|
200
|
+
}
|
|
201
|
+
onScroll();
|
|
202
|
+
window.addEventListener("scroll", onScroll);
|
|
203
|
+
return ()=>{
|
|
204
|
+
window.removeEventListener("scroll", onScroll);
|
|
205
|
+
};
|
|
206
|
+
}, []);
|
|
207
|
+
React__default.default.useEffect(()=>{
|
|
208
|
+
if (!modal) return;
|
|
209
|
+
return ()=>{
|
|
210
|
+
if (typeof document === "undefined") return;
|
|
211
|
+
// Another drawer is opened, safe to ignore the execution
|
|
212
|
+
const hasDrawerOpened = !!document.querySelector("[data-drawer]");
|
|
213
|
+
if (hasDrawerOpened) return;
|
|
214
|
+
restorePositionSetting();
|
|
215
|
+
};
|
|
216
|
+
}, [
|
|
217
|
+
modal,
|
|
218
|
+
restorePositionSetting
|
|
219
|
+
]);
|
|
220
|
+
React__default.default.useEffect(()=>{
|
|
221
|
+
if (nested || !hasBeenOpened) return;
|
|
222
|
+
// This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening
|
|
223
|
+
if (isOpen) {
|
|
224
|
+
// avoid for standalone mode (PWA)
|
|
225
|
+
const isStandalone = window.matchMedia("(display-mode: standalone)").matches;
|
|
226
|
+
!isStandalone && setPositionFixed();
|
|
227
|
+
if (!modal) {
|
|
228
|
+
window.setTimeout(()=>{
|
|
229
|
+
restorePositionSetting();
|
|
230
|
+
}, 500);
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
restorePositionSetting();
|
|
234
|
+
}
|
|
235
|
+
}, [
|
|
236
|
+
isOpen,
|
|
237
|
+
hasBeenOpened,
|
|
238
|
+
activeUrl,
|
|
239
|
+
modal,
|
|
240
|
+
nested,
|
|
241
|
+
setPositionFixed,
|
|
242
|
+
restorePositionSetting
|
|
243
|
+
]);
|
|
244
|
+
return {
|
|
245
|
+
restorePositionSetting
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// This code comes from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts
|
|
250
|
+
const KEYBOARD_BUFFER = 24;
|
|
251
|
+
const useIsomorphicLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
|
|
252
|
+
function chain(...callbacks) {
|
|
253
|
+
return (...args)=>{
|
|
254
|
+
for (let callback of callbacks){
|
|
255
|
+
if (typeof callback === "function") {
|
|
256
|
+
callback(...args);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
const visualViewport = typeof document !== "undefined" && window.visualViewport;
|
|
263
|
+
function isScrollable(node) {
|
|
264
|
+
let style = window.getComputedStyle(node);
|
|
265
|
+
return /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY);
|
|
266
|
+
}
|
|
267
|
+
function getScrollParent(node) {
|
|
268
|
+
if (isScrollable(node)) {
|
|
269
|
+
node = node.parentElement;
|
|
270
|
+
}
|
|
271
|
+
while(node && !isScrollable(node)){
|
|
272
|
+
node = node.parentElement;
|
|
273
|
+
}
|
|
274
|
+
return node || document.scrollingElement || document.documentElement;
|
|
275
|
+
}
|
|
276
|
+
// HTML input types that do not cause the software keyboard to appear.
|
|
277
|
+
const nonTextInputTypes = new Set([
|
|
278
|
+
"checkbox",
|
|
279
|
+
"radio",
|
|
280
|
+
"range",
|
|
281
|
+
"color",
|
|
282
|
+
"file",
|
|
283
|
+
"image",
|
|
284
|
+
"button",
|
|
285
|
+
"submit",
|
|
286
|
+
"reset"
|
|
287
|
+
]);
|
|
288
|
+
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
289
|
+
let preventScrollCount = 0;
|
|
290
|
+
let restore;
|
|
291
|
+
/**
|
|
292
|
+
* Prevents scrolling on the document body on mount, and
|
|
293
|
+
* restores it on unmount. Also ensures that content does not
|
|
294
|
+
* shift due to the scrollbars disappearing.
|
|
295
|
+
*/ function usePreventScroll(options = {}) {
|
|
296
|
+
let { isDisabled } = options;
|
|
297
|
+
useIsomorphicLayoutEffect(()=>{
|
|
298
|
+
if (isDisabled) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
preventScrollCount++;
|
|
302
|
+
if (preventScrollCount === 1) {
|
|
303
|
+
if (isIOS()) {
|
|
304
|
+
restore = preventScrollMobileSafari();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return ()=>{
|
|
308
|
+
preventScrollCount--;
|
|
309
|
+
if (preventScrollCount === 0) {
|
|
310
|
+
restore?.();
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
}, [
|
|
314
|
+
isDisabled
|
|
315
|
+
]);
|
|
316
|
+
}
|
|
317
|
+
// Mobile Safari is a whole different beast. Even with overflow: hidden,
|
|
318
|
+
// it still scrolls the page in many situations:
|
|
319
|
+
//
|
|
320
|
+
// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
|
|
321
|
+
// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
|
|
322
|
+
// it, so it becomes scrollable.
|
|
323
|
+
// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
|
|
324
|
+
// This may cause even fixed position elements to scroll off the screen.
|
|
325
|
+
// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
|
|
326
|
+
// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
|
|
327
|
+
//
|
|
328
|
+
// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
|
|
329
|
+
//
|
|
330
|
+
// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
|
|
331
|
+
// on the window.
|
|
332
|
+
// 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the
|
|
333
|
+
// top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling.
|
|
334
|
+
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
335
|
+
// 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top
|
|
336
|
+
// of the page, which prevents it from scrolling the page. After the input is focused, scroll the element
|
|
337
|
+
// into view ourselves, without scrolling the whole page.
|
|
338
|
+
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
339
|
+
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
340
|
+
// above work or Safari will still try to scroll the page when focusing an input.
|
|
341
|
+
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
342
|
+
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
343
|
+
function preventScrollMobileSafari() {
|
|
344
|
+
let scrollable;
|
|
345
|
+
let lastY = 0;
|
|
346
|
+
let onTouchStart = (e)=>{
|
|
347
|
+
// Store the nearest scrollable parent element from the element that the user touched.
|
|
348
|
+
scrollable = getScrollParent(e.target);
|
|
349
|
+
if (scrollable === document.documentElement && scrollable === document.body) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
lastY = e.changedTouches[0].pageY;
|
|
353
|
+
};
|
|
354
|
+
let onTouchMove = (e)=>{
|
|
355
|
+
// Prevent scrolling the window.
|
|
356
|
+
if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
|
|
357
|
+
e.preventDefault();
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
361
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
362
|
+
// the window instead. Unfortunately, this disables bounce scrolling when at
|
|
363
|
+
// the top but it's the best we can do.
|
|
364
|
+
let y = e.changedTouches[0].pageY;
|
|
365
|
+
let scrollTop = scrollable.scrollTop;
|
|
366
|
+
let bottom = scrollable.scrollHeight - scrollable.clientHeight;
|
|
367
|
+
if (bottom === 0) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (scrollTop <= 0 && y > lastY || scrollTop >= bottom && y < lastY) {
|
|
371
|
+
e.preventDefault();
|
|
372
|
+
}
|
|
373
|
+
lastY = y;
|
|
374
|
+
};
|
|
375
|
+
let onTouchEnd = (e)=>{
|
|
376
|
+
let target = e.target;
|
|
377
|
+
// Apply this change if we're not already focused on the target element
|
|
378
|
+
if (isInput(target) && target !== document.activeElement) {
|
|
379
|
+
e.preventDefault();
|
|
380
|
+
// Apply a transform to trick Safari into thinking the input is at the top of the page
|
|
381
|
+
// so it doesn't try to scroll it into view. When tapping on an input, this needs to
|
|
382
|
+
// be done before the "focus" event, so we have to focus the element ourselves.
|
|
383
|
+
target.style.transform = "translateY(-2000px)";
|
|
384
|
+
target.focus();
|
|
385
|
+
requestAnimationFrame(()=>{
|
|
386
|
+
target.style.transform = "";
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
let onFocus = (e)=>{
|
|
391
|
+
let target = e.target;
|
|
392
|
+
if (isInput(target)) {
|
|
393
|
+
// Transform also needs to be applied in the focus event in cases where focus moves
|
|
394
|
+
// other than tapping on an input directly, e.g. the next/previous buttons in the
|
|
395
|
+
// software keyboard. In these cases, it seems applying the transform in the focus event
|
|
396
|
+
// is good enough, whereas when tapping an input, it must be done before the focus event. 🤷♂️
|
|
397
|
+
target.style.transform = "translateY(-2000px)";
|
|
398
|
+
requestAnimationFrame(()=>{
|
|
399
|
+
target.style.transform = "";
|
|
400
|
+
// This will have prevented the browser from scrolling the focused element into view,
|
|
401
|
+
// so we need to do this ourselves in a way that doesn't cause the whole page to scroll.
|
|
402
|
+
if (visualViewport) {
|
|
403
|
+
if (visualViewport.height < window.innerHeight) {
|
|
404
|
+
// If the keyboard is already visible, do this after one additional frame
|
|
405
|
+
// to wait for the transform to be removed.
|
|
406
|
+
requestAnimationFrame(()=>{
|
|
407
|
+
scrollIntoView(target);
|
|
408
|
+
});
|
|
409
|
+
} else {
|
|
410
|
+
// Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
411
|
+
// measure the correct position to scroll to.
|
|
412
|
+
visualViewport.addEventListener("resize", ()=>scrollIntoView(target), {
|
|
413
|
+
once: true
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
let onWindowScroll = ()=>{
|
|
421
|
+
// Last resort. If the window scrolled, scroll it back to the top.
|
|
422
|
+
// It should always be at the top because the body will have a negative margin (see below).
|
|
423
|
+
window.scrollTo(0, 0);
|
|
424
|
+
};
|
|
425
|
+
// Record the original scroll position so we can restore it.
|
|
426
|
+
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
427
|
+
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
428
|
+
let scrollX = window.pageXOffset;
|
|
429
|
+
let scrollY = window.pageYOffset;
|
|
430
|
+
let restoreStyles = chain(setStyle(document.documentElement, "paddingRight", `${window.innerWidth - document.documentElement.clientWidth}px`));
|
|
431
|
+
// Scroll to the top. The negative margin on the body will make this appear the same.
|
|
432
|
+
window.scrollTo(0, 0);
|
|
433
|
+
let removeEvents = chain(addEvent(document, "touchstart", onTouchStart, {
|
|
434
|
+
passive: false,
|
|
435
|
+
capture: true
|
|
436
|
+
}), addEvent(document, "touchmove", onTouchMove, {
|
|
437
|
+
passive: false,
|
|
438
|
+
capture: true
|
|
439
|
+
}), addEvent(document, "touchend", onTouchEnd, {
|
|
440
|
+
passive: false,
|
|
441
|
+
capture: true
|
|
442
|
+
}), addEvent(document, "focus", onFocus, true), addEvent(window, "scroll", onWindowScroll));
|
|
443
|
+
return ()=>{
|
|
444
|
+
// Restore styles and scroll the page back to where it was.
|
|
445
|
+
restoreStyles();
|
|
446
|
+
removeEvents();
|
|
447
|
+
window.scrollTo(scrollX, scrollY);
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
// Sets a CSS property on an element, and returns a function to revert it to the previous value.
|
|
451
|
+
function setStyle(element, style, value) {
|
|
452
|
+
// https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310
|
|
453
|
+
// @ts-ignore
|
|
454
|
+
let cur = element.style[style];
|
|
455
|
+
// @ts-ignore
|
|
456
|
+
element.style[style] = value;
|
|
457
|
+
return ()=>{
|
|
458
|
+
// @ts-ignore
|
|
459
|
+
element.style[style] = cur;
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
// Adds an event listener to an element, and returns a function to remove it.
|
|
463
|
+
function addEvent(target, event, handler, options) {
|
|
464
|
+
// @ts-ignore
|
|
465
|
+
target.addEventListener(event, handler, options);
|
|
466
|
+
return ()=>{
|
|
467
|
+
// @ts-ignore
|
|
468
|
+
target.removeEventListener(event, handler, options);
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
function scrollIntoView(target) {
|
|
472
|
+
let root = document.scrollingElement || document.documentElement;
|
|
473
|
+
while(target && target !== root){
|
|
474
|
+
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
475
|
+
let scrollable = getScrollParent(target);
|
|
476
|
+
if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== target) {
|
|
477
|
+
let scrollableTop = scrollable.getBoundingClientRect().top;
|
|
478
|
+
let targetTop = target.getBoundingClientRect().top;
|
|
479
|
+
let targetBottom = target.getBoundingClientRect().bottom;
|
|
480
|
+
// Buffer is needed for some edge cases
|
|
481
|
+
const keyboardHeight = scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER;
|
|
482
|
+
if (targetBottom > keyboardHeight) {
|
|
483
|
+
scrollable.scrollTop += targetTop - scrollableTop;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
// @ts-ignore
|
|
487
|
+
target = scrollable.parentElement;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function isInput(target) {
|
|
491
|
+
return target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function useSnapPoints({ activeSnapPointProp, setActiveSnapPointProp, snapPoints, drawerRef, overlayRef, fadeFromIndex, onSnapPointChange, direction = "bottom", container, snapToSequentialPoint }) {
|
|
495
|
+
const [activeSnapPoint, setActiveSnapPoint] = reactUseControllableState.useControllableState({
|
|
496
|
+
prop: activeSnapPointProp,
|
|
497
|
+
defaultProp: snapPoints?.[0],
|
|
498
|
+
onChange: setActiveSnapPointProp
|
|
499
|
+
});
|
|
500
|
+
const [windowDimensions, setWindowDimensions] = React__default.default.useState(typeof window !== "undefined" ? {
|
|
501
|
+
innerWidth: window.innerWidth,
|
|
502
|
+
innerHeight: window.innerHeight
|
|
503
|
+
} : undefined);
|
|
504
|
+
React__default.default.useEffect(()=>{
|
|
505
|
+
function onResize() {
|
|
506
|
+
setWindowDimensions({
|
|
507
|
+
innerWidth: window.innerWidth,
|
|
508
|
+
innerHeight: window.innerHeight
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
window.addEventListener("resize", onResize);
|
|
512
|
+
return ()=>window.removeEventListener("resize", onResize);
|
|
513
|
+
}, []);
|
|
514
|
+
const isLastSnapPoint = React__default.default.useMemo(()=>activeSnapPoint === snapPoints?.[snapPoints.length - 1] || null, [
|
|
515
|
+
snapPoints,
|
|
516
|
+
activeSnapPoint
|
|
517
|
+
]);
|
|
518
|
+
const activeSnapPointIndex = React__default.default.useMemo(()=>snapPoints?.findIndex((snapPoint)=>snapPoint === activeSnapPoint) ?? null, [
|
|
519
|
+
snapPoints,
|
|
520
|
+
activeSnapPoint
|
|
521
|
+
]);
|
|
522
|
+
const shouldFade = snapPoints && snapPoints.length > 0 && (fadeFromIndex || fadeFromIndex === 0) && !Number.isNaN(fadeFromIndex) && snapPoints[fadeFromIndex] === activeSnapPoint || !snapPoints;
|
|
523
|
+
const snapPointsOffset = React__default.default.useMemo(()=>{
|
|
524
|
+
const containerSize = container ? {
|
|
525
|
+
width: container.getBoundingClientRect().width,
|
|
526
|
+
height: container.getBoundingClientRect().height
|
|
527
|
+
} : typeof window !== "undefined" ? {
|
|
528
|
+
width: window.innerWidth,
|
|
529
|
+
height: window.innerHeight
|
|
530
|
+
} : {
|
|
531
|
+
width: 0,
|
|
532
|
+
height: 0
|
|
533
|
+
};
|
|
534
|
+
return snapPoints?.map((snapPoint)=>{
|
|
535
|
+
const isPx = typeof snapPoint === "string";
|
|
536
|
+
let snapPointAsNumber = 0;
|
|
537
|
+
if (isPx) {
|
|
538
|
+
snapPointAsNumber = Number.parseInt(snapPoint, 10);
|
|
539
|
+
}
|
|
540
|
+
if (isVertical(direction)) {
|
|
541
|
+
const height = isPx ? snapPointAsNumber : windowDimensions ? snapPoint * containerSize.height : 0;
|
|
542
|
+
if (windowDimensions) {
|
|
543
|
+
return direction === "bottom" ? containerSize.height - height : -containerSize.height + height;
|
|
544
|
+
}
|
|
545
|
+
return height;
|
|
546
|
+
}
|
|
547
|
+
const width = isPx ? snapPointAsNumber : windowDimensions ? snapPoint * containerSize.width : 0;
|
|
548
|
+
if (windowDimensions) {
|
|
549
|
+
return direction === "right" ? containerSize.width - width : -containerSize.width + width;
|
|
550
|
+
}
|
|
551
|
+
return width;
|
|
552
|
+
}) ?? [];
|
|
553
|
+
}, [
|
|
554
|
+
snapPoints,
|
|
555
|
+
windowDimensions,
|
|
556
|
+
container
|
|
557
|
+
]);
|
|
558
|
+
const activeSnapPointOffset = React__default.default.useMemo(()=>activeSnapPointIndex !== null ? snapPointsOffset?.[activeSnapPointIndex] : null, [
|
|
559
|
+
snapPointsOffset,
|
|
560
|
+
activeSnapPointIndex
|
|
561
|
+
]);
|
|
562
|
+
const snapToPoint = React__default.default.useCallback((dimension)=>{
|
|
563
|
+
const newSnapPointIndex = snapPointsOffset?.findIndex((snapPointDim)=>snapPointDim === dimension) ?? null;
|
|
564
|
+
onSnapPointChange(newSnapPointIndex);
|
|
565
|
+
set(drawerRef.current, {
|
|
566
|
+
transition: `transform ${TRANSITIONS.ENTER_DURATION}s ${TRANSITIONS.CONTENT_ENTER_TIMING_FUNCTION}`,
|
|
567
|
+
transform: isVertical(direction) ? `translate3d(0, ${dimension}px, 0)` : `translate3d(${dimension}px, 0, 0)`
|
|
568
|
+
});
|
|
569
|
+
if (snapPointsOffset && newSnapPointIndex !== snapPointsOffset.length - 1 && fadeFromIndex !== undefined && newSnapPointIndex !== fadeFromIndex && newSnapPointIndex < fadeFromIndex) {
|
|
570
|
+
if (fadeFromIndex !== 0) {
|
|
571
|
+
set(overlayRef.current, {
|
|
572
|
+
transition: `opacity ${TRANSITIONS.EXIT_DURATION}s ${TRANSITIONS.OVERLAY_EXIT_TIMING_FUNCTION}`,
|
|
573
|
+
opacity: "0"
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
} else {
|
|
577
|
+
set(overlayRef.current, {
|
|
578
|
+
transition: `opacity ${TRANSITIONS.ENTER_DURATION}s ${TRANSITIONS.OVERLAY_ENTER_TIMING_FUNCTION}`,
|
|
579
|
+
opacity: "1"
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
setActiveSnapPoint(snapPoints?.[Math.max(newSnapPointIndex, 0)]);
|
|
583
|
+
}, [
|
|
584
|
+
drawerRef,
|
|
585
|
+
overlayRef,
|
|
586
|
+
snapPoints,
|
|
587
|
+
snapPointsOffset,
|
|
588
|
+
fadeFromIndex,
|
|
589
|
+
direction,
|
|
590
|
+
onSnapPointChange,
|
|
591
|
+
setActiveSnapPoint
|
|
592
|
+
]);
|
|
593
|
+
React__default.default.useEffect(()=>{
|
|
594
|
+
if (activeSnapPoint || activeSnapPointProp) {
|
|
595
|
+
const newIndex = snapPoints?.findIndex((snapPoint)=>snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint) ?? -1;
|
|
596
|
+
if (snapPointsOffset && newIndex !== -1 && typeof snapPointsOffset[newIndex] === "number") {
|
|
597
|
+
snapToPoint(snapPointsOffset[newIndex]);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}, [
|
|
601
|
+
activeSnapPoint,
|
|
602
|
+
activeSnapPointProp,
|
|
603
|
+
snapPoints,
|
|
604
|
+
snapPointsOffset,
|
|
605
|
+
snapToPoint
|
|
606
|
+
]);
|
|
607
|
+
function onRelease({ draggedDistance, closeDrawer, velocity, dismissible }) {
|
|
608
|
+
if (fadeFromIndex === undefined) return;
|
|
609
|
+
const currentPosition = direction === "bottom" || direction === "right" ? (activeSnapPointOffset ?? 0) - draggedDistance : (activeSnapPointOffset ?? 0) + draggedDistance;
|
|
610
|
+
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
|
|
611
|
+
const isFirst = activeSnapPointIndex === 0;
|
|
612
|
+
const hasDraggedUp = draggedDistance > 0;
|
|
613
|
+
if (isOverlaySnapPoint) {
|
|
614
|
+
set(overlayRef.current, {
|
|
615
|
+
transition: `opacity ${TRANSITIONS.EXIT_DURATION}s ${TRANSITIONS.OVERLAY_EXIT_TIMING_FUNCTION}`
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
if (!snapToSequentialPoint && velocity > 2 && !hasDraggedUp) {
|
|
619
|
+
if (dismissible) closeDrawer();
|
|
620
|
+
else snapToPoint(snapPointsOffset[0]); // snap to initial point
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
if (!snapToSequentialPoint && velocity > 2 && hasDraggedUp && snapPointsOffset && snapPoints) {
|
|
624
|
+
snapToPoint(snapPointsOffset[snapPoints.length - 1]);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
// Find the closest snap point to the current position
|
|
628
|
+
const closestSnapPoint = snapPointsOffset?.reduce((prev, curr)=>{
|
|
629
|
+
if (typeof prev !== "number" || typeof curr !== "number") return prev;
|
|
630
|
+
return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;
|
|
631
|
+
});
|
|
632
|
+
const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
|
|
633
|
+
if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedDistance) < dim * 0.4) {
|
|
634
|
+
const dragDirection = hasDraggedUp ? 1 : -1; // 1 = up, -1 = down
|
|
635
|
+
// Don't do anything if we swipe upwards while being on the last snap point
|
|
636
|
+
if (dragDirection > 0 && isLastSnapPoint && snapPoints) {
|
|
637
|
+
snapToPoint(snapPointsOffset[snapPoints.length - 1]);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (isFirst && dragDirection < 0 && dismissible) {
|
|
641
|
+
closeDrawer();
|
|
642
|
+
}
|
|
643
|
+
if (activeSnapPointIndex === null) return;
|
|
644
|
+
snapToPoint(snapPointsOffset[activeSnapPointIndex + dragDirection]);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
snapToPoint(closestSnapPoint);
|
|
648
|
+
}
|
|
649
|
+
function onDrag({ draggedDistance }) {
|
|
650
|
+
if (activeSnapPointOffset === null) return;
|
|
651
|
+
const newValue = direction === "bottom" || direction === "right" ? activeSnapPointOffset - draggedDistance : activeSnapPointOffset + draggedDistance;
|
|
652
|
+
// Don't do anything if we exceed the last(biggest) snap point
|
|
653
|
+
if ((direction === "bottom" || direction === "right") && newValue < snapPointsOffset[snapPointsOffset.length - 1]) {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
if ((direction === "top" || direction === "left") && newValue > snapPointsOffset[snapPointsOffset.length - 1]) {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
set(drawerRef.current, {
|
|
660
|
+
transform: isVertical(direction) ? `translate3d(0, ${newValue}px, 0)` : `translate3d(${newValue}px, 0, 0)`
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
function getPercentageDragged(absDraggedDistance, isDraggingDown) {
|
|
664
|
+
if (!snapPoints || typeof activeSnapPointIndex !== "number" || !snapPointsOffset || fadeFromIndex === undefined) return null;
|
|
665
|
+
// If this is true we are dragging to a snap point that is supposed to have an overlay
|
|
666
|
+
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
|
|
667
|
+
const isOverlaySnapPointOrHigher = activeSnapPointIndex >= fadeFromIndex;
|
|
668
|
+
if (isOverlaySnapPointOrHigher && isDraggingDown) {
|
|
669
|
+
return 0;
|
|
670
|
+
}
|
|
671
|
+
// Don't animate, but still use this one if we are dragging away from the overlaySnapPoint
|
|
672
|
+
if (isOverlaySnapPoint && !isDraggingDown) return 1;
|
|
673
|
+
if (!shouldFade && !isOverlaySnapPoint) return null;
|
|
674
|
+
// Either fadeFrom index or the one before
|
|
675
|
+
const targetSnapPointIndex = isOverlaySnapPoint ? activeSnapPointIndex + 1 : activeSnapPointIndex - 1;
|
|
676
|
+
if (fadeFromIndex === 0 && activeSnapPointIndex === 0) {
|
|
677
|
+
let firstSnapPoint = snapPoints[0];
|
|
678
|
+
if (typeof firstSnapPoint === "string") {
|
|
679
|
+
firstSnapPoint = Number.parseInt(firstSnapPoint, 10);
|
|
680
|
+
}
|
|
681
|
+
const snapPointDistance = firstSnapPoint;
|
|
682
|
+
const percentageDragged = absDraggedDistance / Math.abs(snapPointDistance);
|
|
683
|
+
return percentageDragged;
|
|
684
|
+
}
|
|
685
|
+
// Get the distance from overlaySnapPoint to the one before or vice-versa to calculate the opacity percentage accordingly
|
|
686
|
+
const snapPointDistance = isOverlaySnapPoint ? snapPointsOffset[targetSnapPointIndex] - snapPointsOffset[targetSnapPointIndex - 1] : snapPointsOffset[targetSnapPointIndex + 1] - snapPointsOffset[targetSnapPointIndex];
|
|
687
|
+
const percentageDragged = absDraggedDistance / Math.abs(snapPointDistance);
|
|
688
|
+
if (isOverlaySnapPoint) {
|
|
689
|
+
return 1 - percentageDragged;
|
|
690
|
+
}
|
|
691
|
+
return percentageDragged;
|
|
692
|
+
}
|
|
693
|
+
return {
|
|
694
|
+
isLastSnapPoint,
|
|
695
|
+
activeSnapPoint,
|
|
696
|
+
shouldFade,
|
|
697
|
+
getPercentageDragged,
|
|
698
|
+
setActiveSnapPoint,
|
|
699
|
+
activeSnapPointIndex,
|
|
700
|
+
onRelease,
|
|
701
|
+
onDrag,
|
|
702
|
+
snapPointsOffset
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
function useDrawer(props) {
|
|
707
|
+
const { open: openProp, onOpenChange, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = true, direction = "bottom", defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false, closeOnInteractOutside = true, closeOnEscape = true } = props;
|
|
708
|
+
const [isOpen = false, setIsOpen] = reactUseControllableState.useControllableState({
|
|
709
|
+
defaultProp: defaultOpen,
|
|
710
|
+
prop: openProp,
|
|
711
|
+
onChange: (o)=>{
|
|
712
|
+
onOpenChange?.(o);
|
|
713
|
+
if (!o && !nested) {
|
|
714
|
+
restorePositionSetting();
|
|
715
|
+
}
|
|
716
|
+
setTimeout(()=>{
|
|
717
|
+
onAnimationEnd?.(o);
|
|
718
|
+
}, TRANSITIONS.EXIT_DURATION * 1000);
|
|
719
|
+
if (o && !modal) {
|
|
720
|
+
if (typeof window !== "undefined") {
|
|
721
|
+
window.requestAnimationFrame(()=>{
|
|
722
|
+
document.body.style.pointerEvents = "auto";
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
if (!o) {
|
|
727
|
+
document.body.style.pointerEvents = "auto";
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
});
|
|
731
|
+
const [hasBeenOpened, setHasBeenOpened] = React.useState(false);
|
|
732
|
+
const [isDragging, setIsDragging] = React.useState(false);
|
|
733
|
+
const [justReleased, setJustReleased] = React.useState(false);
|
|
734
|
+
const [shouldOverlayAnimate, setShouldOverlayAnimate] = React.useState(false);
|
|
735
|
+
const overlayRef = React.useRef(null);
|
|
736
|
+
const openTime = React.useRef(null);
|
|
737
|
+
const dragStartTime = React.useRef(null);
|
|
738
|
+
const dragEndTime = React.useRef(null);
|
|
739
|
+
const lastTimeDragPrevented = React.useRef(null);
|
|
740
|
+
const isAllowedToDrag = React.useRef(false);
|
|
741
|
+
const pointerStart = React.useRef(0);
|
|
742
|
+
const keyboardIsOpen = React.useRef(false);
|
|
743
|
+
const shouldAnimate = React.useRef(!defaultOpen);
|
|
744
|
+
const previousDiffFromInitial = React.useRef(0);
|
|
745
|
+
const drawerRef = React.useRef(null);
|
|
746
|
+
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
|
|
747
|
+
const drawerWidthRef = React.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
|
|
748
|
+
const initialDrawerHeight = React.useRef(0);
|
|
749
|
+
const onSnapPointChange = React.useCallback((activeSnapPointIndex)=>{
|
|
750
|
+
if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) {
|
|
751
|
+
openTime.current = new Date();
|
|
752
|
+
}
|
|
753
|
+
}, [
|
|
754
|
+
snapPoints
|
|
755
|
+
]);
|
|
756
|
+
const { activeSnapPoint, activeSnapPointIndex, setActiveSnapPoint, onRelease: onReleaseSnapPoints, snapPointsOffset, onDrag: onDragSnapPoints, shouldFade, getPercentageDragged: getSnapPointsPercentageDragged } = useSnapPoints({
|
|
757
|
+
snapPoints,
|
|
758
|
+
activeSnapPointProp,
|
|
759
|
+
setActiveSnapPointProp,
|
|
760
|
+
drawerRef,
|
|
761
|
+
fadeFromIndex,
|
|
762
|
+
overlayRef,
|
|
763
|
+
onSnapPointChange,
|
|
764
|
+
direction,
|
|
765
|
+
snapToSequentialPoint
|
|
766
|
+
});
|
|
767
|
+
usePreventScroll({
|
|
768
|
+
isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened || !repositionInputs || !disablePreventScroll
|
|
769
|
+
});
|
|
770
|
+
const { restorePositionSetting } = usePositionFixed({
|
|
771
|
+
isOpen,
|
|
772
|
+
modal,
|
|
773
|
+
nested: nested ?? false,
|
|
774
|
+
hasBeenOpened,
|
|
775
|
+
preventScrollRestoration,
|
|
776
|
+
noBodyStyles
|
|
777
|
+
});
|
|
778
|
+
function onPress(event) {
|
|
779
|
+
if (!dismissible && !snapPoints) return;
|
|
780
|
+
if (drawerRef.current && !drawerRef.current.contains(event.target)) return;
|
|
781
|
+
drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0;
|
|
782
|
+
drawerWidthRef.current = drawerRef.current?.getBoundingClientRect().width || 0;
|
|
783
|
+
setIsDragging(true);
|
|
784
|
+
dragStartTime.current = new Date();
|
|
785
|
+
if (isIOS()) {
|
|
786
|
+
window.addEventListener("touchend", ()=>{
|
|
787
|
+
isAllowedToDrag.current = false;
|
|
788
|
+
}, {
|
|
789
|
+
once: true
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
event.target.setPointerCapture(event.pointerId);
|
|
793
|
+
pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
|
|
794
|
+
}
|
|
795
|
+
function shouldDrag(el, isDraggingInDirection) {
|
|
796
|
+
let element = el;
|
|
797
|
+
const highlightedText = window.getSelection()?.toString();
|
|
798
|
+
const swipeAmount = drawerRef.current ? getTranslate(drawerRef.current, direction) : null;
|
|
799
|
+
const date = new Date();
|
|
800
|
+
if (element.tagName === "SELECT") {
|
|
801
|
+
return false;
|
|
802
|
+
}
|
|
803
|
+
if (element.hasAttribute("data-no-drag") || element.closest("[data-no-drag]")) {
|
|
804
|
+
return false;
|
|
805
|
+
}
|
|
806
|
+
if (direction === "right" || direction === "left") {
|
|
807
|
+
return true;
|
|
808
|
+
}
|
|
809
|
+
if (openTime.current && date.getTime() - openTime.current.getTime() < 500) {
|
|
810
|
+
return false;
|
|
811
|
+
}
|
|
812
|
+
if (swipeAmount !== null) {
|
|
813
|
+
if (direction === "bottom" ? swipeAmount > 0 : swipeAmount < 0) {
|
|
814
|
+
return true;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
if (highlightedText && highlightedText.length > 0) {
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
if (lastTimeDragPrevented.current && date.getTime() - lastTimeDragPrevented.current.getTime() < scrollLockTimeout && swipeAmount === 0) {
|
|
821
|
+
lastTimeDragPrevented.current = date;
|
|
822
|
+
return false;
|
|
823
|
+
}
|
|
824
|
+
if (isDraggingInDirection) {
|
|
825
|
+
lastTimeDragPrevented.current = date;
|
|
826
|
+
return false;
|
|
827
|
+
}
|
|
828
|
+
while(element){
|
|
829
|
+
if (element.scrollHeight > element.clientHeight) {
|
|
830
|
+
if (element.scrollTop !== 0) {
|
|
831
|
+
lastTimeDragPrevented.current = new Date();
|
|
832
|
+
return false;
|
|
833
|
+
}
|
|
834
|
+
if (element.getAttribute("role") === "dialog") {
|
|
835
|
+
return true;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
element = element.parentNode;
|
|
839
|
+
}
|
|
840
|
+
return true;
|
|
841
|
+
}
|
|
842
|
+
function onDrag(event) {
|
|
843
|
+
if (!drawerRef.current) return;
|
|
844
|
+
if (isDragging) {
|
|
845
|
+
const directionMultiplier = direction === "bottom" || direction === "right" ? 1 : -1;
|
|
846
|
+
const draggedDistance = (pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX)) * directionMultiplier;
|
|
847
|
+
const isDraggingInDirection = draggedDistance > 0;
|
|
848
|
+
const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection;
|
|
849
|
+
if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) return;
|
|
850
|
+
const absDraggedDistance = Math.abs(draggedDistance);
|
|
851
|
+
const drawerDimension = direction === "bottom" || direction === "top" ? drawerHeightRef.current : drawerWidthRef.current;
|
|
852
|
+
let percentageDragged = absDraggedDistance / drawerDimension;
|
|
853
|
+
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection);
|
|
854
|
+
if (snapPointPercentageDragged !== null) {
|
|
855
|
+
percentageDragged = snapPointPercentageDragged;
|
|
856
|
+
}
|
|
857
|
+
if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
if (!isAllowedToDrag.current && !shouldDrag(event.target, isDraggingInDirection)) return;
|
|
861
|
+
drawerRef.current.classList.add(DRAG_CLASS);
|
|
862
|
+
isAllowedToDrag.current = true;
|
|
863
|
+
set(drawerRef.current, {
|
|
864
|
+
transition: "none"
|
|
865
|
+
});
|
|
866
|
+
set(overlayRef.current, {
|
|
867
|
+
transition: "none"
|
|
868
|
+
});
|
|
869
|
+
if (snapPoints) {
|
|
870
|
+
onDragSnapPoints({
|
|
871
|
+
draggedDistance
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
if (isDraggingInDirection && !snapPoints) {
|
|
875
|
+
const dampenedDraggedDistance = dampenValue(draggedDistance);
|
|
876
|
+
const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * directionMultiplier;
|
|
877
|
+
set(drawerRef.current, {
|
|
878
|
+
transform: isVertical(direction) ? `translate3d(0, ${translateValue}px, 0)` : `translate3d(${translateValue}px, 0, 0)`
|
|
879
|
+
});
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
const opacityValue = 1 - percentageDragged;
|
|
883
|
+
if (shouldFade || fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1) {
|
|
884
|
+
onDragProp?.(event, percentageDragged);
|
|
885
|
+
set(overlayRef.current, {
|
|
886
|
+
opacity: `${opacityValue}`,
|
|
887
|
+
transition: "none"
|
|
888
|
+
}, true);
|
|
889
|
+
}
|
|
890
|
+
if (!snapPoints) {
|
|
891
|
+
const translateValue = absDraggedDistance * directionMultiplier;
|
|
892
|
+
set(drawerRef.current, {
|
|
893
|
+
transform: isVertical(direction) ? `translate3d(0, ${translateValue}px, 0)` : `translate3d(${translateValue}px, 0, 0)`
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
const cancelDrag = React.useCallback(()=>{
|
|
899
|
+
if (!isDragging || !drawerRef.current) return;
|
|
900
|
+
drawerRef.current.classList.remove(DRAG_CLASS);
|
|
901
|
+
isAllowedToDrag.current = false;
|
|
902
|
+
setIsDragging(false);
|
|
903
|
+
dragEndTime.current = new Date();
|
|
904
|
+
}, [
|
|
905
|
+
isDragging
|
|
906
|
+
]);
|
|
907
|
+
const closeDrawer = React.useCallback((fromWithin)=>{
|
|
908
|
+
cancelDrag();
|
|
909
|
+
onClose?.();
|
|
910
|
+
if (!fromWithin) {
|
|
911
|
+
setIsOpen(false);
|
|
912
|
+
}
|
|
913
|
+
if (fadeFromIndex !== undefined && fadeFromIndex > 0 && activeSnapPointIndex === 0) {
|
|
914
|
+
set(overlayRef.current, {
|
|
915
|
+
opacity: "0"
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
setTimeout(()=>{
|
|
919
|
+
if (snapPoints) {
|
|
920
|
+
setActiveSnapPoint(snapPoints[0]);
|
|
921
|
+
}
|
|
922
|
+
}, TRANSITIONS.EXIT_DURATION * 1000);
|
|
923
|
+
}, [
|
|
924
|
+
cancelDrag,
|
|
925
|
+
onClose,
|
|
926
|
+
snapPoints,
|
|
927
|
+
setActiveSnapPoint,
|
|
928
|
+
setIsOpen,
|
|
929
|
+
fadeFromIndex,
|
|
930
|
+
activeSnapPointIndex
|
|
931
|
+
]);
|
|
932
|
+
function resetDrawer() {
|
|
933
|
+
if (!drawerRef.current) return;
|
|
934
|
+
set(drawerRef.current, {
|
|
935
|
+
transform: "translate3d(0, 0, 0)",
|
|
936
|
+
transition: `transform ${TRANSITIONS.EXIT_DURATION}s ${TRANSITIONS.CONTENT_EXIT_TIMING_FUNCTION}`
|
|
937
|
+
});
|
|
938
|
+
set(overlayRef.current, {
|
|
939
|
+
transition: `opacity ${TRANSITIONS.EXIT_DURATION}s ${TRANSITIONS.OVERLAY_EXIT_TIMING_FUNCTION}`,
|
|
940
|
+
opacity: "1"
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
function onRelease(event) {
|
|
944
|
+
if (!isDragging || !drawerRef.current) return;
|
|
945
|
+
drawerRef.current.classList.remove(DRAG_CLASS);
|
|
946
|
+
isAllowedToDrag.current = false;
|
|
947
|
+
setIsDragging(false);
|
|
948
|
+
dragEndTime.current = new Date();
|
|
949
|
+
const swipeAmount = getTranslate(drawerRef.current, direction);
|
|
950
|
+
if (!event || !shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount)) return;
|
|
951
|
+
if (dragStartTime.current === null) return;
|
|
952
|
+
const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime();
|
|
953
|
+
const distMoved = pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX);
|
|
954
|
+
const velocity = Math.abs(distMoved) / timeTaken;
|
|
955
|
+
if (velocity > 0.05) {
|
|
956
|
+
setJustReleased(true);
|
|
957
|
+
setTimeout(()=>{
|
|
958
|
+
setJustReleased(false);
|
|
959
|
+
}, 200);
|
|
960
|
+
}
|
|
961
|
+
if (snapPoints) {
|
|
962
|
+
const directionMultiplier = direction === "bottom" || direction === "right" ? 1 : -1;
|
|
963
|
+
onReleaseSnapPoints({
|
|
964
|
+
draggedDistance: distMoved * directionMultiplier,
|
|
965
|
+
closeDrawer,
|
|
966
|
+
velocity,
|
|
967
|
+
dismissible
|
|
968
|
+
});
|
|
969
|
+
onReleaseProp?.(event, true);
|
|
970
|
+
return;
|
|
971
|
+
}
|
|
972
|
+
if (direction === "bottom" || direction === "right" ? distMoved > 0 : distMoved < 0) {
|
|
973
|
+
resetDrawer();
|
|
974
|
+
onReleaseProp?.(event, true);
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
if (velocity > VELOCITY_THRESHOLD) {
|
|
978
|
+
closeDrawer();
|
|
979
|
+
onReleaseProp?.(event, false);
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
const visibleDrawerHeight = Math.min(drawerRef.current.getBoundingClientRect().height ?? 0, window.innerHeight);
|
|
983
|
+
const visibleDrawerWidth = Math.min(drawerRef.current.getBoundingClientRect().width ?? 0, window.innerWidth);
|
|
984
|
+
const isHorizontalSwipe = direction === "left" || direction === "right";
|
|
985
|
+
if (Math.abs(swipeAmount) >= (isHorizontalSwipe ? visibleDrawerWidth : visibleDrawerHeight) * closeThreshold) {
|
|
986
|
+
closeDrawer();
|
|
987
|
+
onReleaseProp?.(event, false);
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
onReleaseProp?.(event, true);
|
|
991
|
+
resetDrawer();
|
|
992
|
+
}
|
|
993
|
+
React.useEffect(()=>{
|
|
994
|
+
if (isOpen) {
|
|
995
|
+
set(document.documentElement, {
|
|
996
|
+
scrollBehavior: "auto"
|
|
997
|
+
});
|
|
998
|
+
openTime.current = new Date();
|
|
999
|
+
}
|
|
1000
|
+
return ()=>{
|
|
1001
|
+
reset(document.documentElement, "scrollBehavior");
|
|
1002
|
+
};
|
|
1003
|
+
}, [
|
|
1004
|
+
isOpen
|
|
1005
|
+
]);
|
|
1006
|
+
React.useEffect(()=>{
|
|
1007
|
+
window.requestAnimationFrame(()=>{
|
|
1008
|
+
shouldAnimate.current = true;
|
|
1009
|
+
});
|
|
1010
|
+
}, []);
|
|
1011
|
+
React.useEffect(()=>{
|
|
1012
|
+
function onVisualViewportChange() {
|
|
1013
|
+
if (!drawerRef.current || !repositionInputs) return;
|
|
1014
|
+
const focusedElement = document.activeElement;
|
|
1015
|
+
if (isInput(focusedElement) || keyboardIsOpen.current) {
|
|
1016
|
+
const visualViewportHeight = window.visualViewport?.height || 0;
|
|
1017
|
+
const totalHeight = window.innerHeight;
|
|
1018
|
+
let diffFromInitial = totalHeight - visualViewportHeight;
|
|
1019
|
+
const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0;
|
|
1020
|
+
const isTallEnough = drawerHeight > totalHeight * 0.8;
|
|
1021
|
+
if (!initialDrawerHeight.current) {
|
|
1022
|
+
initialDrawerHeight.current = drawerHeight;
|
|
1023
|
+
}
|
|
1024
|
+
const offsetFromTop = drawerRef.current.getBoundingClientRect().top;
|
|
1025
|
+
if (Math.abs(previousDiffFromInitial.current - diffFromInitial) > 60) {
|
|
1026
|
+
keyboardIsOpen.current = !keyboardIsOpen.current;
|
|
1027
|
+
}
|
|
1028
|
+
if (snapPoints && snapPoints.length > 0 && snapPointsOffset && activeSnapPointIndex) {
|
|
1029
|
+
const activeSnapPointHeight = snapPointsOffset[activeSnapPointIndex] || 0;
|
|
1030
|
+
diffFromInitial += activeSnapPointHeight;
|
|
1031
|
+
}
|
|
1032
|
+
previousDiffFromInitial.current = diffFromInitial;
|
|
1033
|
+
if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {
|
|
1034
|
+
const height = drawerRef.current.getBoundingClientRect().height;
|
|
1035
|
+
let newDrawerHeight = height;
|
|
1036
|
+
if (height > visualViewportHeight) {
|
|
1037
|
+
newDrawerHeight = visualViewportHeight - (isTallEnough ? offsetFromTop : WINDOW_TOP_OFFSET);
|
|
1038
|
+
}
|
|
1039
|
+
if (fixed) {
|
|
1040
|
+
drawerRef.current.style.height = `${height - Math.max(diffFromInitial, 0)}px`;
|
|
1041
|
+
} else {
|
|
1042
|
+
drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
|
|
1043
|
+
}
|
|
1044
|
+
} else if (!isMobileFirefox()) {
|
|
1045
|
+
drawerRef.current.style.height = `${initialDrawerHeight.current}px`;
|
|
1046
|
+
}
|
|
1047
|
+
if (snapPoints && snapPoints.length > 0 && !keyboardIsOpen.current) {
|
|
1048
|
+
drawerRef.current.style.bottom = "0px";
|
|
1049
|
+
} else {
|
|
1050
|
+
drawerRef.current.style.bottom = `${Math.max(diffFromInitial, 0)}px`;
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
window.visualViewport?.addEventListener("resize", onVisualViewportChange);
|
|
1055
|
+
return ()=>window.visualViewport?.removeEventListener("resize", onVisualViewportChange);
|
|
1056
|
+
}, [
|
|
1057
|
+
activeSnapPointIndex,
|
|
1058
|
+
snapPoints,
|
|
1059
|
+
snapPointsOffset,
|
|
1060
|
+
repositionInputs,
|
|
1061
|
+
fixed
|
|
1062
|
+
]);
|
|
1063
|
+
React.useEffect(()=>{
|
|
1064
|
+
if (!modal) {
|
|
1065
|
+
window.requestAnimationFrame(()=>{
|
|
1066
|
+
document.body.style.pointerEvents = "auto";
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
}, [
|
|
1070
|
+
modal
|
|
1071
|
+
]);
|
|
1072
|
+
React.useEffect(()=>{
|
|
1073
|
+
if (isOpen) {
|
|
1074
|
+
setHasBeenOpened(true);
|
|
1075
|
+
}
|
|
1076
|
+
}, [
|
|
1077
|
+
isOpen
|
|
1078
|
+
]);
|
|
1079
|
+
React.useEffect(()=>{
|
|
1080
|
+
if (isOpen && snapPoints && fadeFromIndex === 0) {
|
|
1081
|
+
setShouldOverlayAnimate(true);
|
|
1082
|
+
const timeoutId = setTimeout(()=>{
|
|
1083
|
+
setShouldOverlayAnimate(false);
|
|
1084
|
+
}, TRANSITIONS.ENTER_DURATION * 1000);
|
|
1085
|
+
return ()=>clearTimeout(timeoutId);
|
|
1086
|
+
}
|
|
1087
|
+
setShouldOverlayAnimate(false);
|
|
1088
|
+
}, [
|
|
1089
|
+
isOpen,
|
|
1090
|
+
snapPoints,
|
|
1091
|
+
fadeFromIndex
|
|
1092
|
+
]);
|
|
1093
|
+
return React.useMemo(()=>({
|
|
1094
|
+
activeSnapPoint,
|
|
1095
|
+
snapPoints,
|
|
1096
|
+
setActiveSnapPoint,
|
|
1097
|
+
drawerRef,
|
|
1098
|
+
overlayRef,
|
|
1099
|
+
shouldOverlayAnimate,
|
|
1100
|
+
onOpenChange,
|
|
1101
|
+
onPress,
|
|
1102
|
+
onRelease,
|
|
1103
|
+
onDrag,
|
|
1104
|
+
dismissible,
|
|
1105
|
+
shouldAnimate,
|
|
1106
|
+
handleOnly,
|
|
1107
|
+
isOpen,
|
|
1108
|
+
isDragging,
|
|
1109
|
+
shouldFade,
|
|
1110
|
+
closeDrawer,
|
|
1111
|
+
keyboardIsOpen,
|
|
1112
|
+
modal,
|
|
1113
|
+
snapPointsOffset,
|
|
1114
|
+
activeSnapPointIndex,
|
|
1115
|
+
direction,
|
|
1116
|
+
noBodyStyles,
|
|
1117
|
+
container,
|
|
1118
|
+
autoFocus,
|
|
1119
|
+
setHasBeenOpened,
|
|
1120
|
+
setIsOpen,
|
|
1121
|
+
closeOnInteractOutside,
|
|
1122
|
+
closeOnEscape
|
|
1123
|
+
}), [
|
|
1124
|
+
activeSnapPoint,
|
|
1125
|
+
snapPoints,
|
|
1126
|
+
setActiveSnapPoint,
|
|
1127
|
+
onOpenChange,
|
|
1128
|
+
dismissible,
|
|
1129
|
+
handleOnly,
|
|
1130
|
+
isOpen,
|
|
1131
|
+
isDragging,
|
|
1132
|
+
shouldFade,
|
|
1133
|
+
shouldOverlayAnimate,
|
|
1134
|
+
closeDrawer,
|
|
1135
|
+
modal,
|
|
1136
|
+
snapPointsOffset,
|
|
1137
|
+
activeSnapPointIndex,
|
|
1138
|
+
direction,
|
|
1139
|
+
noBodyStyles,
|
|
1140
|
+
container,
|
|
1141
|
+
autoFocus,
|
|
1142
|
+
setIsOpen,
|
|
1143
|
+
closeOnInteractOutside,
|
|
1144
|
+
closeOnEscape,
|
|
1145
|
+
onRelease,
|
|
1146
|
+
onDrag,
|
|
1147
|
+
onPress
|
|
1148
|
+
]);
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
const DrawerContext = /*#__PURE__*/ React.createContext(null);
|
|
1152
|
+
const DrawerProvider = DrawerContext.Provider;
|
|
1153
|
+
function useDrawerContext() {
|
|
1154
|
+
const context = React.useContext(DrawerContext);
|
|
1155
|
+
if (context === null) {
|
|
1156
|
+
throw new Error("useDrawerContext must be used within DrawerProvider");
|
|
1157
|
+
}
|
|
1158
|
+
return context;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
const DrawerRoot = (props)=>{
|
|
1162
|
+
const { children, defaultOpen, dismissible, modal } = props;
|
|
1163
|
+
const api = useDrawer(props);
|
|
1164
|
+
return /*#__PURE__*/ jsxRuntime.jsx(DialogPrimitive__namespace.Root, {
|
|
1165
|
+
defaultOpen: defaultOpen,
|
|
1166
|
+
open: api.isOpen,
|
|
1167
|
+
onOpenChange: (open)=>{
|
|
1168
|
+
if (!dismissible && !open) return;
|
|
1169
|
+
if (open) {
|
|
1170
|
+
api.setHasBeenOpened(true);
|
|
1171
|
+
} else {
|
|
1172
|
+
api.closeDrawer(true);
|
|
1173
|
+
}
|
|
1174
|
+
api.setIsOpen(open);
|
|
1175
|
+
},
|
|
1176
|
+
modal: modal,
|
|
1177
|
+
children: /*#__PURE__*/ jsxRuntime.jsx(DrawerProvider, {
|
|
1178
|
+
value: api,
|
|
1179
|
+
children: children
|
|
1180
|
+
})
|
|
1181
|
+
});
|
|
1182
|
+
};
|
|
1183
|
+
const DrawerTrigger = DialogPrimitive__namespace.Trigger;
|
|
1184
|
+
const DrawerPositioner = /*#__PURE__*/ React.forwardRef((props, ref)=>{
|
|
1185
|
+
const api = useDrawerContext();
|
|
1186
|
+
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.div, {
|
|
1187
|
+
ref: ref,
|
|
1188
|
+
...props,
|
|
1189
|
+
style: {
|
|
1190
|
+
pointerEvents: api.isOpen ? undefined : "none",
|
|
1191
|
+
...props.style
|
|
1192
|
+
}
|
|
1193
|
+
});
|
|
1194
|
+
});
|
|
1195
|
+
DrawerPositioner.displayName = "DrawerPositioner";
|
|
1196
|
+
const DrawerBackdrop = /*#__PURE__*/ React.forwardRef((props, ref)=>{
|
|
1197
|
+
const { overlayRef, onRelease, modal, snapPoints, isOpen, shouldFade, shouldAnimate, shouldOverlayAnimate } = useDrawerContext();
|
|
1198
|
+
const composedRef = reactComposeRefs.useComposedRefs(ref, overlayRef);
|
|
1199
|
+
const hasSnapPoints = snapPoints && snapPoints.length > 0;
|
|
1200
|
+
const onMouseUp = reactUseCallbackRef.useCallbackRef((event)=>onRelease(event));
|
|
1201
|
+
// Overlay is the component that is locking scroll, removing it will unlock the scroll without having to dig into Radix's Dialog library
|
|
1202
|
+
if (!modal) {
|
|
1203
|
+
return null;
|
|
1204
|
+
}
|
|
1205
|
+
return /*#__PURE__*/ jsxRuntime.jsx(DialogPrimitive__namespace.Overlay, {
|
|
1206
|
+
ref: composedRef,
|
|
1207
|
+
onMouseUp: onMouseUp,
|
|
1208
|
+
"data-snap-points": isOpen && hasSnapPoints ? "true" : "false",
|
|
1209
|
+
"data-snap-points-overlay": isOpen && shouldFade ? "true" : "false",
|
|
1210
|
+
"data-animate": shouldAnimate?.current ? "true" : "false",
|
|
1211
|
+
"data-should-overlay-animate": shouldOverlayAnimate ? "true" : "false",
|
|
1212
|
+
"data-open": domUtils.dataAttr(isOpen),
|
|
1213
|
+
...props
|
|
1214
|
+
});
|
|
1215
|
+
});
|
|
1216
|
+
DrawerBackdrop.displayName = "DrawerBackdrop";
|
|
1217
|
+
const DrawerContent = /*#__PURE__*/ React.forwardRef((props, ref)=>{
|
|
1218
|
+
const { onPointerDownOutside, style, onOpenAutoFocus, ...restProps } = props;
|
|
1219
|
+
const { drawerRef, onPress, onRelease, onDrag, keyboardIsOpen, snapPointsOffset, activeSnapPointIndex, modal, isOpen, direction, snapPoints, container, handleOnly, shouldAnimate, autoFocus, closeDrawer, closeOnInteractOutside, closeOnEscape, dismissible } = useDrawerContext();
|
|
1220
|
+
// Needed to use transition instead of animations
|
|
1221
|
+
const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
|
|
1222
|
+
const composedRef = reactComposeRefs.useComposedRefs(ref, drawerRef);
|
|
1223
|
+
const pointerStartRef = React.useRef(null);
|
|
1224
|
+
const lastKnownPointerEventRef = React.useRef(null);
|
|
1225
|
+
const wasBeyondThePointRef = React.useRef(false);
|
|
1226
|
+
const hasSnapPoints = snapPoints && snapPoints.length > 0;
|
|
1227
|
+
const isDeltaInDirection = (delta, direction, threshold = 0)=>{
|
|
1228
|
+
if (wasBeyondThePointRef.current) return true;
|
|
1229
|
+
const deltaY = Math.abs(delta.y);
|
|
1230
|
+
const deltaX = Math.abs(delta.x);
|
|
1231
|
+
const isDeltaX = deltaX > deltaY;
|
|
1232
|
+
const dFactor = [
|
|
1233
|
+
"bottom",
|
|
1234
|
+
"right"
|
|
1235
|
+
].includes(direction) ? 1 : -1;
|
|
1236
|
+
if (direction === "left" || direction === "right") {
|
|
1237
|
+
const isReverseDirection = delta.x * dFactor < 0;
|
|
1238
|
+
if (!isReverseDirection && deltaX >= 0 && deltaX <= threshold) {
|
|
1239
|
+
return isDeltaX;
|
|
1240
|
+
}
|
|
1241
|
+
} else {
|
|
1242
|
+
const isReverseDirection = delta.y * dFactor < 0;
|
|
1243
|
+
if (!isReverseDirection && deltaY >= 0 && deltaY <= threshold) {
|
|
1244
|
+
return !isDeltaX;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
wasBeyondThePointRef.current = true;
|
|
1248
|
+
return true;
|
|
1249
|
+
};
|
|
1250
|
+
React.useEffect(()=>{
|
|
1251
|
+
if (hasSnapPoints) {
|
|
1252
|
+
window.requestAnimationFrame(()=>{
|
|
1253
|
+
setDelayedSnapPoints(true);
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
}, []);
|
|
1257
|
+
function handleOnPointerUp(event) {
|
|
1258
|
+
pointerStartRef.current = null;
|
|
1259
|
+
wasBeyondThePointRef.current = false;
|
|
1260
|
+
onRelease(event);
|
|
1261
|
+
}
|
|
1262
|
+
return /*#__PURE__*/ jsxRuntime.jsx(DialogPrimitive__namespace.Content, {
|
|
1263
|
+
"data-delayed-snap-points": delayedSnapPoints ? "true" : "false",
|
|
1264
|
+
"data-drawer-direction": direction,
|
|
1265
|
+
"data-open": domUtils.dataAttr(isOpen),
|
|
1266
|
+
"data-drawer": "",
|
|
1267
|
+
"data-snap-points": isOpen && hasSnapPoints ? "true" : "false",
|
|
1268
|
+
"data-custom-container": container ? "true" : "false",
|
|
1269
|
+
"data-animate": shouldAnimate?.current ? "true" : "false",
|
|
1270
|
+
...restProps,
|
|
1271
|
+
ref: composedRef,
|
|
1272
|
+
style: snapPointsOffset && snapPointsOffset.length > 0 ? {
|
|
1273
|
+
"--snap-point-height": `${snapPointsOffset[activeSnapPointIndex ?? 0]}px`,
|
|
1274
|
+
...style
|
|
1275
|
+
} : style ?? {},
|
|
1276
|
+
onPointerDown: (event)=>{
|
|
1277
|
+
if (handleOnly) return;
|
|
1278
|
+
restProps.onPointerDown?.(event);
|
|
1279
|
+
pointerStartRef.current = {
|
|
1280
|
+
x: event.pageX,
|
|
1281
|
+
y: event.pageY
|
|
1282
|
+
};
|
|
1283
|
+
onPress(event);
|
|
1284
|
+
},
|
|
1285
|
+
onOpenAutoFocus: (e)=>{
|
|
1286
|
+
onOpenAutoFocus?.(e);
|
|
1287
|
+
if (!autoFocus) {
|
|
1288
|
+
e.preventDefault();
|
|
1289
|
+
}
|
|
1290
|
+
},
|
|
1291
|
+
onPointerDownOutside: (e)=>{
|
|
1292
|
+
onPointerDownOutside?.(e);
|
|
1293
|
+
if (!modal || e.defaultPrevented) {
|
|
1294
|
+
e.preventDefault();
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (keyboardIsOpen.current) {
|
|
1298
|
+
keyboardIsOpen.current = false;
|
|
1299
|
+
}
|
|
1300
|
+
},
|
|
1301
|
+
onFocusOutside: (e)=>{
|
|
1302
|
+
if (!modal) {
|
|
1303
|
+
e.preventDefault();
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
},
|
|
1307
|
+
onPointerMove: (event)=>{
|
|
1308
|
+
lastKnownPointerEventRef.current = event;
|
|
1309
|
+
if (handleOnly) return;
|
|
1310
|
+
restProps.onPointerMove?.(event);
|
|
1311
|
+
if (!pointerStartRef.current) return;
|
|
1312
|
+
const yPosition = event.pageY - pointerStartRef.current.y;
|
|
1313
|
+
const xPosition = event.pageX - pointerStartRef.current.x;
|
|
1314
|
+
const swipeStartThreshold = event.pointerType === "touch" ? 10 : 2;
|
|
1315
|
+
const delta = {
|
|
1316
|
+
x: xPosition,
|
|
1317
|
+
y: yPosition
|
|
1318
|
+
};
|
|
1319
|
+
const isAllowedToSwipe = isDeltaInDirection(delta, direction, swipeStartThreshold);
|
|
1320
|
+
if (isAllowedToSwipe) onDrag(event);
|
|
1321
|
+
else if (Math.abs(xPosition) > swipeStartThreshold || Math.abs(yPosition) > swipeStartThreshold) {
|
|
1322
|
+
pointerStartRef.current = null;
|
|
1323
|
+
}
|
|
1324
|
+
},
|
|
1325
|
+
onPointerUp: (event)=>{
|
|
1326
|
+
restProps.onPointerUp?.(event);
|
|
1327
|
+
pointerStartRef.current = null;
|
|
1328
|
+
wasBeyondThePointRef.current = false;
|
|
1329
|
+
onRelease(event);
|
|
1330
|
+
},
|
|
1331
|
+
onPointerOut: (event)=>{
|
|
1332
|
+
restProps.onPointerOut?.(event);
|
|
1333
|
+
handleOnPointerUp(lastKnownPointerEventRef.current);
|
|
1334
|
+
},
|
|
1335
|
+
onContextMenu: (event)=>{
|
|
1336
|
+
restProps.onContextMenu?.(event);
|
|
1337
|
+
if (lastKnownPointerEventRef.current) {
|
|
1338
|
+
handleOnPointerUp(lastKnownPointerEventRef.current);
|
|
1339
|
+
}
|
|
1340
|
+
},
|
|
1341
|
+
onInteractOutside: (e)=>{
|
|
1342
|
+
if (dismissible && closeOnInteractOutside) {
|
|
1343
|
+
closeDrawer();
|
|
1344
|
+
}
|
|
1345
|
+
props.onInteractOutside?.(e);
|
|
1346
|
+
},
|
|
1347
|
+
onEscapeKeyDown: (e)=>{
|
|
1348
|
+
if (dismissible && closeOnEscape) {
|
|
1349
|
+
closeDrawer();
|
|
1350
|
+
}
|
|
1351
|
+
props.onEscapeKeyDown?.(e);
|
|
1352
|
+
}
|
|
1353
|
+
});
|
|
1354
|
+
});
|
|
1355
|
+
DrawerContent.displayName = "DrawerContent";
|
|
1356
|
+
const DrawerTitle = DialogPrimitive__namespace.Title;
|
|
1357
|
+
const DrawerDescription = DialogPrimitive__namespace.Description;
|
|
1358
|
+
const DrawerCloseButton = /*#__PURE__*/ React.forwardRef((props, ref)=>{
|
|
1359
|
+
const api = useDrawerContext();
|
|
1360
|
+
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.button, {
|
|
1361
|
+
ref: ref,
|
|
1362
|
+
...props,
|
|
1363
|
+
onClick: (e)=>{
|
|
1364
|
+
props.onClick?.(e);
|
|
1365
|
+
if (e.defaultPrevented) return;
|
|
1366
|
+
api.setIsOpen(false);
|
|
1367
|
+
}
|
|
1368
|
+
});
|
|
1369
|
+
});
|
|
1370
|
+
const LONG_HANDLE_PRESS_TIMEOUT = 250;
|
|
1371
|
+
const DOUBLE_TAP_TIMEOUT = 120;
|
|
1372
|
+
const DrawerHandle = /*#__PURE__*/ React.forwardRef((props, ref)=>{
|
|
1373
|
+
const { preventCycle = false, children, ...rest } = props;
|
|
1374
|
+
const { closeDrawer, isDragging, snapPoints, activeSnapPoint, setActiveSnapPoint, dismissible, handleOnly, isOpen, onPress, onDrag } = useDrawerContext();
|
|
1375
|
+
const closeTimeoutIdRef = React.useRef(null);
|
|
1376
|
+
const shouldCancelInteractionRef = React.useRef(false);
|
|
1377
|
+
function handleStartCycle() {
|
|
1378
|
+
// Stop if this is the second click of a double click
|
|
1379
|
+
if (shouldCancelInteractionRef.current) {
|
|
1380
|
+
handleCancelInteraction();
|
|
1381
|
+
return;
|
|
1382
|
+
}
|
|
1383
|
+
window.setTimeout(()=>{
|
|
1384
|
+
handleCycleSnapPoints();
|
|
1385
|
+
}, DOUBLE_TAP_TIMEOUT);
|
|
1386
|
+
}
|
|
1387
|
+
function handleCycleSnapPoints() {
|
|
1388
|
+
// Prevent accidental taps while resizing drawer
|
|
1389
|
+
if (isDragging || preventCycle || shouldCancelInteractionRef.current) {
|
|
1390
|
+
handleCancelInteraction();
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
// Make sure to clear the timeout id if the user releases the handle before the cancel timeout
|
|
1394
|
+
handleCancelInteraction();
|
|
1395
|
+
if (!snapPoints || snapPoints.length === 0) {
|
|
1396
|
+
if (!dismissible) {
|
|
1397
|
+
closeDrawer();
|
|
1398
|
+
}
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
const isLastSnapPoint = activeSnapPoint === snapPoints[snapPoints.length - 1];
|
|
1402
|
+
if (isLastSnapPoint && dismissible) {
|
|
1403
|
+
closeDrawer();
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
const currentSnapIndex = snapPoints.findIndex((point)=>point === activeSnapPoint);
|
|
1407
|
+
if (currentSnapIndex === -1) return; // activeSnapPoint not found in snapPoints
|
|
1408
|
+
const nextSnapPoint = snapPoints[currentSnapIndex + 1];
|
|
1409
|
+
setActiveSnapPoint(nextSnapPoint);
|
|
1410
|
+
}
|
|
1411
|
+
function handleStartInteraction() {
|
|
1412
|
+
closeTimeoutIdRef.current = window.setTimeout(()=>{
|
|
1413
|
+
// Cancel click interaction on a long press
|
|
1414
|
+
shouldCancelInteractionRef.current = true;
|
|
1415
|
+
}, LONG_HANDLE_PRESS_TIMEOUT);
|
|
1416
|
+
}
|
|
1417
|
+
function handleCancelInteraction() {
|
|
1418
|
+
if (closeTimeoutIdRef.current) {
|
|
1419
|
+
window.clearTimeout(closeTimeoutIdRef.current);
|
|
1420
|
+
}
|
|
1421
|
+
shouldCancelInteractionRef.current = false;
|
|
1422
|
+
}
|
|
1423
|
+
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.div, {
|
|
1424
|
+
ref: ref,
|
|
1425
|
+
onClick: handleStartCycle,
|
|
1426
|
+
onPointerCancel: handleCancelInteraction,
|
|
1427
|
+
onPointerDown: (e)=>{
|
|
1428
|
+
if (handleOnly) onPress(e);
|
|
1429
|
+
handleStartInteraction();
|
|
1430
|
+
},
|
|
1431
|
+
onPointerMove: (e)=>{
|
|
1432
|
+
if (handleOnly) onDrag(e);
|
|
1433
|
+
},
|
|
1434
|
+
// onPointerUp is already handled by the content component
|
|
1435
|
+
"data-drawer-visible": isOpen ? "true" : "false",
|
|
1436
|
+
"data-handle": "",
|
|
1437
|
+
"aria-hidden": "true",
|
|
1438
|
+
...rest,
|
|
1439
|
+
children: children
|
|
1440
|
+
});
|
|
1441
|
+
});
|
|
1442
|
+
DrawerHandle.displayName = "DrawerHandle";
|
|
1443
|
+
|
|
1444
|
+
exports.DrawerBackdrop = DrawerBackdrop;
|
|
1445
|
+
exports.DrawerCloseButton = DrawerCloseButton;
|
|
1446
|
+
exports.DrawerContent = DrawerContent;
|
|
1447
|
+
exports.DrawerDescription = DrawerDescription;
|
|
1448
|
+
exports.DrawerHandle = DrawerHandle;
|
|
1449
|
+
exports.DrawerPositioner = DrawerPositioner;
|
|
1450
|
+
exports.DrawerRoot = DrawerRoot;
|
|
1451
|
+
exports.DrawerTitle = DrawerTitle;
|
|
1452
|
+
exports.DrawerTrigger = DrawerTrigger;
|
|
1453
|
+
exports.useDrawer = useDrawer;
|
|
1454
|
+
exports.useDrawerContext = useDrawerContext;
|