@reshaped/utilities 4.0.0-canary.7 → 4.0.1
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/README.md +1 -1
- package/dist/a11y/TrapFocus.js +7 -3
- package/dist/scroll/lock.js +3 -3
- package/dist/scroll/lockStandard.js +7 -2
- package/dist/scroll/tests/lock.test.js +19 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -200,7 +200,7 @@ class Modal {
|
|
|
200
200
|
}
|
|
201
201
|
```
|
|
202
202
|
|
|
203
|
-
By default, `lockScroll` will lock the scrolling of the document
|
|
203
|
+
By default, `lockScroll` will lock the scrolling of the document and there are a few additional options you can pass to customize the behavior. Calling the returned `unlockScroll` function will unlock the scrolling based on the originally passed options.
|
|
204
204
|
|
|
205
205
|
`lockScroll` options:
|
|
206
206
|
|
package/dist/a11y/TrapFocus.js
CHANGED
|
@@ -128,9 +128,13 @@ class TrapFocus {
|
|
|
128
128
|
// Don't add back to the chain if we're traversing back
|
|
129
129
|
if (!isLastInChain) {
|
|
130
130
|
this.#chainId = _a.chain.add(this);
|
|
131
|
-
|
|
132
|
-
if (
|
|
133
|
-
focusElement(initialFocusEl
|
|
131
|
+
const focusInside = this.#root.contains(currentActiveElement);
|
|
132
|
+
if (initialFocusEl) {
|
|
133
|
+
focusElement(initialFocusEl, { pseudoFocus });
|
|
134
|
+
// If the focus was moved manually (e.g. with autoFocus) - keep it there
|
|
135
|
+
}
|
|
136
|
+
else if (!focusInside) {
|
|
137
|
+
focusElement(focusable[0], { pseudoFocus });
|
|
134
138
|
}
|
|
135
139
|
}
|
|
136
140
|
this.trapped = true;
|
package/dist/scroll/lock.js
CHANGED
|
@@ -7,12 +7,12 @@ export const lockScroll = (args) => {
|
|
|
7
7
|
let reset = () => { };
|
|
8
8
|
const container = args?.containerEl ??
|
|
9
9
|
(args?.originEl && findClosestScrollableContainer({ el: args.originEl })) ??
|
|
10
|
-
document.
|
|
11
|
-
const
|
|
10
|
+
document.documentElement;
|
|
11
|
+
const lockedDocumentScroll = container === document.documentElement;
|
|
12
12
|
// Already locked so no need to lock again and trigger the callback
|
|
13
13
|
if (container.style.overflow === "hidden")
|
|
14
14
|
return;
|
|
15
|
-
if (isIOSLock &&
|
|
15
|
+
if (isIOSLock && lockedDocumentScroll) {
|
|
16
16
|
reset = lockSafariScroll();
|
|
17
17
|
}
|
|
18
18
|
else {
|
|
@@ -7,8 +7,13 @@ const lockStandardScroll = (args) => {
|
|
|
7
7
|
const isOverflowing = rect.left + rect.right < window.innerWidth;
|
|
8
8
|
styleCache.set(container, { overflow: "hidden" });
|
|
9
9
|
if (isOverflowing) {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
if (CSS.supports("scrollbar-gutter", "stable")) {
|
|
11
|
+
styleCache.set(container, { scrollbarGutter: "stable" });
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
const scrollBarWidth = getScrollbarWidth();
|
|
15
|
+
styleCache.set(container, { paddingRight: `${scrollBarWidth}px` });
|
|
16
|
+
}
|
|
12
17
|
}
|
|
13
18
|
return () => styleCache.reset();
|
|
14
19
|
};
|
|
@@ -6,22 +6,24 @@ vi.mock("@/platform", () => ({
|
|
|
6
6
|
}));
|
|
7
7
|
describe("scroll/lockScroll", () => {
|
|
8
8
|
beforeEach(() => {
|
|
9
|
-
// Reset the
|
|
10
|
-
document.
|
|
11
|
-
document.
|
|
12
|
-
document.
|
|
9
|
+
// Reset the document styles before each test
|
|
10
|
+
document.documentElement.style.overflow = "";
|
|
11
|
+
document.documentElement.style.paddingRight = "";
|
|
12
|
+
document.documentElement.style.scrollbarGutter = "";
|
|
13
|
+
document.documentElement.innerHTML = "";
|
|
13
14
|
});
|
|
14
15
|
afterEach(() => {
|
|
15
16
|
// Clean up
|
|
16
|
-
document.
|
|
17
|
-
document.
|
|
18
|
-
document.
|
|
17
|
+
document.documentElement.style.overflow = "";
|
|
18
|
+
document.documentElement.style.paddingRight = "";
|
|
19
|
+
document.documentElement.style.scrollbarGutter = "";
|
|
20
|
+
document.documentElement.innerHTML = "";
|
|
19
21
|
});
|
|
20
|
-
test("locks scroll on
|
|
22
|
+
test("locks scroll on document", () => {
|
|
21
23
|
const unlock = lockScroll({});
|
|
22
|
-
expect(document.
|
|
24
|
+
expect(document.documentElement.style.overflow).toBe("hidden");
|
|
23
25
|
unlock?.();
|
|
24
|
-
expect(document.
|
|
26
|
+
expect(document.documentElement.style.overflow).toBe("");
|
|
25
27
|
});
|
|
26
28
|
test("locks scroll on custom container element", () => {
|
|
27
29
|
const container = document.createElement("div");
|
|
@@ -30,7 +32,7 @@ describe("scroll/lockScroll", () => {
|
|
|
30
32
|
document.body.appendChild(container);
|
|
31
33
|
const unlock = lockScroll({ containerEl: container });
|
|
32
34
|
expect(container.style.overflow).toBe("hidden");
|
|
33
|
-
expect(document.
|
|
35
|
+
expect(document.documentElement.style.overflow).not.toBe("hidden");
|
|
34
36
|
unlock?.();
|
|
35
37
|
expect(container.style.overflow).toBe("auto");
|
|
36
38
|
});
|
|
@@ -53,11 +55,11 @@ describe("scroll/lockScroll", () => {
|
|
|
53
55
|
test("unlocks after multiple locks", () => {
|
|
54
56
|
const unlock1 = lockScroll({});
|
|
55
57
|
const unlock2 = lockScroll({});
|
|
56
|
-
expect(document.
|
|
58
|
+
expect(document.documentElement.style.overflow).toBe("hidden");
|
|
57
59
|
unlock1?.();
|
|
58
|
-
expect(document.
|
|
60
|
+
expect(document.documentElement.style.overflow).toBe("");
|
|
59
61
|
unlock2?.();
|
|
60
|
-
expect(document.
|
|
62
|
+
expect(document.documentElement.style.overflow).toBe("");
|
|
61
63
|
});
|
|
62
64
|
test("calls lock callback immediately", () => {
|
|
63
65
|
const lockCb = vi.fn();
|
|
@@ -70,12 +72,12 @@ describe("scroll/lockScroll", () => {
|
|
|
70
72
|
unlock?.({ callback: unlockCb });
|
|
71
73
|
expect(unlockCb).toHaveBeenCalledTimes(1);
|
|
72
74
|
});
|
|
73
|
-
test("handles origin element when no scrollable container is found (falls back to
|
|
75
|
+
test("handles origin element when no scrollable container is found (falls back to document)", () => {
|
|
74
76
|
const originEl = document.createElement("div");
|
|
75
77
|
document.body.appendChild(originEl);
|
|
76
78
|
const unlock = lockScroll({ originEl });
|
|
77
|
-
expect(document.
|
|
79
|
+
expect(document.documentElement.style.overflow).toBe("hidden");
|
|
78
80
|
unlock?.();
|
|
79
|
-
expect(document.
|
|
81
|
+
expect(document.documentElement.style.overflow).toBe("");
|
|
80
82
|
});
|
|
81
83
|
});
|