@zag-js/remove-scroll 0.1.5 → 0.1.6

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.js CHANGED
@@ -1,4 +1,30 @@
1
- // ../dom/dist/index.js
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ preventBodyScroll: () => preventBodyScroll
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // ../dom/dist/index.mjs
2
28
  var isDom = () => typeof window !== "undefined";
3
29
  function getPlatform() {
4
30
  const agent = navigator.userAgentData;
@@ -79,6 +105,7 @@ function preventBodyScroll(_document) {
79
105
  body.removeAttribute(LOCK_CLASSNAME);
80
106
  };
81
107
  }
82
- export {
108
+ // Annotate the CommonJS export names for ESM import in node:
109
+ 0 && (module.exports = {
83
110
  preventBodyScroll
84
- };
111
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,84 @@
1
+ // ../dom/dist/index.mjs
2
+ var isDom = () => typeof window !== "undefined";
3
+ function getPlatform() {
4
+ const agent = navigator.userAgentData;
5
+ return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
6
+ }
7
+ var pt = (v) => isDom() && v.test(getPlatform());
8
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
9
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
10
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
11
+ var isIos = () => isApple() && !isMac();
12
+
13
+ // src/index.ts
14
+ var LOCK_CLASSNAME = "data-scroll-lock";
15
+ function assignStyle(el, style) {
16
+ if (!el)
17
+ return () => {
18
+ };
19
+ const previousStyle = el.style.cssText;
20
+ Object.assign(el.style, style);
21
+ return () => {
22
+ el.style.cssText = previousStyle;
23
+ };
24
+ }
25
+ function setCSSProperty(el, property, value) {
26
+ if (!el)
27
+ return () => {
28
+ };
29
+ const previousValue = el.style.getPropertyValue(property);
30
+ el.style.setProperty(property, value);
31
+ return () => {
32
+ if (previousValue) {
33
+ el.style.setProperty(property, previousValue);
34
+ } else {
35
+ el.style.removeProperty(property);
36
+ }
37
+ };
38
+ }
39
+ function getPaddingProperty(documentElement) {
40
+ const documentLeft = documentElement.getBoundingClientRect().left;
41
+ const scrollbarX = Math.round(documentLeft) + documentElement.scrollLeft;
42
+ return scrollbarX ? "paddingLeft" : "paddingRight";
43
+ }
44
+ function preventBodyScroll(_document) {
45
+ const doc = _document ?? document;
46
+ const win = doc.defaultView ?? window;
47
+ const { documentElement, body } = doc;
48
+ const locked = body.hasAttribute(LOCK_CLASSNAME);
49
+ if (locked)
50
+ return;
51
+ body.setAttribute(LOCK_CLASSNAME, "");
52
+ const scrollbarWidth = win.innerWidth - documentElement.clientWidth;
53
+ const setScrollbarWidthProperty = () => setCSSProperty(documentElement, "--scrollbar-width", `${scrollbarWidth}px`);
54
+ const paddingProperty = getPaddingProperty(documentElement);
55
+ const setStyle = () => assignStyle(body, {
56
+ overflow: "hidden",
57
+ [paddingProperty]: `${scrollbarWidth}px`
58
+ });
59
+ const setIOSStyle = () => {
60
+ const { scrollX, scrollY, visualViewport } = win;
61
+ const offsetLeft = (visualViewport == null ? void 0 : visualViewport.offsetLeft) ?? 0;
62
+ const offsetTop = (visualViewport == null ? void 0 : visualViewport.offsetTop) ?? 0;
63
+ const restoreStyle = assignStyle(body, {
64
+ position: "fixed",
65
+ overflow: "hidden",
66
+ top: `${-(scrollY - Math.floor(offsetTop))}px`,
67
+ left: `${-(scrollX - Math.floor(offsetLeft))}px`,
68
+ right: "0",
69
+ [paddingProperty]: `${scrollbarWidth}px`
70
+ });
71
+ return () => {
72
+ restoreStyle();
73
+ win.scrollTo(scrollX, scrollY);
74
+ };
75
+ };
76
+ const cleanups = [setScrollbarWidthProperty(), isIos() ? setIOSStyle() : setStyle()];
77
+ return () => {
78
+ cleanups.forEach((cleanup) => cleanup());
79
+ body.removeAttribute(LOCK_CLASSNAME);
80
+ };
81
+ }
82
+ export {
83
+ preventBodyScroll
84
+ };
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
- "type": "module",
3
2
  "name": "@zag-js/remove-scroll",
4
- "version": "0.1.5",
3
+ "version": "0.1.6",
5
4
  "description": "JavaScript utility to remove scroll on body",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
6
8
  "keywords": [
7
9
  "js",
8
10
  "utils",
@@ -11,8 +13,6 @@
11
13
  "author": "Segun Adebayo <sage@adebayosegun.com>",
12
14
  "homepage": "https://github.com/chakra-ui/zag#readme",
13
15
  "license": "MIT",
14
- "main": "dist/index.js",
15
- "types": "dist/index.d.ts",
16
16
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/utilities/remove-scroll",
17
17
  "sideEffects": false,
18
18
  "files": [
@@ -22,15 +22,15 @@
22
22
  "access": "public"
23
23
  },
24
24
  "devDependencies": {
25
- "@zag-js/dom-utils": "0.1.12"
25
+ "@zag-js/dom-utils": "0.1.13"
26
26
  },
27
27
  "bugs": {
28
28
  "url": "https://github.com/chakra-ui/zag/issues"
29
29
  },
30
30
  "scripts": {
31
- "build-fast": "tsup src/index.ts --format=esm",
31
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
32
32
  "start": "pnpm build --watch",
33
- "build": "tsup src/index.ts --format=esm --dts",
33
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
34
34
  "test": "jest --config ../../../jest.config.js --rootDir tests",
35
35
  "lint": "eslint src --ext .ts,.tsx",
36
36
  "test-ci": "pnpm test --ci --runInBand -u",