@zag-js/remove-scroll 1.40.0 → 1.41.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/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- declare function preventBodyScroll(_document?: Document): (() => void) | undefined;
1
+ declare function preventBodyScroll(_document?: Document): VoidFunction;
2
2
 
3
3
  export { preventBodyScroll };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare function preventBodyScroll(_document?: Document): (() => void) | undefined;
1
+ declare function preventBodyScroll(_document?: Document): VoidFunction;
2
2
 
3
3
  export { preventBodyScroll };
package/dist/index.js CHANGED
@@ -25,6 +25,7 @@ __export(index_exports, {
25
25
  module.exports = __toCommonJS(index_exports);
26
26
  var import_dom_query = require("@zag-js/dom-query");
27
27
  var LOCK_CLASSNAME = "data-scroll-lock";
28
+ var lockMap = /* @__PURE__ */ new WeakMap();
28
29
  function getPaddingProperty(documentElement) {
29
30
  const documentLeft = documentElement.getBoundingClientRect().left;
30
31
  const scrollbarX = Math.round(documentLeft) + documentElement.scrollLeft;
@@ -35,12 +36,9 @@ function hasStableScrollbarGutter(element) {
35
36
  const scrollbarGutter = styles?.scrollbarGutter;
36
37
  return scrollbarGutter === "stable" || scrollbarGutter?.startsWith("stable ") === true;
37
38
  }
38
- function preventBodyScroll(_document) {
39
- const doc = _document ?? document;
39
+ function applyLock(doc) {
40
40
  const win = doc.defaultView ?? window;
41
41
  const { documentElement, body } = doc;
42
- const locked = body.hasAttribute(LOCK_CLASSNAME);
43
- if (locked) return;
44
42
  const hasStableGutter = hasStableScrollbarGutter(documentElement) || hasStableScrollbarGutter(body);
45
43
  const scrollbarWidth = win.innerWidth - documentElement.clientWidth;
46
44
  body.setAttribute(LOCK_CLASSNAME, "");
@@ -81,6 +79,26 @@ function preventBodyScroll(_document) {
81
79
  body.removeAttribute(LOCK_CLASSNAME);
82
80
  };
83
81
  }
82
+ function preventBodyScroll(_document) {
83
+ const doc = _document ?? document;
84
+ let state = lockMap.get(doc);
85
+ if (!state) {
86
+ state = { count: 0, cleanup: applyLock(doc) };
87
+ lockMap.set(doc, state);
88
+ }
89
+ state.count++;
90
+ const lockState = state;
91
+ let released = false;
92
+ return () => {
93
+ if (released) return;
94
+ released = true;
95
+ lockState.count--;
96
+ if (lockState.count === 0) {
97
+ lockState.cleanup();
98
+ lockMap.delete(doc);
99
+ }
100
+ };
101
+ }
84
102
  // Annotate the CommonJS export names for ESM import in node:
85
103
  0 && (module.exports = {
86
104
  preventBodyScroll
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/index.ts
2
2
  import { getComputedStyle, isIos, setStyleProperty, setStyle } from "@zag-js/dom-query";
3
3
  var LOCK_CLASSNAME = "data-scroll-lock";
4
+ var lockMap = /* @__PURE__ */ new WeakMap();
4
5
  function getPaddingProperty(documentElement) {
5
6
  const documentLeft = documentElement.getBoundingClientRect().left;
6
7
  const scrollbarX = Math.round(documentLeft) + documentElement.scrollLeft;
@@ -11,12 +12,9 @@ function hasStableScrollbarGutter(element) {
11
12
  const scrollbarGutter = styles?.scrollbarGutter;
12
13
  return scrollbarGutter === "stable" || scrollbarGutter?.startsWith("stable ") === true;
13
14
  }
14
- function preventBodyScroll(_document) {
15
- const doc = _document ?? document;
15
+ function applyLock(doc) {
16
16
  const win = doc.defaultView ?? window;
17
17
  const { documentElement, body } = doc;
18
- const locked = body.hasAttribute(LOCK_CLASSNAME);
19
- if (locked) return;
20
18
  const hasStableGutter = hasStableScrollbarGutter(documentElement) || hasStableScrollbarGutter(body);
21
19
  const scrollbarWidth = win.innerWidth - documentElement.clientWidth;
22
20
  body.setAttribute(LOCK_CLASSNAME, "");
@@ -57,6 +55,26 @@ function preventBodyScroll(_document) {
57
55
  body.removeAttribute(LOCK_CLASSNAME);
58
56
  };
59
57
  }
58
+ function preventBodyScroll(_document) {
59
+ const doc = _document ?? document;
60
+ let state = lockMap.get(doc);
61
+ if (!state) {
62
+ state = { count: 0, cleanup: applyLock(doc) };
63
+ lockMap.set(doc, state);
64
+ }
65
+ state.count++;
66
+ const lockState = state;
67
+ let released = false;
68
+ return () => {
69
+ if (released) return;
70
+ released = true;
71
+ lockState.count--;
72
+ if (lockState.count === 0) {
73
+ lockState.cleanup();
74
+ lockMap.delete(doc);
75
+ }
76
+ };
77
+ }
60
78
  export {
61
79
  preventBodyScroll
62
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/remove-scroll",
3
- "version": "1.40.0",
3
+ "version": "1.41.1",
4
4
  "description": "JavaScript utility to remove scroll on body",
5
5
  "keywords": [
6
6
  "js",
@@ -19,7 +19,7 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@zag-js/dom-query": "1.40.0"
22
+ "@zag-js/dom-query": "1.41.1"
23
23
  },
24
24
  "devDependencies": {
25
25
  "clean-package": "2.2.0"