base-vaul 0.0.1

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