@seed-design/react-drawer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +49 -0
- package/src/Drawer.namespace.ts +20 -0
- package/src/Drawer.tsx +410 -0
- package/src/browser.ts +37 -0
- package/src/constants.ts +21 -0
- package/src/helpers.ts +124 -0
- package/src/index.ts +25 -0
- package/src/types.ts +7 -0
- package/src/use-position-fixed.ts +149 -0
- package/src/use-prevent-scroll.ts +309 -0
- package/src/use-snap-points.ts +345 -0
- package/src/useDrawer.ts +715 -0
- package/src/useDrawerContext.tsx +15 -0
package/src/useDrawer.ts
ADDED
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { isIOS, isMobileFirefox } from "./browser";
|
|
4
|
+
import {
|
|
5
|
+
CLOSE_THRESHOLD,
|
|
6
|
+
DRAG_CLASS,
|
|
7
|
+
SCROLL_LOCK_TIMEOUT,
|
|
8
|
+
TRANSITIONS,
|
|
9
|
+
VELOCITY_THRESHOLD,
|
|
10
|
+
WINDOW_TOP_OFFSET,
|
|
11
|
+
} from "./constants";
|
|
12
|
+
import { dampenValue, getTranslate, isVertical, reset, set } from "./helpers";
|
|
13
|
+
import { usePositionFixed } from "./use-position-fixed";
|
|
14
|
+
import { isInput, usePreventScroll } from "./use-prevent-scroll";
|
|
15
|
+
import { useSnapPoints } from "./use-snap-points";
|
|
16
|
+
|
|
17
|
+
export interface DialogProps {
|
|
18
|
+
activeSnapPoint?: number | string | null;
|
|
19
|
+
setActiveSnapPoint?: (snapPoint: number | string | null) => void;
|
|
20
|
+
children?: React.ReactNode;
|
|
21
|
+
open?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Number between 0 and 1 that determines when the drawer should be closed.
|
|
24
|
+
* Example: threshold of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more.
|
|
25
|
+
* @default 0.25
|
|
26
|
+
*/
|
|
27
|
+
closeThreshold?: number;
|
|
28
|
+
/**
|
|
29
|
+
* When `true` the `body` doesn't get any styles assigned from Vaul
|
|
30
|
+
*/
|
|
31
|
+
noBodyStyles?: boolean;
|
|
32
|
+
onOpenChange?: (open: boolean) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Duration for which the drawer is not draggable after scrolling content inside of the drawer.
|
|
35
|
+
* @default 500ms
|
|
36
|
+
*/
|
|
37
|
+
scrollLockTimeout?: number;
|
|
38
|
+
/**
|
|
39
|
+
* 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
|
|
40
|
+
*/
|
|
41
|
+
fixed?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* When `true` only allows the drawer to be dragged by the `<Drawer.Handle />` component.
|
|
44
|
+
* @default false
|
|
45
|
+
*/
|
|
46
|
+
handleOnly?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* When `false` dragging, clicking outside, pressing esc, etc. will not close the drawer.
|
|
49
|
+
* Use this in comination with the `open` prop, otherwise you won't be able to open/close the drawer.
|
|
50
|
+
* @default true
|
|
51
|
+
*/
|
|
52
|
+
dismissible?: boolean;
|
|
53
|
+
onDrag?: (event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) => void;
|
|
54
|
+
onRelease?: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
|
|
55
|
+
/**
|
|
56
|
+
* When `false` it allows to interact with elements outside of the drawer without closing it.
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
modal?: boolean;
|
|
60
|
+
nested?: boolean;
|
|
61
|
+
onClose?: () => void;
|
|
62
|
+
/**
|
|
63
|
+
* Direction of the drawer. Can be `top` or `bottom`, `left`, `right`.
|
|
64
|
+
* @default 'bottom'
|
|
65
|
+
*/
|
|
66
|
+
direction?: "top" | "bottom" | "left" | "right";
|
|
67
|
+
/**
|
|
68
|
+
* Opened by default, skips initial enter animation. Still reacts to `open` state changes
|
|
69
|
+
* @default false
|
|
70
|
+
*/
|
|
71
|
+
defaultOpen?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* When set to `true` prevents scrolling on the document body on mount, and restores it on unmount.
|
|
74
|
+
* @default false
|
|
75
|
+
*/
|
|
76
|
+
disablePreventScroll?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* When `true` Vaul will reposition inputs rather than scroll then into view if the keyboard is in the way.
|
|
79
|
+
* Setting it to `false` will fall back to the default browser behavior.
|
|
80
|
+
* @default true when {@link snapPoints} is defined
|
|
81
|
+
*/
|
|
82
|
+
repositionInputs?: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Disabled velocity based swiping for snap points.
|
|
85
|
+
* This means that a snap point won't be skipped even if the velocity is high enough.
|
|
86
|
+
* Useful if each snap point in a drawer is equally important.
|
|
87
|
+
* @default false
|
|
88
|
+
*/
|
|
89
|
+
snapToSequentialPoint?: boolean;
|
|
90
|
+
container?: HTMLElement | null;
|
|
91
|
+
/**
|
|
92
|
+
* 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.
|
|
93
|
+
* Useful to revert any state changes for example.
|
|
94
|
+
*/
|
|
95
|
+
onAnimationEnd?: (open: boolean) => void;
|
|
96
|
+
preventScrollRestoration?: boolean;
|
|
97
|
+
autoFocus?: boolean;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Array of snap points to use.
|
|
101
|
+
* Example: snapPoints={[0, 100, 200]} will use the first snap point at 0px, the second at 100px, and the third at 200px.
|
|
102
|
+
* @default undefined
|
|
103
|
+
*/
|
|
104
|
+
snapPoints?: (number | string)[];
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Index of the snap point to start fading from.
|
|
108
|
+
* Example: fadeFromIndex={0} will start fading from the first snap point.
|
|
109
|
+
* @default snapPoints.length - 1
|
|
110
|
+
*/
|
|
111
|
+
fadeFromIndex?: number;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether to close the drawer when interacting outside of the drawer.
|
|
115
|
+
* @default true
|
|
116
|
+
*/
|
|
117
|
+
closeOnInteractOutside?: boolean;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Whether to close the drawer when pressing the escape key.
|
|
121
|
+
* @default true
|
|
122
|
+
*/
|
|
123
|
+
closeOnEscape?: boolean;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function useDrawer(props: DialogProps) {
|
|
127
|
+
const {
|
|
128
|
+
open: openProp,
|
|
129
|
+
onOpenChange,
|
|
130
|
+
onDrag: onDragProp,
|
|
131
|
+
onRelease: onReleaseProp,
|
|
132
|
+
snapPoints,
|
|
133
|
+
closeThreshold = CLOSE_THRESHOLD,
|
|
134
|
+
scrollLockTimeout = SCROLL_LOCK_TIMEOUT,
|
|
135
|
+
dismissible = true,
|
|
136
|
+
handleOnly = false,
|
|
137
|
+
fadeFromIndex = snapPoints && snapPoints.length - 1,
|
|
138
|
+
activeSnapPoint: activeSnapPointProp,
|
|
139
|
+
setActiveSnapPoint: setActiveSnapPointProp,
|
|
140
|
+
fixed,
|
|
141
|
+
modal = true,
|
|
142
|
+
onClose,
|
|
143
|
+
nested,
|
|
144
|
+
noBodyStyles = false,
|
|
145
|
+
direction = "bottom",
|
|
146
|
+
defaultOpen = false,
|
|
147
|
+
disablePreventScroll = true,
|
|
148
|
+
snapToSequentialPoint = false,
|
|
149
|
+
preventScrollRestoration = false,
|
|
150
|
+
repositionInputs = true,
|
|
151
|
+
onAnimationEnd,
|
|
152
|
+
container,
|
|
153
|
+
autoFocus = false,
|
|
154
|
+
closeOnInteractOutside = true,
|
|
155
|
+
closeOnEscape = true,
|
|
156
|
+
} = props;
|
|
157
|
+
|
|
158
|
+
const [isOpen = false, setIsOpen] = useControllableState({
|
|
159
|
+
defaultProp: defaultOpen,
|
|
160
|
+
prop: openProp,
|
|
161
|
+
onChange: (o: boolean) => {
|
|
162
|
+
onOpenChange?.(o);
|
|
163
|
+
|
|
164
|
+
if (!o && !nested) {
|
|
165
|
+
restorePositionSetting();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
setTimeout(() => {
|
|
169
|
+
onAnimationEnd?.(o);
|
|
170
|
+
}, TRANSITIONS.EXIT_DURATION * 1000);
|
|
171
|
+
|
|
172
|
+
if (o && !modal) {
|
|
173
|
+
if (typeof window !== "undefined") {
|
|
174
|
+
window.requestAnimationFrame(() => {
|
|
175
|
+
document.body.style.pointerEvents = "auto";
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (!o) {
|
|
181
|
+
document.body.style.pointerEvents = "auto";
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const [hasBeenOpened, setHasBeenOpened] = useState<boolean>(false);
|
|
187
|
+
const [isDragging, setIsDragging] = useState<boolean>(false);
|
|
188
|
+
const [justReleased, setJustReleased] = useState<boolean>(false);
|
|
189
|
+
const [shouldOverlayAnimate, setShouldOverlayAnimate] = useState<boolean>(false);
|
|
190
|
+
|
|
191
|
+
const overlayRef = useRef<HTMLDivElement>(null);
|
|
192
|
+
const openTime = useRef<Date | null>(null);
|
|
193
|
+
const dragStartTime = useRef<Date | null>(null);
|
|
194
|
+
const dragEndTime = useRef<Date | null>(null);
|
|
195
|
+
const lastTimeDragPrevented = useRef<Date | null>(null);
|
|
196
|
+
const isAllowedToDrag = useRef<boolean>(false);
|
|
197
|
+
const pointerStart = useRef(0);
|
|
198
|
+
const keyboardIsOpen = useRef(false);
|
|
199
|
+
const shouldAnimate = useRef(!defaultOpen);
|
|
200
|
+
const previousDiffFromInitial = useRef(0);
|
|
201
|
+
const drawerRef = useRef<HTMLDivElement>(null);
|
|
202
|
+
const drawerHeightRef = useRef(drawerRef.current?.getBoundingClientRect().height || 0);
|
|
203
|
+
const drawerWidthRef = useRef(drawerRef.current?.getBoundingClientRect().width || 0);
|
|
204
|
+
const initialDrawerHeight = useRef(0);
|
|
205
|
+
|
|
206
|
+
const onSnapPointChange = useCallback(
|
|
207
|
+
(activeSnapPointIndex: number) => {
|
|
208
|
+
if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) {
|
|
209
|
+
openTime.current = new Date();
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
[snapPoints],
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
const {
|
|
216
|
+
activeSnapPoint,
|
|
217
|
+
activeSnapPointIndex,
|
|
218
|
+
setActiveSnapPoint,
|
|
219
|
+
onRelease: onReleaseSnapPoints,
|
|
220
|
+
snapPointsOffset,
|
|
221
|
+
onDrag: onDragSnapPoints,
|
|
222
|
+
shouldFade,
|
|
223
|
+
getPercentageDragged: getSnapPointsPercentageDragged,
|
|
224
|
+
} = useSnapPoints({
|
|
225
|
+
snapPoints,
|
|
226
|
+
activeSnapPointProp,
|
|
227
|
+
setActiveSnapPointProp,
|
|
228
|
+
drawerRef,
|
|
229
|
+
fadeFromIndex,
|
|
230
|
+
overlayRef,
|
|
231
|
+
onSnapPointChange,
|
|
232
|
+
direction,
|
|
233
|
+
snapToSequentialPoint,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
usePreventScroll({
|
|
237
|
+
isDisabled:
|
|
238
|
+
!isOpen ||
|
|
239
|
+
isDragging ||
|
|
240
|
+
!modal ||
|
|
241
|
+
justReleased ||
|
|
242
|
+
!hasBeenOpened ||
|
|
243
|
+
!repositionInputs ||
|
|
244
|
+
!disablePreventScroll,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const { restorePositionSetting } = usePositionFixed({
|
|
248
|
+
isOpen,
|
|
249
|
+
modal,
|
|
250
|
+
nested: nested ?? false,
|
|
251
|
+
hasBeenOpened,
|
|
252
|
+
preventScrollRestoration,
|
|
253
|
+
noBodyStyles,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
function onPress(event: React.PointerEvent<HTMLDivElement>) {
|
|
257
|
+
if (!dismissible && !snapPoints) return;
|
|
258
|
+
if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;
|
|
259
|
+
|
|
260
|
+
drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0;
|
|
261
|
+
drawerWidthRef.current = drawerRef.current?.getBoundingClientRect().width || 0;
|
|
262
|
+
setIsDragging(true);
|
|
263
|
+
dragStartTime.current = new Date();
|
|
264
|
+
|
|
265
|
+
if (isIOS()) {
|
|
266
|
+
window.addEventListener("touchend", () => (isAllowedToDrag.current = false), { once: true });
|
|
267
|
+
}
|
|
268
|
+
(event.target as HTMLElement).setPointerCapture(event.pointerId);
|
|
269
|
+
|
|
270
|
+
pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function shouldDrag(el: EventTarget, isDraggingInDirection: boolean) {
|
|
274
|
+
let element = el as HTMLElement;
|
|
275
|
+
const highlightedText = window.getSelection()?.toString();
|
|
276
|
+
const swipeAmount = drawerRef.current ? getTranslate(drawerRef.current, direction) : null;
|
|
277
|
+
const date = new Date();
|
|
278
|
+
|
|
279
|
+
if (element.tagName === "SELECT") {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (element.hasAttribute("data-no-drag") || element.closest("[data-no-drag]")) {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (direction === "right" || direction === "left") {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (openTime.current && date.getTime() - openTime.current.getTime() < 500) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (swipeAmount !== null) {
|
|
296
|
+
if (direction === "bottom" ? swipeAmount > 0 : swipeAmount < 0) {
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (highlightedText && highlightedText.length > 0) {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (
|
|
306
|
+
lastTimeDragPrevented.current &&
|
|
307
|
+
date.getTime() - lastTimeDragPrevented.current.getTime() < scrollLockTimeout &&
|
|
308
|
+
swipeAmount === 0
|
|
309
|
+
) {
|
|
310
|
+
lastTimeDragPrevented.current = date;
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (isDraggingInDirection) {
|
|
315
|
+
lastTimeDragPrevented.current = date;
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
while (element) {
|
|
320
|
+
if (element.scrollHeight > element.clientHeight) {
|
|
321
|
+
if (element.scrollTop !== 0) {
|
|
322
|
+
lastTimeDragPrevented.current = new Date();
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (element.getAttribute("role") === "dialog") {
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
element = element.parentNode as HTMLElement;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function onDrag(event: React.PointerEvent<HTMLDivElement>) {
|
|
338
|
+
if (!drawerRef.current) return;
|
|
339
|
+
|
|
340
|
+
if (isDragging) {
|
|
341
|
+
const directionMultiplier = direction === "bottom" || direction === "right" ? 1 : -1;
|
|
342
|
+
const draggedDistance =
|
|
343
|
+
(pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX)) *
|
|
344
|
+
directionMultiplier;
|
|
345
|
+
const isDraggingInDirection = draggedDistance > 0;
|
|
346
|
+
|
|
347
|
+
const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection;
|
|
348
|
+
|
|
349
|
+
if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) return;
|
|
350
|
+
|
|
351
|
+
const absDraggedDistance = Math.abs(draggedDistance);
|
|
352
|
+
const drawerDimension =
|
|
353
|
+
direction === "bottom" || direction === "top"
|
|
354
|
+
? drawerHeightRef.current
|
|
355
|
+
: drawerWidthRef.current;
|
|
356
|
+
|
|
357
|
+
let percentageDragged = absDraggedDistance / drawerDimension;
|
|
358
|
+
const snapPointPercentageDragged = getSnapPointsPercentageDragged(
|
|
359
|
+
absDraggedDistance,
|
|
360
|
+
isDraggingInDirection,
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
if (snapPointPercentageDragged !== null) {
|
|
364
|
+
percentageDragged = snapPointPercentageDragged;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (!isAllowedToDrag.current && !shouldDrag(event.target, isDraggingInDirection)) return;
|
|
372
|
+
drawerRef.current.classList.add(DRAG_CLASS);
|
|
373
|
+
|
|
374
|
+
isAllowedToDrag.current = true;
|
|
375
|
+
set(drawerRef.current, {
|
|
376
|
+
transition: "none",
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
set(overlayRef.current, {
|
|
380
|
+
transition: "none",
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
if (snapPoints) {
|
|
384
|
+
onDragSnapPoints({ draggedDistance });
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (isDraggingInDirection && !snapPoints) {
|
|
388
|
+
const dampenedDraggedDistance = dampenValue(draggedDistance);
|
|
389
|
+
const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * directionMultiplier;
|
|
390
|
+
set(drawerRef.current, {
|
|
391
|
+
transform: isVertical(direction)
|
|
392
|
+
? `translate3d(0, ${translateValue}px, 0)`
|
|
393
|
+
: `translate3d(${translateValue}px, 0, 0)`,
|
|
394
|
+
});
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const opacityValue = 1 - percentageDragged;
|
|
399
|
+
|
|
400
|
+
if (shouldFade || (fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1)) {
|
|
401
|
+
onDragProp?.(event, percentageDragged);
|
|
402
|
+
|
|
403
|
+
set(
|
|
404
|
+
overlayRef.current,
|
|
405
|
+
{
|
|
406
|
+
opacity: `${opacityValue}`,
|
|
407
|
+
transition: "none",
|
|
408
|
+
},
|
|
409
|
+
true,
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (!snapPoints) {
|
|
414
|
+
const translateValue = absDraggedDistance * directionMultiplier;
|
|
415
|
+
|
|
416
|
+
set(drawerRef.current, {
|
|
417
|
+
transform: isVertical(direction)
|
|
418
|
+
? `translate3d(0, ${translateValue}px, 0)`
|
|
419
|
+
: `translate3d(${translateValue}px, 0, 0)`,
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const cancelDrag = useCallback(() => {
|
|
426
|
+
if (!isDragging || !drawerRef.current) return;
|
|
427
|
+
|
|
428
|
+
drawerRef.current.classList.remove(DRAG_CLASS);
|
|
429
|
+
isAllowedToDrag.current = false;
|
|
430
|
+
setIsDragging(false);
|
|
431
|
+
dragEndTime.current = new Date();
|
|
432
|
+
}, [isDragging]);
|
|
433
|
+
|
|
434
|
+
const closeDrawer = useCallback(
|
|
435
|
+
(fromWithin?: boolean) => {
|
|
436
|
+
cancelDrag();
|
|
437
|
+
onClose?.();
|
|
438
|
+
|
|
439
|
+
if (!fromWithin) {
|
|
440
|
+
setIsOpen(false);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (fadeFromIndex !== undefined && fadeFromIndex > 0 && activeSnapPointIndex === 0) {
|
|
444
|
+
set(overlayRef.current, {
|
|
445
|
+
opacity: "0",
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
setTimeout(() => {
|
|
450
|
+
if (snapPoints) {
|
|
451
|
+
setActiveSnapPoint(snapPoints[0]);
|
|
452
|
+
}
|
|
453
|
+
}, TRANSITIONS.EXIT_DURATION * 1000);
|
|
454
|
+
},
|
|
455
|
+
[
|
|
456
|
+
cancelDrag,
|
|
457
|
+
onClose,
|
|
458
|
+
snapPoints,
|
|
459
|
+
setActiveSnapPoint,
|
|
460
|
+
setIsOpen,
|
|
461
|
+
fadeFromIndex,
|
|
462
|
+
activeSnapPointIndex,
|
|
463
|
+
],
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
function resetDrawer() {
|
|
467
|
+
if (!drawerRef.current) return;
|
|
468
|
+
|
|
469
|
+
set(drawerRef.current, {
|
|
470
|
+
transform: "translate3d(0, 0, 0)",
|
|
471
|
+
transition: `transform ${TRANSITIONS.EXIT_DURATION}s ${TRANSITIONS.CONTENT_EXIT_TIMING_FUNCTION}`,
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
set(overlayRef.current, {
|
|
475
|
+
transition: `opacity ${TRANSITIONS.EXIT_DURATION}s ${TRANSITIONS.OVERLAY_EXIT_TIMING_FUNCTION}`,
|
|
476
|
+
opacity: "1",
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function onRelease(event: React.PointerEvent<HTMLDivElement> | null) {
|
|
481
|
+
if (!isDragging || !drawerRef.current) return;
|
|
482
|
+
|
|
483
|
+
drawerRef.current.classList.remove(DRAG_CLASS);
|
|
484
|
+
isAllowedToDrag.current = false;
|
|
485
|
+
setIsDragging(false);
|
|
486
|
+
dragEndTime.current = new Date();
|
|
487
|
+
const swipeAmount = getTranslate(drawerRef.current, direction);
|
|
488
|
+
|
|
489
|
+
if (!event || !shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount))
|
|
490
|
+
return;
|
|
491
|
+
|
|
492
|
+
if (dragStartTime.current === null) return;
|
|
493
|
+
|
|
494
|
+
const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime();
|
|
495
|
+
const distMoved = pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX);
|
|
496
|
+
const velocity = Math.abs(distMoved) / timeTaken;
|
|
497
|
+
|
|
498
|
+
if (velocity > 0.05) {
|
|
499
|
+
setJustReleased(true);
|
|
500
|
+
setTimeout(() => {
|
|
501
|
+
setJustReleased(false);
|
|
502
|
+
}, 200);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (snapPoints) {
|
|
506
|
+
const directionMultiplier = direction === "bottom" || direction === "right" ? 1 : -1;
|
|
507
|
+
onReleaseSnapPoints({
|
|
508
|
+
draggedDistance: distMoved * directionMultiplier,
|
|
509
|
+
closeDrawer,
|
|
510
|
+
velocity,
|
|
511
|
+
dismissible,
|
|
512
|
+
});
|
|
513
|
+
onReleaseProp?.(event, true);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (direction === "bottom" || direction === "right" ? distMoved > 0 : distMoved < 0) {
|
|
518
|
+
resetDrawer();
|
|
519
|
+
onReleaseProp?.(event, true);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (velocity > VELOCITY_THRESHOLD) {
|
|
524
|
+
closeDrawer();
|
|
525
|
+
onReleaseProp?.(event, false);
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const visibleDrawerHeight = Math.min(
|
|
530
|
+
drawerRef.current.getBoundingClientRect().height ?? 0,
|
|
531
|
+
window.innerHeight,
|
|
532
|
+
);
|
|
533
|
+
const visibleDrawerWidth = Math.min(
|
|
534
|
+
drawerRef.current.getBoundingClientRect().width ?? 0,
|
|
535
|
+
window.innerWidth,
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
const isHorizontalSwipe = direction === "left" || direction === "right";
|
|
539
|
+
if (
|
|
540
|
+
Math.abs(swipeAmount) >=
|
|
541
|
+
(isHorizontalSwipe ? visibleDrawerWidth : visibleDrawerHeight) * closeThreshold
|
|
542
|
+
) {
|
|
543
|
+
closeDrawer();
|
|
544
|
+
onReleaseProp?.(event, false);
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
onReleaseProp?.(event, true);
|
|
549
|
+
resetDrawer();
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
useEffect(() => {
|
|
553
|
+
if (isOpen) {
|
|
554
|
+
set(document.documentElement, {
|
|
555
|
+
scrollBehavior: "auto",
|
|
556
|
+
});
|
|
557
|
+
openTime.current = new Date();
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return () => {
|
|
561
|
+
reset(document.documentElement, "scrollBehavior");
|
|
562
|
+
};
|
|
563
|
+
}, [isOpen]);
|
|
564
|
+
|
|
565
|
+
useEffect(() => {
|
|
566
|
+
window.requestAnimationFrame(() => {
|
|
567
|
+
shouldAnimate.current = true;
|
|
568
|
+
});
|
|
569
|
+
}, []);
|
|
570
|
+
|
|
571
|
+
useEffect(() => {
|
|
572
|
+
function onVisualViewportChange() {
|
|
573
|
+
if (!drawerRef.current || !repositionInputs) return;
|
|
574
|
+
|
|
575
|
+
const focusedElement = document.activeElement as HTMLElement;
|
|
576
|
+
if (isInput(focusedElement) || keyboardIsOpen.current) {
|
|
577
|
+
const visualViewportHeight = window.visualViewport?.height || 0;
|
|
578
|
+
const totalHeight = window.innerHeight;
|
|
579
|
+
let diffFromInitial = totalHeight - visualViewportHeight;
|
|
580
|
+
const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0;
|
|
581
|
+
const isTallEnough = drawerHeight > totalHeight * 0.8;
|
|
582
|
+
|
|
583
|
+
if (!initialDrawerHeight.current) {
|
|
584
|
+
initialDrawerHeight.current = drawerHeight;
|
|
585
|
+
}
|
|
586
|
+
const offsetFromTop = drawerRef.current.getBoundingClientRect().top;
|
|
587
|
+
|
|
588
|
+
if (Math.abs(previousDiffFromInitial.current - diffFromInitial) > 60) {
|
|
589
|
+
keyboardIsOpen.current = !keyboardIsOpen.current;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
if (snapPoints && snapPoints.length > 0 && snapPointsOffset && activeSnapPointIndex) {
|
|
593
|
+
const activeSnapPointHeight = snapPointsOffset[activeSnapPointIndex] || 0;
|
|
594
|
+
diffFromInitial += activeSnapPointHeight;
|
|
595
|
+
}
|
|
596
|
+
previousDiffFromInitial.current = diffFromInitial;
|
|
597
|
+
|
|
598
|
+
if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {
|
|
599
|
+
const height = drawerRef.current.getBoundingClientRect().height;
|
|
600
|
+
let newDrawerHeight = height;
|
|
601
|
+
|
|
602
|
+
if (height > visualViewportHeight) {
|
|
603
|
+
newDrawerHeight =
|
|
604
|
+
visualViewportHeight - (isTallEnough ? offsetFromTop : WINDOW_TOP_OFFSET);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (fixed) {
|
|
608
|
+
drawerRef.current.style.height = `${height - Math.max(diffFromInitial, 0)}px`;
|
|
609
|
+
} else {
|
|
610
|
+
drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
|
|
611
|
+
}
|
|
612
|
+
} else if (!isMobileFirefox()) {
|
|
613
|
+
drawerRef.current.style.height = `${initialDrawerHeight.current}px`;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (snapPoints && snapPoints.length > 0 && !keyboardIsOpen.current) {
|
|
617
|
+
drawerRef.current.style.bottom = `0px`;
|
|
618
|
+
} else {
|
|
619
|
+
drawerRef.current.style.bottom = `${Math.max(diffFromInitial, 0)}px`;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
window.visualViewport?.addEventListener("resize", onVisualViewportChange);
|
|
625
|
+
return () => window.visualViewport?.removeEventListener("resize", onVisualViewportChange);
|
|
626
|
+
}, [activeSnapPointIndex, snapPoints, snapPointsOffset, repositionInputs, fixed]);
|
|
627
|
+
|
|
628
|
+
useEffect(() => {
|
|
629
|
+
if (!modal) {
|
|
630
|
+
window.requestAnimationFrame(() => {
|
|
631
|
+
document.body.style.pointerEvents = "auto";
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
}, [modal]);
|
|
635
|
+
|
|
636
|
+
useEffect(() => {
|
|
637
|
+
if (isOpen) {
|
|
638
|
+
setHasBeenOpened(true);
|
|
639
|
+
}
|
|
640
|
+
}, [isOpen]);
|
|
641
|
+
|
|
642
|
+
useEffect(() => {
|
|
643
|
+
if (isOpen && snapPoints && fadeFromIndex === 0) {
|
|
644
|
+
setShouldOverlayAnimate(true);
|
|
645
|
+
|
|
646
|
+
const timeoutId = setTimeout(() => {
|
|
647
|
+
setShouldOverlayAnimate(false);
|
|
648
|
+
}, TRANSITIONS.ENTER_DURATION * 1000);
|
|
649
|
+
|
|
650
|
+
return () => clearTimeout(timeoutId);
|
|
651
|
+
} else {
|
|
652
|
+
setShouldOverlayAnimate(false);
|
|
653
|
+
}
|
|
654
|
+
}, [isOpen, snapPoints, fadeFromIndex]);
|
|
655
|
+
|
|
656
|
+
return useMemo(
|
|
657
|
+
() => ({
|
|
658
|
+
activeSnapPoint,
|
|
659
|
+
snapPoints,
|
|
660
|
+
setActiveSnapPoint,
|
|
661
|
+
drawerRef,
|
|
662
|
+
overlayRef,
|
|
663
|
+
shouldOverlayAnimate,
|
|
664
|
+
onOpenChange,
|
|
665
|
+
onPress,
|
|
666
|
+
onRelease,
|
|
667
|
+
onDrag,
|
|
668
|
+
dismissible,
|
|
669
|
+
shouldAnimate,
|
|
670
|
+
handleOnly,
|
|
671
|
+
isOpen,
|
|
672
|
+
isDragging,
|
|
673
|
+
shouldFade,
|
|
674
|
+
closeDrawer,
|
|
675
|
+
keyboardIsOpen,
|
|
676
|
+
modal,
|
|
677
|
+
snapPointsOffset,
|
|
678
|
+
activeSnapPointIndex,
|
|
679
|
+
direction,
|
|
680
|
+
noBodyStyles,
|
|
681
|
+
container,
|
|
682
|
+
autoFocus,
|
|
683
|
+
setHasBeenOpened,
|
|
684
|
+
setIsOpen,
|
|
685
|
+
closeOnInteractOutside,
|
|
686
|
+
closeOnEscape,
|
|
687
|
+
}),
|
|
688
|
+
[
|
|
689
|
+
activeSnapPoint,
|
|
690
|
+
snapPoints,
|
|
691
|
+
setActiveSnapPoint,
|
|
692
|
+
onOpenChange,
|
|
693
|
+
dismissible,
|
|
694
|
+
handleOnly,
|
|
695
|
+
isOpen,
|
|
696
|
+
isDragging,
|
|
697
|
+
shouldFade,
|
|
698
|
+
shouldOverlayAnimate,
|
|
699
|
+
closeDrawer,
|
|
700
|
+
modal,
|
|
701
|
+
snapPointsOffset,
|
|
702
|
+
activeSnapPointIndex,
|
|
703
|
+
direction,
|
|
704
|
+
noBodyStyles,
|
|
705
|
+
container,
|
|
706
|
+
autoFocus,
|
|
707
|
+
setIsOpen,
|
|
708
|
+
closeOnInteractOutside,
|
|
709
|
+
closeOnEscape,
|
|
710
|
+
onRelease,
|
|
711
|
+
onDrag,
|
|
712
|
+
onPress,
|
|
713
|
+
],
|
|
714
|
+
);
|
|
715
|
+
}
|