@stokr/components-library 4.2.1 → 4.2.2
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.
|
@@ -4,6 +4,7 @@ import { useState, useEffect } from "react";
|
|
|
4
4
|
import { ModalRoot, Dimmer, Box, ModalClose } from "./Modal.styles.js";
|
|
5
5
|
import { ModalBack, ModalInner, ModalInnerSmartContractAddress, ModalInnerWithBorder, ModalLink, ModalLinkWrap, ModalScroll } from "./Modal.styles.js";
|
|
6
6
|
import { OutlinedClose } from "./OutlinedClose.js";
|
|
7
|
+
import { lockPageScroll } from "./lockPageScroll.js";
|
|
7
8
|
const timeout = 300;
|
|
8
9
|
const Modal = ({
|
|
9
10
|
children,
|
|
@@ -35,29 +36,7 @@ const Modal = ({
|
|
|
35
36
|
}
|
|
36
37
|
}, [isOpen]);
|
|
37
38
|
useEffect(() => {
|
|
38
|
-
if (isOpen)
|
|
39
|
-
const scrollY = window.scrollY;
|
|
40
|
-
const bodyWidth = document.body.offsetWidth;
|
|
41
|
-
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
|
|
42
|
-
document.documentElement.style.setProperty("--scrollbar-compensation", `${scrollbarWidth}px`);
|
|
43
|
-
document.body.style.width = `${bodyWidth}px`;
|
|
44
|
-
document.body.style.position = "fixed";
|
|
45
|
-
document.body.style.top = `-${scrollY}px`;
|
|
46
|
-
document.body.style.left = "0";
|
|
47
|
-
document.body.style.overflow = "hidden";
|
|
48
|
-
}
|
|
49
|
-
return () => {
|
|
50
|
-
if (isOpen) {
|
|
51
|
-
const scrollY = Math.abs(parseInt(document.body.style.top || "0", 10));
|
|
52
|
-
document.documentElement.style.setProperty("--scrollbar-compensation", "0px");
|
|
53
|
-
document.body.style.width = "";
|
|
54
|
-
document.body.style.position = "";
|
|
55
|
-
document.body.style.top = "";
|
|
56
|
-
document.body.style.left = "";
|
|
57
|
-
document.body.style.overflow = "";
|
|
58
|
-
window.scrollTo(0, scrollY);
|
|
59
|
-
}
|
|
60
|
-
};
|
|
39
|
+
if (isOpen) return lockPageScroll();
|
|
61
40
|
}, [isOpen]);
|
|
62
41
|
if (!shouldRender) return null;
|
|
63
42
|
return /* @__PURE__ */ jsx(Portal, { isOpened: isOpen, children: /* @__PURE__ */ jsx(
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Whether an element between `target` and the page itself can still scroll by `deltaY`. */
|
|
2
|
+
export declare function canScrollInside(target: EventTarget | null, deltaY: number): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Stops the page behind an overlay from scrolling by blocking wheel, touch and keyboard scrolling
|
|
5
|
+
* that would reach it. Unlike hiding overflow or fixing the body, the page scrollbar stays exactly
|
|
6
|
+
* as it is, so nothing shifts or flashes while the overlay fades in. Returns the unlock function.
|
|
7
|
+
*/
|
|
8
|
+
export declare function lockPageScroll(): () => void;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const SCROLL_KEY_DIRECTION = {
|
|
2
|
+
ArrowUp: -1,
|
|
3
|
+
PageUp: -1,
|
|
4
|
+
Home: -1,
|
|
5
|
+
ArrowDown: 1,
|
|
6
|
+
PageDown: 1,
|
|
7
|
+
End: 1,
|
|
8
|
+
" ": 1
|
|
9
|
+
};
|
|
10
|
+
const EDITABLE = 'input, textarea, select, [contenteditable]:not([contenteditable="false"])';
|
|
11
|
+
const SPACE_ACTIVATED = `${EDITABLE}, button, a[href], summary, [role="button"], [role="checkbox"], [role="switch"]`;
|
|
12
|
+
function canScrollInside(target, deltaY) {
|
|
13
|
+
let node = target instanceof Element ? target : null;
|
|
14
|
+
while (node && node !== document.body && node !== document.documentElement) {
|
|
15
|
+
const { overflowY } = getComputedStyle(node);
|
|
16
|
+
if (overflowY === "auto" || overflowY === "scroll") {
|
|
17
|
+
const maxScrollTop = node.scrollHeight - node.clientHeight;
|
|
18
|
+
if (maxScrollTop > 0 && (deltaY < 0 ? node.scrollTop > 0 : node.scrollTop < maxScrollTop - 1)) return true;
|
|
19
|
+
}
|
|
20
|
+
node = node.parentElement;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
function lockPageScroll() {
|
|
25
|
+
let lastTouchY = 0;
|
|
26
|
+
const onWheel = (event) => {
|
|
27
|
+
if (event.ctrlKey || event.deltaY === 0) return;
|
|
28
|
+
if (!canScrollInside(event.target, event.deltaY)) event.preventDefault();
|
|
29
|
+
};
|
|
30
|
+
const onTouchStart = (event) => {
|
|
31
|
+
lastTouchY = event.touches[0]?.clientY ?? 0;
|
|
32
|
+
};
|
|
33
|
+
const onTouchMove = (event) => {
|
|
34
|
+
if (event.touches.length !== 1) return;
|
|
35
|
+
const touchY = event.touches[0].clientY;
|
|
36
|
+
const deltaY = lastTouchY - touchY;
|
|
37
|
+
lastTouchY = touchY;
|
|
38
|
+
if (deltaY !== 0 && !canScrollInside(event.target, deltaY)) event.preventDefault();
|
|
39
|
+
};
|
|
40
|
+
const onKeyDown = (event) => {
|
|
41
|
+
const direction = SCROLL_KEY_DIRECTION[event.key];
|
|
42
|
+
if (!direction || event.altKey || event.ctrlKey || event.metaKey) return;
|
|
43
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
44
|
+
if (target?.closest(event.key === " " ? SPACE_ACTIVATED : EDITABLE)) return;
|
|
45
|
+
if (!canScrollInside(target, event.shiftKey && event.key === " " ? -1 : direction)) event.preventDefault();
|
|
46
|
+
};
|
|
47
|
+
const options = { capture: true, passive: false };
|
|
48
|
+
document.addEventListener("wheel", onWheel, options);
|
|
49
|
+
document.addEventListener("touchstart", onTouchStart, { capture: true, passive: true });
|
|
50
|
+
document.addEventListener("touchmove", onTouchMove, options);
|
|
51
|
+
document.addEventListener("keydown", onKeyDown, options);
|
|
52
|
+
return () => {
|
|
53
|
+
document.removeEventListener("wheel", onWheel, options);
|
|
54
|
+
document.removeEventListener("touchstart", onTouchStart, { capture: true });
|
|
55
|
+
document.removeEventListener("touchmove", onTouchMove, options);
|
|
56
|
+
document.removeEventListener("keydown", onKeyDown, options);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
canScrollInside,
|
|
61
|
+
lockPageScroll
|
|
62
|
+
};
|