-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathindex.tsx
More file actions
1147 lines (1006 loc) · 37.4 KB
/
index.tsx
File metadata and controls
1147 lines (1006 loc) · 37.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use client';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import React from 'react';
import { DrawerContext, useDrawerContext } from './context';
import './style.css';
import { usePreventScroll, isInput } from './use-prevent-scroll';
import { useComposedRefs } from './use-composed-refs';
import { useSnapPoints } from './use-snap-points';
import { set, getTranslate, dampenValue, isVertical, reset } from './helpers';
import {
TRANSITIONS,
VELOCITY_THRESHOLD,
CLOSE_THRESHOLD,
SCROLL_LOCK_TIMEOUT,
BORDER_RADIUS,
NESTED_DISPLACEMENT,
WINDOW_TOP_OFFSET,
DRAG_CLASS,
} from './constants';
import { DrawerDirection } from './types';
import { useControllableState } from './use-controllable-state';
import { useScaleBackground } from './use-scale-background';
import { usePositionFixed } from './use-position-fixed';
import { isIOS, isMobileFirefox } from './browser';
export interface WithFadeFromProps {
/**
* Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up.
* Should go from least visible. Example `[0.2, 0.5, 0.8]`.
* You can also use px values, which doesn't take screen height into account.
*/
snapPoints: (number | string)[];
/**
* Index of a `snapPoint` from which the overlay fade should be applied. Defaults to the last snap point.
*/
fadeFromIndex: number;
}
export interface WithoutFadeFromProps {
/**
* Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up.
* Should go from least visible. Example `[0.2, 0.5, 0.8]`.
* You can also use px values, which doesn't take screen height into account.
*/
snapPoints?: (number | string)[];
fadeFromIndex?: never;
}
export type DialogProps = {
activeSnapPoint?: number | string | null;
setActiveSnapPoint?: (snapPoint: number | string | null) => void;
children?: React.ReactNode;
open?: boolean;
/**
* Number between 0 and 1 that determines when the drawer should be closed.
* Example: threshold of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more.
* @default 0.25
*/
closeThreshold?: number;
/**
* When `true` the `body` doesn't get any styles assigned from Vaul
*/
noBodyStyles?: boolean;
onOpenChange?: (open: boolean) => void;
shouldScaleBackground?: boolean;
/**
* When `false` we don't change body's background color when the drawer is open.
* @default true
*/
setBackgroundColorOnScale?: boolean;
/**
* Duration for which the drawer is not draggable after scrolling content inside of the drawer.
* @default 500ms
*/
scrollLockTimeout?: number;
/**
* When `true`, 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
*/
fixed?: boolean;
/**
* When `true` only allows the drawer to be dragged by the `<Drawer.Handle />` component.
* @default false
*/
handleOnly?: boolean;
/**
* When `false` dragging, clicking outside, pressing esc, etc. will not close the drawer.
* Use this in comination with the `open` prop, otherwise you won't be able to open/close the drawer.
* @default true
*/
dismissible?: boolean;
onDrag?: (event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) => void;
onRelease?: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
/**
* When `false` it allows to interact with elements outside of the drawer without closing it.
* @default true
*/
modal?: boolean;
nested?: boolean;
onClose?: () => void;
/**
* Direction of the drawer. Can be `top` or `bottom`, `left`, `right`.
* @default 'bottom'
*/
direction?: 'top' | 'bottom' | 'left' | 'right';
/**
* Opened by default, skips initial enter animation. Still reacts to `open` state changes
* @default false
*/
defaultOpen?: boolean;
/**
* When set to `true` prevents scrolling on the document body on mount, and restores it on unmount.
* @default false
*/
disablePreventScroll?: boolean;
/**
* When `true` Vaul will reposition inputs rather than scroll then into view if the keyboard is in the way.
* Setting it to `false` will fall back to the default browser behavior.
* @default true when {@link snapPoints} is defined
*/
repositionInputs?: boolean;
/**
* Disabled velocity based swiping for snap points.
* This means that a snap point won't be skipped even if the velocity is high enough.
* Useful if each snap point in a drawer is equally important.
* @default false
*/
snapToSequentialPoint?: boolean;
container?: HTMLElement | null;
/**
* Gets triggered after the open or close animation ends, it receives an `open` argument with the `open` state of the drawer by the time the function was triggered.
* Useful to revert any state changes for example.
*/
onAnimationEnd?: (open: boolean) => void;
preventScrollRestoration?: boolean;
autoFocus?: boolean;
} & (WithFadeFromProps | WithoutFadeFromProps);
export 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,
}: DialogProps) {
const [isOpen = false, setIsOpen] = useControllableState({
defaultProp: defaultOpen,
prop: openProp,
onChange: (o: boolean) => {
onOpenChange?.(o);
if (!o && !nested) {
restorePositionSetting();
}
setTimeout(() => {
onAnimationEnd?.(o);
}, TRANSITIONS.DURATION * 1000);
if (o && !modal) {
if (typeof window !== 'undefined') {
window.requestAnimationFrame(() => {
document.body.style.pointerEvents = 'auto';
});
}
}
if (!o) {
// This will be removed when the exit animation ends (`500ms`)
document.body.style.pointerEvents = 'auto';
}
},
});
const [hasBeenOpened, setHasBeenOpened] = React.useState<boolean>(false);
const [isDragging, setIsDragging] = React.useState<boolean>(false);
const [justReleased, setJustReleased] = React.useState<boolean>(false);
const overlayRef = React.useRef<HTMLDivElement>(null);
const openTime = React.useRef<Date | null>(null);
const dragStartTime = React.useRef<Date | null>(null);
const dragEndTime = React.useRef<Date | null>(null);
const lastTimeDragPrevented = React.useRef<Date | null>(null);
const isAllowedToDrag = React.useRef<boolean>(false);
const nestedOpenChangeTimer = React.useRef<NodeJS.Timeout | null>(null);
const pointerStart = React.useRef(0);
const keyboardIsOpen = React.useRef(false);
const shouldAnimate = React.useRef(!defaultOpen);
const previousDiffFromInitial = React.useRef(0);
const drawerRef = React.useRef<HTMLDivElement>(null);
const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
const drawerWidthRef = React.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
const initialDrawerHeight = React.useRef(0);
const onSnapPointChange = React.useCallback((activeSnapPointIndex: number) => {
// Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
}, []);
const {
activeSnapPoint,
activeSnapPointIndex,
setActiveSnapPoint,
onRelease: onReleaseSnapPoints,
snapPointsOffset,
onDrag: onDragSnapPoints,
shouldFade,
getPercentageDragged: getSnapPointsPercentageDragged,
} = useSnapPoints({
snapPoints,
activeSnapPointProp,
setActiveSnapPointProp,
drawerRef,
fadeFromIndex,
overlayRef,
onSnapPointChange,
direction,
container,
snapToSequentialPoint,
});
usePreventScroll({
isDisabled:
!isOpen || isDragging || !modal || justReleased || !hasBeenOpened || !repositionInputs || !disablePreventScroll,
});
const { restorePositionSetting } = usePositionFixed({
isOpen,
modal,
nested: nested ?? false,
hasBeenOpened,
preventScrollRestoration,
noBodyStyles,
});
function getScale() {
return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
}
function onPress(event: React.PointerEvent<HTMLDivElement>) {
if (!dismissible && !snapPoints) return;
if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;
drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0;
drawerWidthRef.current = drawerRef.current?.getBoundingClientRect().width || 0;
setIsDragging(true);
dragStartTime.current = new Date();
// iOS doesn't trigger mouseUp after scrolling so we need to listen to touched in order to disallow dragging
if (isIOS()) {
window.addEventListener('touchend', () => (isAllowedToDrag.current = false), { once: true });
}
// Ensure we maintain correct pointer capture even when going outside of the drawer
(event.target as HTMLElement).setPointerCapture(event.pointerId);
pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
}
function shouldDrag(el: EventTarget, isDraggingInDirection: boolean) {
let element = el as HTMLElement;
const highlightedText = window.getSelection()?.toString();
const swipeAmount = drawerRef.current ? getTranslate(drawerRef.current, direction) : null;
const date = new Date();
// Fixes https://github.com/emilkowalski/vaul/issues/483
if (element.tagName === 'SELECT') {
return false;
}
if (element.hasAttribute('data-vaul-no-drag') || element.closest('[data-vaul-no-drag]')) {
return false;
}
if (direction === 'right' || direction === 'left') {
return true;
}
// Allow scrolling when animating
if (openTime.current && date.getTime() - openTime.current.getTime() < 500) {
return false;
}
if (swipeAmount !== null) {
if (direction === 'bottom' ? swipeAmount > 0 : swipeAmount < 0) {
return true;
}
}
// Don't drag if there's highlighted text
if (highlightedText && highlightedText.length > 0) {
return false;
}
// Disallow dragging if drawer was scrolled within `scrollLockTimeout`
if (
lastTimeDragPrevented.current &&
date.getTime() - lastTimeDragPrevented.current.getTime() < scrollLockTimeout &&
swipeAmount === 0
) {
lastTimeDragPrevented.current = date;
return false;
}
if (isDraggingInDirection) {
lastTimeDragPrevented.current = date;
// We are dragging down so we should allow scrolling
return false;
}
// Keep climbing up the DOM tree as long as there's a parent
while (element) {
// Check if the element is scrollable
if (element.scrollHeight > element.clientHeight) {
if (element.scrollTop !== 0) {
lastTimeDragPrevented.current = new Date();
// The element is scrollable and not scrolled to the top, so don't drag
return false;
}
if (element.getAttribute('role') === 'dialog') {
return true;
}
}
// Move up to the parent element
element = element.parentNode as HTMLElement;
}
// No scrollable parents not scrolled to the top found, so drag
return true;
}
function onDrag(event: React.PointerEvent<HTMLDivElement>) {
if (!drawerRef.current) {
return;
}
// We need to know how much of the drawer has been dragged in percentages so that we can transform background accordingly
if (isDragging) {
const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1;
const draggedDistance =
(pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX)) * directionMultiplier;
const isDraggingInDirection = draggedDistance > 0;
// Pre condition for disallowing dragging in the close direction.
const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection;
// Disallow dragging down to close when first snap point is the active one and dismissible prop is set to false.
if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) return;
// We need to capture last time when drag with scroll was triggered and have a timeout between
const absDraggedDistance = Math.abs(draggedDistance);
const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
const drawerDimension =
direction === 'bottom' || direction === 'top' ? drawerHeightRef.current : drawerWidthRef.current;
// Calculate the percentage dragged, where 1 is the closed position
let percentageDragged = absDraggedDistance / drawerDimension;
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection);
if (snapPointPercentageDragged !== null) {
percentageDragged = snapPointPercentageDragged;
}
// Disallow close dragging beyond the smallest snap point.
if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
return;
}
if (!isAllowedToDrag.current && !shouldDrag(event.target, isDraggingInDirection)) return;
drawerRef.current.classList.add(DRAG_CLASS);
// 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
isAllowedToDrag.current = true;
set(drawerRef.current, {
transition: 'none',
});
set(overlayRef.current, {
transition: 'none',
});
if (snapPoints) {
onDragSnapPoints({ draggedDistance });
}
// Run this only if snapPoints are not defined or if we are at the last snap point (highest one)
if (isDraggingInDirection && !snapPoints) {
const dampenedDraggedDistance = dampenValue(draggedDistance);
const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * directionMultiplier;
set(drawerRef.current, {
transform: isVertical(direction)
? `translate3d(0, ${translateValue}px, 0)`
: `translate3d(${translateValue}px, 0, 0)`,
});
return;
}
const opacityValue = 1 - percentageDragged;
if (shouldFade || (fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1)) {
onDragProp?.(event, percentageDragged);
set(
overlayRef.current,
{
opacity: `${opacityValue}`,
transition: 'none',
},
true,
);
}
if (wrapper && overlayRef.current && shouldScaleBackground) {
// Calculate percentageDragged as a fraction (0 to 1)
const scaleValue = Math.min(getScale() + percentageDragged * (1 - getScale()), 1);
const borderRadiusValue = 8 - percentageDragged * 8;
const translateValue = Math.max(0, 14 - percentageDragged * 14);
set(
wrapper,
{
borderRadius: `${borderRadiusValue}px`,
transform: isVertical(direction)
? `scale(${scaleValue}) translate3d(0, ${translateValue}px, 0)`
: `scale(${scaleValue}) translate3d(${translateValue}px, 0, 0)`,
transition: 'none',
},
true,
);
}
if (!snapPoints) {
const translateValue = absDraggedDistance * directionMultiplier;
set(drawerRef.current, {
transform: isVertical(direction)
? `translate3d(0, ${translateValue}px, 0)`
: `translate3d(${translateValue}px, 0, 0)`,
});
}
}
}
React.useEffect(() => {
window.requestAnimationFrame(() => {
shouldAnimate.current = true;
});
}, []);
React.useEffect(() => {
function onVisualViewportChange() {
if (!drawerRef.current || !repositionInputs) return;
const focusedElement = document.activeElement as HTMLElement;
if (isInput(focusedElement) || keyboardIsOpen.current) {
const visualViewportHeight = window.visualViewport?.height || 0;
const totalHeight = window.innerHeight;
// This is the height of the keyboard
let diffFromInitial = totalHeight - visualViewportHeight;
const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0;
// Adjust drawer height only if it's tall enough
const isTallEnough = drawerHeight > totalHeight * 0.8;
if (!initialDrawerHeight.current) {
initialDrawerHeight.current = drawerHeight;
}
const offsetFromTop = drawerRef.current.getBoundingClientRect().top;
// 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.
if (Math.abs(previousDiffFromInitial.current - diffFromInitial) > 60) {
keyboardIsOpen.current = !keyboardIsOpen.current;
}
if (snapPoints && snapPoints.length > 0 && snapPointsOffset && activeSnapPointIndex) {
const activeSnapPointHeight = snapPointsOffset[activeSnapPointIndex] || 0;
diffFromInitial += activeSnapPointHeight;
}
previousDiffFromInitial.current = diffFromInitial;
// 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
if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {
const height = drawerRef.current.getBoundingClientRect().height;
let newDrawerHeight = height;
if (height > visualViewportHeight) {
newDrawerHeight = visualViewportHeight - (isTallEnough ? offsetFromTop : WINDOW_TOP_OFFSET);
}
// 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
if (fixed) {
drawerRef.current.style.height = `${height - Math.max(diffFromInitial, 0)}px`;
} else {
drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
}
} else if (!isMobileFirefox()) {
drawerRef.current.style.height = `${initialDrawerHeight.current}px`;
}
if (snapPoints && snapPoints.length > 0 && !keyboardIsOpen.current) {
drawerRef.current.style.bottom = `0px`;
} else {
// Negative bottom value would never make sense
drawerRef.current.style.bottom = `${Math.max(diffFromInitial, 0)}px`;
}
}
}
window.visualViewport?.addEventListener('resize', onVisualViewportChange);
return () => window.visualViewport?.removeEventListener('resize', onVisualViewportChange);
}, [activeSnapPointIndex, snapPoints, snapPointsOffset]);
function closeDrawer(fromWithin?: boolean) {
cancelDrag();
onClose?.();
if (!fromWithin) {
setIsOpen(false);
}
setTimeout(() => {
if (snapPoints) {
setActiveSnapPoint(snapPoints[0]);
}
}, TRANSITIONS.DURATION * 1000); // seconds to ms
}
function resetDrawer() {
if (!drawerRef.current) return;
const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
const currentSwipeAmount = getTranslate(drawerRef.current, direction);
set(drawerRef.current, {
transform: 'translate3d(0, 0, 0)',
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
});
set(overlayRef.current, {
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
opacity: '1',
});
// Don't reset background if swiped upwards
if (shouldScaleBackground && currentSwipeAmount && currentSwipeAmount > 0 && isOpen) {
set(
wrapper,
{
borderRadius: `${BORDER_RADIUS}px`,
overflow: 'hidden',
...(isVertical(direction)
? {
transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`,
transformOrigin: 'top',
}
: {
transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`,
transformOrigin: 'left',
}),
transitionProperty: 'transform, border-radius',
transitionDuration: `${TRANSITIONS.DURATION}s`,
transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
},
true,
);
}
}
function cancelDrag() {
if (!isDragging || !drawerRef.current) return;
drawerRef.current.classList.remove(DRAG_CLASS);
isAllowedToDrag.current = false;
setIsDragging(false);
dragEndTime.current = new Date();
}
function onRelease(event: React.PointerEvent<HTMLDivElement> | null) {
if (!isDragging || !drawerRef.current) return;
drawerRef.current.classList.remove(DRAG_CLASS);
isAllowedToDrag.current = false;
setIsDragging(false);
dragEndTime.current = new Date();
const swipeAmount = getTranslate(drawerRef.current, direction);
if (!event || !shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount)) return;
if (dragStartTime.current === null) return;
const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime();
const distMoved = pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX);
const velocity = Math.abs(distMoved) / timeTaken;
if (velocity > 0.05) {
// `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.
setJustReleased(true);
setTimeout(() => {
setJustReleased(false);
}, 200);
}
if (snapPoints) {
const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1;
onReleaseSnapPoints({
draggedDistance: distMoved * directionMultiplier,
closeDrawer,
velocity,
dismissible,
});
onReleaseProp?.(event, true);
return;
}
// Moved upwards, don't do anything
if (direction === 'bottom' || direction === 'right' ? distMoved > 0 : distMoved < 0) {
resetDrawer();
onReleaseProp?.(event, true);
return;
}
if (velocity > VELOCITY_THRESHOLD) {
closeDrawer();
onReleaseProp?.(event, false);
return;
}
const visibleDrawerHeight = Math.min(drawerRef.current.getBoundingClientRect().height ?? 0, window.innerHeight);
const visibleDrawerWidth = Math.min(drawerRef.current.getBoundingClientRect().width ?? 0, window.innerWidth);
const isHorizontalSwipe = direction === 'left' || direction === 'right';
if (Math.abs(swipeAmount) >= (isHorizontalSwipe ? visibleDrawerWidth : visibleDrawerHeight) * closeThreshold) {
closeDrawer();
onReleaseProp?.(event, false);
return;
}
onReleaseProp?.(event, true);
resetDrawer();
}
React.useEffect(() => {
// Trigger enter animation without using CSS animation
if (isOpen) {
set(document.documentElement, {
scrollBehavior: 'auto',
});
openTime.current = new Date();
}
return () => {
reset(document.documentElement, 'scrollBehavior');
};
}, [isOpen]);
function onNestedOpenChange(o: boolean) {
const scale = o ? (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth : 1;
const initialTranslate = o ? -NESTED_DISPLACEMENT : 0;
if (nestedOpenChangeTimer.current) {
window.clearTimeout(nestedOpenChangeTimer.current);
}
set(drawerRef.current, {
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
transform: isVertical(direction)
? `scale(${scale}) translate3d(0, ${initialTranslate}px, 0)`
: `scale(${scale}) translate3d(${initialTranslate}px, 0, 0)`,
});
if (!o && drawerRef.current) {
nestedOpenChangeTimer.current = setTimeout(() => {
const translateValue = getTranslate(drawerRef.current as HTMLElement, direction);
set(drawerRef.current, {
transition: 'none',
transform: isVertical(direction)
? `translate3d(0, ${translateValue}px, 0)`
: `translate3d(${translateValue}px, 0, 0)`,
});
}, 500);
}
}
function onNestedDrag(_event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) {
if (percentageDragged < 0) return;
const initialScale = (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth;
const newScale = initialScale + percentageDragged * (1 - initialScale);
const newTranslate = -NESTED_DISPLACEMENT + percentageDragged * NESTED_DISPLACEMENT;
set(drawerRef.current, {
transform: isVertical(direction)
? `scale(${newScale}) translate3d(0, ${newTranslate}px, 0)`
: `scale(${newScale}) translate3d(${newTranslate}px, 0, 0)`,
transition: 'none',
});
}
function onNestedRelease(_event: React.PointerEvent<HTMLDivElement>, o: boolean) {
const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
const scale = o ? (dim - NESTED_DISPLACEMENT) / dim : 1;
const translate = o ? -NESTED_DISPLACEMENT : 0;
if (o) {
set(drawerRef.current, {
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
transform: isVertical(direction)
? `scale(${scale}) translate3d(0, ${translate}px, 0)`
: `scale(${scale}) translate3d(${translate}px, 0, 0)`,
});
}
}
React.useEffect(() => {
if (!modal && isOpen) {
// Need to do this manually unfortunately
window.requestAnimationFrame(() => {
document.body.style.pointerEvents = 'auto';
});
}
}, [modal,isOpen]);
return (
<DialogPrimitive.Root
defaultOpen={defaultOpen}
onOpenChange={(open) => {
if (!dismissible && !open) return;
if (open) {
setHasBeenOpened(true);
} else {
closeDrawer(true);
}
setIsOpen(open);
}}
open={isOpen}
>
<DrawerContext.Provider
value={{
activeSnapPoint,
snapPoints,
setActiveSnapPoint,
drawerRef,
overlayRef,
onOpenChange,
onPress,
onRelease,
onDrag,
dismissible,
shouldAnimate,
handleOnly,
isOpen,
isDragging,
shouldFade,
closeDrawer,
onNestedDrag,
onNestedOpenChange,
onNestedRelease,
keyboardIsOpen,
modal,
snapPointsOffset,
activeSnapPointIndex,
direction,
shouldScaleBackground,
setBackgroundColorOnScale,
noBodyStyles,
container,
autoFocus,
}}
>
{children}
</DrawerContext.Provider>
</DialogPrimitive.Root>
);
}
export const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>>(
function ({ ...rest }, ref) {
const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, modal, shouldAnimate } = useDrawerContext();
const composedRef = useComposedRefs(ref, overlayRef);
const hasSnapPoints = snapPoints && snapPoints.length > 0;
const onMouseUp = React.useCallback((event: React.PointerEvent<HTMLDivElement>) => onRelease(event), [onRelease]);
// Overlay is the component that is locking scroll, removing it will unlock the scroll without having to dig into Radix's Dialog library
if (!modal) {
return null;
}
return (
<DialogPrimitive.Overlay
onMouseUp={onMouseUp}
ref={composedRef}
data-vaul-overlay=""
data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
data-vaul-snap-points-overlay={isOpen && shouldFade ? 'true' : 'false'}
data-vaul-animate={shouldAnimate?.current ? 'true' : 'false'}
{...rest}
/>
);
},
);
Overlay.displayName = 'Drawer.Overlay';
export type ContentProps = React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>;
export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
{ onPointerDownOutside, style, onOpenAutoFocus, ...rest },
ref,
) {
const {
drawerRef,
onPress,
onRelease,
onDrag,
keyboardIsOpen,
snapPointsOffset,
activeSnapPointIndex,
modal,
isOpen,
direction,
snapPoints,
container,
handleOnly,
shouldAnimate,
autoFocus,
} = useDrawerContext();
// Needed to use transition instead of animations
const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
const composedRef = useComposedRefs(ref, drawerRef);
const pointerStartRef = React.useRef<{ x: number; y: number } | null>(null);
const lastKnownPointerEventRef = React.useRef<React.PointerEvent<HTMLDivElement> | null>(null);
const wasBeyondThePointRef = React.useRef(false);
const hasSnapPoints = snapPoints && snapPoints.length > 0;
useScaleBackground();
const isDeltaInDirection = (delta: { x: number; y: number }, direction: DrawerDirection, threshold = 0) => {
if (wasBeyondThePointRef.current) return true;
const deltaY = Math.abs(delta.y);
const deltaX = Math.abs(delta.x);
const isDeltaX = deltaX > deltaY;
const dFactor = ['bottom', 'right'].includes(direction) ? 1 : -1;
if (direction === 'left' || direction === 'right') {
const isReverseDirection = delta.x * dFactor < 0;
if (!isReverseDirection && deltaX >= 0 && deltaX <= threshold) {
return isDeltaX;
}
} else {
const isReverseDirection = delta.y * dFactor < 0;
if (!isReverseDirection && deltaY >= 0 && deltaY <= threshold) {
return !isDeltaX;
}
}
wasBeyondThePointRef.current = true;
return true;
};
React.useEffect(() => {
if (hasSnapPoints) {
window.requestAnimationFrame(() => {
setDelayedSnapPoints(true);
});
}
}, []);
function handleOnPointerUp(event: React.PointerEvent<HTMLDivElement> | null) {
pointerStartRef.current = null;
wasBeyondThePointRef.current = false;
onRelease(event);
}
return (
<DialogPrimitive.Content
data-vaul-drawer-direction={direction}
data-vaul-drawer=""
data-vaul-delayed-snap-points={delayedSnapPoints ? 'true' : 'false'}
data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
data-vaul-custom-container={container ? 'true' : 'false'}
data-vaul-animate={shouldAnimate?.current ? 'true' : 'false'}
{...rest}
ref={composedRef}
style={
snapPointsOffset && snapPointsOffset.length > 0
? ({
'--snap-point-height': `${snapPointsOffset[activeSnapPointIndex ?? 0]!}px`,
...style,
} as React.CSSProperties)
: style
}
onPointerDown={(event) => {
if (handleOnly) return;
rest.onPointerDown?.(event);
pointerStartRef.current = { x: event.pageX, y: event.pageY };
onPress(event);
}}
onOpenAutoFocus={(e) => {
onOpenAutoFocus?.(e);
if (!autoFocus) {
e.preventDefault();
}
}}
onPointerDownOutside={(e) => {
onPointerDownOutside?.(e);
if (!modal || e.defaultPrevented) {
e.preventDefault();
return;
}
if (keyboardIsOpen.current) {
keyboardIsOpen.current = false;
}
}}
onFocusOutside={(e) => {
if (!modal) {
e.preventDefault();
return;
}
}}
onPointerMove={(event) => {
lastKnownPointerEventRef.current = event;
if (handleOnly) return;
rest.onPointerMove?.(event);
if (!pointerStartRef.current) return;
const yPosition = event.pageY - pointerStartRef.current.y;
const xPosition = event.pageX - pointerStartRef.current.x;
const swipeStartThreshold = event.pointerType === 'touch' ? 10 : 2;
const delta = { x: xPosition, y: yPosition };
const isAllowedToSwipe = isDeltaInDirection(delta, direction, swipeStartThreshold);
if (isAllowedToSwipe) onDrag(event);
else if (Math.abs(xPosition) > swipeStartThreshold || Math.abs(yPosition) > swipeStartThreshold) {
pointerStartRef.current = null;
}
}}
onPointerUp={(event) => {
rest.onPointerUp?.(event);
pointerStartRef.current = null;
wasBeyondThePointRef.current = false;
onRelease(event);
}}
onPointerOut={(event) => {
rest.onPointerOut?.(event);
handleOnPointerUp(lastKnownPointerEventRef.current);
}}
onContextMenu={(event) => {
rest.onContextMenu?.(event);
if (lastKnownPointerEventRef.current) {
handleOnPointerUp(lastKnownPointerEventRef.current);
}
}}
/>
);
});
Content.displayName = 'Drawer.Content';
export type HandleProps = React.ComponentPropsWithoutRef<'div'> & {
preventCycle?: boolean;
};
const LONG_HANDLE_PRESS_TIMEOUT = 250;
const DOUBLE_TAP_TIMEOUT = 120;
export const Handle = React.forwardRef<HTMLDivElement, HandleProps>(function (
{ preventCycle = false, children, ...rest },
ref,
) {
const {
closeDrawer,