@simoneggert/react-modal-sheet 5.4.3
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 +21 -0
- package/README.md +994 -0
- package/dist/index.d.mts +188 -0
- package/dist/index.d.ts +188 -0
- package/dist/index.js +1511 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1502 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +87 -0
- package/src/SheetBackdrop.tsx +46 -0
- package/src/SheetContainer.tsx +53 -0
- package/src/SheetContent.tsx +117 -0
- package/src/SheetDragIndicator.tsx +52 -0
- package/src/SheetHeader.tsx +52 -0
- package/src/constants.ts +19 -0
- package/src/context.tsx +12 -0
- package/src/debug.ts +38 -0
- package/src/hooks/use-dimensions.ts +30 -0
- package/src/hooks/use-drag-constraints.ts +14 -0
- package/src/hooks/use-isomorphic-layout-effect.ts +5 -0
- package/src/hooks/use-keyboard-avoidance.ts +75 -0
- package/src/hooks/use-modal-effect.ts +161 -0
- package/src/hooks/use-prevent-scroll.ts +357 -0
- package/src/hooks/use-resize-observer.ts +31 -0
- package/src/hooks/use-safe-area-insets.ts +40 -0
- package/src/hooks/use-scroll-position.ts +142 -0
- package/src/hooks/use-sheet-state.ts +63 -0
- package/src/hooks/use-stable-callback.ts +18 -0
- package/src/hooks/use-virtual-keyboard.ts +269 -0
- package/src/index.tsx +41 -0
- package/src/sheet.tsx +414 -0
- package/src/snap.ts +242 -0
- package/src/styles.ts +110 -0
- package/src/types.tsx +154 -0
- package/src/utils.ts +116 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// This code originates from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts
|
|
2
|
+
|
|
3
|
+
import { isIOS, willOpenKeyboard } from '../utils';
|
|
4
|
+
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect';
|
|
5
|
+
|
|
6
|
+
const KEYBOARD_BUFFER = 24;
|
|
7
|
+
|
|
8
|
+
interface PreventScrollOptions {
|
|
9
|
+
/** Whether the scroll lock is disabled. */
|
|
10
|
+
isDisabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function chain(...callbacks: any[]): (...args: any[]) => void {
|
|
14
|
+
return (...args: any[]) => {
|
|
15
|
+
for (const callback of callbacks) {
|
|
16
|
+
if (typeof callback === 'function') {
|
|
17
|
+
callback(...args);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const visualViewport = typeof document !== 'undefined' && window.visualViewport;
|
|
24
|
+
|
|
25
|
+
export function isScrollable(
|
|
26
|
+
node: Element | null,
|
|
27
|
+
checkForOverflow?: boolean
|
|
28
|
+
): boolean {
|
|
29
|
+
if (!node) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const style = window.getComputedStyle(node);
|
|
34
|
+
|
|
35
|
+
let scrollable = /(auto|scroll)/.test(
|
|
36
|
+
style.overflow + style.overflowX + style.overflowY
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
if (scrollable && checkForOverflow) {
|
|
40
|
+
scrollable =
|
|
41
|
+
node.scrollHeight !== node.clientHeight ||
|
|
42
|
+
node.scrollWidth !== node.clientWidth;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return scrollable;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getScrollParent(
|
|
49
|
+
node: Element,
|
|
50
|
+
checkForOverflow?: boolean
|
|
51
|
+
): Element {
|
|
52
|
+
let scrollableNode: Element | null = node;
|
|
53
|
+
|
|
54
|
+
if (isScrollable(scrollableNode, checkForOverflow)) {
|
|
55
|
+
scrollableNode = scrollableNode.parentElement;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
while (scrollableNode && !isScrollable(scrollableNode, checkForOverflow)) {
|
|
59
|
+
scrollableNode = scrollableNode.parentElement;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
scrollableNode || document.scrollingElement || document.documentElement
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
|
|
68
|
+
let preventScrollCount = 0;
|
|
69
|
+
let restore: () => void;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Prevents scrolling on the document body on mount, and
|
|
73
|
+
* restores it on unmount. Also ensures that content does not
|
|
74
|
+
* shift due to the scrollbars disappearing.
|
|
75
|
+
*/
|
|
76
|
+
export function usePreventScroll(options: PreventScrollOptions = {}) {
|
|
77
|
+
const { isDisabled } = options;
|
|
78
|
+
|
|
79
|
+
useIsomorphicLayoutEffect(() => {
|
|
80
|
+
if (isDisabled) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
preventScrollCount++;
|
|
85
|
+
if (preventScrollCount === 1) {
|
|
86
|
+
if (isIOS()) {
|
|
87
|
+
restore = preventScrollMobileSafari();
|
|
88
|
+
} else {
|
|
89
|
+
restore = preventScrollStandard();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return () => {
|
|
94
|
+
preventScrollCount--;
|
|
95
|
+
if (preventScrollCount === 0) {
|
|
96
|
+
restore?.();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}, [isDisabled]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// For most browsers, all we need to do is set `overflow: hidden` on the root element, and
|
|
103
|
+
// add some padding to prevent the page from shifting when the scrollbar is hidden.
|
|
104
|
+
function preventScrollStandard() {
|
|
105
|
+
return chain(
|
|
106
|
+
setStyle(
|
|
107
|
+
document.documentElement,
|
|
108
|
+
'paddingRight',
|
|
109
|
+
`${window.innerWidth - document.documentElement.clientWidth}px`
|
|
110
|
+
),
|
|
111
|
+
setStyle(document.documentElement, 'overflow', 'hidden')
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Mobile Safari is a whole different beast. Even with overflow: hidden,
|
|
116
|
+
// it still scrolls the page in many situations:
|
|
117
|
+
//
|
|
118
|
+
// 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
|
|
119
|
+
// 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
|
|
120
|
+
// it, so it becomes scrollable.
|
|
121
|
+
// 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
|
|
122
|
+
// This may cause even fixed position elements to scroll off the screen.
|
|
123
|
+
// 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
|
|
124
|
+
// scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
|
|
125
|
+
//
|
|
126
|
+
// In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
|
|
127
|
+
//
|
|
128
|
+
// 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
|
|
129
|
+
// on the window.
|
|
130
|
+
// 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the
|
|
131
|
+
// top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling.
|
|
132
|
+
// 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
|
|
133
|
+
// 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top
|
|
134
|
+
// of the page, which prevents it from scrolling the page. After the input is focused, scroll the element
|
|
135
|
+
// into view ourselves, without scrolling the whole page.
|
|
136
|
+
// 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
|
|
137
|
+
// same visually, but makes the actual scroll position always zero. This is required to make all of the
|
|
138
|
+
// above work or Safari will still try to scroll the page when focusing an input.
|
|
139
|
+
// 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
|
|
140
|
+
// to navigate to an input with the next/previous buttons that's outside a modal.
|
|
141
|
+
function preventScrollMobileSafari() {
|
|
142
|
+
let scrollable: Element | undefined;
|
|
143
|
+
let lastY = 0;
|
|
144
|
+
|
|
145
|
+
const onTouchStart = (e: TouchEvent) => {
|
|
146
|
+
// Use `composedPath` to support shadow DOM.
|
|
147
|
+
const target = e.composedPath()?.[0] as HTMLElement;
|
|
148
|
+
|
|
149
|
+
// Store the nearest scrollable parent element from the element that the user touched.
|
|
150
|
+
scrollable = getScrollParent(target, true);
|
|
151
|
+
|
|
152
|
+
if (
|
|
153
|
+
scrollable === document.documentElement &&
|
|
154
|
+
scrollable === document.body
|
|
155
|
+
) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
lastY = e.changedTouches[0].pageY;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const onTouchMove = (e: TouchEvent) => {
|
|
163
|
+
// In special situations, `onTouchStart` may be called without `onTouchStart` being called.
|
|
164
|
+
// (e.g. when the user places a finger on the screen before the <Sheet> is mounted and then moves the finger after it is mounted).
|
|
165
|
+
// If `onTouchStart` is not called, `scrollable` is `undefined`. Therefore, such cases are ignored.
|
|
166
|
+
if (scrollable === undefined) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Prevent scrolling the window.
|
|
171
|
+
if (
|
|
172
|
+
!scrollable ||
|
|
173
|
+
scrollable === document.documentElement ||
|
|
174
|
+
scrollable === document.body
|
|
175
|
+
) {
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Prevent scrolling up when at the top and scrolling down when at the bottom
|
|
181
|
+
// of a nested scrollable area, otherwise mobile Safari will start scrolling
|
|
182
|
+
// the window instead. Unfortunately, this disables bounce scrolling when at
|
|
183
|
+
// the top but it's the best we can do.
|
|
184
|
+
const y = e.changedTouches[0].pageY;
|
|
185
|
+
const scrollTop = scrollable.scrollTop;
|
|
186
|
+
const bottom = scrollable.scrollHeight - scrollable.clientHeight;
|
|
187
|
+
|
|
188
|
+
if (bottom === 0) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if ((scrollTop <= 0 && y > lastY) || (scrollTop >= bottom && y < lastY)) {
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
lastY = y;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const onTouchEnd = (e: TouchEvent) => {
|
|
200
|
+
// Use `composedPath` to support shadow DOM.
|
|
201
|
+
const target = e.composedPath()?.[0] as HTMLElement;
|
|
202
|
+
|
|
203
|
+
// Apply this change if we're not already focused on the target element
|
|
204
|
+
if (willOpenKeyboard(target) && target !== document.activeElement) {
|
|
205
|
+
e.preventDefault();
|
|
206
|
+
|
|
207
|
+
// Apply a transform to trick Safari into thinking the input is at the top of the page
|
|
208
|
+
// so it doesn't try to scroll it into view. When tapping on an input, this needs to
|
|
209
|
+
// be done before the "focus" event, so we have to focus the element ourselves.
|
|
210
|
+
target.style.transform = 'translateY(-2000px)';
|
|
211
|
+
target.focus();
|
|
212
|
+
requestAnimationFrame(() => {
|
|
213
|
+
target.style.transform = '';
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const onFocus = (e: FocusEvent) => {
|
|
219
|
+
// Use `composedPath` to support shadow DOM.
|
|
220
|
+
const target = e.composedPath()?.[0] as HTMLElement;
|
|
221
|
+
|
|
222
|
+
if (willOpenKeyboard(target)) {
|
|
223
|
+
// Transform also needs to be applied in the focus event in cases where focus moves
|
|
224
|
+
// other than tapping on an input directly, e.g. the next/previous buttons in the
|
|
225
|
+
// software keyboard. In these cases, it seems applying the transform in the focus event
|
|
226
|
+
// is good enough, whereas when tapping an input, it must be done before the focus event. 🤷♂️
|
|
227
|
+
target.style.transform = 'translateY(-2000px)';
|
|
228
|
+
requestAnimationFrame(() => {
|
|
229
|
+
target.style.transform = '';
|
|
230
|
+
|
|
231
|
+
// This will have prevented the browser from scrolling the focused element into view,
|
|
232
|
+
// so we need to do this ourselves in a way that doesn't cause the whole page to scroll.
|
|
233
|
+
if (visualViewport) {
|
|
234
|
+
if (visualViewport.height < window.innerHeight) {
|
|
235
|
+
// If the keyboard is already visible, do this after one additional frame
|
|
236
|
+
// to wait for the transform to be removed.
|
|
237
|
+
requestAnimationFrame(() => {
|
|
238
|
+
scrollIntoView(target);
|
|
239
|
+
});
|
|
240
|
+
} else {
|
|
241
|
+
// Otherwise, wait for the visual viewport to resize before scrolling so we can
|
|
242
|
+
// measure the correct position to scroll to.
|
|
243
|
+
visualViewport.addEventListener(
|
|
244
|
+
'resize',
|
|
245
|
+
() => scrollIntoView(target),
|
|
246
|
+
{ once: true }
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const onWindowScroll = () => {
|
|
255
|
+
// Last resort. If the window scrolled, scroll it back to the top.
|
|
256
|
+
// It should always be at the top because the body will have a negative margin (see below).
|
|
257
|
+
window.scrollTo(0, 0);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
// Record the original scroll position so we can restore it.
|
|
261
|
+
// Then apply a negative margin to the body to offset it by the scroll position. This will
|
|
262
|
+
// enable us to scroll the window to the top, which is required for the rest of this to work.
|
|
263
|
+
const scrollX = window.pageXOffset;
|
|
264
|
+
const scrollY = window.pageYOffset;
|
|
265
|
+
|
|
266
|
+
const restoreStyles = chain(
|
|
267
|
+
setStyle(
|
|
268
|
+
document.documentElement,
|
|
269
|
+
'paddingRight',
|
|
270
|
+
`${window.innerWidth - document.documentElement.clientWidth}px`
|
|
271
|
+
),
|
|
272
|
+
setStyle(document.documentElement, 'overflow', 'hidden')
|
|
273
|
+
// setStyle(document.body, 'marginTop', `-${scrollY}px`)
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// Scroll to the top. The negative margin on the body will make this appear the same.
|
|
277
|
+
// This produces a flicker on Safari iOS, so we skip the `marginTop` and this altogether for now.
|
|
278
|
+
// window.scrollTo(0, 0);
|
|
279
|
+
|
|
280
|
+
const removeEvents = chain(
|
|
281
|
+
addEvent(document, 'touchstart', onTouchStart, {
|
|
282
|
+
passive: false,
|
|
283
|
+
capture: true,
|
|
284
|
+
}),
|
|
285
|
+
addEvent(document, 'touchmove', onTouchMove, {
|
|
286
|
+
passive: false,
|
|
287
|
+
capture: true,
|
|
288
|
+
}),
|
|
289
|
+
addEvent(document, 'touchend', onTouchEnd, {
|
|
290
|
+
passive: false,
|
|
291
|
+
capture: true,
|
|
292
|
+
}),
|
|
293
|
+
addEvent(document, 'focus', onFocus, true),
|
|
294
|
+
addEvent(window, 'scroll', onWindowScroll)
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
return () => {
|
|
298
|
+
// Restore styles and scroll the page back to where it was.
|
|
299
|
+
restoreStyles();
|
|
300
|
+
removeEvents();
|
|
301
|
+
window.scrollTo(scrollX, scrollY);
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// Sets a CSS property on an element, and returns a function to revert it to the previous value.
|
|
306
|
+
function setStyle(element: any, style: string, value: string) {
|
|
307
|
+
// https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310
|
|
308
|
+
const cur = element.style[style];
|
|
309
|
+
element.style[style] = value;
|
|
310
|
+
|
|
311
|
+
return () => {
|
|
312
|
+
element.style[style] = cur;
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Adds an event listener to an element, and returns a function to remove it.
|
|
317
|
+
function addEvent<K extends keyof GlobalEventHandlersEventMap>(
|
|
318
|
+
target: EventTarget,
|
|
319
|
+
event: K,
|
|
320
|
+
handler: (this: Document, ev: GlobalEventHandlersEventMap[K]) => any,
|
|
321
|
+
options?: boolean | AddEventListenerOptions
|
|
322
|
+
) {
|
|
323
|
+
// @ts-expect-error
|
|
324
|
+
target.addEventListener(event, handler, options);
|
|
325
|
+
|
|
326
|
+
return () => {
|
|
327
|
+
// @ts-expect-error
|
|
328
|
+
target.removeEventListener(event, handler, options);
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function scrollIntoView(target: Element) {
|
|
333
|
+
const root = document.scrollingElement || document.documentElement;
|
|
334
|
+
while (target && target !== root) {
|
|
335
|
+
// Find the parent scrollable element and adjust the scroll position if the target is not already in view.
|
|
336
|
+
const scrollable = getScrollParent(target);
|
|
337
|
+
if (
|
|
338
|
+
scrollable !== document.documentElement &&
|
|
339
|
+
scrollable !== document.body &&
|
|
340
|
+
scrollable !== target
|
|
341
|
+
) {
|
|
342
|
+
const scrollableTop = scrollable.getBoundingClientRect().top;
|
|
343
|
+
const targetTop = target.getBoundingClientRect().top;
|
|
344
|
+
const targetBottom = target.getBoundingClientRect().bottom;
|
|
345
|
+
// Buffer is needed for some edge cases
|
|
346
|
+
const keyboardHeight =
|
|
347
|
+
scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER;
|
|
348
|
+
|
|
349
|
+
if (targetBottom > keyboardHeight) {
|
|
350
|
+
scrollable.scrollTop += targetTop - scrollableTop;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// @ts-expect-error
|
|
355
|
+
target = scrollable.parentElement;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
import { useStableCallback } from './use-stable-callback';
|
|
3
|
+
|
|
4
|
+
export function useResizeObserver<T extends Element = HTMLDivElement>(
|
|
5
|
+
callback: ResizeObserverCallback
|
|
6
|
+
) {
|
|
7
|
+
const observeRef = useRef<T | null>(null);
|
|
8
|
+
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
9
|
+
|
|
10
|
+
const debouncedCallback: ResizeObserverCallback = useStableCallback(
|
|
11
|
+
(entries, observer) => {
|
|
12
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
13
|
+
timeoutRef.current = setTimeout(() => callback(entries, observer), 32);
|
|
14
|
+
}
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
const element = observeRef.current;
|
|
19
|
+
if (!element) return;
|
|
20
|
+
|
|
21
|
+
const observer = new ResizeObserver(debouncedCallback);
|
|
22
|
+
observer.observe(element, { box: 'border-box' });
|
|
23
|
+
|
|
24
|
+
return () => {
|
|
25
|
+
observer.disconnect();
|
|
26
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
27
|
+
};
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
return { observeRef };
|
|
31
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { IS_SSR } from '../constants';
|
|
4
|
+
|
|
5
|
+
export function useSafeAreaInsets() {
|
|
6
|
+
const [insets] = useState(() => {
|
|
7
|
+
const fallback = { top: 0, left: 0, right: 0, bottom: 0 };
|
|
8
|
+
|
|
9
|
+
if (IS_SSR) return fallback;
|
|
10
|
+
|
|
11
|
+
const root = document.querySelector<HTMLElement>(':root');
|
|
12
|
+
|
|
13
|
+
if (!root) return fallback;
|
|
14
|
+
|
|
15
|
+
root.style.setProperty('--rms-sat', 'env(safe-area-inset-top)');
|
|
16
|
+
root.style.setProperty('--rms-sal', 'env(safe-area-inset-left)');
|
|
17
|
+
root.style.setProperty('--rms-sar', 'env(safe-area-inset-right)');
|
|
18
|
+
root.style.setProperty('--rms-sab', 'env(safe-area-inset-bottom)');
|
|
19
|
+
|
|
20
|
+
const computedStyle = getComputedStyle(root);
|
|
21
|
+
const sat = getComputedValue(computedStyle, '--rms-sat');
|
|
22
|
+
const sal = getComputedValue(computedStyle, '--rms-sal');
|
|
23
|
+
const sar = getComputedValue(computedStyle, '--rms-sar');
|
|
24
|
+
const sab = getComputedValue(computedStyle, '--rms-sab');
|
|
25
|
+
|
|
26
|
+
root.style.removeProperty('--rms-sat');
|
|
27
|
+
root.style.removeProperty('--rms-sal');
|
|
28
|
+
root.style.removeProperty('--rms-sar');
|
|
29
|
+
root.style.removeProperty('--rms-sab');
|
|
30
|
+
|
|
31
|
+
return { top: sat, left: sal, right: sar, bottom: sab };
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return insets;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function getComputedValue(computed: CSSStyleDeclaration, property: string) {
|
|
38
|
+
const strValue = computed.getPropertyValue(property).replace('px', '').trim();
|
|
39
|
+
return parseInt(strValue, 10) || 0;
|
|
40
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { useStableCallback } from './use-stable-callback';
|
|
3
|
+
|
|
4
|
+
type UseScrollPositionOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Debounce delay in ms for scroll event handling.
|
|
7
|
+
* @default 32
|
|
8
|
+
*/
|
|
9
|
+
debounceDelay?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Enable or disable the hook entirely.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
isEnabled?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Hook to track the scroll position of an element.
|
|
19
|
+
*
|
|
20
|
+
* The scroll position can be 'top', 'bottom', 'middle', or undefined if the content is not scrollable.
|
|
21
|
+
* The hook provides a `scrollRef` callback to assign to the scrollable element.
|
|
22
|
+
*
|
|
23
|
+
* Note that the scroll position is only updated when the user stops scrolling
|
|
24
|
+
* for a short moment (debounced). You can set `debounceDelay` to `0` to disable debouncing entirely.
|
|
25
|
+
*
|
|
26
|
+
* @param options Configuration options for the hook.
|
|
27
|
+
* @returns An object containing the `scrollRef` callback and the current `scrollPosition`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* import { useScrollPosition } from 'react-modal-sheet';
|
|
32
|
+
*
|
|
33
|
+
* function MyComponent() {
|
|
34
|
+
* const { scrollRef, scrollPosition } = useScrollPosition();
|
|
35
|
+
*
|
|
36
|
+
* return (
|
|
37
|
+
* <div>
|
|
38
|
+
* <p>Scroll position: {scrollPosition}</p>
|
|
39
|
+
* <div ref={scrollRef} style={{ overflowY: 'auto', maxHeight: '300px' }}>
|
|
40
|
+
* ...long content...
|
|
41
|
+
* </div>
|
|
42
|
+
* </div>
|
|
43
|
+
* );
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function useScrollPosition(options: UseScrollPositionOptions = {}) {
|
|
48
|
+
const { debounceDelay = 32, isEnabled = true } = options;
|
|
49
|
+
|
|
50
|
+
const scrollTimeoutRef = useRef<number | null>(null);
|
|
51
|
+
const [element, setElement] = useState<HTMLElement | null>(null);
|
|
52
|
+
const [scrollPosition, setScrollPosition] = useState<
|
|
53
|
+
'top' | 'bottom' | 'middle' | undefined
|
|
54
|
+
>(undefined);
|
|
55
|
+
|
|
56
|
+
const scrollRef = useMemo(
|
|
57
|
+
() => (element: HTMLElement | null) => {
|
|
58
|
+
setElement(element);
|
|
59
|
+
},
|
|
60
|
+
[]
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const determineScrollPosition = useStableCallback((element: HTMLElement) => {
|
|
64
|
+
const { scrollTop, scrollHeight, clientHeight } = element;
|
|
65
|
+
const isScrollable = scrollHeight > clientHeight;
|
|
66
|
+
|
|
67
|
+
if (!isScrollable) {
|
|
68
|
+
// Reset scroll position if the content is not scrollable anymore
|
|
69
|
+
if (scrollPosition) setScrollPosition(undefined);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const isAtTop = scrollTop <= 0;
|
|
74
|
+
const isAtBottom =
|
|
75
|
+
Math.ceil(scrollHeight) - Math.ceil(scrollTop) ===
|
|
76
|
+
Math.ceil(clientHeight);
|
|
77
|
+
|
|
78
|
+
let position: 'top' | 'bottom' | 'middle';
|
|
79
|
+
|
|
80
|
+
if (isAtTop) {
|
|
81
|
+
position = 'top';
|
|
82
|
+
} else if (isAtBottom) {
|
|
83
|
+
position = 'bottom';
|
|
84
|
+
} else {
|
|
85
|
+
position = 'middle';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Let browser only handle downward pan gestures (scrolling content up)
|
|
89
|
+
element.style.touchAction = isAtTop ? 'pan-down' : '';
|
|
90
|
+
|
|
91
|
+
if (position === scrollPosition) return;
|
|
92
|
+
setScrollPosition(position);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const onScroll = useStableCallback((event: Event) => {
|
|
96
|
+
if (event.currentTarget instanceof HTMLElement) {
|
|
97
|
+
const el = event.currentTarget;
|
|
98
|
+
|
|
99
|
+
if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
|
|
100
|
+
|
|
101
|
+
if (debounceDelay === 0) {
|
|
102
|
+
determineScrollPosition(el);
|
|
103
|
+
} else {
|
|
104
|
+
// Debounce the scroll handler
|
|
105
|
+
scrollTimeoutRef.current = setTimeout(
|
|
106
|
+
() => determineScrollPosition(el),
|
|
107
|
+
debounceDelay
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const onTouchStart = useStableCallback((event: Event) => {
|
|
114
|
+
if (event.currentTarget instanceof HTMLElement) {
|
|
115
|
+
const element = event.currentTarget;
|
|
116
|
+
requestAnimationFrame(() => {
|
|
117
|
+
determineScrollPosition(element);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (!element || !isEnabled) return;
|
|
124
|
+
|
|
125
|
+
// Determine initial scroll position
|
|
126
|
+
determineScrollPosition(element);
|
|
127
|
+
|
|
128
|
+
element.addEventListener('scroll', onScroll);
|
|
129
|
+
element.addEventListener('touchstart', onTouchStart);
|
|
130
|
+
|
|
131
|
+
return () => {
|
|
132
|
+
if (scrollTimeoutRef.current) clearTimeout(scrollTimeoutRef.current);
|
|
133
|
+
element.removeEventListener('scroll', onScroll);
|
|
134
|
+
element.removeEventListener('touchstart', onTouchStart);
|
|
135
|
+
};
|
|
136
|
+
}, [element, isEnabled]);
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
scrollRef,
|
|
140
|
+
scrollPosition,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { useStableCallback } from './use-stable-callback';
|
|
3
|
+
|
|
4
|
+
type SheetState = 'closed' | 'opening' | 'open' | 'closing';
|
|
5
|
+
|
|
6
|
+
type UseSheetStatesProps = {
|
|
7
|
+
isOpen: boolean;
|
|
8
|
+
onClosed?: () => Promise<void> | void;
|
|
9
|
+
onOpening?: () => Promise<void> | void;
|
|
10
|
+
onOpen?: () => Promise<void> | void;
|
|
11
|
+
onClosing?: () => Promise<void> | void;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function useSheetState({
|
|
15
|
+
isOpen,
|
|
16
|
+
onClosed: _onClosed,
|
|
17
|
+
onOpening: _onOpening,
|
|
18
|
+
onOpen: _onOpen,
|
|
19
|
+
onClosing: _onClosing,
|
|
20
|
+
}: UseSheetStatesProps) {
|
|
21
|
+
const [state, setState] = useState<SheetState>(isOpen ? 'opening' : 'closed');
|
|
22
|
+
const onClosed = useStableCallback(() => _onClosed?.());
|
|
23
|
+
const onOpening = useStableCallback(() => _onOpening?.());
|
|
24
|
+
const onOpen = useStableCallback(() => _onOpen?.());
|
|
25
|
+
const onClosing = useStableCallback(() => _onClosing?.());
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (isOpen && state === 'closed') {
|
|
29
|
+
setState('opening');
|
|
30
|
+
} else if (!isOpen && (state === 'open' || state === 'opening')) {
|
|
31
|
+
setState('closing');
|
|
32
|
+
}
|
|
33
|
+
}, [isOpen, state]);
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
async function handle() {
|
|
37
|
+
switch (state) {
|
|
38
|
+
case 'closed':
|
|
39
|
+
await onClosed?.();
|
|
40
|
+
break;
|
|
41
|
+
|
|
42
|
+
case 'opening':
|
|
43
|
+
await onOpening?.();
|
|
44
|
+
setState('open');
|
|
45
|
+
break;
|
|
46
|
+
|
|
47
|
+
case 'open':
|
|
48
|
+
await onOpen?.();
|
|
49
|
+
break;
|
|
50
|
+
|
|
51
|
+
case 'closing':
|
|
52
|
+
await onClosing?.();
|
|
53
|
+
setState('closed');
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
handle().catch((error) => {
|
|
58
|
+
console.error('Internal sheet state error:', error);
|
|
59
|
+
});
|
|
60
|
+
}, [state]);
|
|
61
|
+
|
|
62
|
+
return state;
|
|
63
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useCallback, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect';
|
|
4
|
+
|
|
5
|
+
export function useStableCallback<T extends (...args: any[]) => any>(
|
|
6
|
+
handler: T
|
|
7
|
+
) {
|
|
8
|
+
const handlerRef = useRef<T>(undefined);
|
|
9
|
+
|
|
10
|
+
useIsomorphicLayoutEffect(() => {
|
|
11
|
+
handlerRef.current = handler;
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
return useCallback((...args: any[]) => {
|
|
15
|
+
const fn = handlerRef.current;
|
|
16
|
+
return fn?.(...args);
|
|
17
|
+
}, []) as T;
|
|
18
|
+
}
|