@johly/vaul-svelte 1.0.0-next.8

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.
Files changed (53) hide show
  1. package/LICENSE +10 -0
  2. package/README.md +58 -0
  3. package/dist/components/drawer/drawer-content.svelte +60 -0
  4. package/dist/components/drawer/drawer-content.svelte.d.ts +5 -0
  5. package/dist/components/drawer/drawer-handle.svelte +31 -0
  6. package/dist/components/drawer/drawer-handle.svelte.d.ts +4 -0
  7. package/dist/components/drawer/drawer-nested.svelte +37 -0
  8. package/dist/components/drawer/drawer-nested.svelte.d.ts +3 -0
  9. package/dist/components/drawer/drawer-overlay.svelte +32 -0
  10. package/dist/components/drawer/drawer-overlay.svelte.d.ts +5 -0
  11. package/dist/components/drawer/drawer-portal.svelte +10 -0
  12. package/dist/components/drawer/drawer-portal.svelte.d.ts +3 -0
  13. package/dist/components/drawer/drawer.svelte +383 -0
  14. package/dist/components/drawer/drawer.svelte.d.ts +3 -0
  15. package/dist/components/drawer/index.d.ts +12 -0
  16. package/dist/components/drawer/index.js +11 -0
  17. package/dist/components/drawer/types.d.ts +126 -0
  18. package/dist/components/drawer/types.js +1 -0
  19. package/dist/components/index.d.ts +1 -0
  20. package/dist/components/index.js +1 -0
  21. package/dist/components/utils/mounted.svelte +12 -0
  22. package/dist/components/utils/mounted.svelte.d.ts +6 -0
  23. package/dist/context.d.ts +42 -0
  24. package/dist/context.js +2 -0
  25. package/dist/helpers.d.ts +16 -0
  26. package/dist/helpers.js +95 -0
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +2 -0
  29. package/dist/internal/browser.d.ts +8 -0
  30. package/dist/internal/browser.js +30 -0
  31. package/dist/internal/constants.d.ts +11 -0
  32. package/dist/internal/constants.js +11 -0
  33. package/dist/internal/noop.d.ts +1 -0
  34. package/dist/internal/noop.js +3 -0
  35. package/dist/internal/use-id.d.ts +4 -0
  36. package/dist/internal/use-id.js +8 -0
  37. package/dist/types.d.ts +12 -0
  38. package/dist/types.js +1 -0
  39. package/dist/use-drawer-content.svelte.js +187 -0
  40. package/dist/use-drawer-handle.svelte.d.ts +18 -0
  41. package/dist/use-drawer-handle.svelte.js +83 -0
  42. package/dist/use-drawer-overlay.svelte.d.ts +15 -0
  43. package/dist/use-drawer-overlay.svelte.js +40 -0
  44. package/dist/use-drawer-root.svelte.js +575 -0
  45. package/dist/use-position-fixed.svelte.d.ts +20 -0
  46. package/dist/use-position-fixed.svelte.js +114 -0
  47. package/dist/use-prevent-scroll.svelte.d.ts +15 -0
  48. package/dist/use-prevent-scroll.svelte.js +235 -0
  49. package/dist/use-scale-background.svelte.d.ts +1 -0
  50. package/dist/use-scale-background.svelte.js +57 -0
  51. package/dist/use-snap-points.svelte.d.ts +34 -0
  52. package/dist/use-snap-points.svelte.js +260 -0
  53. package/package.json +64 -0
