@reshaped/utilities 4.0.0-canary.6 → 4.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/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 body 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.
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
 
@@ -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
- // If the focus was moved manually (e.g. with autoFocus) - keep it there
132
- if (!this.#root.contains(currentActiveElement)) {
133
- focusElement(initialFocusEl || focusable[0], { pseudoFocus });
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;
@@ -75,6 +75,8 @@ class Flyout {
75
75
  observer.observe(this.#options.trigger);
76
76
  if (this.#options.content)
77
77
  observer.observe(this.#options.content);
78
+ if (this.#options.container)
79
+ observer.observe(this.#options.container);
78
80
  this.#handlerCleanupMap.resize = () => {
79
81
  observer.disconnect();
80
82
  window.removeEventListener("resize", handleResize);
@@ -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.body;
11
- const lockedBodyScroll = container === document.body;
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 && lockedBodyScroll) {
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
- const scrollBarWidth = getScrollbarWidth();
11
- styleCache.set(container, { paddingRight: `${scrollBarWidth}px` });
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 body styles before each test
10
- document.body.style.overflow = "";
11
- document.body.style.paddingRight = "";
12
- document.body.innerHTML = "";
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.body.style.overflow = "";
17
- document.body.style.paddingRight = "";
18
- document.body.innerHTML = "";
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 body", () => {
22
+ test("locks scroll on document", () => {
21
23
  const unlock = lockScroll({});
22
- expect(document.body.style.overflow).toBe("hidden");
24
+ expect(document.documentElement.style.overflow).toBe("hidden");
23
25
  unlock?.();
24
- expect(document.body.style.overflow).toBe("");
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.body.style.overflow).not.toBe("hidden");
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.body.style.overflow).toBe("hidden");
58
+ expect(document.documentElement.style.overflow).toBe("hidden");
57
59
  unlock1?.();
58
- expect(document.body.style.overflow).toBe("");
60
+ expect(document.documentElement.style.overflow).toBe("");
59
61
  unlock2?.();
60
- expect(document.body.style.overflow).toBe("");
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 body)", () => {
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.body.style.overflow).toBe("hidden");
79
+ expect(document.documentElement.style.overflow).toBe("hidden");
78
80
  unlock?.();
79
- expect(document.body.style.overflow).toBe("");
81
+ expect(document.documentElement.style.overflow).toBe("");
80
82
  });
81
83
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reshaped/utilities",
3
- "version": "4.0.0-canary.6",
3
+ "version": "4.0.0",
4
4
  "description": "Vanilla JS utilities for implementing common UI patterns",
5
5
  "homepage": "https://reshaped.so",
6
6
  "bugs": {