@zag-js/remove-scroll 0.1.5 → 0.2.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/dist/index.js +38 -8
- package/dist/index.mjs +87 -0
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
|
|
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() {
|
|
30
|
+
var _a;
|
|
4
31
|
const agent = navigator.userAgentData;
|
|
5
|
-
return (agent == null ? void 0 : agent.platform)
|
|
32
|
+
return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
|
|
6
33
|
}
|
|
7
34
|
var pt = (v) => isDom() && v.test(getPlatform());
|
|
8
35
|
var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
|
|
@@ -42,8 +69,9 @@ function getPaddingProperty(documentElement) {
|
|
|
42
69
|
return scrollbarX ? "paddingLeft" : "paddingRight";
|
|
43
70
|
}
|
|
44
71
|
function preventBodyScroll(_document) {
|
|
45
|
-
|
|
46
|
-
const
|
|
72
|
+
var _a;
|
|
73
|
+
const doc = _document != null ? _document : document;
|
|
74
|
+
const win = (_a = doc.defaultView) != null ? _a : window;
|
|
47
75
|
const { documentElement, body } = doc;
|
|
48
76
|
const locked = body.hasAttribute(LOCK_CLASSNAME);
|
|
49
77
|
if (locked)
|
|
@@ -57,9 +85,10 @@ function preventBodyScroll(_document) {
|
|
|
57
85
|
[paddingProperty]: `${scrollbarWidth}px`
|
|
58
86
|
});
|
|
59
87
|
const setIOSStyle = () => {
|
|
88
|
+
var _a2, _b;
|
|
60
89
|
const { scrollX, scrollY, visualViewport } = win;
|
|
61
|
-
const offsetLeft = (visualViewport == null ? void 0 : visualViewport.offsetLeft)
|
|
62
|
-
const offsetTop = (visualViewport == null ? void 0 : visualViewport.offsetTop)
|
|
90
|
+
const offsetLeft = (_a2 = visualViewport == null ? void 0 : visualViewport.offsetLeft) != null ? _a2 : 0;
|
|
91
|
+
const offsetTop = (_b = visualViewport == null ? void 0 : visualViewport.offsetTop) != null ? _b : 0;
|
|
63
92
|
const restoreStyle = assignStyle(body, {
|
|
64
93
|
position: "fixed",
|
|
65
94
|
overflow: "hidden",
|
|
@@ -79,6 +108,7 @@ function preventBodyScroll(_document) {
|
|
|
79
108
|
body.removeAttribute(LOCK_CLASSNAME);
|
|
80
109
|
};
|
|
81
110
|
}
|
|
82
|
-
export
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
83
113
|
preventBodyScroll
|
|
84
|
-
};
|
|
114
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// ../dom/dist/index.mjs
|
|
2
|
+
var isDom = () => typeof window !== "undefined";
|
|
3
|
+
function getPlatform() {
|
|
4
|
+
var _a;
|
|
5
|
+
const agent = navigator.userAgentData;
|
|
6
|
+
return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
|
|
7
|
+
}
|
|
8
|
+
var pt = (v) => isDom() && v.test(getPlatform());
|
|
9
|
+
var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
|
|
10
|
+
var isMac = () => pt(/^Mac/) && !isTouchDevice;
|
|
11
|
+
var isApple = () => pt(/mac|iphone|ipad|ipod/i);
|
|
12
|
+
var isIos = () => isApple() && !isMac();
|
|
13
|
+
|
|
14
|
+
// src/index.ts
|
|
15
|
+
var LOCK_CLASSNAME = "data-scroll-lock";
|
|
16
|
+
function assignStyle(el, style) {
|
|
17
|
+
if (!el)
|
|
18
|
+
return () => {
|
|
19
|
+
};
|
|
20
|
+
const previousStyle = el.style.cssText;
|
|
21
|
+
Object.assign(el.style, style);
|
|
22
|
+
return () => {
|
|
23
|
+
el.style.cssText = previousStyle;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function setCSSProperty(el, property, value) {
|
|
27
|
+
if (!el)
|
|
28
|
+
return () => {
|
|
29
|
+
};
|
|
30
|
+
const previousValue = el.style.getPropertyValue(property);
|
|
31
|
+
el.style.setProperty(property, value);
|
|
32
|
+
return () => {
|
|
33
|
+
if (previousValue) {
|
|
34
|
+
el.style.setProperty(property, previousValue);
|
|
35
|
+
} else {
|
|
36
|
+
el.style.removeProperty(property);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function getPaddingProperty(documentElement) {
|
|
41
|
+
const documentLeft = documentElement.getBoundingClientRect().left;
|
|
42
|
+
const scrollbarX = Math.round(documentLeft) + documentElement.scrollLeft;
|
|
43
|
+
return scrollbarX ? "paddingLeft" : "paddingRight";
|
|
44
|
+
}
|
|
45
|
+
function preventBodyScroll(_document) {
|
|
46
|
+
var _a;
|
|
47
|
+
const doc = _document != null ? _document : document;
|
|
48
|
+
const win = (_a = doc.defaultView) != null ? _a : window;
|
|
49
|
+
const { documentElement, body } = doc;
|
|
50
|
+
const locked = body.hasAttribute(LOCK_CLASSNAME);
|
|
51
|
+
if (locked)
|
|
52
|
+
return;
|
|
53
|
+
body.setAttribute(LOCK_CLASSNAME, "");
|
|
54
|
+
const scrollbarWidth = win.innerWidth - documentElement.clientWidth;
|
|
55
|
+
const setScrollbarWidthProperty = () => setCSSProperty(documentElement, "--scrollbar-width", `${scrollbarWidth}px`);
|
|
56
|
+
const paddingProperty = getPaddingProperty(documentElement);
|
|
57
|
+
const setStyle = () => assignStyle(body, {
|
|
58
|
+
overflow: "hidden",
|
|
59
|
+
[paddingProperty]: `${scrollbarWidth}px`
|
|
60
|
+
});
|
|
61
|
+
const setIOSStyle = () => {
|
|
62
|
+
var _a2, _b;
|
|
63
|
+
const { scrollX, scrollY, visualViewport } = win;
|
|
64
|
+
const offsetLeft = (_a2 = visualViewport == null ? void 0 : visualViewport.offsetLeft) != null ? _a2 : 0;
|
|
65
|
+
const offsetTop = (_b = visualViewport == null ? void 0 : visualViewport.offsetTop) != null ? _b : 0;
|
|
66
|
+
const restoreStyle = assignStyle(body, {
|
|
67
|
+
position: "fixed",
|
|
68
|
+
overflow: "hidden",
|
|
69
|
+
top: `${-(scrollY - Math.floor(offsetTop))}px`,
|
|
70
|
+
left: `${-(scrollX - Math.floor(offsetLeft))}px`,
|
|
71
|
+
right: "0",
|
|
72
|
+
[paddingProperty]: `${scrollbarWidth}px`
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
restoreStyle();
|
|
76
|
+
win.scrollTo(scrollX, scrollY);
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const cleanups = [setScrollbarWidthProperty(), isIos() ? setIOSStyle() : setStyle()];
|
|
80
|
+
return () => {
|
|
81
|
+
cleanups.forEach((cleanup) => cleanup());
|
|
82
|
+
body.removeAttribute(LOCK_CLASSNAME);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
preventBodyScroll
|
|
87
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@zag-js/remove-scroll",
|
|
4
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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.
|
|
25
|
+
"@zag-js/dom-utils": "0.2.0"
|
|
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",
|