@samline/drawer 2.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/LICENSE.md +22 -0
- package/README.md +66 -0
- package/dist/browser/components-client-BC8MrVsa.mjs +2107 -0
- package/dist/browser/components-client-BHUFVfXB.js +2133 -0
- package/dist/browser/index.cjs +29211 -0
- package/dist/browser/index.d.mts +25 -0
- package/dist/browser/index.d.ts +25 -0
- package/dist/browser/index.js +29213 -0
- package/dist/browser/index.mjs +29201 -0
- package/dist/components-client-BC8MrVsa.mjs +2107 -0
- package/dist/components-client-BHUFVfXB.js +2133 -0
- package/dist/core/index.d.mts +56 -0
- package/dist/core/index.d.ts +56 -0
- package/dist/core/index.js +69 -0
- package/dist/core/index.mjs +67 -0
- package/dist/index.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +29190 -0
- package/dist/index.mjs +29180 -0
- package/dist/react/index.d.mts +146 -0
- package/dist/react/index.d.ts +146 -0
- package/dist/react/index.js +2719 -0
- package/dist/react/index.mjs +2678 -0
- package/dist/style.css +256 -0
- package/dist/svelte/components-client-BC8MrVsa.mjs +2107 -0
- package/dist/svelte/components-client-BHUFVfXB.js +2133 -0
- package/dist/svelte/index.d.mts +13 -0
- package/dist/svelte/index.d.ts +13 -0
- package/dist/svelte/index.js +29226 -0
- package/dist/svelte/index.mjs +29216 -0
- package/dist/vue/components-client-BHUFVfXB.js +2133 -0
- package/dist/vue/components-client-rq_o2zwK.mjs +2107 -0
- package/dist/vue/index.d.mts +107 -0
- package/dist/vue/index.d.ts +107 -0
- package/dist/vue/index.js +29359 -0
- package/dist/vue/index.mjs +29351 -0
- package/package.json +151 -0
|
@@ -0,0 +1,2107 @@
|
|
|
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 * as DialogPrimitive from '@radix-ui/react-dialog';
|
|
12
|
+
import * as React from 'react';
|
|
13
|
+
import React__default, { useLayoutEffect, useEffect, useMemo } from 'react';
|
|
14
|
+
|
|
15
|
+
const DEFAULT_OPTIONS = {
|
|
16
|
+
direction: 'bottom',
|
|
17
|
+
dismissible: true,
|
|
18
|
+
modal: true
|
|
19
|
+
};
|
|
20
|
+
function toSnapshot(options) {
|
|
21
|
+
var _options_open, _ref, _options_activeSnapPoint, _options_direction, _options_snapPoints, _options_dismissible, _options_modal;
|
|
22
|
+
var _options_snapPoints1;
|
|
23
|
+
return {
|
|
24
|
+
options,
|
|
25
|
+
state: {
|
|
26
|
+
isOpen: Boolean((_options_open = options.open) != null ? _options_open : options.defaultOpen),
|
|
27
|
+
activeSnapPoint: (_ref = (_options_activeSnapPoint = options.activeSnapPoint) != null ? _options_activeSnapPoint : (_options_snapPoints1 = options.snapPoints) == null ? void 0 : _options_snapPoints1[0]) != null ? _ref : null,
|
|
28
|
+
direction: (_options_direction = options.direction) != null ? _options_direction : DEFAULT_OPTIONS.direction,
|
|
29
|
+
snapPoints: (_options_snapPoints = options.snapPoints) != null ? _options_snapPoints : [],
|
|
30
|
+
dismissible: (_options_dismissible = options.dismissible) != null ? _options_dismissible : DEFAULT_OPTIONS.dismissible,
|
|
31
|
+
modal: (_options_modal = options.modal) != null ? _options_modal : DEFAULT_OPTIONS.modal
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function createDrawerController(initialOptions = {}) {
|
|
36
|
+
let options = {
|
|
37
|
+
...initialOptions
|
|
38
|
+
};
|
|
39
|
+
let snapshot = toSnapshot(options);
|
|
40
|
+
const listeners = new Set();
|
|
41
|
+
function publish() {
|
|
42
|
+
snapshot = toSnapshot(options);
|
|
43
|
+
listeners.forEach((listener)=>listener(snapshot));
|
|
44
|
+
return snapshot;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
getSnapshot () {
|
|
48
|
+
return snapshot;
|
|
49
|
+
},
|
|
50
|
+
setOpen (open) {
|
|
51
|
+
options = {
|
|
52
|
+
...options,
|
|
53
|
+
open
|
|
54
|
+
};
|
|
55
|
+
return publish();
|
|
56
|
+
},
|
|
57
|
+
setActiveSnapPoint (activeSnapPoint) {
|
|
58
|
+
options = {
|
|
59
|
+
...options,
|
|
60
|
+
activeSnapPoint
|
|
61
|
+
};
|
|
62
|
+
return publish();
|
|
63
|
+
},
|
|
64
|
+
patch (nextOptions) {
|
|
65
|
+
options = {
|
|
66
|
+
...options,
|
|
67
|
+
...nextOptions
|
|
68
|
+
};
|
|
69
|
+
return publish();
|
|
70
|
+
},
|
|
71
|
+
subscribe (listener) {
|
|
72
|
+
listeners.add(listener);
|
|
73
|
+
listener(snapshot);
|
|
74
|
+
return ()=>{
|
|
75
|
+
listeners.delete(listener);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const TRANSITIONS = {
|
|
82
|
+
DURATION: 0.5,
|
|
83
|
+
EASE: [
|
|
84
|
+
0.32,
|
|
85
|
+
0.72,
|
|
86
|
+
0,
|
|
87
|
+
1
|
|
88
|
+
]
|
|
89
|
+
};
|
|
90
|
+
const VELOCITY_THRESHOLD = 0.4;
|
|
91
|
+
const CLOSE_THRESHOLD = 0.25;
|
|
92
|
+
const SCROLL_LOCK_TIMEOUT = 100;
|
|
93
|
+
const BORDER_RADIUS = 8;
|
|
94
|
+
const NESTED_DISPLACEMENT = 16;
|
|
95
|
+
const WINDOW_TOP_OFFSET = 26;
|
|
96
|
+
const DRAG_CLASS = 'drawer-dragging';
|
|
97
|
+
|
|
98
|
+
const cache = new WeakMap();
|
|
99
|
+
function set(el, styles, ignoreCache = false) {
|
|
100
|
+
if (!el || !(el instanceof HTMLElement)) return;
|
|
101
|
+
let originalStyles = {};
|
|
102
|
+
Object.entries(styles).forEach(([key, value])=>{
|
|
103
|
+
if (key.startsWith('--')) {
|
|
104
|
+
el.style.setProperty(key, value);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
originalStyles[key] = el.style[key];
|
|
108
|
+
el.style[key] = value;
|
|
109
|
+
});
|
|
110
|
+
if (ignoreCache) return;
|
|
111
|
+
cache.set(el, originalStyles);
|
|
112
|
+
}
|
|
113
|
+
function reset(el, prop) {
|
|
114
|
+
if (!el || !(el instanceof HTMLElement)) return;
|
|
115
|
+
let originalStyles = cache.get(el);
|
|
116
|
+
if (!originalStyles) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
{
|
|
120
|
+
el.style[prop] = originalStyles[prop];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const isVertical = (direction)=>{
|
|
124
|
+
switch(direction){
|
|
125
|
+
case 'top':
|
|
126
|
+
case 'bottom':
|
|
127
|
+
return true;
|
|
128
|
+
case 'left':
|
|
129
|
+
case 'right':
|
|
130
|
+
return false;
|
|
131
|
+
default:
|
|
132
|
+
return direction;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
function getTranslate(element, direction) {
|
|
136
|
+
if (!element) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const style = window.getComputedStyle(element);
|
|
140
|
+
const transform = // @ts-ignore
|
|
141
|
+
style.transform || style.webkitTransform || style.mozTransform;
|
|
142
|
+
let mat = transform.match(/^matrix3d\((.+)\)$/);
|
|
143
|
+
if (mat) {
|
|
144
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix3d
|
|
145
|
+
return parseFloat(mat[1].split(', ')[isVertical(direction) ? 13 : 12]);
|
|
146
|
+
}
|
|
147
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix
|
|
148
|
+
mat = transform.match(/^matrix\((.+)\)$/);
|
|
149
|
+
return mat ? parseFloat(mat[1].split(', ')[isVertical(direction) ? 5 : 4]) : null;
|
|
150
|
+
}
|
|
151
|
+
function dampenValue(v) {
|
|
152
|
+
return 8 * (Math.log(v + 1) - 2);
|
|
153
|
+
}
|
|
154
|
+
function assignStyle(element, style) {
|
|
155
|
+
if (!element) return ()=>{};
|
|
156
|
+
const prevStyle = element.style.cssText;
|
|
157
|
+
Object.assign(element.style, style);
|
|
158
|
+
return ()=>{
|
|
159
|
+
element.style.cssText = prevStyle;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Receives functions as arguments and returns a new function that calls all.
|
|
164
|
+
*/ function chain$1(...fns) {
|
|
165
|
+
return (...args)=>{
|
|
166
|
+
for (const fn of fns){
|
|
167
|
+
if (typeof fn === 'function') {
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
fn(...args);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function getAxisAwareTranslate(direction, value) {
|
|
176
|
+
return direction === 'top' || direction === 'bottom' ? `translate3d(0, ${value}px, 0)` : `translate3d(${value}px, 0, 0)`;
|
|
177
|
+
}
|
|
178
|
+
function getScaleTranslateTransform({ direction, scale, translate }) {
|
|
179
|
+
return direction === 'top' || direction === 'bottom' ? `scale(${scale}) translate3d(0, ${translate}, 0)` : `scale(${scale}) translate3d(${translate}, 0, 0)`;
|
|
180
|
+
}
|
|
181
|
+
function getNestedDrawerTransform({ direction, isOpen, viewportSize, displacement }) {
|
|
182
|
+
const scale = isOpen ? (viewportSize - displacement) / viewportSize : 1;
|
|
183
|
+
const translate = isOpen ? -displacement : 0;
|
|
184
|
+
return {
|
|
185
|
+
scale,
|
|
186
|
+
translate,
|
|
187
|
+
transform: getScaleTranslateTransform({
|
|
188
|
+
direction,
|
|
189
|
+
scale,
|
|
190
|
+
translate: `${translate}px`
|
|
191
|
+
})
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function getNestedDragTransform({ direction, viewportSize, displacement, percentageDragged }) {
|
|
195
|
+
const initialScale = (viewportSize - displacement) / viewportSize;
|
|
196
|
+
const scale = initialScale + percentageDragged * (1 - initialScale);
|
|
197
|
+
const translate = -displacement + percentageDragged * displacement;
|
|
198
|
+
return {
|
|
199
|
+
scale,
|
|
200
|
+
translate,
|
|
201
|
+
transform: getScaleTranslateTransform({
|
|
202
|
+
direction,
|
|
203
|
+
scale,
|
|
204
|
+
translate: `${translate}px`
|
|
205
|
+
})
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function getBackgroundDragState({ baseScale, percentageDragged }) {
|
|
209
|
+
const scaleValue = Math.min(baseScale + percentageDragged * (1 - baseScale), 1);
|
|
210
|
+
const borderRadiusValue = 8 - percentageDragged * 8;
|
|
211
|
+
const translateValue = Math.max(0, 14 - percentageDragged * 14);
|
|
212
|
+
return {
|
|
213
|
+
scaleValue,
|
|
214
|
+
borderRadiusValue,
|
|
215
|
+
translateValue
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function getBackgroundResetState({ direction, baseScale }) {
|
|
219
|
+
return {
|
|
220
|
+
borderRadius: '8px',
|
|
221
|
+
overflow: 'hidden',
|
|
222
|
+
transform: getScaleTranslateTransform({
|
|
223
|
+
direction,
|
|
224
|
+
scale: baseScale,
|
|
225
|
+
translate: 'calc(env(safe-area-inset-top) + 14px)'
|
|
226
|
+
}),
|
|
227
|
+
transformOrigin: direction === 'top' || direction === 'bottom' ? 'top' : 'left'
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const defaultController = createDrawerController();
|
|
232
|
+
const DrawerContext = React__default.createContext({
|
|
233
|
+
controller: defaultController,
|
|
234
|
+
runtimeSnapshot: defaultController.getSnapshot(),
|
|
235
|
+
drawerRef: {
|
|
236
|
+
current: null
|
|
237
|
+
},
|
|
238
|
+
overlayRef: {
|
|
239
|
+
current: null
|
|
240
|
+
},
|
|
241
|
+
onPress: ()=>{},
|
|
242
|
+
onRelease: ()=>{},
|
|
243
|
+
onDrag: ()=>{},
|
|
244
|
+
onNestedDrag: ()=>{},
|
|
245
|
+
onNestedOpenChange: ()=>{},
|
|
246
|
+
onNestedRelease: ()=>{},
|
|
247
|
+
openProp: undefined,
|
|
248
|
+
dismissible: false,
|
|
249
|
+
isOpen: false,
|
|
250
|
+
isDragging: false,
|
|
251
|
+
keyboardIsOpen: {
|
|
252
|
+
current: false
|
|
253
|
+
},
|
|
254
|
+
snapPointsOffset: null,
|
|
255
|
+
snapPoints: null,
|
|
256
|
+
handleOnly: false,
|
|
257
|
+
modal: false,
|
|
258
|
+
shouldFade: false,
|
|
259
|
+
activeSnapPoint: null,
|
|
260
|
+
onOpenChange: ()=>{},
|
|
261
|
+
setActiveSnapPoint: ()=>{},
|
|
262
|
+
closeDrawer: ()=>{},
|
|
263
|
+
direction: 'bottom',
|
|
264
|
+
shouldAnimate: {
|
|
265
|
+
current: true
|
|
266
|
+
},
|
|
267
|
+
shouldScaleBackground: false,
|
|
268
|
+
setBackgroundColorOnScale: true,
|
|
269
|
+
noBodyStyles: false,
|
|
270
|
+
container: null,
|
|
271
|
+
autoFocus: false
|
|
272
|
+
});
|
|
273
|
+
const useDrawerContext = ()=>{
|
|
274
|
+
const context = React__default.useContext(DrawerContext);
|
|
275
|
+
if (!context) {
|
|
276
|
+
throw new Error('useDrawerContext must be used within a Drawer.Root');
|
|
277
|
+
}
|
|
278
|
+
return context;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
__insertCSS("[data-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-drawer][data-drawer-snap-points=false][data-drawer-direction=bottom][data-state=open]{animation-name:slideFromBottom}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=bottom][data-state=closed]{animation-name:slideToBottom}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=top][data-state=open]{animation-name:slideFromTop}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=top][data-state=closed]{animation-name:slideToTop}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=left][data-state=open]{animation-name:slideFromLeft}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=left][data-state=closed]{animation-name:slideToLeft}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=right][data-state=open]{animation-name:slideFromRight}[data-drawer][data-drawer-snap-points=false][data-drawer-direction=right][data-state=closed]{animation-name:slideToRight}[data-drawer][data-drawer-snap-points=true][data-drawer-direction=bottom]{transform:translate3d(0,var(--initial-transform,100%),0)}[data-drawer][data-drawer-snap-points=true][data-drawer-direction=top]{transform:translate3d(0,calc(var(--initial-transform,100%) * -1),0)}[data-drawer][data-drawer-snap-points=true][data-drawer-direction=left]{transform:translate3d(calc(var(--initial-transform,100%) * -1),0,0)}[data-drawer][data-drawer-snap-points=true][data-drawer-direction=right]{transform:translate3d(var(--initial-transform,100%),0,0)}[data-drawer][data-drawer-delayed-snap-points=true][data-drawer-direction=top]{transform:translate3d(0,var(--snap-point-height,0),0)}[data-drawer][data-drawer-delayed-snap-points=true][data-drawer-direction=bottom]{transform:translate3d(0,var(--snap-point-height,0),0)}[data-drawer][data-drawer-delayed-snap-points=true][data-drawer-direction=left]{transform:translate3d(var(--snap-point-height,0),0,0)}[data-drawer][data-drawer-delayed-snap-points=true][data-drawer-direction=right]{transform:translate3d(var(--snap-point-height,0),0,0)}[data-drawer-overlay][data-drawer-snap-points=false]{animation-duration:.5s;animation-timing-function:cubic-bezier(0.32,0.72,0,1)}[data-drawer-overlay][data-drawer-snap-points=false][data-state=open]{animation-name:fadeIn}[data-drawer-overlay][data-state=closed]{animation-name:fadeOut}[data-drawer-animate=false]{animation:none!important}[data-drawer-overlay][data-drawer-snap-points=true]{opacity:0;transition:opacity .5s cubic-bezier(.32, .72, 0, 1)}[data-drawer-overlay][data-drawer-snap-points=true]{opacity:1}[data-drawer]:not([data-drawer-custom-container=true])::after{content:'';position:absolute;background:inherit;background-color:inherit}[data-drawer][data-drawer-direction=top]::after{top:initial;bottom:100%;left:0;right:0;height:200%}[data-drawer][data-drawer-direction=bottom]::after{top:100%;bottom:initial;left:0;right:0;height:200%}[data-drawer][data-drawer-direction=left]::after{left:initial;right:100%;top:0;bottom:0;width:200%}[data-drawer][data-drawer-direction=right]::after{left:100%;right:initial;top:0;bottom:0;width:200%}[data-drawer-overlay][data-drawer-snap-points=true]:not([data-drawer-snap-points-overlay=true]):not(\n[data-state=closed]\n){opacity:0}[data-drawer-overlay][data-drawer-snap-points-overlay=true]{opacity:1}[data-drawer-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-drawer-handle]:active,[data-drawer-handle]:hover{opacity:1}[data-drawer-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-drawer]{user-select:none}}@media (pointer:fine){[data-drawer-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)}}");
|
|
282
|
+
|
|
283
|
+
function isMobileFirefox() {
|
|
284
|
+
const userAgent = navigator.userAgent;
|
|
285
|
+
return typeof window !== 'undefined' && (/Firefox/.test(userAgent) && /Mobile/.test(userAgent) || /FxiOS/.test(userAgent));
|
|
286
|
+
}
|
|
287
|
+
function isMac() {
|
|
288
|
+
return testPlatform(/^Mac/);
|
|
289
|
+
}
|
|
290
|
+
function isIPhone() {
|
|
291
|
+
return testPlatform(/^iPhone/);
|
|
292
|
+
}
|
|
293
|
+
function isSafari() {
|
|
294
|
+
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
295
|
+
}
|
|
296
|
+
function isIPad() {
|
|
297
|
+
return testPlatform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
|
|
298
|
+
}
|
|
299
|
+
function isIOS() {
|
|
300
|
+
return isIPhone() || isIPad();
|
|
301
|
+
}
|
|
302
|
+
function testPlatform(re) {
|
|
303
|
+
return typeof window !== 'undefined' && window.navigator != null ? re.test(window.navigator.platform) : undefined;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// This code comes from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts
|
|
307
|
+
const KEYBOARD_BUFFER = 24;
|
|
308
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
309
|
+
function chain(...callbacks) {
|
|
310
|
+
return (...args)=>{
|
|
311
|
+
for (let callback of callbacks){
|
|
312
|
+
if (typeof callback === 'function') {
|
|
313
|
+
callback(...args);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
// @ts-ignore
|
|
319
|
+
const visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
320
|
+
function isScrollable(node) {
|
|
321
|
+
let style = window.getComputedStyle(node);
|
|
322
|
+
return /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY);
|
|
323
|
+
}
|
|
324
|
+
function getScrollParent(node) {
|
|
325
|
+
if (isScrollable(node)) {
|
|
326
|
+
node = node.parentElement;
|
|
327
|
+
}
|
|
328
|
+
while(node && !isScrollable(node)){
|
|
329
|
+
node = node.parentElement;
|
|
330
|
+
}
|
|
331
|
+
return node || document.scrollingElement || document.documentElement;
|
|
332
|
+
}
|
|
333
|
+
// HTML input types that do not cause the software keyboard to appear.
|
|
334
|
+
const nonTextInputTypes = new Set([
|
|
335
|
+
'checkbox',
|
|
336
|
+
'radio',
|
|
337
|
+
'range',
|
|
338
|
+
'color',
|
|
339
|
+
'file',
|
|
340
|
+
'image',
|
|
341
|
+
'button',
|
|
342
|
+
'submit',
|
|
343
|
+
'reset'
|
|
344
|
+
]);
|
|
345
|
+
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
346
|
+
let preventScrollCount = 0;
|
|
347
|
+
let restore;
|
|
348
|
+
/**
|
|
349
|
+
* Prevents scrolling on the document body on mount, and
|
|
350
|
+
* restores it on unmount. Also ensures that content does not
|
|
351
|
+
* shift due to the scrollbars disappearing.
|
|
352
|
+
*/ function usePreventScroll(options = {}) {
|
|
353
|
+
let { isDisabled } = options;
|
|
354
|
+
useIsomorphicLayoutEffect(()=>{
|
|
355
|
+
if (isDisabled) {
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
preventScrollCount++;
|
|
359
|
+
if (preventScrollCount === 1) {
|
|
360
|
+
if (isIOS()) {
|
|
361
|
+
restore = preventScrollMobileSafari();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return ()=>{
|
|
365
|
+
preventScrollCount--;
|
|
366
|
+
if (preventScrollCount === 0) {
|
|
367
|
+
restore == null ? void 0 : restore();
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}, [
|
|
371
|
+
isDisabled
|
|
372
|
+
]);
|
|
373
|
+
}
|
|
374
|
+
// Mobile Safari is a whole different beast. Even with overflow: hidden,
|
|
375
|
+
// it still scrolls the page in many situations:
|
|
376
|
+
//
|
|
377
|
+
// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
|
|
378
|
+
// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
|
|
379
|
+
// it, so it becomes scrollable.
|
|
380
|
+
// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
|
|
381
|
+
// This may cause even fixed position elements to scroll off the screen.
|
|
382
|
+
// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
|
|
383
|
+
// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
|
|
384
|
+
//
|
|
385
|
+
// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
|
|
386
|
+
//
|
|
387
|
+
// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
|
|
388
|
+
// on the window.
|
|
389
|
+
// 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the
|
|
390
|
+
// top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling.
|
|
391
|
+
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
392
|
+
// 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top
|
|
393
|
+
// of the page, which prevents it from scrolling the page. After the input is focused, scroll the element
|
|
394
|
+
// into view ourselves, without scrolling the whole page.
|
|
395
|
+
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
396
|
+
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
397
|
+
// above work or Safari will still try to scroll the page when focusing an input.
|
|
398
|
+
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
399
|
+
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
400
|
+
function preventScrollMobileSafari() {
|
|
401
|
+
let scrollable;
|
|
402
|
+
let lastY = 0;
|
|
403
|
+
let onTouchStart = (e)=>{
|
|
404
|
+
// Store the nearest scrollable parent element from the element that the user touched.
|
|
405
|
+
scrollable = getScrollParent(e.target);
|
|
406
|
+
if (scrollable === document.documentElement && scrollable === document.body) {
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
lastY = e.changedTouches[0].pageY;
|
|
410
|
+
};
|
|
411
|
+
let onTouchMove = (e)=>{
|
|
412
|
+
// Prevent scrolling the window.
|
|
413
|
+
if (!scrollable || scrollable === document.documentElement || scrollable === document.body) {
|
|
414
|
+
e.preventDefault();
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
418
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
419
|
+
// the window instead. Unfortunately, this disables bounce scrolling when at
|
|
420
|
+
// the top but it's the best we can do.
|
|
421
|
+
let y = e.changedTouches[0].pageY;
|
|
422
|
+
let scrollTop = scrollable.scrollTop;
|
|
423
|
+
let bottom = scrollable.scrollHeight - scrollable.clientHeight;
|
|
424
|
+
if (bottom === 0) {
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
if (scrollTop <= 0 && y > lastY || scrollTop >= bottom && y < lastY) {
|
|
428
|
+
e.preventDefault();
|
|
429
|
+
}
|
|
430
|
+
lastY = y;
|
|
431
|
+
};
|
|
432
|
+
let onTouchEnd = (e)=>{
|
|
433
|
+
let target = e.target;
|
|
434
|
+
// Apply this change if we're not already focused on the target element
|
|
435
|
+
if (isInput(target) && target !== document.activeElement) {
|
|
436
|
+
e.preventDefault();
|
|
437
|
+
// Apply a transform to trick Safari into thinking the input is at the top of the page
|
|
438
|
+
// so it doesn't try to scroll it into view. When tapping on an input, this needs to
|
|
439
|
+
// be done before the "focus" event, so we have to focus the element ourselves.
|
|
440
|
+
target.style.transform = 'translateY(-2000px)';
|
|
441
|
+
target.focus();
|
|
442
|
+
requestAnimationFrame(()=>{
|
|
443
|
+
target.style.transform = '';
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
let onFocus = (e)=>{
|
|
448
|
+
let target = e.target;
|
|
449
|
+
if (isInput(target)) {
|
|
450
|
+
// Transform also needs to be applied in the focus event in cases where focus moves
|
|
451
|
+
// other than tapping on an input directly, e.g. the next/previous buttons in the
|
|
452
|
+
// software keyboard. In these cases, it seems applying the transform in the focus event
|
|
453
|
+
// is good enough, whereas when tapping an input, it must be done before the focus event. 🤷♂️
|
|
454
|
+
target.style.transform = 'translateY(-2000px)';
|
|
455
|
+
requestAnimationFrame(()=>{
|
|
456
|
+
target.style.transform = '';
|
|
457
|
+
// This will have prevented the browser from scrolling the focused element into view,
|
|
458
|
+
// so we need to do this ourselves in a way that doesn't cause the whole page to scroll.
|
|
459
|
+
if (visualViewport) {
|
|
460
|
+
if (visualViewport.height < window.innerHeight) {
|
|
461
|
+
// If the keyboard is already visible, do this after one additional frame
|
|
462
|
+
// to wait for the transform to be removed.
|
|
463
|
+
requestAnimationFrame(()=>{
|
|
464
|
+
scrollIntoView(target);
|
|
465
|
+
});
|
|
466
|
+
} else {
|
|
467
|
+
// Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
468
|
+
// measure the correct position to scroll to.
|
|
469
|
+
visualViewport.addEventListener('resize', ()=>scrollIntoView(target), {
|
|
470
|
+
once: true
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
let onWindowScroll = ()=>{
|
|
478
|
+
// Last resort. If the window scrolled, scroll it back to the top.
|
|
479
|
+
// It should always be at the top because the body will have a negative margin (see below).
|
|
480
|
+
window.scrollTo(0, 0);
|
|
481
|
+
};
|
|
482
|
+
// Record the original scroll position so we can restore it.
|
|
483
|
+
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
484
|
+
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
485
|
+
let scrollX = window.pageXOffset;
|
|
486
|
+
let scrollY = window.pageYOffset;
|
|
487
|
+
let restoreStyles = chain(setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`));
|
|
488
|
+
// Scroll to the top. The negative margin on the body will make this appear the same.
|
|
489
|
+
window.scrollTo(0, 0);
|
|
490
|
+
let removeEvents = chain(addEvent(document, 'touchstart', onTouchStart, {
|
|
491
|
+
passive: false,
|
|
492
|
+
capture: true
|
|
493
|
+
}), addEvent(document, 'touchmove', onTouchMove, {
|
|
494
|
+
passive: false,
|
|
495
|
+
capture: true
|
|
496
|
+
}), addEvent(document, 'touchend', onTouchEnd, {
|
|
497
|
+
passive: false,
|
|
498
|
+
capture: true
|
|
499
|
+
}), addEvent(document, 'focus', onFocus, true), addEvent(window, 'scroll', onWindowScroll));
|
|
500
|
+
return ()=>{
|
|
501
|
+
// Restore styles and scroll the page back to where it was.
|
|
502
|
+
restoreStyles();
|
|
503
|
+
removeEvents();
|
|
504
|
+
window.scrollTo(scrollX, scrollY);
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
// Sets a CSS property on an element, and returns a function to revert it to the previous value.
|
|
508
|
+
function setStyle(element, style, value) {
|
|
509
|
+
// https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310
|
|
510
|
+
// @ts-ignore
|
|
511
|
+
let cur = element.style[style];
|
|
512
|
+
// @ts-ignore
|
|
513
|
+
element.style[style] = value;
|
|
514
|
+
return ()=>{
|
|
515
|
+
// @ts-ignore
|
|
516
|
+
element.style[style] = cur;
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
// Adds an event listener to an element, and returns a function to remove it.
|
|
520
|
+
function addEvent(target, event, handler, options) {
|
|
521
|
+
// @ts-ignore
|
|
522
|
+
target.addEventListener(event, handler, options);
|
|
523
|
+
return ()=>{
|
|
524
|
+
// @ts-ignore
|
|
525
|
+
target.removeEventListener(event, handler, options);
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
function scrollIntoView(target) {
|
|
529
|
+
let root = document.scrollingElement || document.documentElement;
|
|
530
|
+
while(target && target !== root){
|
|
531
|
+
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
532
|
+
let scrollable = getScrollParent(target);
|
|
533
|
+
if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== target) {
|
|
534
|
+
let scrollableTop = scrollable.getBoundingClientRect().top;
|
|
535
|
+
let targetTop = target.getBoundingClientRect().top;
|
|
536
|
+
let targetBottom = target.getBoundingClientRect().bottom;
|
|
537
|
+
// Buffer is needed for some edge cases
|
|
538
|
+
const keyboardHeight = scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER;
|
|
539
|
+
if (targetBottom > keyboardHeight) {
|
|
540
|
+
scrollable.scrollTop += targetTop - scrollableTop;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
// @ts-ignore
|
|
544
|
+
target = scrollable.parentElement;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
function isInput(target) {
|
|
548
|
+
return target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type) || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// This code comes from https://github.com/radix-ui/primitives/tree/main/packages/react/compose-refs
|
|
552
|
+
/**
|
|
553
|
+
* Set a given ref to a given value
|
|
554
|
+
* This utility takes care of different types of refs: callback refs and RefObject(s)
|
|
555
|
+
*/ function setRef(ref, value) {
|
|
556
|
+
if (typeof ref === 'function') {
|
|
557
|
+
ref(value);
|
|
558
|
+
} else if (ref !== null && ref !== undefined) {
|
|
559
|
+
ref.current = value;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* A utility to compose multiple refs together
|
|
564
|
+
* Accepts callback refs and RefObject(s)
|
|
565
|
+
*/ function composeRefs(...refs) {
|
|
566
|
+
return (node)=>refs.forEach((ref)=>setRef(ref, node));
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* A custom hook that composes multiple refs
|
|
570
|
+
* Accepts callback refs and RefObject(s)
|
|
571
|
+
*/ function useComposedRefs(...refs) {
|
|
572
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
573
|
+
return React.useCallback(composeRefs(...refs), refs);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// This code comes from https://github.com/radix-ui/primitives/blob/main/packages/react/use-controllable-state/src/useControllableState.tsx
|
|
577
|
+
function useCallbackRef(callback) {
|
|
578
|
+
const callbackRef = React__default.useRef(callback);
|
|
579
|
+
React__default.useEffect(()=>{
|
|
580
|
+
callbackRef.current = callback;
|
|
581
|
+
});
|
|
582
|
+
// https://github.com/facebook/react/issues/19240
|
|
583
|
+
return React__default.useMemo(()=>(...args)=>callbackRef.current == null ? void 0 : callbackRef.current.call(callbackRef, ...args), []);
|
|
584
|
+
}
|
|
585
|
+
function useUncontrolledState({ defaultProp, onChange }) {
|
|
586
|
+
const uncontrolledState = React__default.useState(defaultProp);
|
|
587
|
+
const [value] = uncontrolledState;
|
|
588
|
+
const prevValueRef = React__default.useRef(value);
|
|
589
|
+
const handleChange = useCallbackRef(onChange);
|
|
590
|
+
React__default.useEffect(()=>{
|
|
591
|
+
if (prevValueRef.current !== value) {
|
|
592
|
+
handleChange(value);
|
|
593
|
+
prevValueRef.current = value;
|
|
594
|
+
}
|
|
595
|
+
}, [
|
|
596
|
+
value,
|
|
597
|
+
prevValueRef,
|
|
598
|
+
handleChange
|
|
599
|
+
]);
|
|
600
|
+
return uncontrolledState;
|
|
601
|
+
}
|
|
602
|
+
function useControllableState({ prop, defaultProp, onChange = ()=>{} }) {
|
|
603
|
+
const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({
|
|
604
|
+
defaultProp,
|
|
605
|
+
onChange
|
|
606
|
+
});
|
|
607
|
+
const isControlled = prop !== undefined;
|
|
608
|
+
const value = isControlled ? prop : uncontrolledProp;
|
|
609
|
+
const handleChange = useCallbackRef(onChange);
|
|
610
|
+
const setValue = React__default.useCallback((nextValue)=>{
|
|
611
|
+
if (isControlled) {
|
|
612
|
+
const setter = nextValue;
|
|
613
|
+
const value = typeof nextValue === 'function' ? setter(prop) : nextValue;
|
|
614
|
+
if (value !== prop) handleChange(value);
|
|
615
|
+
} else {
|
|
616
|
+
setUncontrolledProp(nextValue);
|
|
617
|
+
}
|
|
618
|
+
}, [
|
|
619
|
+
isControlled,
|
|
620
|
+
prop,
|
|
621
|
+
setUncontrolledProp,
|
|
622
|
+
handleChange
|
|
623
|
+
]);
|
|
624
|
+
return [
|
|
625
|
+
value,
|
|
626
|
+
setValue
|
|
627
|
+
];
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function getDragDirectionMultiplier(direction) {
|
|
631
|
+
return direction === 'bottom' || direction === 'right' ? 1 : -1;
|
|
632
|
+
}
|
|
633
|
+
function getDraggedDistance({ pointerStart, currentPointer, direction }) {
|
|
634
|
+
return (pointerStart - currentPointer) * getDragDirectionMultiplier(direction);
|
|
635
|
+
}
|
|
636
|
+
function isDraggingTowardExpandedState(draggedDistance) {
|
|
637
|
+
return draggedDistance > 0;
|
|
638
|
+
}
|
|
639
|
+
function getDragPercentage({ draggedDistance, drawerDimension, snapPointPercentageDragged }) {
|
|
640
|
+
const absDraggedDistance = Math.abs(draggedDistance);
|
|
641
|
+
let percentageDragged = absDraggedDistance / drawerDimension;
|
|
642
|
+
if (snapPointPercentageDragged !== null) {
|
|
643
|
+
percentageDragged = snapPointPercentageDragged;
|
|
644
|
+
}
|
|
645
|
+
return {
|
|
646
|
+
absDraggedDistance,
|
|
647
|
+
percentageDragged
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
function isReleaseTowardExpandedState({ direction, distMoved }) {
|
|
651
|
+
return direction === 'bottom' || direction === 'right' ? distMoved > 0 : distMoved < 0;
|
|
652
|
+
}
|
|
653
|
+
function shouldCloseDrawerOnRelease({ velocity, velocityThreshold, swipeAmount, drawerDimension, closeThreshold }) {
|
|
654
|
+
if (velocity > velocityThreshold) {
|
|
655
|
+
return true;
|
|
656
|
+
}
|
|
657
|
+
return Math.abs(swipeAmount) >= drawerDimension * closeThreshold;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function isVerticalDirection(direction) {
|
|
661
|
+
return direction === 'top' || direction === 'bottom';
|
|
662
|
+
}
|
|
663
|
+
function toSnapPointNumber(snapPoint) {
|
|
664
|
+
return typeof snapPoint === 'string' ? parseInt(snapPoint, 10) : snapPoint;
|
|
665
|
+
}
|
|
666
|
+
function getActiveSnapPointIndex({ snapPoints, activeSnapPoint }) {
|
|
667
|
+
var _ref;
|
|
668
|
+
return (_ref = snapPoints == null ? void 0 : snapPoints.findIndex((snapPoint)=>snapPoint === activeSnapPoint)) != null ? _ref : null;
|
|
669
|
+
}
|
|
670
|
+
function getShouldFade({ snapPoints, fadeFromIndex, activeSnapPoint }) {
|
|
671
|
+
return snapPoints && snapPoints.length > 0 && (fadeFromIndex || fadeFromIndex === 0) && !Number.isNaN(fadeFromIndex) && snapPoints[fadeFromIndex] === activeSnapPoint || !snapPoints;
|
|
672
|
+
}
|
|
673
|
+
function getSnapPointOffset({ snapPoint, direction, containerSize }) {
|
|
674
|
+
const isPx = typeof snapPoint === 'string';
|
|
675
|
+
const snapPointAsNumber = toSnapPointNumber(snapPoint);
|
|
676
|
+
if (isVerticalDirection(direction)) {
|
|
677
|
+
const height = isPx ? snapPointAsNumber : snapPointAsNumber * containerSize.height;
|
|
678
|
+
return direction === 'bottom' ? containerSize.height - height : -containerSize.height + height;
|
|
679
|
+
}
|
|
680
|
+
const width = isPx ? snapPointAsNumber : snapPointAsNumber * containerSize.width;
|
|
681
|
+
return direction === 'right' ? containerSize.width - width : -containerSize.width + width;
|
|
682
|
+
}
|
|
683
|
+
function getSnapPointsOffset({ snapPoints, direction, containerSize }) {
|
|
684
|
+
var _ref;
|
|
685
|
+
return (_ref = snapPoints == null ? void 0 : snapPoints.map((snapPoint)=>getSnapPointOffset({
|
|
686
|
+
snapPoint,
|
|
687
|
+
direction,
|
|
688
|
+
containerSize
|
|
689
|
+
}))) != null ? _ref : [];
|
|
690
|
+
}
|
|
691
|
+
function getSnapDragValue({ activeSnapPointOffset, draggedDistance, direction }) {
|
|
692
|
+
return direction === 'bottom' || direction === 'right' ? activeSnapPointOffset - draggedDistance : activeSnapPointOffset + draggedDistance;
|
|
693
|
+
}
|
|
694
|
+
function getClosestSnapPoint({ snapPointsOffset, currentPosition }) {
|
|
695
|
+
return snapPointsOffset == null ? void 0 : snapPointsOffset.reduce((prev, curr)=>{
|
|
696
|
+
if (typeof prev !== 'number' || typeof curr !== 'number') return prev;
|
|
697
|
+
return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
function getSnapPointPercentageDragged({ snapPoints, activeSnapPointIndex, snapPointsOffset, fadeFromIndex, shouldFade, absDraggedDistance, isDraggingDown }) {
|
|
701
|
+
if (!snapPoints || typeof activeSnapPointIndex !== 'number' || !snapPointsOffset || fadeFromIndex === undefined) {
|
|
702
|
+
return null;
|
|
703
|
+
}
|
|
704
|
+
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
|
|
705
|
+
const isOverlaySnapPointOrHigher = activeSnapPointIndex >= fadeFromIndex;
|
|
706
|
+
if (isOverlaySnapPointOrHigher && isDraggingDown) {
|
|
707
|
+
return 0;
|
|
708
|
+
}
|
|
709
|
+
if (isOverlaySnapPoint && !isDraggingDown) return 1;
|
|
710
|
+
if (!shouldFade && !isOverlaySnapPoint) return null;
|
|
711
|
+
const targetSnapPointIndex = isOverlaySnapPoint ? activeSnapPointIndex + 1 : activeSnapPointIndex - 1;
|
|
712
|
+
const snapPointDistance = isOverlaySnapPoint ? snapPointsOffset[targetSnapPointIndex] - snapPointsOffset[targetSnapPointIndex - 1] : snapPointsOffset[targetSnapPointIndex + 1] - snapPointsOffset[targetSnapPointIndex];
|
|
713
|
+
const percentageDragged = absDraggedDistance / Math.abs(snapPointDistance);
|
|
714
|
+
return isOverlaySnapPoint ? 1 - percentageDragged : percentageDragged;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
function shouldPreventFocusOnRelease(velocity, threshold = 0.05) {
|
|
718
|
+
return velocity > threshold;
|
|
719
|
+
}
|
|
720
|
+
function getReleaseAction({ direction, distMoved, velocity, velocityThreshold, swipeAmount, drawerDimension, closeThreshold }) {
|
|
721
|
+
if (isReleaseTowardExpandedState({
|
|
722
|
+
direction,
|
|
723
|
+
distMoved
|
|
724
|
+
})) {
|
|
725
|
+
return 'reset';
|
|
726
|
+
}
|
|
727
|
+
if (shouldCloseDrawerOnRelease({
|
|
728
|
+
velocity,
|
|
729
|
+
velocityThreshold,
|
|
730
|
+
swipeAmount,
|
|
731
|
+
drawerDimension,
|
|
732
|
+
closeThreshold
|
|
733
|
+
})) {
|
|
734
|
+
return 'close';
|
|
735
|
+
}
|
|
736
|
+
return 'reset';
|
|
737
|
+
}
|
|
738
|
+
function getDismissibleReleaseResult({ direction, distMoved, velocity, velocityThreshold, swipeAmount, drawerDimension, closeThreshold, focusVelocityThreshold }) {
|
|
739
|
+
const action = getReleaseAction({
|
|
740
|
+
direction,
|
|
741
|
+
distMoved,
|
|
742
|
+
velocity,
|
|
743
|
+
velocityThreshold,
|
|
744
|
+
swipeAmount,
|
|
745
|
+
drawerDimension,
|
|
746
|
+
closeThreshold
|
|
747
|
+
});
|
|
748
|
+
return {
|
|
749
|
+
action,
|
|
750
|
+
shouldPreventFocus: shouldPreventFocusOnRelease(velocity, focusVelocityThreshold),
|
|
751
|
+
nextOpen: action !== 'close'
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
function getSnapPointReleaseAction({ fadeFromIndex, direction, activeSnapPointOffset, activeSnapPointIndex, snapPointsOffset, snapPointsCount, draggedDistance, velocity, dismissible, snapToSequentialPoint, velocityThreshold, highVelocityThreshold = 2, viewportSize }) {
|
|
755
|
+
if (fadeFromIndex === undefined || activeSnapPointOffset === null || snapPointsOffset.length === 0) {
|
|
756
|
+
return {
|
|
757
|
+
type: 'noop'
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
const currentPosition = direction === 'bottom' || direction === 'right' ? activeSnapPointOffset - draggedDistance : activeSnapPointOffset + draggedDistance;
|
|
761
|
+
const isFirst = activeSnapPointIndex === 0;
|
|
762
|
+
const hasDraggedTowardExpandedState = draggedDistance > 0;
|
|
763
|
+
const isLastSnapPoint = activeSnapPointIndex === snapPointsCount - 1;
|
|
764
|
+
if (!snapToSequentialPoint && velocity > highVelocityThreshold && !hasDraggedTowardExpandedState) {
|
|
765
|
+
if (dismissible) {
|
|
766
|
+
return {
|
|
767
|
+
type: 'close'
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
return {
|
|
771
|
+
type: 'snap',
|
|
772
|
+
targetOffset: snapPointsOffset[0]
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
if (!snapToSequentialPoint && velocity > highVelocityThreshold && hasDraggedTowardExpandedState) {
|
|
776
|
+
const lastOffset = snapPointsOffset[snapPointsCount - 1];
|
|
777
|
+
return typeof lastOffset === 'number' ? {
|
|
778
|
+
type: 'snap',
|
|
779
|
+
targetOffset: lastOffset
|
|
780
|
+
} : {
|
|
781
|
+
type: 'noop'
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
const closestSnapPoint = getClosestSnapPoint({
|
|
785
|
+
snapPointsOffset,
|
|
786
|
+
currentPosition
|
|
787
|
+
});
|
|
788
|
+
if (velocity > velocityThreshold && Math.abs(draggedDistance) < viewportSize * 0.4) {
|
|
789
|
+
const dragDirection = hasDraggedTowardExpandedState ? 1 : -1;
|
|
790
|
+
if (dragDirection > 0 && isLastSnapPoint) {
|
|
791
|
+
const lastOffset = snapPointsOffset[snapPointsCount - 1];
|
|
792
|
+
return typeof lastOffset === 'number' ? {
|
|
793
|
+
type: 'snap',
|
|
794
|
+
targetOffset: lastOffset
|
|
795
|
+
} : {
|
|
796
|
+
type: 'noop'
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
if (isFirst && dragDirection < 0 && dismissible) {
|
|
800
|
+
return {
|
|
801
|
+
type: 'close'
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
if (activeSnapPointIndex === null) {
|
|
805
|
+
return {
|
|
806
|
+
type: 'noop'
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
const nextOffset = snapPointsOffset[activeSnapPointIndex + dragDirection];
|
|
810
|
+
return typeof nextOffset === 'number' ? {
|
|
811
|
+
type: 'snap',
|
|
812
|
+
targetOffset: nextOffset
|
|
813
|
+
} : {
|
|
814
|
+
type: 'noop'
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
return typeof closestSnapPoint === 'number' ? {
|
|
818
|
+
type: 'snap',
|
|
819
|
+
targetOffset: closestSnapPoint
|
|
820
|
+
} : {
|
|
821
|
+
type: 'noop'
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
function useSnapPoints({ activeSnapPointProp, setActiveSnapPointProp, snapPoints, drawerRef, overlayRef, fadeFromIndex, onSnapPointChange, direction = 'bottom', container, snapToSequentialPoint }) {
|
|
826
|
+
const [activeSnapPoint, setActiveSnapPoint] = useControllableState({
|
|
827
|
+
prop: activeSnapPointProp,
|
|
828
|
+
defaultProp: snapPoints == null ? void 0 : snapPoints[0],
|
|
829
|
+
onChange: setActiveSnapPointProp
|
|
830
|
+
});
|
|
831
|
+
const [windowDimensions, setWindowDimensions] = React__default.useState(typeof window !== 'undefined' ? {
|
|
832
|
+
innerWidth: window.innerWidth,
|
|
833
|
+
innerHeight: window.innerHeight
|
|
834
|
+
} : undefined);
|
|
835
|
+
React__default.useEffect(()=>{
|
|
836
|
+
function onResize() {
|
|
837
|
+
setWindowDimensions({
|
|
838
|
+
innerWidth: window.innerWidth,
|
|
839
|
+
innerHeight: window.innerHeight
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
window.addEventListener('resize', onResize);
|
|
843
|
+
return ()=>window.removeEventListener('resize', onResize);
|
|
844
|
+
}, []);
|
|
845
|
+
const isLastSnapPoint = React__default.useMemo(()=>activeSnapPoint === (snapPoints == null ? void 0 : snapPoints[snapPoints.length - 1]) || null, [
|
|
846
|
+
snapPoints,
|
|
847
|
+
activeSnapPoint
|
|
848
|
+
]);
|
|
849
|
+
const activeSnapPointIndex = React__default.useMemo(()=>getActiveSnapPointIndex({
|
|
850
|
+
snapPoints,
|
|
851
|
+
activeSnapPoint
|
|
852
|
+
}), [
|
|
853
|
+
snapPoints,
|
|
854
|
+
activeSnapPoint
|
|
855
|
+
]);
|
|
856
|
+
const shouldFade = getShouldFade({
|
|
857
|
+
snapPoints,
|
|
858
|
+
fadeFromIndex,
|
|
859
|
+
activeSnapPoint
|
|
860
|
+
});
|
|
861
|
+
const snapPointsOffset = React__default.useMemo(()=>{
|
|
862
|
+
const containerSize = container ? {
|
|
863
|
+
width: container.getBoundingClientRect().width,
|
|
864
|
+
height: container.getBoundingClientRect().height
|
|
865
|
+
} : typeof window !== 'undefined' ? {
|
|
866
|
+
width: window.innerWidth,
|
|
867
|
+
height: window.innerHeight
|
|
868
|
+
} : {
|
|
869
|
+
width: 0,
|
|
870
|
+
height: 0
|
|
871
|
+
};
|
|
872
|
+
return getSnapPointsOffset({
|
|
873
|
+
snapPoints,
|
|
874
|
+
direction,
|
|
875
|
+
containerSize
|
|
876
|
+
});
|
|
877
|
+
}, [
|
|
878
|
+
snapPoints,
|
|
879
|
+
direction,
|
|
880
|
+
windowDimensions,
|
|
881
|
+
container
|
|
882
|
+
]);
|
|
883
|
+
const activeSnapPointOffset = React__default.useMemo(()=>activeSnapPointIndex !== null ? snapPointsOffset == null ? void 0 : snapPointsOffset[activeSnapPointIndex] : null, [
|
|
884
|
+
snapPointsOffset,
|
|
885
|
+
activeSnapPointIndex
|
|
886
|
+
]);
|
|
887
|
+
const snapToPoint = React__default.useCallback((dimension)=>{
|
|
888
|
+
var _ref;
|
|
889
|
+
const newSnapPointIndex = (_ref = snapPointsOffset == null ? void 0 : snapPointsOffset.findIndex((snapPointDim)=>snapPointDim === dimension)) != null ? _ref : null;
|
|
890
|
+
onSnapPointChange(newSnapPointIndex);
|
|
891
|
+
set(drawerRef.current, {
|
|
892
|
+
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
|
|
893
|
+
transform: isVertical(direction) ? `translate3d(0, ${dimension}px, 0)` : `translate3d(${dimension}px, 0, 0)`
|
|
894
|
+
});
|
|
895
|
+
if (snapPointsOffset && newSnapPointIndex !== snapPointsOffset.length - 1 && fadeFromIndex !== undefined && newSnapPointIndex !== fadeFromIndex && newSnapPointIndex < fadeFromIndex) {
|
|
896
|
+
set(overlayRef.current, {
|
|
897
|
+
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
|
|
898
|
+
opacity: '0'
|
|
899
|
+
});
|
|
900
|
+
} else {
|
|
901
|
+
set(overlayRef.current, {
|
|
902
|
+
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
|
|
903
|
+
opacity: '1'
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
setActiveSnapPoint(snapPoints == null ? void 0 : snapPoints[Math.max(newSnapPointIndex, 0)]);
|
|
907
|
+
}, [
|
|
908
|
+
drawerRef.current,
|
|
909
|
+
snapPoints,
|
|
910
|
+
snapPointsOffset,
|
|
911
|
+
fadeFromIndex,
|
|
912
|
+
overlayRef,
|
|
913
|
+
setActiveSnapPoint
|
|
914
|
+
]);
|
|
915
|
+
React__default.useEffect(()=>{
|
|
916
|
+
if (activeSnapPoint || activeSnapPointProp) {
|
|
917
|
+
var _ref;
|
|
918
|
+
const newIndex = (_ref = snapPoints == null ? void 0 : snapPoints.findIndex((snapPoint)=>snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint)) != null ? _ref : -1;
|
|
919
|
+
if (snapPointsOffset && newIndex !== -1 && typeof snapPointsOffset[newIndex] === 'number') {
|
|
920
|
+
snapToPoint(snapPointsOffset[newIndex]);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}, [
|
|
924
|
+
activeSnapPoint,
|
|
925
|
+
activeSnapPointProp,
|
|
926
|
+
snapPoints,
|
|
927
|
+
snapPointsOffset,
|
|
928
|
+
snapToPoint
|
|
929
|
+
]);
|
|
930
|
+
function onRelease({ draggedDistance, closeDrawer, velocity, dismissible }) {
|
|
931
|
+
var _ref;
|
|
932
|
+
if (fadeFromIndex === undefined) return;
|
|
933
|
+
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
|
|
934
|
+
if (isOverlaySnapPoint) {
|
|
935
|
+
set(overlayRef.current, {
|
|
936
|
+
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
|
|
940
|
+
const releaseAction = getSnapPointReleaseAction({
|
|
941
|
+
fadeFromIndex,
|
|
942
|
+
direction,
|
|
943
|
+
activeSnapPointOffset,
|
|
944
|
+
activeSnapPointIndex,
|
|
945
|
+
snapPointsOffset,
|
|
946
|
+
snapPointsCount: (_ref = snapPoints == null ? void 0 : snapPoints.length) != null ? _ref : 0,
|
|
947
|
+
draggedDistance,
|
|
948
|
+
velocity,
|
|
949
|
+
dismissible,
|
|
950
|
+
snapToSequentialPoint,
|
|
951
|
+
velocityThreshold: VELOCITY_THRESHOLD,
|
|
952
|
+
viewportSize: dim
|
|
953
|
+
});
|
|
954
|
+
if (releaseAction.type === 'close') {
|
|
955
|
+
closeDrawer();
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
if (releaseAction.type === 'snap') {
|
|
959
|
+
snapToPoint(releaseAction.targetOffset);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
function onDrag({ draggedDistance }) {
|
|
963
|
+
if (activeSnapPointOffset === null) return;
|
|
964
|
+
const newValue = getSnapDragValue({
|
|
965
|
+
activeSnapPointOffset,
|
|
966
|
+
draggedDistance,
|
|
967
|
+
direction
|
|
968
|
+
});
|
|
969
|
+
// Don't do anything if we exceed the last(biggest) snap point
|
|
970
|
+
if ((direction === 'bottom' || direction === 'right') && newValue < snapPointsOffset[snapPointsOffset.length - 1]) {
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
if ((direction === 'top' || direction === 'left') && newValue > snapPointsOffset[snapPointsOffset.length - 1]) {
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
set(drawerRef.current, {
|
|
977
|
+
transform: isVertical(direction) ? `translate3d(0, ${newValue}px, 0)` : `translate3d(${newValue}px, 0, 0)`
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
function getPercentageDragged(absDraggedDistance, isDraggingDown) {
|
|
981
|
+
return getSnapPointPercentageDragged({
|
|
982
|
+
snapPoints,
|
|
983
|
+
activeSnapPointIndex,
|
|
984
|
+
snapPointsOffset,
|
|
985
|
+
fadeFromIndex,
|
|
986
|
+
shouldFade,
|
|
987
|
+
absDraggedDistance,
|
|
988
|
+
isDraggingDown
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
return {
|
|
992
|
+
isLastSnapPoint,
|
|
993
|
+
activeSnapPoint,
|
|
994
|
+
shouldFade,
|
|
995
|
+
getPercentageDragged,
|
|
996
|
+
setActiveSnapPoint,
|
|
997
|
+
activeSnapPointIndex,
|
|
998
|
+
onRelease,
|
|
999
|
+
onDrag,
|
|
1000
|
+
snapPointsOffset
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
const noop = ()=>()=>{};
|
|
1005
|
+
function useScaleBackground() {
|
|
1006
|
+
const { direction, isOpen, shouldScaleBackground, setBackgroundColorOnScale, noBodyStyles } = useDrawerContext();
|
|
1007
|
+
const timeoutIdRef = React__default.useRef(null);
|
|
1008
|
+
const initialBackgroundColor = useMemo(()=>document.body.style.backgroundColor, []);
|
|
1009
|
+
function getScale() {
|
|
1010
|
+
return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
|
|
1011
|
+
}
|
|
1012
|
+
React__default.useEffect(()=>{
|
|
1013
|
+
if (isOpen && shouldScaleBackground) {
|
|
1014
|
+
if (timeoutIdRef.current) clearTimeout(timeoutIdRef.current);
|
|
1015
|
+
const wrapper = document.querySelector('[data-drawer-wrapper]');
|
|
1016
|
+
if (!wrapper) return;
|
|
1017
|
+
chain$1(setBackgroundColorOnScale && !noBodyStyles ? assignStyle(document.body, {
|
|
1018
|
+
background: 'black'
|
|
1019
|
+
}) : noop, assignStyle(wrapper, {
|
|
1020
|
+
transformOrigin: isVertical(direction) ? 'top' : 'left',
|
|
1021
|
+
transitionProperty: 'transform, border-radius',
|
|
1022
|
+
transitionDuration: `${TRANSITIONS.DURATION}s`,
|
|
1023
|
+
transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`
|
|
1024
|
+
}));
|
|
1025
|
+
const wrapperStylesCleanup = assignStyle(wrapper, {
|
|
1026
|
+
borderRadius: `${BORDER_RADIUS}px`,
|
|
1027
|
+
overflow: 'hidden',
|
|
1028
|
+
...isVertical(direction) ? {
|
|
1029
|
+
transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`
|
|
1030
|
+
} : {
|
|
1031
|
+
transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
return ()=>{
|
|
1035
|
+
wrapperStylesCleanup();
|
|
1036
|
+
timeoutIdRef.current = window.setTimeout(()=>{
|
|
1037
|
+
if (initialBackgroundColor) {
|
|
1038
|
+
document.body.style.background = initialBackgroundColor;
|
|
1039
|
+
} else {
|
|
1040
|
+
document.body.style.removeProperty('background');
|
|
1041
|
+
}
|
|
1042
|
+
}, TRANSITIONS.DURATION * 1000);
|
|
1043
|
+
};
|
|
1044
|
+
}
|
|
1045
|
+
}, [
|
|
1046
|
+
isOpen,
|
|
1047
|
+
shouldScaleBackground,
|
|
1048
|
+
initialBackgroundColor
|
|
1049
|
+
]);
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
let previousBodyPosition = null;
|
|
1053
|
+
/**
|
|
1054
|
+
* This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android).
|
|
1055
|
+
* 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.
|
|
1056
|
+
* It prevents several Safari viewport and toolbar edge cases that otherwise shift the page while the drawer is open.
|
|
1057
|
+
*/ function usePositionFixed({ isOpen, modal, nested, hasBeenOpened, preventScrollRestoration, noBodyStyles }) {
|
|
1058
|
+
const [activeUrl, setActiveUrl] = React__default.useState(()=>typeof window !== 'undefined' ? window.location.href : '');
|
|
1059
|
+
const scrollPos = React__default.useRef(0);
|
|
1060
|
+
const setPositionFixed = React__default.useCallback(()=>{
|
|
1061
|
+
// All browsers on iOS will return true here.
|
|
1062
|
+
if (!isSafari()) return;
|
|
1063
|
+
// If previousBodyPosition is already set, don't set it again.
|
|
1064
|
+
if (previousBodyPosition === null && isOpen && !noBodyStyles) {
|
|
1065
|
+
previousBodyPosition = {
|
|
1066
|
+
position: document.body.style.position,
|
|
1067
|
+
top: document.body.style.top,
|
|
1068
|
+
left: document.body.style.left,
|
|
1069
|
+
height: document.body.style.height,
|
|
1070
|
+
right: 'unset'
|
|
1071
|
+
};
|
|
1072
|
+
// Update the dom inside an animation frame
|
|
1073
|
+
const { scrollX, innerHeight } = window;
|
|
1074
|
+
document.body.style.setProperty('position', 'fixed', 'important');
|
|
1075
|
+
Object.assign(document.body.style, {
|
|
1076
|
+
top: `${-scrollPos.current}px`,
|
|
1077
|
+
left: `${-scrollX}px`,
|
|
1078
|
+
right: '0px',
|
|
1079
|
+
height: 'auto'
|
|
1080
|
+
});
|
|
1081
|
+
window.setTimeout(()=>window.requestAnimationFrame(()=>{
|
|
1082
|
+
// Attempt to check if the bottom bar appeared due to the position change
|
|
1083
|
+
const bottomBarHeight = innerHeight - window.innerHeight;
|
|
1084
|
+
if (bottomBarHeight && scrollPos.current >= innerHeight) {
|
|
1085
|
+
// Move the content further up so that the bottom bar doesn't hide it
|
|
1086
|
+
document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`;
|
|
1087
|
+
}
|
|
1088
|
+
}), 300);
|
|
1089
|
+
}
|
|
1090
|
+
}, [
|
|
1091
|
+
isOpen
|
|
1092
|
+
]);
|
|
1093
|
+
const restorePositionSetting = React__default.useCallback(()=>{
|
|
1094
|
+
// All browsers on iOS will return true here.
|
|
1095
|
+
if (!isSafari()) return;
|
|
1096
|
+
if (previousBodyPosition !== null && !noBodyStyles) {
|
|
1097
|
+
// Convert the position from "px" to Int
|
|
1098
|
+
const y = -parseInt(document.body.style.top, 10);
|
|
1099
|
+
const x = -parseInt(document.body.style.left, 10);
|
|
1100
|
+
// Restore styles
|
|
1101
|
+
Object.assign(document.body.style, previousBodyPosition);
|
|
1102
|
+
window.requestAnimationFrame(()=>{
|
|
1103
|
+
if (preventScrollRestoration && activeUrl !== window.location.href) {
|
|
1104
|
+
setActiveUrl(window.location.href);
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
window.scrollTo(x, y);
|
|
1108
|
+
});
|
|
1109
|
+
previousBodyPosition = null;
|
|
1110
|
+
}
|
|
1111
|
+
}, [
|
|
1112
|
+
activeUrl
|
|
1113
|
+
]);
|
|
1114
|
+
React__default.useEffect(()=>{
|
|
1115
|
+
function onScroll() {
|
|
1116
|
+
scrollPos.current = window.scrollY;
|
|
1117
|
+
}
|
|
1118
|
+
onScroll();
|
|
1119
|
+
window.addEventListener('scroll', onScroll);
|
|
1120
|
+
return ()=>{
|
|
1121
|
+
window.removeEventListener('scroll', onScroll);
|
|
1122
|
+
};
|
|
1123
|
+
}, []);
|
|
1124
|
+
React__default.useEffect(()=>{
|
|
1125
|
+
if (!modal) return;
|
|
1126
|
+
return ()=>{
|
|
1127
|
+
if (typeof document === 'undefined') return;
|
|
1128
|
+
// Another drawer is opened, safe to ignore the execution
|
|
1129
|
+
const hasDrawerOpened = !!document.querySelector('[data-drawer]');
|
|
1130
|
+
if (hasDrawerOpened) return;
|
|
1131
|
+
restorePositionSetting();
|
|
1132
|
+
};
|
|
1133
|
+
}, [
|
|
1134
|
+
modal,
|
|
1135
|
+
restorePositionSetting
|
|
1136
|
+
]);
|
|
1137
|
+
React__default.useEffect(()=>{
|
|
1138
|
+
if (nested || !hasBeenOpened) return;
|
|
1139
|
+
// This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening
|
|
1140
|
+
if (isOpen) {
|
|
1141
|
+
// avoid for standalone mode (PWA)
|
|
1142
|
+
const isStandalone = window.matchMedia('(display-mode: standalone)').matches;
|
|
1143
|
+
!isStandalone && setPositionFixed();
|
|
1144
|
+
if (!modal) {
|
|
1145
|
+
window.setTimeout(()=>{
|
|
1146
|
+
restorePositionSetting();
|
|
1147
|
+
}, 500);
|
|
1148
|
+
}
|
|
1149
|
+
} else {
|
|
1150
|
+
restorePositionSetting();
|
|
1151
|
+
}
|
|
1152
|
+
}, [
|
|
1153
|
+
isOpen,
|
|
1154
|
+
hasBeenOpened,
|
|
1155
|
+
activeUrl,
|
|
1156
|
+
modal,
|
|
1157
|
+
nested,
|
|
1158
|
+
setPositionFixed,
|
|
1159
|
+
restorePositionSetting
|
|
1160
|
+
]);
|
|
1161
|
+
return {
|
|
1162
|
+
restorePositionSetting
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
function isEqualArray(left, right) {
|
|
1167
|
+
return left.length === right.length && left.every((value, index)=>Object.is(value, right[index]));
|
|
1168
|
+
}
|
|
1169
|
+
function isEqualOptionValue(left, right) {
|
|
1170
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
1171
|
+
return isEqualArray(left, right);
|
|
1172
|
+
}
|
|
1173
|
+
return Object.is(left, right);
|
|
1174
|
+
}
|
|
1175
|
+
function areDrawerRuntimeOptionsEqual(left, right) {
|
|
1176
|
+
const leftKeys = Object.keys(left);
|
|
1177
|
+
const rightKeys = Object.keys(right);
|
|
1178
|
+
if (leftKeys.length !== rightKeys.length) {
|
|
1179
|
+
return false;
|
|
1180
|
+
}
|
|
1181
|
+
return leftKeys.every((key)=>isEqualOptionValue(left[key], right[key]));
|
|
1182
|
+
}
|
|
1183
|
+
function useDrawerRuntime(options) {
|
|
1184
|
+
const controllerRef = React__default.useRef();
|
|
1185
|
+
const appliedOptionsRef = React__default.useRef(options);
|
|
1186
|
+
if (!controllerRef.current) {
|
|
1187
|
+
controllerRef.current = createDrawerController(options);
|
|
1188
|
+
}
|
|
1189
|
+
const controller = controllerRef.current;
|
|
1190
|
+
const [snapshot, setSnapshot] = React__default.useState(()=>controller.getSnapshot());
|
|
1191
|
+
React__default.useEffect(()=>controller.subscribe(setSnapshot), [
|
|
1192
|
+
controller
|
|
1193
|
+
]);
|
|
1194
|
+
React__default.useEffect(()=>{
|
|
1195
|
+
if (areDrawerRuntimeOptionsEqual(appliedOptionsRef.current, options)) {
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
appliedOptionsRef.current = options;
|
|
1199
|
+
controller.patch(options);
|
|
1200
|
+
}, [
|
|
1201
|
+
controller,
|
|
1202
|
+
options
|
|
1203
|
+
]);
|
|
1204
|
+
return {
|
|
1205
|
+
controller,
|
|
1206
|
+
snapshot
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function getSwipeIntent({ delta, direction, threshold = 0, wasBeyondThePoint }) {
|
|
1211
|
+
if (wasBeyondThePoint) {
|
|
1212
|
+
return {
|
|
1213
|
+
isAllowed: true,
|
|
1214
|
+
reachedIntentBoundary: true
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
const deltaY = Math.abs(delta.y);
|
|
1218
|
+
const deltaX = Math.abs(delta.x);
|
|
1219
|
+
const isDeltaX = deltaX > deltaY;
|
|
1220
|
+
const directionFactor = direction === 'bottom' || direction === 'right' ? 1 : -1;
|
|
1221
|
+
if (direction === 'left' || direction === 'right') {
|
|
1222
|
+
const isReverseDirection = delta.x * directionFactor < 0;
|
|
1223
|
+
if (!isReverseDirection && deltaX >= 0 && deltaX <= threshold) {
|
|
1224
|
+
return {
|
|
1225
|
+
isAllowed: isDeltaX,
|
|
1226
|
+
reachedIntentBoundary: false
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
} else {
|
|
1230
|
+
const isReverseDirection = delta.y * directionFactor < 0;
|
|
1231
|
+
if (!isReverseDirection && deltaY >= 0 && deltaY <= threshold) {
|
|
1232
|
+
return {
|
|
1233
|
+
isAllowed: !isDeltaX,
|
|
1234
|
+
reachedIntentBoundary: false
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return {
|
|
1239
|
+
isAllowed: true,
|
|
1240
|
+
reachedIntentBoundary: true
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
function getKeyboardOpenState({ previousDiffFromInitial, diffFromInitial, keyboardIsOpen, threshold = 60 }) {
|
|
1245
|
+
if (Math.abs(previousDiffFromInitial - diffFromInitial) > threshold) {
|
|
1246
|
+
return !keyboardIsOpen;
|
|
1247
|
+
}
|
|
1248
|
+
return keyboardIsOpen;
|
|
1249
|
+
}
|
|
1250
|
+
function getViewportDrivenDrawerLayout({ visualViewportHeight, totalHeight, drawerHeight, offsetFromTop, fixed, previousDiffFromInitial, keyboardIsOpen, initialDrawerHeight, activeSnapPointOffset, isMobileFirefox, windowTopOffset }) {
|
|
1251
|
+
let diffFromInitial = totalHeight - visualViewportHeight;
|
|
1252
|
+
const isTallEnough = drawerHeight > totalHeight * 0.8;
|
|
1253
|
+
const nextInitialDrawerHeight = initialDrawerHeight || drawerHeight;
|
|
1254
|
+
if (typeof activeSnapPointOffset === 'number') {
|
|
1255
|
+
diffFromInitial += activeSnapPointOffset;
|
|
1256
|
+
}
|
|
1257
|
+
const nextKeyboardIsOpen = getKeyboardOpenState({
|
|
1258
|
+
previousDiffFromInitial,
|
|
1259
|
+
diffFromInitial,
|
|
1260
|
+
keyboardIsOpen
|
|
1261
|
+
});
|
|
1262
|
+
let height = null;
|
|
1263
|
+
if (drawerHeight > visualViewportHeight || nextKeyboardIsOpen) {
|
|
1264
|
+
let newDrawerHeight = drawerHeight;
|
|
1265
|
+
if (drawerHeight > visualViewportHeight) {
|
|
1266
|
+
newDrawerHeight = visualViewportHeight - (isTallEnough ? offsetFromTop : windowTopOffset);
|
|
1267
|
+
}
|
|
1268
|
+
height = fixed ? `${drawerHeight - Math.max(diffFromInitial, 0)}px` : `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
|
|
1269
|
+
} else if (!isMobileFirefox) {
|
|
1270
|
+
height = `${nextInitialDrawerHeight}px`;
|
|
1271
|
+
}
|
|
1272
|
+
const bottom = activeSnapPointOffset !== undefined && !nextKeyboardIsOpen ? '0px' : `${Math.max(diffFromInitial, 0)}px`;
|
|
1273
|
+
return {
|
|
1274
|
+
diffFromInitial,
|
|
1275
|
+
nextKeyboardIsOpen,
|
|
1276
|
+
nextInitialDrawerHeight,
|
|
1277
|
+
height,
|
|
1278
|
+
bottom
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
function getNextHandleState({ isDragging, preventCycle, shouldCancelInteraction, snapPoints, activeSnapPoint, dismissible }) {
|
|
1283
|
+
var _snapPoints_;
|
|
1284
|
+
if (isDragging || preventCycle || shouldCancelInteraction) {
|
|
1285
|
+
return {
|
|
1286
|
+
type: 'noop'
|
|
1287
|
+
};
|
|
1288
|
+
}
|
|
1289
|
+
if (!snapPoints || snapPoints.length === 0) {
|
|
1290
|
+
return dismissible ? {
|
|
1291
|
+
type: 'noop'
|
|
1292
|
+
} : {
|
|
1293
|
+
type: 'close'
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
const isLastSnapPoint = activeSnapPoint === snapPoints[snapPoints.length - 1];
|
|
1297
|
+
if (isLastSnapPoint && dismissible) {
|
|
1298
|
+
return {
|
|
1299
|
+
type: 'close'
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
const currentSnapIndex = snapPoints.findIndex((point)=>point === activeSnapPoint);
|
|
1303
|
+
if (currentSnapIndex === -1) {
|
|
1304
|
+
return {
|
|
1305
|
+
type: 'noop'
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
return {
|
|
1309
|
+
type: 'snap',
|
|
1310
|
+
snapPoint: (_snapPoints_ = snapPoints[currentSnapIndex + 1]) != null ? _snapPoints_ : null
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeSinceOpenMs, swipeAmount, hasHighlightedText, timeSinceLastPreventedMs, scrollLockTimeout, isDraggingInDirection, ancestors }) {
|
|
1315
|
+
if (targetTagName === 'SELECT' || hasNoDragAttribute) {
|
|
1316
|
+
return {
|
|
1317
|
+
allow: false,
|
|
1318
|
+
updatePreventedAt: false
|
|
1319
|
+
};
|
|
1320
|
+
}
|
|
1321
|
+
if (direction === 'left' || direction === 'right') {
|
|
1322
|
+
return {
|
|
1323
|
+
allow: true,
|
|
1324
|
+
updatePreventedAt: false
|
|
1325
|
+
};
|
|
1326
|
+
}
|
|
1327
|
+
if (timeSinceOpenMs !== null && timeSinceOpenMs < 500) {
|
|
1328
|
+
return {
|
|
1329
|
+
allow: false,
|
|
1330
|
+
updatePreventedAt: false
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
if (swipeAmount !== null) {
|
|
1334
|
+
const isClosingSwipeOffset = direction === 'bottom' ? swipeAmount > 0 : swipeAmount < 0;
|
|
1335
|
+
if (isClosingSwipeOffset) {
|
|
1336
|
+
return {
|
|
1337
|
+
allow: true,
|
|
1338
|
+
updatePreventedAt: false
|
|
1339
|
+
};
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
if (hasHighlightedText) {
|
|
1343
|
+
return {
|
|
1344
|
+
allow: false,
|
|
1345
|
+
updatePreventedAt: false
|
|
1346
|
+
};
|
|
1347
|
+
}
|
|
1348
|
+
if (timeSinceLastPreventedMs !== null && timeSinceLastPreventedMs < scrollLockTimeout && swipeAmount === 0) {
|
|
1349
|
+
return {
|
|
1350
|
+
allow: false,
|
|
1351
|
+
updatePreventedAt: true
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
if (isDraggingInDirection) {
|
|
1355
|
+
return {
|
|
1356
|
+
allow: false,
|
|
1357
|
+
updatePreventedAt: true
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
for (const ancestor of ancestors){
|
|
1361
|
+
if (ancestor.scrollHeight > ancestor.clientHeight) {
|
|
1362
|
+
if (ancestor.scrollTop !== 0) {
|
|
1363
|
+
return {
|
|
1364
|
+
allow: false,
|
|
1365
|
+
updatePreventedAt: true
|
|
1366
|
+
};
|
|
1367
|
+
}
|
|
1368
|
+
if (ancestor.role === 'dialog') {
|
|
1369
|
+
return {
|
|
1370
|
+
allow: true,
|
|
1371
|
+
updatePreventedAt: false
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
return {
|
|
1377
|
+
allow: true,
|
|
1378
|
+
updatePreventedAt: false
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
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 }) {
|
|
1383
|
+
var _drawerRef_current, _drawerRef_current1;
|
|
1384
|
+
const [isOpen = false, setIsOpen] = useControllableState({
|
|
1385
|
+
defaultProp: defaultOpen,
|
|
1386
|
+
prop: openProp,
|
|
1387
|
+
onChange: (o)=>{
|
|
1388
|
+
onOpenChange == null ? void 0 : onOpenChange(o);
|
|
1389
|
+
if (!o && !nested) {
|
|
1390
|
+
restorePositionSetting();
|
|
1391
|
+
}
|
|
1392
|
+
setTimeout(()=>{
|
|
1393
|
+
onAnimationEnd == null ? void 0 : onAnimationEnd(o);
|
|
1394
|
+
}, TRANSITIONS.DURATION * 1000);
|
|
1395
|
+
if (o && !modal) {
|
|
1396
|
+
if (typeof window !== 'undefined') {
|
|
1397
|
+
window.requestAnimationFrame(()=>{
|
|
1398
|
+
document.body.style.pointerEvents = 'auto';
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
if (!o) {
|
|
1403
|
+
// This will be removed when the exit animation ends (`500ms`)
|
|
1404
|
+
document.body.style.pointerEvents = 'auto';
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
});
|
|
1408
|
+
const [hasBeenOpened, setHasBeenOpened] = React__default.useState(false);
|
|
1409
|
+
const [isDragging, setIsDragging] = React__default.useState(false);
|
|
1410
|
+
const [justReleased, setJustReleased] = React__default.useState(false);
|
|
1411
|
+
const overlayRef = React__default.useRef(null);
|
|
1412
|
+
const openTime = React__default.useRef(null);
|
|
1413
|
+
const dragStartTime = React__default.useRef(null);
|
|
1414
|
+
const dragEndTime = React__default.useRef(null);
|
|
1415
|
+
const lastTimeDragPrevented = React__default.useRef(null);
|
|
1416
|
+
const isAllowedToDrag = React__default.useRef(false);
|
|
1417
|
+
const nestedOpenChangeTimer = React__default.useRef(null);
|
|
1418
|
+
const pointerStart = React__default.useRef(0);
|
|
1419
|
+
const keyboardIsOpen = React__default.useRef(false);
|
|
1420
|
+
const shouldAnimate = React__default.useRef(!defaultOpen);
|
|
1421
|
+
const previousDiffFromInitial = React__default.useRef(0);
|
|
1422
|
+
const drawerRef = React__default.useRef(null);
|
|
1423
|
+
const drawerHeightRef = React__default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
|
|
1424
|
+
const drawerWidthRef = React__default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
|
|
1425
|
+
const initialDrawerHeight = React__default.useRef(0);
|
|
1426
|
+
const onSnapPointChange = React__default.useCallback((activeSnapPointIndex)=>{
|
|
1427
|
+
// Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
|
|
1428
|
+
if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
|
|
1429
|
+
}, []);
|
|
1430
|
+
const { activeSnapPoint, activeSnapPointIndex, setActiveSnapPoint, onRelease: onReleaseSnapPoints, snapPointsOffset, onDrag: onDragSnapPoints, shouldFade, getPercentageDragged: getSnapPointsPercentageDragged } = useSnapPoints({
|
|
1431
|
+
snapPoints,
|
|
1432
|
+
activeSnapPointProp,
|
|
1433
|
+
setActiveSnapPointProp,
|
|
1434
|
+
drawerRef,
|
|
1435
|
+
fadeFromIndex,
|
|
1436
|
+
overlayRef,
|
|
1437
|
+
onSnapPointChange,
|
|
1438
|
+
direction,
|
|
1439
|
+
container,
|
|
1440
|
+
snapToSequentialPoint
|
|
1441
|
+
});
|
|
1442
|
+
usePreventScroll({
|
|
1443
|
+
isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened || !repositionInputs || !disablePreventScroll
|
|
1444
|
+
});
|
|
1445
|
+
const { restorePositionSetting } = usePositionFixed({
|
|
1446
|
+
isOpen,
|
|
1447
|
+
modal,
|
|
1448
|
+
nested: nested != null ? nested : false,
|
|
1449
|
+
hasBeenOpened,
|
|
1450
|
+
preventScrollRestoration,
|
|
1451
|
+
noBodyStyles
|
|
1452
|
+
});
|
|
1453
|
+
const { controller, snapshot: runtimeSnapshot } = useDrawerRuntime({
|
|
1454
|
+
open: isOpen,
|
|
1455
|
+
defaultOpen,
|
|
1456
|
+
dismissible,
|
|
1457
|
+
modal,
|
|
1458
|
+
nested,
|
|
1459
|
+
direction,
|
|
1460
|
+
snapPoints,
|
|
1461
|
+
fadeFromIndex,
|
|
1462
|
+
activeSnapPoint,
|
|
1463
|
+
closeThreshold,
|
|
1464
|
+
scrollLockTimeout,
|
|
1465
|
+
shouldScaleBackground,
|
|
1466
|
+
setBackgroundColorOnScale,
|
|
1467
|
+
handleOnly,
|
|
1468
|
+
fixed,
|
|
1469
|
+
disablePreventScroll,
|
|
1470
|
+
repositionInputs,
|
|
1471
|
+
snapToSequentialPoint,
|
|
1472
|
+
preventScrollRestoration,
|
|
1473
|
+
noBodyStyles,
|
|
1474
|
+
autoFocus
|
|
1475
|
+
});
|
|
1476
|
+
function getScale() {
|
|
1477
|
+
return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
|
|
1478
|
+
}
|
|
1479
|
+
function onPress(event) {
|
|
1480
|
+
var _drawerRef_current, _drawerRef_current1;
|
|
1481
|
+
if (!dismissible && !snapPoints) return;
|
|
1482
|
+
if (drawerRef.current && !drawerRef.current.contains(event.target)) return;
|
|
1483
|
+
drawerHeightRef.current = ((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0;
|
|
1484
|
+
drawerWidthRef.current = ((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0;
|
|
1485
|
+
setIsDragging(true);
|
|
1486
|
+
dragStartTime.current = new Date();
|
|
1487
|
+
// iOS doesn't trigger mouseUp after scrolling so we need to listen to touched in order to disallow dragging
|
|
1488
|
+
if (isIOS()) {
|
|
1489
|
+
window.addEventListener('touchend', ()=>isAllowedToDrag.current = false, {
|
|
1490
|
+
once: true
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
// Ensure we maintain correct pointer capture even when going outside of the drawer
|
|
1494
|
+
event.target.setPointerCapture(event.pointerId);
|
|
1495
|
+
pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
|
|
1496
|
+
}
|
|
1497
|
+
function shouldDrag(el, isDraggingInDirection) {
|
|
1498
|
+
var _window_getSelection;
|
|
1499
|
+
let element = el;
|
|
1500
|
+
const swipeAmount = drawerRef.current ? getTranslate(drawerRef.current, direction) : null;
|
|
1501
|
+
const date = new Date();
|
|
1502
|
+
const ancestors = [];
|
|
1503
|
+
// Keep climbing up the DOM tree as long as there's a parent
|
|
1504
|
+
while(element){
|
|
1505
|
+
ancestors.push({
|
|
1506
|
+
scrollHeight: element.scrollHeight,
|
|
1507
|
+
clientHeight: element.clientHeight,
|
|
1508
|
+
scrollTop: element.scrollTop,
|
|
1509
|
+
role: element.getAttribute('role')
|
|
1510
|
+
});
|
|
1511
|
+
// Move up to the parent element
|
|
1512
|
+
element = element.parentNode;
|
|
1513
|
+
}
|
|
1514
|
+
const result = getDragPermission({
|
|
1515
|
+
targetTagName: el.tagName,
|
|
1516
|
+
hasNoDragAttribute: el.hasAttribute('data-drawer-no-drag') || Boolean(el.closest('[data-drawer-no-drag]')),
|
|
1517
|
+
direction,
|
|
1518
|
+
timeSinceOpenMs: openTime.current ? date.getTime() - openTime.current.getTime() : null,
|
|
1519
|
+
swipeAmount,
|
|
1520
|
+
hasHighlightedText: Boolean((_window_getSelection = window.getSelection()) == null ? void 0 : _window_getSelection.toString()),
|
|
1521
|
+
timeSinceLastPreventedMs: lastTimeDragPrevented.current ? date.getTime() - lastTimeDragPrevented.current.getTime() : null,
|
|
1522
|
+
scrollLockTimeout,
|
|
1523
|
+
isDraggingInDirection,
|
|
1524
|
+
ancestors
|
|
1525
|
+
});
|
|
1526
|
+
if (result.updatePreventedAt) {
|
|
1527
|
+
lastTimeDragPrevented.current = date;
|
|
1528
|
+
}
|
|
1529
|
+
return result.allow;
|
|
1530
|
+
}
|
|
1531
|
+
function onDrag(event) {
|
|
1532
|
+
if (!drawerRef.current) {
|
|
1533
|
+
return;
|
|
1534
|
+
}
|
|
1535
|
+
// We need to know how much of the drawer has been dragged in percentages so that we can transform background accordingly
|
|
1536
|
+
if (isDragging) {
|
|
1537
|
+
const draggedDistance = getDraggedDistance({
|
|
1538
|
+
pointerStart: pointerStart.current,
|
|
1539
|
+
currentPointer: isVertical(direction) ? event.pageY : event.pageX,
|
|
1540
|
+
direction
|
|
1541
|
+
});
|
|
1542
|
+
const isDraggingInDirection = isDraggingTowardExpandedState(draggedDistance);
|
|
1543
|
+
// Pre condition for disallowing dragging in the close direction.
|
|
1544
|
+
const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection;
|
|
1545
|
+
// Disallow dragging down to close when first snap point is the active one and dismissible prop is set to false.
|
|
1546
|
+
if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) return;
|
|
1547
|
+
// We need to capture last time when drag with scroll was triggered and have a timeout between
|
|
1548
|
+
const absDraggedDistance = Math.abs(draggedDistance);
|
|
1549
|
+
const wrapper = document.querySelector('[data-drawer-wrapper]');
|
|
1550
|
+
const drawerDimension = direction === 'bottom' || direction === 'top' ? drawerHeightRef.current : drawerWidthRef.current;
|
|
1551
|
+
// Calculate the percentage dragged, where 1 is the closed position
|
|
1552
|
+
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection);
|
|
1553
|
+
const { percentageDragged } = getDragPercentage({
|
|
1554
|
+
draggedDistance,
|
|
1555
|
+
drawerDimension,
|
|
1556
|
+
snapPointPercentageDragged
|
|
1557
|
+
});
|
|
1558
|
+
// Disallow close dragging beyond the smallest snap point.
|
|
1559
|
+
if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
if (!isAllowedToDrag.current && !shouldDrag(event.target, isDraggingInDirection)) return;
|
|
1563
|
+
drawerRef.current.classList.add(DRAG_CLASS);
|
|
1564
|
+
// 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
|
|
1565
|
+
isAllowedToDrag.current = true;
|
|
1566
|
+
set(drawerRef.current, {
|
|
1567
|
+
transition: 'none'
|
|
1568
|
+
});
|
|
1569
|
+
set(overlayRef.current, {
|
|
1570
|
+
transition: 'none'
|
|
1571
|
+
});
|
|
1572
|
+
if (snapPoints) {
|
|
1573
|
+
onDragSnapPoints({
|
|
1574
|
+
draggedDistance
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
// Run this only if snapPoints are not defined or if we are at the last snap point (highest one)
|
|
1578
|
+
if (isDraggingInDirection && !snapPoints) {
|
|
1579
|
+
const dampenedDraggedDistance = dampenValue(draggedDistance);
|
|
1580
|
+
const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * (direction === 'bottom' || direction === 'right' ? 1 : -1);
|
|
1581
|
+
set(drawerRef.current, {
|
|
1582
|
+
transform: getAxisAwareTranslate(direction, translateValue)
|
|
1583
|
+
});
|
|
1584
|
+
return;
|
|
1585
|
+
}
|
|
1586
|
+
const opacityValue = 1 - percentageDragged;
|
|
1587
|
+
if (shouldFade || fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1) {
|
|
1588
|
+
onDragProp == null ? void 0 : onDragProp(event, percentageDragged);
|
|
1589
|
+
set(overlayRef.current, {
|
|
1590
|
+
opacity: `${opacityValue}`,
|
|
1591
|
+
transition: 'none'
|
|
1592
|
+
}, true);
|
|
1593
|
+
}
|
|
1594
|
+
if (wrapper && overlayRef.current && shouldScaleBackground) {
|
|
1595
|
+
const { scaleValue, borderRadiusValue, translateValue } = getBackgroundDragState({
|
|
1596
|
+
baseScale: getScale(),
|
|
1597
|
+
percentageDragged
|
|
1598
|
+
});
|
|
1599
|
+
set(wrapper, {
|
|
1600
|
+
borderRadius: `${borderRadiusValue}px`,
|
|
1601
|
+
transform: getScaleTranslateTransform({
|
|
1602
|
+
direction,
|
|
1603
|
+
scale: scaleValue,
|
|
1604
|
+
translate: `${translateValue}px`
|
|
1605
|
+
}),
|
|
1606
|
+
transition: 'none'
|
|
1607
|
+
}, true);
|
|
1608
|
+
}
|
|
1609
|
+
if (!snapPoints) {
|
|
1610
|
+
const translateValue = absDraggedDistance * (direction === 'bottom' || direction === 'right' ? 1 : -1);
|
|
1611
|
+
set(drawerRef.current, {
|
|
1612
|
+
transform: getAxisAwareTranslate(direction, translateValue)
|
|
1613
|
+
});
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
React__default.useEffect(()=>{
|
|
1618
|
+
window.requestAnimationFrame(()=>{
|
|
1619
|
+
shouldAnimate.current = true;
|
|
1620
|
+
});
|
|
1621
|
+
}, []);
|
|
1622
|
+
React__default.useEffect(()=>{
|
|
1623
|
+
var _window_visualViewport;
|
|
1624
|
+
function onVisualViewportChange() {
|
|
1625
|
+
if (!drawerRef.current || !repositionInputs) return;
|
|
1626
|
+
const focusedElement = document.activeElement;
|
|
1627
|
+
if (isInput(focusedElement) || keyboardIsOpen.current) {
|
|
1628
|
+
var _window_visualViewport;
|
|
1629
|
+
const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0;
|
|
1630
|
+
const offsetFromTop = drawerRef.current.getBoundingClientRect().top;
|
|
1631
|
+
const activeSnapPointOffset = snapPoints && snapPoints.length > 0 && snapPointsOffset && activeSnapPointIndex ? snapPointsOffset[activeSnapPointIndex] || 0 : undefined;
|
|
1632
|
+
const layout = getViewportDrivenDrawerLayout({
|
|
1633
|
+
visualViewportHeight: ((_window_visualViewport = window.visualViewport) == null ? void 0 : _window_visualViewport.height) || 0,
|
|
1634
|
+
totalHeight: window.innerHeight,
|
|
1635
|
+
drawerHeight,
|
|
1636
|
+
offsetFromTop,
|
|
1637
|
+
fixed,
|
|
1638
|
+
previousDiffFromInitial: previousDiffFromInitial.current,
|
|
1639
|
+
keyboardIsOpen: keyboardIsOpen.current,
|
|
1640
|
+
initialDrawerHeight: initialDrawerHeight.current,
|
|
1641
|
+
activeSnapPointOffset,
|
|
1642
|
+
isMobileFirefox: Boolean(isMobileFirefox()),
|
|
1643
|
+
windowTopOffset: WINDOW_TOP_OFFSET
|
|
1644
|
+
});
|
|
1645
|
+
initialDrawerHeight.current = layout.nextInitialDrawerHeight;
|
|
1646
|
+
previousDiffFromInitial.current = layout.diffFromInitial;
|
|
1647
|
+
keyboardIsOpen.current = layout.nextKeyboardIsOpen;
|
|
1648
|
+
if (layout.height !== null) {
|
|
1649
|
+
drawerRef.current.style.height = layout.height;
|
|
1650
|
+
}
|
|
1651
|
+
drawerRef.current.style.bottom = layout.bottom;
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
(_window_visualViewport = window.visualViewport) == null ? void 0 : _window_visualViewport.addEventListener('resize', onVisualViewportChange);
|
|
1655
|
+
return ()=>{
|
|
1656
|
+
var _window_visualViewport;
|
|
1657
|
+
return (_window_visualViewport = window.visualViewport) == null ? void 0 : _window_visualViewport.removeEventListener('resize', onVisualViewportChange);
|
|
1658
|
+
};
|
|
1659
|
+
}, [
|
|
1660
|
+
activeSnapPointIndex,
|
|
1661
|
+
snapPoints,
|
|
1662
|
+
snapPointsOffset
|
|
1663
|
+
]);
|
|
1664
|
+
function closeDrawer(fromWithin) {
|
|
1665
|
+
cancelDrag();
|
|
1666
|
+
onClose == null ? void 0 : onClose();
|
|
1667
|
+
if (!fromWithin) {
|
|
1668
|
+
setIsOpen(false);
|
|
1669
|
+
}
|
|
1670
|
+
setTimeout(()=>{
|
|
1671
|
+
if (snapPoints) {
|
|
1672
|
+
setActiveSnapPoint(snapPoints[0]);
|
|
1673
|
+
}
|
|
1674
|
+
}, TRANSITIONS.DURATION * 1000); // seconds to ms
|
|
1675
|
+
}
|
|
1676
|
+
function resetDrawer() {
|
|
1677
|
+
if (!drawerRef.current) return;
|
|
1678
|
+
const wrapper = document.querySelector('[data-drawer-wrapper]');
|
|
1679
|
+
const currentSwipeAmount = getTranslate(drawerRef.current, direction);
|
|
1680
|
+
set(drawerRef.current, {
|
|
1681
|
+
transform: 'translate3d(0, 0, 0)',
|
|
1682
|
+
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`
|
|
1683
|
+
});
|
|
1684
|
+
set(overlayRef.current, {
|
|
1685
|
+
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
|
|
1686
|
+
opacity: '1'
|
|
1687
|
+
});
|
|
1688
|
+
// Don't reset background if swiped upwards
|
|
1689
|
+
if (shouldScaleBackground && currentSwipeAmount && currentSwipeAmount > 0 && isOpen) {
|
|
1690
|
+
const backgroundResetState = getBackgroundResetState({
|
|
1691
|
+
direction,
|
|
1692
|
+
baseScale: getScale()
|
|
1693
|
+
});
|
|
1694
|
+
set(wrapper, {
|
|
1695
|
+
...backgroundResetState,
|
|
1696
|
+
transitionProperty: 'transform, border-radius',
|
|
1697
|
+
transitionDuration: `${TRANSITIONS.DURATION}s`,
|
|
1698
|
+
transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`
|
|
1699
|
+
}, true);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
function cancelDrag() {
|
|
1703
|
+
if (!isDragging || !drawerRef.current) return;
|
|
1704
|
+
drawerRef.current.classList.remove(DRAG_CLASS);
|
|
1705
|
+
isAllowedToDrag.current = false;
|
|
1706
|
+
setIsDragging(false);
|
|
1707
|
+
dragEndTime.current = new Date();
|
|
1708
|
+
}
|
|
1709
|
+
function onRelease(event) {
|
|
1710
|
+
var _drawerRef_current_getBoundingClientRect_height, _drawerRef_current_getBoundingClientRect_width;
|
|
1711
|
+
if (!isDragging || !drawerRef.current) return;
|
|
1712
|
+
drawerRef.current.classList.remove(DRAG_CLASS);
|
|
1713
|
+
isAllowedToDrag.current = false;
|
|
1714
|
+
setIsDragging(false);
|
|
1715
|
+
dragEndTime.current = new Date();
|
|
1716
|
+
const swipeAmount = getTranslate(drawerRef.current, direction);
|
|
1717
|
+
if (!event || !shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount)) return;
|
|
1718
|
+
if (dragStartTime.current === null) return;
|
|
1719
|
+
const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime();
|
|
1720
|
+
const distMoved = pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX);
|
|
1721
|
+
const velocity = Math.abs(distMoved) / timeTaken;
|
|
1722
|
+
if (snapPoints) {
|
|
1723
|
+
const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1;
|
|
1724
|
+
onReleaseSnapPoints({
|
|
1725
|
+
draggedDistance: distMoved * directionMultiplier,
|
|
1726
|
+
closeDrawer,
|
|
1727
|
+
velocity,
|
|
1728
|
+
dismissible
|
|
1729
|
+
});
|
|
1730
|
+
onReleaseProp == null ? void 0 : onReleaseProp(event, true);
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
const visibleDrawerHeight = Math.min((_drawerRef_current_getBoundingClientRect_height = drawerRef.current.getBoundingClientRect().height) != null ? _drawerRef_current_getBoundingClientRect_height : 0, window.innerHeight);
|
|
1734
|
+
const visibleDrawerWidth = Math.min((_drawerRef_current_getBoundingClientRect_width = drawerRef.current.getBoundingClientRect().width) != null ? _drawerRef_current_getBoundingClientRect_width : 0, window.innerWidth);
|
|
1735
|
+
const releaseResult = getDismissibleReleaseResult({
|
|
1736
|
+
direction,
|
|
1737
|
+
distMoved,
|
|
1738
|
+
velocity,
|
|
1739
|
+
velocityThreshold: VELOCITY_THRESHOLD,
|
|
1740
|
+
swipeAmount,
|
|
1741
|
+
drawerDimension: direction === 'left' || direction === 'right' ? visibleDrawerWidth : visibleDrawerHeight,
|
|
1742
|
+
closeThreshold
|
|
1743
|
+
});
|
|
1744
|
+
if (releaseResult.shouldPreventFocus) {
|
|
1745
|
+
// `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.
|
|
1746
|
+
setJustReleased(true);
|
|
1747
|
+
setTimeout(()=>{
|
|
1748
|
+
setJustReleased(false);
|
|
1749
|
+
}, 200);
|
|
1750
|
+
}
|
|
1751
|
+
if (releaseResult.action === 'close') {
|
|
1752
|
+
closeDrawer();
|
|
1753
|
+
onReleaseProp == null ? void 0 : onReleaseProp(event, false);
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
onReleaseProp == null ? void 0 : onReleaseProp(event, true);
|
|
1757
|
+
resetDrawer();
|
|
1758
|
+
}
|
|
1759
|
+
React__default.useEffect(()=>{
|
|
1760
|
+
// Trigger enter animation without using CSS animation
|
|
1761
|
+
if (isOpen) {
|
|
1762
|
+
set(document.documentElement, {
|
|
1763
|
+
scrollBehavior: 'auto'
|
|
1764
|
+
});
|
|
1765
|
+
openTime.current = new Date();
|
|
1766
|
+
}
|
|
1767
|
+
return ()=>{
|
|
1768
|
+
reset(document.documentElement, 'scrollBehavior');
|
|
1769
|
+
};
|
|
1770
|
+
}, [
|
|
1771
|
+
isOpen
|
|
1772
|
+
]);
|
|
1773
|
+
function onNestedOpenChange(o) {
|
|
1774
|
+
const { transform } = getNestedDrawerTransform({
|
|
1775
|
+
direction,
|
|
1776
|
+
isOpen: o,
|
|
1777
|
+
viewportSize: window.innerWidth,
|
|
1778
|
+
displacement: NESTED_DISPLACEMENT
|
|
1779
|
+
});
|
|
1780
|
+
if (nestedOpenChangeTimer.current) {
|
|
1781
|
+
window.clearTimeout(nestedOpenChangeTimer.current);
|
|
1782
|
+
}
|
|
1783
|
+
set(drawerRef.current, {
|
|
1784
|
+
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
|
|
1785
|
+
transform
|
|
1786
|
+
});
|
|
1787
|
+
if (!o && drawerRef.current) {
|
|
1788
|
+
nestedOpenChangeTimer.current = setTimeout(()=>{
|
|
1789
|
+
const translateValue = getTranslate(drawerRef.current, direction);
|
|
1790
|
+
set(drawerRef.current, {
|
|
1791
|
+
transition: 'none',
|
|
1792
|
+
transform: getAxisAwareTranslate(direction, translateValue != null ? translateValue : 0)
|
|
1793
|
+
});
|
|
1794
|
+
}, 500);
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
function onNestedDrag(_event, percentageDragged) {
|
|
1798
|
+
if (percentageDragged < 0) return;
|
|
1799
|
+
const { transform } = getNestedDragTransform({
|
|
1800
|
+
direction,
|
|
1801
|
+
viewportSize: window.innerWidth,
|
|
1802
|
+
displacement: NESTED_DISPLACEMENT,
|
|
1803
|
+
percentageDragged
|
|
1804
|
+
});
|
|
1805
|
+
set(drawerRef.current, {
|
|
1806
|
+
transform,
|
|
1807
|
+
transition: 'none'
|
|
1808
|
+
});
|
|
1809
|
+
}
|
|
1810
|
+
function onNestedRelease(_event, o) {
|
|
1811
|
+
const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
|
|
1812
|
+
const { transform } = getNestedDrawerTransform({
|
|
1813
|
+
direction,
|
|
1814
|
+
isOpen: o,
|
|
1815
|
+
viewportSize: dim,
|
|
1816
|
+
displacement: NESTED_DISPLACEMENT
|
|
1817
|
+
});
|
|
1818
|
+
if (o) {
|
|
1819
|
+
set(drawerRef.current, {
|
|
1820
|
+
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
|
|
1821
|
+
transform
|
|
1822
|
+
});
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
React__default.useEffect(()=>{
|
|
1826
|
+
if (!modal) {
|
|
1827
|
+
// Need to do this manually unfortunately
|
|
1828
|
+
window.requestAnimationFrame(()=>{
|
|
1829
|
+
document.body.style.pointerEvents = 'auto';
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
}, [
|
|
1833
|
+
modal
|
|
1834
|
+
]);
|
|
1835
|
+
return /*#__PURE__*/ React__default.createElement(DialogPrimitive.Root, {
|
|
1836
|
+
defaultOpen: defaultOpen,
|
|
1837
|
+
onOpenChange: (open)=>{
|
|
1838
|
+
if (!dismissible && !open) return;
|
|
1839
|
+
if (open) {
|
|
1840
|
+
setHasBeenOpened(true);
|
|
1841
|
+
} else {
|
|
1842
|
+
closeDrawer(true);
|
|
1843
|
+
}
|
|
1844
|
+
setIsOpen(open);
|
|
1845
|
+
},
|
|
1846
|
+
open: isOpen,
|
|
1847
|
+
modal: modal
|
|
1848
|
+
}, /*#__PURE__*/ React__default.createElement(DrawerContext.Provider, {
|
|
1849
|
+
value: {
|
|
1850
|
+
controller,
|
|
1851
|
+
runtimeSnapshot,
|
|
1852
|
+
activeSnapPoint,
|
|
1853
|
+
snapPoints,
|
|
1854
|
+
setActiveSnapPoint,
|
|
1855
|
+
drawerRef,
|
|
1856
|
+
overlayRef,
|
|
1857
|
+
onOpenChange,
|
|
1858
|
+
onPress,
|
|
1859
|
+
onRelease,
|
|
1860
|
+
onDrag,
|
|
1861
|
+
dismissible,
|
|
1862
|
+
shouldAnimate,
|
|
1863
|
+
handleOnly,
|
|
1864
|
+
isOpen,
|
|
1865
|
+
isDragging,
|
|
1866
|
+
shouldFade,
|
|
1867
|
+
closeDrawer,
|
|
1868
|
+
onNestedDrag,
|
|
1869
|
+
onNestedOpenChange,
|
|
1870
|
+
onNestedRelease,
|
|
1871
|
+
keyboardIsOpen,
|
|
1872
|
+
modal,
|
|
1873
|
+
snapPointsOffset,
|
|
1874
|
+
activeSnapPointIndex,
|
|
1875
|
+
direction,
|
|
1876
|
+
shouldScaleBackground,
|
|
1877
|
+
setBackgroundColorOnScale,
|
|
1878
|
+
noBodyStyles,
|
|
1879
|
+
container,
|
|
1880
|
+
autoFocus
|
|
1881
|
+
}
|
|
1882
|
+
}, children));
|
|
1883
|
+
}
|
|
1884
|
+
const Overlay = /*#__PURE__*/ React__default.forwardRef(function({ ...rest }, ref) {
|
|
1885
|
+
const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, modal, shouldAnimate } = useDrawerContext();
|
|
1886
|
+
const composedRef = useComposedRefs(ref, overlayRef);
|
|
1887
|
+
const hasSnapPoints = snapPoints && snapPoints.length > 0;
|
|
1888
|
+
const onMouseUp = React__default.useCallback((event)=>onRelease(event), [
|
|
1889
|
+
onRelease
|
|
1890
|
+
]);
|
|
1891
|
+
// Overlay is the component that is locking scroll, removing it will unlock the scroll without having to dig into Radix's Dialog library
|
|
1892
|
+
if (!modal) {
|
|
1893
|
+
return null;
|
|
1894
|
+
}
|
|
1895
|
+
return /*#__PURE__*/ React__default.createElement(DialogPrimitive.Overlay, {
|
|
1896
|
+
onMouseUp: onMouseUp,
|
|
1897
|
+
ref: composedRef,
|
|
1898
|
+
"data-drawer-overlay": "",
|
|
1899
|
+
"data-drawer-snap-points": isOpen && hasSnapPoints ? 'true' : 'false',
|
|
1900
|
+
"data-drawer-snap-points-overlay": isOpen && shouldFade ? 'true' : 'false',
|
|
1901
|
+
"data-drawer-animate": (shouldAnimate == null ? void 0 : shouldAnimate.current) ? 'true' : 'false',
|
|
1902
|
+
...rest
|
|
1903
|
+
});
|
|
1904
|
+
});
|
|
1905
|
+
Overlay.displayName = 'Drawer.Overlay';
|
|
1906
|
+
const Content = /*#__PURE__*/ React__default.forwardRef(function({ onPointerDownOutside, style, onOpenAutoFocus, ...rest }, ref) {
|
|
1907
|
+
const { drawerRef, onPress, onRelease, onDrag, keyboardIsOpen, snapPointsOffset, activeSnapPointIndex, modal, isOpen, direction, snapPoints, container, handleOnly, shouldAnimate, autoFocus } = useDrawerContext();
|
|
1908
|
+
// Needed to use transition instead of animations
|
|
1909
|
+
const [delayedSnapPoints, setDelayedSnapPoints] = React__default.useState(false);
|
|
1910
|
+
const composedRef = useComposedRefs(ref, drawerRef);
|
|
1911
|
+
const pointerStartRef = React__default.useRef(null);
|
|
1912
|
+
const lastKnownPointerEventRef = React__default.useRef(null);
|
|
1913
|
+
const wasBeyondThePointRef = React__default.useRef(false);
|
|
1914
|
+
const hasSnapPoints = snapPoints && snapPoints.length > 0;
|
|
1915
|
+
useScaleBackground();
|
|
1916
|
+
React__default.useEffect(()=>{
|
|
1917
|
+
if (hasSnapPoints) {
|
|
1918
|
+
window.requestAnimationFrame(()=>{
|
|
1919
|
+
setDelayedSnapPoints(true);
|
|
1920
|
+
});
|
|
1921
|
+
}
|
|
1922
|
+
}, []);
|
|
1923
|
+
function handleOnPointerUp(event) {
|
|
1924
|
+
pointerStartRef.current = null;
|
|
1925
|
+
wasBeyondThePointRef.current = false;
|
|
1926
|
+
onRelease(event);
|
|
1927
|
+
}
|
|
1928
|
+
return /*#__PURE__*/ React__default.createElement(DialogPrimitive.Content, {
|
|
1929
|
+
"data-drawer-direction": direction,
|
|
1930
|
+
"data-drawer": "",
|
|
1931
|
+
"data-drawer-delayed-snap-points": delayedSnapPoints ? 'true' : 'false',
|
|
1932
|
+
"data-drawer-snap-points": isOpen && hasSnapPoints ? 'true' : 'false',
|
|
1933
|
+
"data-drawer-custom-container": container ? 'true' : 'false',
|
|
1934
|
+
"data-drawer-animate": (shouldAnimate == null ? void 0 : shouldAnimate.current) ? 'true' : 'false',
|
|
1935
|
+
...rest,
|
|
1936
|
+
ref: composedRef,
|
|
1937
|
+
style: snapPointsOffset && snapPointsOffset.length > 0 ? {
|
|
1938
|
+
'--snap-point-height': `${snapPointsOffset[activeSnapPointIndex != null ? activeSnapPointIndex : 0]}px`,
|
|
1939
|
+
...style
|
|
1940
|
+
} : style,
|
|
1941
|
+
onPointerDown: (event)=>{
|
|
1942
|
+
if (handleOnly) return;
|
|
1943
|
+
rest.onPointerDown == null ? void 0 : rest.onPointerDown.call(rest, event);
|
|
1944
|
+
pointerStartRef.current = {
|
|
1945
|
+
x: event.pageX,
|
|
1946
|
+
y: event.pageY
|
|
1947
|
+
};
|
|
1948
|
+
onPress(event);
|
|
1949
|
+
},
|
|
1950
|
+
onOpenAutoFocus: (e)=>{
|
|
1951
|
+
onOpenAutoFocus == null ? void 0 : onOpenAutoFocus(e);
|
|
1952
|
+
if (!autoFocus) {
|
|
1953
|
+
e.preventDefault();
|
|
1954
|
+
}
|
|
1955
|
+
},
|
|
1956
|
+
onPointerDownOutside: (e)=>{
|
|
1957
|
+
onPointerDownOutside == null ? void 0 : onPointerDownOutside(e);
|
|
1958
|
+
if (!modal || e.defaultPrevented) {
|
|
1959
|
+
e.preventDefault();
|
|
1960
|
+
return;
|
|
1961
|
+
}
|
|
1962
|
+
if (keyboardIsOpen.current) {
|
|
1963
|
+
keyboardIsOpen.current = false;
|
|
1964
|
+
}
|
|
1965
|
+
},
|
|
1966
|
+
onFocusOutside: (e)=>{
|
|
1967
|
+
if (!modal) {
|
|
1968
|
+
e.preventDefault();
|
|
1969
|
+
return;
|
|
1970
|
+
}
|
|
1971
|
+
},
|
|
1972
|
+
onPointerMove: (event)=>{
|
|
1973
|
+
lastKnownPointerEventRef.current = event;
|
|
1974
|
+
if (handleOnly) return;
|
|
1975
|
+
rest.onPointerMove == null ? void 0 : rest.onPointerMove.call(rest, event);
|
|
1976
|
+
if (!pointerStartRef.current) return;
|
|
1977
|
+
const yPosition = event.pageY - pointerStartRef.current.y;
|
|
1978
|
+
const xPosition = event.pageX - pointerStartRef.current.x;
|
|
1979
|
+
const swipeStartThreshold = event.pointerType === 'touch' ? 10 : 2;
|
|
1980
|
+
const delta = {
|
|
1981
|
+
x: xPosition,
|
|
1982
|
+
y: yPosition
|
|
1983
|
+
};
|
|
1984
|
+
const { isAllowed, reachedIntentBoundary } = getSwipeIntent({
|
|
1985
|
+
delta,
|
|
1986
|
+
direction,
|
|
1987
|
+
threshold: swipeStartThreshold,
|
|
1988
|
+
wasBeyondThePoint: wasBeyondThePointRef.current
|
|
1989
|
+
});
|
|
1990
|
+
if (reachedIntentBoundary) {
|
|
1991
|
+
wasBeyondThePointRef.current = true;
|
|
1992
|
+
}
|
|
1993
|
+
const isAllowedToSwipe = isAllowed;
|
|
1994
|
+
if (isAllowedToSwipe) onDrag(event);
|
|
1995
|
+
else if (Math.abs(xPosition) > swipeStartThreshold || Math.abs(yPosition) > swipeStartThreshold) {
|
|
1996
|
+
pointerStartRef.current = null;
|
|
1997
|
+
}
|
|
1998
|
+
},
|
|
1999
|
+
onPointerUp: (event)=>{
|
|
2000
|
+
rest.onPointerUp == null ? void 0 : rest.onPointerUp.call(rest, event);
|
|
2001
|
+
pointerStartRef.current = null;
|
|
2002
|
+
wasBeyondThePointRef.current = false;
|
|
2003
|
+
onRelease(event);
|
|
2004
|
+
},
|
|
2005
|
+
onPointerOut: (event)=>{
|
|
2006
|
+
rest.onPointerOut == null ? void 0 : rest.onPointerOut.call(rest, event);
|
|
2007
|
+
handleOnPointerUp(lastKnownPointerEventRef.current);
|
|
2008
|
+
},
|
|
2009
|
+
onContextMenu: (event)=>{
|
|
2010
|
+
rest.onContextMenu == null ? void 0 : rest.onContextMenu.call(rest, event);
|
|
2011
|
+
if (lastKnownPointerEventRef.current) {
|
|
2012
|
+
handleOnPointerUp(lastKnownPointerEventRef.current);
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
});
|
|
2016
|
+
});
|
|
2017
|
+
Content.displayName = 'Drawer.Content';
|
|
2018
|
+
const LONG_HANDLE_PRESS_TIMEOUT = 250;
|
|
2019
|
+
const DOUBLE_TAP_TIMEOUT = 120;
|
|
2020
|
+
const Handle = /*#__PURE__*/ React__default.forwardRef(function({ preventCycle = false, children, ...rest }, ref) {
|
|
2021
|
+
const { closeDrawer, isDragging, snapPoints, activeSnapPoint, setActiveSnapPoint, dismissible, handleOnly, isOpen, onPress, onDrag } = useDrawerContext();
|
|
2022
|
+
const closeTimeoutIdRef = React__default.useRef(null);
|
|
2023
|
+
const shouldCancelInteractionRef = React__default.useRef(false);
|
|
2024
|
+
function handleStartCycle() {
|
|
2025
|
+
// Stop if this is the second click of a double click
|
|
2026
|
+
if (shouldCancelInteractionRef.current) {
|
|
2027
|
+
handleCancelInteraction();
|
|
2028
|
+
return;
|
|
2029
|
+
}
|
|
2030
|
+
window.setTimeout(()=>{
|
|
2031
|
+
handleCycleSnapPoints();
|
|
2032
|
+
}, DOUBLE_TAP_TIMEOUT);
|
|
2033
|
+
}
|
|
2034
|
+
function handleCycleSnapPoints() {
|
|
2035
|
+
// Make sure to clear the timeout id if the user releases the handle before the cancel timeout
|
|
2036
|
+
handleCancelInteraction();
|
|
2037
|
+
const nextState = getNextHandleState({
|
|
2038
|
+
isDragging,
|
|
2039
|
+
preventCycle,
|
|
2040
|
+
shouldCancelInteraction: shouldCancelInteractionRef.current,
|
|
2041
|
+
snapPoints: snapPoints != null ? snapPoints : undefined,
|
|
2042
|
+
activeSnapPoint,
|
|
2043
|
+
dismissible
|
|
2044
|
+
});
|
|
2045
|
+
if (nextState.type === 'noop') {
|
|
2046
|
+
return;
|
|
2047
|
+
}
|
|
2048
|
+
if (nextState.type === 'close') {
|
|
2049
|
+
closeDrawer();
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
setActiveSnapPoint(nextState.snapPoint);
|
|
2053
|
+
}
|
|
2054
|
+
function handleStartInteraction() {
|
|
2055
|
+
closeTimeoutIdRef.current = window.setTimeout(()=>{
|
|
2056
|
+
// Cancel click interaction on a long press
|
|
2057
|
+
shouldCancelInteractionRef.current = true;
|
|
2058
|
+
}, LONG_HANDLE_PRESS_TIMEOUT);
|
|
2059
|
+
}
|
|
2060
|
+
function handleCancelInteraction() {
|
|
2061
|
+
if (closeTimeoutIdRef.current) {
|
|
2062
|
+
window.clearTimeout(closeTimeoutIdRef.current);
|
|
2063
|
+
}
|
|
2064
|
+
shouldCancelInteractionRef.current = false;
|
|
2065
|
+
}
|
|
2066
|
+
return /*#__PURE__*/ React__default.createElement("div", {
|
|
2067
|
+
onClick: handleStartCycle,
|
|
2068
|
+
onPointerCancel: handleCancelInteraction,
|
|
2069
|
+
onPointerDown: (e)=>{
|
|
2070
|
+
if (handleOnly) onPress(e);
|
|
2071
|
+
handleStartInteraction();
|
|
2072
|
+
},
|
|
2073
|
+
onPointerMove: (e)=>{
|
|
2074
|
+
if (handleOnly) onDrag(e);
|
|
2075
|
+
},
|
|
2076
|
+
// onPointerUp is already handled by the content component
|
|
2077
|
+
ref: ref,
|
|
2078
|
+
"data-drawer-visible": isOpen ? 'true' : 'false',
|
|
2079
|
+
"data-drawer-handle": "",
|
|
2080
|
+
"aria-hidden": "true",
|
|
2081
|
+
...rest
|
|
2082
|
+
}, /*#__PURE__*/ React__default.createElement("span", {
|
|
2083
|
+
"data-drawer-handle-hitarea": "",
|
|
2084
|
+
"aria-hidden": "true"
|
|
2085
|
+
}, children));
|
|
2086
|
+
});
|
|
2087
|
+
Handle.displayName = 'Drawer.Handle';
|
|
2088
|
+
function Portal(props) {
|
|
2089
|
+
const context = useDrawerContext();
|
|
2090
|
+
const { container = context.container, ...portalProps } = props;
|
|
2091
|
+
return /*#__PURE__*/ React__default.createElement(DialogPrimitive.Portal, {
|
|
2092
|
+
container: container,
|
|
2093
|
+
...portalProps
|
|
2094
|
+
});
|
|
2095
|
+
}
|
|
2096
|
+
const Drawer = {
|
|
2097
|
+
Root,
|
|
2098
|
+
Content,
|
|
2099
|
+
Overlay,
|
|
2100
|
+
Trigger: DialogPrimitive.Trigger,
|
|
2101
|
+
Portal,
|
|
2102
|
+
Handle,
|
|
2103
|
+
Title: DialogPrimitive.Title,
|
|
2104
|
+
Description: DialogPrimitive.Description
|
|
2105
|
+
};
|
|
2106
|
+
|
|
2107
|
+
export { Drawer as D, NESTED_DISPLACEMENT as N, TRANSITIONS as T, getNestedDrawerTransform as a, createDrawerController as c, getNestedDragTransform as g, set as s };
|