@@ -0,0 +1,575 @@
1
+ import { afterSleep, afterTick, box, } from "svelte-toolbelt";
2
+ import { useSnapPoints } from "./use-snap-points.svelte.js";
3
+ import { isInput, usePreventScroll } from "./use-prevent-scroll.svelte.js";
4
+ import { usePositionFixed } from "./use-position-fixed.svelte.js";
5
+ import { BORDER_RADIUS, DRAG_CLASS, NESTED_DISPLACEMENT, TRANSITIONS, VELOCITY_THRESHOLD, WINDOW_TOP_OFFSET, } from "./internal/constants.js";
6
+ import { isIOS, isMobileFirefox } from "./internal/browser.js";
7
+ import { on } from "svelte/events";
8
+ import { dampenValue, getTranslate, isVertical, reset, set } from "./helpers.js";
9
+ import { watch } from "runed";
10
+ import { DrawerContext } from "./context.js";
11
+ export function useDrawerRoot(opts) {
12
+ let hasBeenOpened = $state(false);
13
+ let isDragging = $state(false);
14
+ let justReleased = $state(false);
15
+ let overlayNode = $state(null);
16
+ let drawerNode = $state(null);
17
+ let openTime = null;
18
+ let dragStartTime = null;
19
+ let dragEndTime = null;
20
+ let lastTimeDragPrevented = null;
21
+ let isAllowedToDrag = false;
22
+ let nestedOpenChangeTimer = null;
23
+ let pointerStart = 0;
24
+ let keyboardIsOpen = box(false);
25
+ let shouldAnimate = $state(!opts.open.current);
26
+ let previousDiffFromInitial = 0;
27
+ let drawerHeight = 0;
28
+ let drawerWidth = 0;
29
+ let initialDrawerHeight = 0;
30
+ let isReleasing = false;
31
+ const snapPointsState = useSnapPoints({
32
+ snapPoints: opts.snapPoints,
33
+ drawerNode: () => drawerNode,
34
+ activeSnapPoint: opts.activeSnapPoint,
35
+ container: opts.container,
36
+ direction: opts.direction,
37
+ fadeFromIndex: opts.fadeFromIndex,
38
+ overlayNode: () => overlayNode,
39
+ setOpenTime: (time) => {
40
+ openTime = time;
41
+ },
42
+ snapToSequentialPoint: opts.snapToSequentialPoint,
43
+ open: opts.open,
44
+ isReleasing: () => isReleasing,
45
+ });
46
+ usePreventScroll({
47
+ isDisabled: () => !opts.open.current ||
48
+ isDragging ||
49
+ !opts.modal.current ||
50
+ justReleased ||
51
+ !hasBeenOpened ||
52
+ !opts.repositionInputs.current ||
53
+ !opts.disablePreventScroll.current,
54
+ });
55
+ const { restorePositionSetting } = usePositionFixed({
56
+ ...opts,
57
+ hasBeenOpened: () => hasBeenOpened,
58
+ });
59
+ function getScale() {
60
+ return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
61
+ }
62
+ function onPress(event) {
63
+ if (!opts.dismissible.current && !opts.snapPoints.current)
64
+ return;
65
+ if (drawerNode && !drawerNode.contains(event.target))
66
+ return;
67
+ drawerHeight = drawerNode?.getBoundingClientRect().height || 0;
68
+ drawerWidth = drawerNode?.getBoundingClientRect().width || 0;
69
+ isDragging = true;
70
+ dragStartTime = new Date();
71
+ // iOS doesn't trigger mouseUp after scrolling so we need to listen to touched in order to disallow dragging
72
+ if (isIOS()) {
73
+ on(window, "touchend", () => (isAllowedToDrag = false), { once: true });
74
+ }
75
+ // Ensure we maintain correct pointer capture even when going outside of the drawer
76
+ event.target.setPointerCapture(event.pointerId);
77
+ pointerStart = isVertical(opts.direction.current) ? event.pageY : event.pageX;
78
+ }
79
+ function shouldDrag(el, isDraggingInDirection) {
80
+ let element = el;
81
+ const highlightedText = window.getSelection()?.toString();
82
+ const swipeAmount = drawerNode ? getTranslate(drawerNode, opts.direction.current) : null;
83
+ const date = new Date();
84
+ // Fixes https://github.com/emilkowalski/vaul/issues/483
85
+ if (element.tagName === "SELECT")
86
+ return false;
87
+ if (element.hasAttribute("data-vaul-no-drag") || element.closest("[data-vaul-no-drag]")) {
88
+ return false;
89
+ }
90
+ if (opts.direction.current === "right" || opts.direction.current === "left") {
91
+ return true;
92
+ }
93
+ // Allow scrolling when animating
94
+ if (openTime && date.getTime() - openTime.getTime() < 500) {
95
+ return false;
96
+ }
97
+ if (swipeAmount !== null) {
98
+ if (opts.direction.current === "bottom" ? swipeAmount > 0 : swipeAmount < 0) {
99
+ return true;
100
+ }
101
+ }
102
+ // Don't drag if there's highlighted text
103
+ if (highlightedText && highlightedText.length > 0) {
104
+ return false;
105
+ }
106
+ // Disallow dragging if drawer was scrolled within `scrollLockTimeout`
107
+ if (lastTimeDragPrevented &&
108
+ date.getTime() - lastTimeDragPrevented.getTime() < opts.scrollLockTimeout.current &&
109
+ swipeAmount === 0) {
110
+ lastTimeDragPrevented = date;
111
+ return false;
112
+ }
113
+ if (isDraggingInDirection) {
114
+ lastTimeDragPrevented = date;
115
+ // We are dragging down so we should allow scrolling
116
+ return false;
117
+ }
118
+ // Keep climbing up the DOM tree as long as there's a parent
119
+ while (element) {
120
+ // Check if the element is scrollable
121
+ if (element.scrollHeight > element.clientHeight) {
122
+ if (element.scrollTop !== 0) {
123
+ lastTimeDragPrevented = new Date();
124
+ // The element is scrollable and not scrolled to the top, so don't drag
125
+ return false;
126
+ }
127
+ if (element.getAttribute("role") === "dialog") {
128
+ return true;
129
+ }
130
+ }
131
+ // Move up to the parent element
132
+ element = element.parentNode;
133
+ }
134
+ // No scrollable parents not scrolled to the top found, so drag
135
+ return true;
136
+ }
137
+ function onDrag(event) {
138
+ if (!drawerNode || !isDragging)
139
+ return;
140
+ // We need to know how much of the drawer has been dragged in percentages so that we can transform background accordingly
141
+ const directionMultiplier = opts.direction.current === "bottom" || opts.direction.current === "right" ? 1 : -1;
142
+ const draggedDistance = (pointerStart - (isVertical(opts.direction.current) ? event.pageY : event.pageX)) *
143
+ directionMultiplier;
144
+ const isDraggingInDirection = draggedDistance > 0;
145
+ // Pre condition for disallowing dragging in the close direction.
146
+ const noCloseSnapPointsPreCondition = opts.snapPoints.current && !opts.dismissible.current && !isDraggingInDirection;
147
+ // Disallow dragging down to close when first snap point is the active one and dismissible prop is set to false.
148
+ if (noCloseSnapPointsPreCondition && snapPointsState.activeSnapPointIndex === 0)
149
+ return;
150
+ // We need to capture last time when drag with scroll was triggered and have a timeout between
151
+ const absDraggedDistance = Math.abs(draggedDistance);
152
+ const wrapper = document.querySelector("[data-vaul-drawer-wrapper]");
153
+ const drawerDimension = opts.direction.current === "bottom" || opts.direction.current === "top"
154
+ ? drawerHeight
155
+ : drawerWidth;
156
+ // Calculate the percentage dragged, where 1 is the closed position
157
+ let percentageDragged = absDraggedDistance / drawerDimension;
158
+ const snapPointPercentageDragged = snapPointsState.getPercentageDragged(absDraggedDistance, isDraggingInDirection);
159
+ if (snapPointPercentageDragged !== null) {
160
+ percentageDragged = snapPointPercentageDragged;
161
+ }
162
+ // Disallow close dragging beyond the smallest snap point.
163
+ if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
164
+ return;
165
+ }
166
+ if (!isAllowedToDrag && !shouldDrag(event.target, isDraggingInDirection))
167
+ return;
168
+ drawerNode.classList.add(DRAG_CLASS);
169
+ // 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
170
+ isAllowedToDrag = true;
171
+ set(drawerNode, {
172
+ transition: "none",
173
+ });
174
+ set(overlayNode, {
175
+ transition: "none",
176
+ });
177
+ if (opts.snapPoints.current) {
178
+ snapPointsState.onDrag({ draggedDistance });
179
+ }
180
+ // Run this only if snapPoints are not defined or if we are at the last snap point (highest one)
181
+ if (isDraggingInDirection && !opts.snapPoints.current) {
182
+ const dampenedDraggedDistance = dampenValue(draggedDistance);
183
+ const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * directionMultiplier;
184
+ set(drawerNode, {
185
+ transform: isVertical(opts.direction.current)
186
+ ? `translate3d(0, ${translateValue}px, 0)`
187
+ : `translate3d(${translateValue}px, 0, 0)`,
188
+ });
189
+ return;
190
+ }
191
+ const opacityValue = 1 - percentageDragged;
192
+ if (snapPointsState.shouldFade ||
193
+ (opts.fadeFromIndex.current &&
194
+ snapPointsState.activeSnapPointIndex === opts.fadeFromIndex.current - 1)) {
195
+ opts.onDrag.current?.(event, percentageDragged);
196
+ set(overlayNode, {
197
+ opacity: `${opacityValue}`,
198
+ transition: "none",
199
+ }, true);
200
+ }
201
+ if (wrapper && overlayNode && opts.shouldScaleBackground.current) {
202
+ // Calculate percentageDragged as a fraction (0 to 1)
203
+ const scaleValue = Math.min(getScale() + percentageDragged * (1 - getScale()), 1);
204
+ const borderRadiusValue = 8 - percentageDragged * 8;
205
+ const translateValue = Math.max(0, 14 - percentageDragged * 14);
206
+ set(wrapper, {
207
+ borderRadius: `${borderRadiusValue}px`,
208
+ transform: isVertical(opts.direction.current)
209
+ ? `scale(${scaleValue}) translate3d(0, ${translateValue}px, 0)`
210
+ : `scale(${scaleValue}) translate3d(${translateValue}px, 0, 0)`,
211
+ transition: "none",
212
+ }, true);
213
+ }
214
+ if (!opts.snapPoints.current) {
215
+ const translateValue = absDraggedDistance * directionMultiplier;
216
+ set(drawerNode, {
217
+ transform: isVertical(opts.direction.current)
218
+ ? `translate3d(0, ${translateValue}px, 0)`
219
+ : `translate3d(${translateValue}px, 0, 0)`,
220
+ });
221
+ }
222
+ }
223
+ $effect(() => {
224
+ window.requestAnimationFrame(() => {
225
+ shouldAnimate = true;
226
+ });
227
+ });
228
+ function onDialogOpenChange(o) {
229
+ if (!opts.dismissible.current && !o)
230
+ return;
231
+ if (o) {
232
+ hasBeenOpened = true;
233
+ }
234
+ else {
235
+ closeDrawer(true);
236
+ }
237
+ opts.open.current = o;
238
+ }
239
+ function onVisualViewportChange() {
240
+ if (!drawerNode || !opts.repositionInputs.current)
241
+ return;
242
+ const focusedElement = document.activeElement;
243
+ if (isInput(focusedElement) || keyboardIsOpen.current) {
244
+ const visualViewportHeight = window.visualViewport?.height || 0;
245
+ const totalHeight = window.innerHeight;
246
+ // This is the height of the keyboard
247
+ let diffFromInitial = totalHeight - visualViewportHeight;
248
+ const drawerHeight = drawerNode.getBoundingClientRect().height || 0;
249
+ // Adjust drawer height only if it's tall enough
250
+ const isTallEnough = drawerHeight > totalHeight * 0.8;
251
+ if (!initialDrawerHeight) {
252
+ initialDrawerHeight = drawerHeight;
253
+ }
254
+ const offsetFromTop = drawerNode.getBoundingClientRect().top;
255
+ // visualViewport height may change due to some 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.
256
+ if (Math.abs(previousDiffFromInitial - diffFromInitial) > 60) {
257
+ keyboardIsOpen.current = !keyboardIsOpen.current;
258
+ }
259
+ if (opts.snapPoints.current &&
260
+ opts.snapPoints.current.length > 0 &&
261
+ snapPointsState.snapPointsOffset &&
262
+ snapPointsState.activeSnapPointIndex) {
263
+ const activeSnapPointHeight = snapPointsState.snapPointsOffset[snapPointsState.activeSnapPointIndex] || 0;
264
+ diffFromInitial += activeSnapPointHeight;
265
+ }
266
+ previousDiffFromInitial = diffFromInitial;
267
+ // 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
268
+ if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {
269
+ const height = drawerNode.getBoundingClientRect().height;
270
+ let newDrawerHeight = height;
271
+ if (height > visualViewportHeight) {
272
+ newDrawerHeight =
273
+ visualViewportHeight - (isTallEnough ? offsetFromTop : WINDOW_TOP_OFFSET);
274
+ }
275
+ // 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
276
+ if (opts.fixed.current) {
277
+ drawerNode.style.height = `${height - Math.max(diffFromInitial, 0)}px`;
278
+ }
279
+ else {
280
+ drawerNode.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
281
+ }
282
+ }
283
+ else if (!isMobileFirefox()) {
284
+ drawerNode.style.height = `${initialDrawerHeight}px`;
285
+ }
286
+ if (opts.snapPoints.current &&
287
+ opts.snapPoints.current.length > 0 &&
288
+ !keyboardIsOpen.current) {
289
+ drawerNode.style.bottom = `0px`;
290
+ }
291
+ else {
292
+ // Negative bottom value would never make sense
293
+ drawerNode.style.bottom = `${Math.max(diffFromInitial, 0)}px`;
294
+ }
295
+ }
296
+ }
297
+ watch([
298
+ () => snapPointsState.activeSnapPointIndex,
299
+ () => opts.snapPoints.current,
300
+ () => snapPointsState.snapPointsOffset,
301
+ () => drawerNode,
302
+ ], () => {
303
+ if (!window.visualViewport)
304
+ return;
305
+ return on(window.visualViewport, "resize", onVisualViewportChange);
306
+ });
307
+ function cancelDrag() {
308
+ if (!isDragging || !drawerNode)
309
+ return;
310
+ drawerNode.classList.remove(DRAG_CLASS);
311
+ isAllowedToDrag = false;
312
+ isDragging = false;
313
+ dragEndTime = new Date();
314
+ }
315
+ function closeDrawer(fromWithin) {
316
+ cancelDrag();
317
+ opts.onClose?.current();
318
+ if (!fromWithin) {
319
+ handleOpenChange(false);
320
+ opts.open.current = false;
321
+ }
322
+ window.setTimeout(() => {
323
+ if (opts.snapPoints.current && opts.snapPoints.current.length > 0) {
324
+ opts.activeSnapPoint.current = opts.snapPoints.current[0];
325
+ }
326
+ }, TRANSITIONS.DURATION * 1000);
327
+ }
328
+ function resetDrawer() {
329
+ if (!drawerNode)
330
+ return;
331
+ const wrapper = document.querySelector("[data-vaul-drawer-wrapper]");
332
+ const currentSwipeAmount = getTranslate(drawerNode, opts.direction.current);
333
+ set(drawerNode, {
334
+ transform: "translate3d(0, 0, 0)",
335
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(",")})`,
336
+ });
337
+ set(overlayNode, {
338
+ transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(",")})`,
339
+ opacity: "1",
340
+ });
341
+ // Don't reset background if swiped upwards
342
+ if (opts.shouldScaleBackground.current &&
343
+ currentSwipeAmount &&
344
+ currentSwipeAmount > 0 &&
345
+ opts.open.current) {
346
+ set(wrapper, {
347
+ borderRadius: `${BORDER_RADIUS}px`,
348
+ overflow: "hidden",
349
+ ...(isVertical(opts.direction.current)
350
+ ? {
351
+ transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`,
352
+ transformOrigin: "top",
353
+ }
354
+ : {
355
+ transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`,
356
+ transformOrigin: "left",
357
+ }),
358
+ transitionProperty: "transform, border-radius",
359
+ transitionDuration: `${TRANSITIONS.DURATION}s`,
360
+ transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(",")})`,
361
+ }, true);
362
+ }
363
+ }
364
+ function onRelease(event) {
365
+ // We keep track of whether we are releasing or not
366
+ // because we need to differentiate release from outside click/escape keydown
367
+ isReleasing = true;
368
+ handleRelease(event);
369
+ afterTick(() => {
370
+ isReleasing = false;
371
+ });
372
+ }
373
+ function handleRelease(event) {
374
+ if (!isDragging || !drawerNode)
375
+ return;
376
+ drawerNode.classList.remove(DRAG_CLASS);
377
+ isAllowedToDrag = false;
378
+ isDragging = false;
379
+ dragEndTime = new Date();
380
+ const swipeAmount = getTranslate(drawerNode, opts.direction.current);
381
+ if (!event ||
382
+ (event.target && !shouldDrag(event.target, false)) ||
383
+ !swipeAmount ||
384
+ Number.isNaN(swipeAmount)) {
385
+ return;
386
+ }
387
+ if (dragStartTime === null)
388
+ return;
389
+ const timeTaken = dragEndTime.getTime() - dragStartTime.getTime();
390
+ const distMoved = pointerStart - (isVertical(opts.direction.current) ? event.pageY : event.pageX);
391
+ const velocity = Math.abs(distMoved) / timeTaken;
392
+ if (velocity > 0.05) {
393
+ // `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.
394
+ justReleased = true;
395
+ setTimeout(() => {
396
+ justReleased = false;
397
+ }, 200);
398
+ }
399
+ if (opts.snapPoints.current) {
400
+ const directionMultiplier = opts.direction.current === "bottom" || opts.direction.current === "right" ? 1 : -1;
401
+ snapPointsState.onRelease({
402
+ draggedDistance: distMoved * directionMultiplier,
403
+ closeDrawer,
404
+ velocity,
405
+ dismissible: opts.dismissible.current,
406
+ });
407
+ opts.onRelease.current?.(event, true);
408
+ return;
409
+ }
410
+ // Moved upwards, don't do anything
411
+ if (opts.direction.current === "bottom" || opts.direction.current === "right"
412
+ ? distMoved > 0
413
+ : distMoved < 0) {
414
+ resetDrawer();
415
+ opts.onRelease.current?.(event, true);
416
+ return;
417
+ }
418
+ if (velocity > VELOCITY_THRESHOLD) {
419
+ closeDrawer();
420
+ opts.onRelease.current?.(event, false);
421
+ return;
422
+ }
423
+ const visibleDrawerHeight = Math.min(drawerNode.getBoundingClientRect().height ?? 0, window.innerHeight);
424
+ const visibleDrawerWidth = Math.min(drawerNode.getBoundingClientRect().width ?? 0, window.innerWidth);
425
+ const isHorizontalSwipe = opts.direction.current === "left" || opts.direction.current === "right";
426
+ if (Math.abs(swipeAmount) >=
427
+ (isHorizontalSwipe ? visibleDrawerWidth : visibleDrawerHeight) *
428
+ opts.closeThreshold.current) {
429
+ closeDrawer();
430
+ opts.onRelease.current?.(event, false);
431
+ return;
432
+ }
433
+ opts.onRelease.current?.(event, true);
434
+ resetDrawer();
435
+ }
436
+ watch(() => opts.open.current, () => {
437
+ // Trigger enter animation without using CSS animation
438
+ if (opts.open.current) {
439
+ set(document.documentElement, {
440
+ scrollBehavior: "auto",
441
+ });
442
+ openTime = new Date();
443
+ }
444
+ return () => {
445
+ reset(document.documentElement, "scrollBehavior");
446
+ };
447
+ });
448
+ function onNestedOpenChange(o) {
449
+ const scale = o ? (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth : 1;
450
+ const initialTranslate = o ? -NESTED_DISPLACEMENT : 0;
451
+ if (nestedOpenChangeTimer) {
452
+ window.clearTimeout(nestedOpenChangeTimer);
453
+ }
454
+ set(drawerNode, {
455
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(",")})`,
456
+ transform: isVertical(opts.direction.current)
457
+ ? `scale(${scale}) translate3d(0, ${initialTranslate}px, 0)`
458
+ : `scale(${scale}) translate3d(${initialTranslate}px, 0, 0)`,
459
+ });
460
+ if (!o && drawerNode) {
461
+ nestedOpenChangeTimer = window.setTimeout(() => {
462
+ const translateValue = getTranslate(drawerNode, opts.direction.current);
463
+ set(drawerNode, {
464
+ transition: "none",
465
+ transform: isVertical(opts.direction.current)
466
+ ? `translate3d(0, ${translateValue}px, 0)`
467
+ : `translate3d(${translateValue}px, 0, 0)`,
468
+ });
469
+ }, 500);
470
+ }
471
+ }
472
+ function onNestedDrag(_event, percentageDragged) {
473
+ if (percentageDragged < 0)
474
+ return;
475
+ const initialScale = (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth;
476
+ const newScale = initialScale + percentageDragged * (1 - initialScale);
477
+ const newTranslate = -NESTED_DISPLACEMENT + percentageDragged * NESTED_DISPLACEMENT;
478
+ set(drawerNode, {
479
+ transform: isVertical(opts.direction.current)
480
+ ? `scale(${newScale}) translate3d(0, ${newTranslate}px, 0)`
481
+ : `scale(${newScale}) translate3d(${newTranslate}px, 0, 0)`,
482
+ transition: "none",
483
+ });
484
+ }
485
+ function onNestedRelease(_event, o) {
486
+ const dim = isVertical(opts.direction.current) ? window.innerHeight : window.innerWidth;
487
+ const scale = o ? (dim - NESTED_DISPLACEMENT) / dim : 1;
488
+ const translate = o ? -NESTED_DISPLACEMENT : 0;
489
+ if (o) {
490
+ set(drawerNode, {
491
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(",")})`,
492
+ transform: isVertical(opts.direction.current)
493
+ ? `scale(${scale}) translate3d(0, ${translate}px, 0)`
494
+ : `scale(${scale}) translate3d(${translate}px, 0, 0)`,
495
+ });
496
+ }
497
+ }
498
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
499
+ let bodyStyles;
500
+ function handleOpenChange(o) {
501
+ opts.onOpenChange.current?.(o);
502
+ if (o && !opts.nested.current) {
503
+ bodyStyles = document.body.style.cssText;
504
+ }
505
+ else if (!o && !opts.nested.current) {
506
+ afterSleep(TRANSITIONS.DURATION * 1000, () => {
507
+ document.body.style.cssText = bodyStyles;
508
+ });
509
+ }
510
+ if (!o && !opts.nested.current) {
511
+ restorePositionSetting();
512
+ }
513
+ setTimeout(() => {
514
+ opts.onAnimationEnd.current?.(o);
515
+ }, TRANSITIONS.DURATION * 1000);
516
+ if (o && !opts.modal.current) {
517
+ if (typeof window !== "undefined") {
518
+ window.requestAnimationFrame(() => {
519
+ document.body.style.pointerEvents = "auto";
520
+ });
521
+ }
522
+ }
523
+ if (!o) {
524
+ // This will be removed when the exit animation ends (`500ms`)
525
+ document.body.style.pointerEvents = "auto";
526
+ }
527
+ }
528
+ watch(() => opts.modal.current, () => {
529
+ if (!opts.modal.current) {
530
+ window.requestAnimationFrame(() => {
531
+ document.body.style.pointerEvents = "auto";
532
+ });
533
+ }
534
+ });
535
+ function setOverlayNode(node) {
536
+ overlayNode = node;
537
+ }
538
+ function setDrawerNode(node) {
539
+ drawerNode = node;
540
+ }
541
+ return DrawerContext.set({
542
+ ...opts,
543
+ keyboardIsOpen,
544
+ closeDrawer,
545
+ setDrawerNode,
546
+ setOverlayNode,
547
+ onDrag,
548
+ onNestedDrag,
549
+ onNestedOpenChange,
550
+ onNestedRelease,
551
+ onRelease,
552
+ onPress,
553
+ onDialogOpenChange,
554
+ get shouldAnimate() {
555
+ return shouldAnimate;
556
+ },
557
+ get isDragging() {
558
+ return isDragging;
559
+ },
560
+ get overlayNode() {
561
+ return overlayNode;
562
+ },
563
+ get drawerNode() {
564
+ return drawerNode;
565
+ },
566
+ get snapPointsOffset() {
567
+ return snapPointsState.snapPointsOffset;
568
+ },
569
+ get shouldFade() {
570
+ return snapPointsState.shouldFade;
571
+ },
572
+ restorePositionSetting,
573
+ handleOpenChange,
574
+ });
575
+ }
@@ -0,0 +1,20 @@
1
+ import type { Box, Getter } from "svelte-toolbelt";
2
+ /**
3
+ * This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android).
4
+ * 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.
5
+ * Issues that this hook solves:
6
+ * https://github.com/emilkowalski/vaul/issues/435
7
+ * https://github.com/emilkowalski/vaul/issues/433
8
+ * And more that I discovered, but were just not reported.
9
+ */
10
+ export declare function usePositionFixed({ open, modal, nested, hasBeenOpened, preventScrollRestoration, noBodyStyles, }: {
11
+ open: Box<boolean>;
12
+ modal: Box<boolean>;
13
+ nested: Box<boolean>;
14
+ preventScrollRestoration: Box<boolean>;
15
+ noBodyStyles: Box<boolean>;
16
+ } & {
17
+ hasBeenOpened: Getter<boolean>;
18
+ }): {
19
+ restorePositionSetting: () => void;
20
+ };