@ramstack/pagelock 1.0.0 → 1.1.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/LICENSE CHANGED
@@ -1,9 +1,9 @@
1
- MIT License
2
-
3
- Copyright 2024 Rameel (https://github.com/rameel)
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright 2024 Rameel (https://github.com/rameel)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,101 +1,103 @@
1
- # Pagelock
2
-
3
- The `@ramstack/pagelock` represents a simple utility for managing page scroll locking.
4
- The library weighs around 750 bytes, and has no external dependencies.
5
-
6
- ## Installation
7
-
8
- ### Via NPM
9
- ```sh
10
- npm install --save @ramstack/pagelock
11
- ```
12
-
13
- ### Via CDN
14
- ```html
15
- <script src="https://cdn.jsdelivr.net/npm/@ramstack/pagelock@1.0.0/dist/pagelock.min.js"></script>
16
- ```
17
-
18
- ## Usage examples
19
-
20
- #### Vanilla JS
21
-
22
- ```vue
23
- <button onclick="show()">Show Modal</button>
24
-
25
- <script>
26
- let release;
27
-
28
- async function show() {
29
- // Lock the page scroll before show modal dialog
30
- release = pagelock();
31
-
32
- await showModal();
33
-
34
- // Unlock the page scroll
35
- release();
36
- }
37
- </script>
38
- ```
39
-
40
- #### Vue
41
-
42
- ```vue
43
- <script setup>
44
-
45
- import { ref, watch } from "vue";
46
- import { pagelock } from "@ramstack/pagelock";
47
-
48
- let show = ref(false);
49
- let release;
50
-
51
- watch(show, value => {
52
- if (value) {
53
- // Lock page scroll
54
- release = pagelock();
55
- }
56
- else {
57
- // Unlock page scroll
58
- release?.();
59
- }
60
- });
61
-
62
- </script>
63
-
64
- <template>
65
- <label>
66
- <input v-model="show" type="checkbox" />
67
- Lock page
68
- </label>
69
- <div style="height: 1000px; width: 100%">
70
- </div>
71
- </template>
72
- ```
73
-
74
- ## Functions
75
-
76
- ```js
77
- /**
78
- * Locks the page scroll. Subsequent calls to the function add to the queue of lock holders.
79
- * Page scroll remains locked until the queue is empty. To forcibly release the page scroll,
80
- * bypassing the queue, call the {@link pageunlock} function with `force = true`.
81
- *
82
- * @returns {() => void} - A function to unlock the page scroll.
83
- * Subsequent calls to the release function are safe and only release its own captured lock,
84
- * without affecting the other locks in the queue.
85
- */
86
- function pagelock(): () => void;
87
- ```
88
-
89
- ```js
90
- /**
91
- * Unlocks the page scroll. In contrast to the release function returned by {@link pagelock},
92
- * the {@link pageunlock} function sequentially clears the queue of lock holders.
93
- *
94
- * @param {boolean} [force] - If `true`, forcibly unlocks the page scroll, bypassing the queue;
95
- * otherwise, only the last lock in the queue will be released. The default is `false`.
96
- */
97
- function pageunlock(force?: boolean): void;
98
- ```
99
-
100
- ## License
101
- This package is released under the **MIT License**.
1
+ # @ramstack/pagelock
2
+ [![NPM](https://img.shields.io/npm/v/@ramstack/pagelock)](https://www.npmjs.com/package/@ramstack/pagelock)
3
+ [![MIT](https://img.shields.io/github/license/rameel/pagelock)](https://github.com/rameel/pagelock/blob/main/LICENSE)
4
+
5
+ `@ramstack/pagelock` is a simple utility for managing page scroll locking.
6
+ The library is around 750 bytes in size and has no external dependencies.
7
+
8
+ ## Installation
9
+
10
+ ### Using NPM
11
+ ```sh
12
+ npm install --save @ramstack/pagelock
13
+ ```
14
+
15
+ ### Using CDN
16
+ ```html
17
+ <script src="https://cdn.jsdelivr.net/npm/@ramstack/pagelock@1/dist/pagelock.min.js"></script>
18
+ ```
19
+
20
+ ## Usage examples
21
+
22
+ #### Vanilla JS
23
+
24
+ ```vue
25
+ <button onclick="show()">Show Modal</button>
26
+
27
+ <script>
28
+ let release;
29
+
30
+ async function show() {
31
+ // Lock the page scroll before showing the modal dialog
32
+ release = pagelock();
33
+
34
+ await showModal();
35
+
36
+ // Unlock the page scroll
37
+ release();
38
+ }
39
+ </script>
40
+ ```
41
+
42
+ #### Vue
43
+
44
+ ```vue
45
+ <script setup>
46
+
47
+ import { ref, watch } from "vue";
48
+ import { pagelock } from "@ramstack/pagelock";
49
+
50
+ let show = ref(false);
51
+ let release;
52
+
53
+ watch(show, value => {
54
+ if (value) {
55
+ // Lock page scroll
56
+ release = pagelock();
57
+ }
58
+ else {
59
+ // Unlock page scroll
60
+ release?.();
61
+ }
62
+ });
63
+
64
+ </script>
65
+
66
+ <template>
67
+ <label>
68
+ <input v-model="show" type="checkbox" />
69
+ Lock page
70
+ </label>
71
+ <div style="height: 1000px; width: 100%">
72
+ </div>
73
+ </template>
74
+ ```
75
+
76
+ ## Functions
77
+
78
+ ```js
79
+ /**
80
+ * Locks the page scroll. Subsequent calls to the function add to the queue of lock holders.
81
+ * The page scroll remains locked until the queue is empty. To forcibly release the page scroll,
82
+ * bypassing the queue, call the {@link pageunlock} function with `force = true`.
83
+ *
84
+ * @returns {() => void} - A function to unlock the page scroll.
85
+ * Subsequent calls to this release function are safe: it only releases its own lock
86
+ * without affecting the others in the queue.
87
+ */
88
+ function pagelock(): () => void;
89
+ ```
90
+
91
+ ```js
92
+ /**
93
+ * Unlocks the page scroll. Unlike the release function returned by {@link pagelock},
94
+ * this function removes locks from the end of the queue (one by one).
95
+ *
96
+ * @param {boolean} [force] - If `true`, forcibly unlocks the page scroll, clearing the entire queue.
97
+ * Otherwise, only the most recent lock is released. Defaults to `false`.
98
+ */
99
+ function pageunlock(force?: boolean): void;
100
+ ```
101
+
102
+ ## License
103
+ This package is released under the **MIT License**.
@@ -1,47 +1,44 @@
1
- let locks = [];
2
- let sequence = 0;
3
- let width;
4
- let props;
1
+ const storage = document._r_pagelock ??= { locks: [], sequence: 0 };
5
2
  function pagelock() {
6
- if (!props) {
3
+ if (!storage.props) {
7
4
  const { offsetHeight, clientHeight } = document.documentElement;
8
5
  const style = document.body.style;
9
- props = {
6
+ storage.props = {
10
7
  overflow: style.overflow,
11
8
  paddingRight: style.paddingRight
12
9
  };
13
10
  if (offsetHeight > clientHeight) {
14
- style.paddingRight = scrollbarWidth();
11
+ style.paddingRight = scrollbar_width();
15
12
  }
16
13
  style.overflow = "hidden";
17
14
  }
18
- const seq = ++sequence;
19
- locks.push(seq);
15
+ const seq = ++storage.sequence;
16
+ storage.locks.push(seq);
20
17
  return () => {
21
- locks = locks.filter(v => v !== seq);
18
+ storage.locks = storage.locks.filter(v => v !== seq);
22
19
  restore();
23
20
  };
24
21
  }
25
22
  function pageunlock(force) {
26
- force ? locks = [] : locks.pop();
23
+ force ? storage.locks = [] : storage.locks.pop();
27
24
  restore();
28
25
  }
29
26
  function restore() {
30
- if (props && !locks.length) {
31
- Object.assign(document.body.style, props);
32
- props = null;
27
+ if (storage.props && !storage.locks.length) {
28
+ Object.assign(document.body.style, storage.props);
29
+ storage.props = null;
33
30
  }
34
31
  }
35
- function scrollbarWidth() {
36
- if (!width) {
32
+ function scrollbar_width() {
33
+ if (!storage.width) {
37
34
  const outer = document.createElement("div");
38
35
  outer.innerHTML = "<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";
39
36
  const inner = outer.firstChild;
40
37
  document.body.appendChild(outer);
41
- width = (inner.offsetWidth - inner.clientWidth) + "px";
38
+ storage.width = (inner.offsetWidth - inner.clientWidth) + "px";
42
39
  document.body.removeChild(outer);
43
40
  }
44
- return width;
41
+ return storage.width;
45
42
  }
46
43
 
47
- export { pagelock, pageunlock };
44
+ export { pagelock, pageunlock };
@@ -1 +1 @@
1
- let t,e,o=[],i=0;function n(){if(!e){const{offsetHeight:o,clientHeight:i}=document.documentElement,n=document.body.style;e={overflow:n.overflow,paddingRight:n.paddingRight},o>i&&(n.paddingRight=function(){if(!t){const e=document.createElement("div");e.innerHTML="<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";const o=e.firstChild;document.body.appendChild(e),t=o.offsetWidth-o.clientWidth+"px",document.body.removeChild(e)}return t}()),n.overflow="hidden"}const n=++i;return o.push(n),()=>{o=o.filter((t=>t!==n)),l()}}function d(t){t?o=[]:o.pop(),l()}function l(){e&&!o.length&&(Object.assign(document.body.style,e),e=null)}export{n as pagelock,d as pageunlock};
1
+ const t=document._r_pagelock??={locks:[],sequence:0};function o(){if(!t.props){const{offsetHeight:o,clientHeight:e}=document.documentElement,n=document.body.style;t.props={overflow:n.overflow,paddingRight:n.paddingRight},o>e&&(n.paddingRight=function(){if(!t.width){const o=document.createElement("div");o.innerHTML="<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";const e=o.firstChild;document.body.appendChild(o),t.width=e.offsetWidth-e.clientWidth+"px",document.body.removeChild(o)}return t.width}()),n.overflow="hidden"}const o=++t.sequence;return t.locks.push(o),()=>{t.locks=t.locks.filter(t=>t!==o),n()}}function e(o){o?t.locks=[]:t.locks.pop(),n()}function n(){t.props&&!t.locks.length&&(Object.assign(document.body.style,t.props),t.props=null)}export{o as pagelock,e as pageunlock};
package/dist/pagelock.js CHANGED
@@ -1,53 +1,50 @@
1
1
  (function (exports) {
2
2
  'use strict';
3
3
 
4
- let locks = [];
5
- let sequence = 0;
6
- let width;
7
- let props;
4
+ const storage = document._r_pagelock ??= { locks: [], sequence: 0 };
8
5
  function pagelock() {
9
- if (!props) {
6
+ if (!storage.props) {
10
7
  const { offsetHeight, clientHeight } = document.documentElement;
11
8
  const style = document.body.style;
12
- props = {
9
+ storage.props = {
13
10
  overflow: style.overflow,
14
11
  paddingRight: style.paddingRight
15
12
  };
16
13
  if (offsetHeight > clientHeight) {
17
- style.paddingRight = scrollbarWidth();
14
+ style.paddingRight = scrollbar_width();
18
15
  }
19
16
  style.overflow = "hidden";
20
17
  }
21
- const seq = ++sequence;
22
- locks.push(seq);
18
+ const seq = ++storage.sequence;
19
+ storage.locks.push(seq);
23
20
  return () => {
24
- locks = locks.filter(v => v !== seq);
21
+ storage.locks = storage.locks.filter(v => v !== seq);
25
22
  restore();
26
23
  };
27
24
  }
28
25
  function pageunlock(force) {
29
- force ? locks = [] : locks.pop();
26
+ force ? storage.locks = [] : storage.locks.pop();
30
27
  restore();
31
28
  }
32
29
  function restore() {
33
- if (props && !locks.length) {
34
- Object.assign(document.body.style, props);
35
- props = null;
30
+ if (storage.props && !storage.locks.length) {
31
+ Object.assign(document.body.style, storage.props);
32
+ storage.props = null;
36
33
  }
37
34
  }
38
- function scrollbarWidth() {
39
- if (!width) {
35
+ function scrollbar_width() {
36
+ if (!storage.width) {
40
37
  const outer = document.createElement("div");
41
38
  outer.innerHTML = "<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";
42
39
  const inner = outer.firstChild;
43
40
  document.body.appendChild(outer);
44
- width = (inner.offsetWidth - inner.clientWidth) + "px";
41
+ storage.width = (inner.offsetWidth - inner.clientWidth) + "px";
45
42
  document.body.removeChild(outer);
46
43
  }
47
- return width;
44
+ return storage.width;
48
45
  }
49
46
 
50
47
  exports.pagelock = pagelock;
51
48
  exports.pageunlock = pageunlock;
52
49
 
53
- })(this.window = this.window || {});
50
+ })(this.window = this.window || {});
@@ -1 +1 @@
1
- !function(t){"use strict";let e,i,n=[],o=0;function d(){i&&!n.length&&(Object.assign(document.body.style,i),i=null)}t.pagelock=function(){if(!i){const{offsetHeight:t,clientHeight:n}=document.documentElement,o=document.body.style;i={overflow:o.overflow,paddingRight:o.paddingRight},t>n&&(o.paddingRight=function(){if(!e){const t=document.createElement("div");t.innerHTML="<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";const i=t.firstChild;document.body.appendChild(t),e=i.offsetWidth-i.clientWidth+"px",document.body.removeChild(t)}return e}()),o.overflow="hidden"}const t=++o;return n.push(t),()=>{n=n.filter((e=>e!==t)),d()}},t.pageunlock=function(t){t?n=[]:n.pop(),d()}}(this.window=this.window||{});
1
+ !function(t){"use strict";const o=document._r_pagelock??={locks:[],sequence:0};function e(){o.props&&!o.locks.length&&(Object.assign(document.body.style,o.props),o.props=null)}t.pagelock=function(){if(!o.props){const{offsetHeight:t,clientHeight:e}=document.documentElement,i=document.body.style;o.props={overflow:i.overflow,paddingRight:i.paddingRight},t>e&&(i.paddingRight=function(){if(!o.width){const t=document.createElement("div");t.innerHTML="<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";const e=t.firstChild;document.body.appendChild(t),o.width=e.offsetWidth-e.clientWidth+"px",document.body.removeChild(t)}return o.width}()),i.overflow="hidden"}const t=++o.sequence;return o.locks.push(t),()=>{o.locks=o.locks.filter(o=>o!==t),e()}},t.pageunlock=function(t){t?o.locks=[]:o.locks.pop(),e()}}(this.window=this.window||{});
@@ -1,18 +1,18 @@
1
1
  /**
2
2
  * Locks the page scroll. Subsequent calls to the function add to the queue of lock holders.
3
- * Page scroll remains locked until the queue is empty. To forcibly release the page scroll,
3
+ * The page scroll remains locked until the queue is empty. To forcibly release the page scroll,
4
4
  * bypassing the queue, call the {@link pageunlock} function with `force = true`.
5
5
  *
6
6
  * @returns {() => void} - A function to unlock the page scroll.
7
- * Subsequent calls to the release function are safe and only release its own captured lock,
8
- * without affecting the other locks in the queue.
7
+ * Subsequent calls to this release function are safe: it only releases its own lock
8
+ * without affecting the others in the queue.
9
9
  */
10
10
  export declare function pagelock(): () => void;
11
11
  /**
12
- * Unlocks the page scroll. In contrast to the release function returned by {@link pagelock},
13
- * the {@link pageunlock} function sequentially clears the queue of lock holders.
12
+ * Unlocks the page scroll. Unlike the release function returned by {@link pagelock},
13
+ * this function removes locks from the end of the queue (one by one).
14
14
  *
15
- * @param {boolean} [force] - If `true`, forcibly unlocks the page scroll, bypassing the queue;
16
- * otherwise, only the last lock in the queue will be released. The default is `false`.
15
+ * @param {boolean} [force] - If `true`, forcibly unlocks the page scroll, clearing the entire queue.
16
+ * Otherwise, only the most recent lock is released. Defaults to `false`.
17
17
  */
18
18
  export declare function pageunlock(force?: boolean): void;
package/package.json CHANGED
@@ -1,26 +1,32 @@
1
1
  {
2
2
  "name": "@ramstack/pagelock",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "A simple utility for managing page scroll locking. No external dependencies.",
5
5
  "type": "module",
6
- "main": "dist/pagelock.esm.js",
7
- "module": "dist/pagelock.esm.js",
8
- "types": "dist/types/pagelock.d.ts",
6
+ "author": "rameel <rameel-b@hotmail.com>",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/rameel/pagelock.git"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "import": {
15
+ "types": "./dist/types/pagelock.d.ts",
16
+ "default": "./dist/pagelock.esm.js"
17
+ }
18
+ }
19
+ },
9
20
  "files": [
10
21
  "dist",
11
22
  "README.md",
12
23
  "LICENSE"
13
24
  ],
14
- "author": "rameel <rameel-b@hotmail.com>",
15
- "license": "MIT",
16
25
  "scripts": {
17
- "build": "cross-env NODE_ENV=production rollup -c",
26
+ "build": "rollup -c",
18
27
  "prebuild": "npm run clean",
19
- "clean": "rimraf dist"
20
- },
21
- "repository": {
22
- "type": "git",
23
- "url": "git+https://github.com/rameel/pagelock.git"
28
+ "clean": "rimraf dist",
29
+ "size-report": "node scripts/compare-package-sizes.js"
24
30
  },
25
31
  "keywords": [
26
32
  "pagelock",
@@ -29,17 +35,15 @@
29
35
  "scroll-lock"
30
36
  ],
31
37
  "devDependencies": {
32
- "@rollup/plugin-node-resolve": "^15.2.3",
33
- "@rollup/plugin-replace": "^5.0.5",
38
+ "@rollup/plugin-node-resolve": "^16.0.3",
34
39
  "@rollup/plugin-terser": "^0.4.4",
35
- "@rollup/plugin-typescript": "^11.1.6",
36
- "cross-env": "^7.0.3",
37
- "rimraf": "^5.0.5",
38
- "rollup": "^4.9.6",
40
+ "@rollup/plugin-typescript": "^12.3.0",
41
+ "rimraf": "^6.1.2",
42
+ "rollup": "^4.55.1",
39
43
  "rollup-plugin-bundle-size": "^1.0.3",
40
44
  "strip-comments": "^2.0.1",
41
- "terser": "^5.27.0",
42
- "tslib": "^2.6.2",
43
- "typescript": "^5.3.3"
45
+ "terser": "^5.44.1",
46
+ "tslib": "^2.8.1",
47
+ "typescript": "^5.9.3"
44
48
  }
45
49
  }