@ramstack/pagelock 1.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/LICENSE +9 -0
- package/README.md +101 -0
- package/dist/pagelock.esm.js +47 -0
- package/dist/pagelock.esm.min.js +1 -0
- package/dist/pagelock.js +53 -0
- package/dist/pagelock.min.js +1 -0
- package/dist/types/pagelock.d.ts +18 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +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.
|
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
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**.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
let locks = [];
|
|
2
|
+
let sequence = 0;
|
|
3
|
+
let width;
|
|
4
|
+
let props;
|
|
5
|
+
function pagelock() {
|
|
6
|
+
if (!props) {
|
|
7
|
+
const { offsetHeight, clientHeight } = document.documentElement;
|
|
8
|
+
const style = document.body.style;
|
|
9
|
+
props = {
|
|
10
|
+
overflow: style.overflow,
|
|
11
|
+
paddingRight: style.paddingRight
|
|
12
|
+
};
|
|
13
|
+
if (offsetHeight > clientHeight) {
|
|
14
|
+
style.paddingRight = scrollbarWidth();
|
|
15
|
+
}
|
|
16
|
+
style.overflow = "hidden";
|
|
17
|
+
}
|
|
18
|
+
const seq = ++sequence;
|
|
19
|
+
locks.push(seq);
|
|
20
|
+
return () => {
|
|
21
|
+
locks = locks.filter(v => v !== seq);
|
|
22
|
+
restore();
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function pageunlock(force) {
|
|
26
|
+
force ? locks = [] : locks.pop();
|
|
27
|
+
restore();
|
|
28
|
+
}
|
|
29
|
+
function restore() {
|
|
30
|
+
if (props && !locks.length) {
|
|
31
|
+
Object.assign(document.body.style, props);
|
|
32
|
+
props = null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function scrollbarWidth() {
|
|
36
|
+
if (!width) {
|
|
37
|
+
const outer = document.createElement("div");
|
|
38
|
+
outer.innerHTML = "<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";
|
|
39
|
+
const inner = outer.firstChild;
|
|
40
|
+
document.body.appendChild(outer);
|
|
41
|
+
width = (inner.offsetWidth - inner.clientWidth) + "px";
|
|
42
|
+
document.body.removeChild(outer);
|
|
43
|
+
}
|
|
44
|
+
return width;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export { pagelock, pageunlock };
|
|
@@ -0,0 +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};
|
package/dist/pagelock.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
(function (exports) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
let locks = [];
|
|
5
|
+
let sequence = 0;
|
|
6
|
+
let width;
|
|
7
|
+
let props;
|
|
8
|
+
function pagelock() {
|
|
9
|
+
if (!props) {
|
|
10
|
+
const { offsetHeight, clientHeight } = document.documentElement;
|
|
11
|
+
const style = document.body.style;
|
|
12
|
+
props = {
|
|
13
|
+
overflow: style.overflow,
|
|
14
|
+
paddingRight: style.paddingRight
|
|
15
|
+
};
|
|
16
|
+
if (offsetHeight > clientHeight) {
|
|
17
|
+
style.paddingRight = scrollbarWidth();
|
|
18
|
+
}
|
|
19
|
+
style.overflow = "hidden";
|
|
20
|
+
}
|
|
21
|
+
const seq = ++sequence;
|
|
22
|
+
locks.push(seq);
|
|
23
|
+
return () => {
|
|
24
|
+
locks = locks.filter(v => v !== seq);
|
|
25
|
+
restore();
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function pageunlock(force) {
|
|
29
|
+
force ? locks = [] : locks.pop();
|
|
30
|
+
restore();
|
|
31
|
+
}
|
|
32
|
+
function restore() {
|
|
33
|
+
if (props && !locks.length) {
|
|
34
|
+
Object.assign(document.body.style, props);
|
|
35
|
+
props = null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function scrollbarWidth() {
|
|
39
|
+
if (!width) {
|
|
40
|
+
const outer = document.createElement("div");
|
|
41
|
+
outer.innerHTML = "<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";
|
|
42
|
+
const inner = outer.firstChild;
|
|
43
|
+
document.body.appendChild(outer);
|
|
44
|
+
width = (inner.offsetWidth - inner.clientWidth) + "px";
|
|
45
|
+
document.body.removeChild(outer);
|
|
46
|
+
}
|
|
47
|
+
return width;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.pagelock = pagelock;
|
|
51
|
+
exports.pageunlock = pageunlock;
|
|
52
|
+
|
|
53
|
+
})(this.window = this.window || {});
|
|
@@ -0,0 +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||{});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
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,
|
|
4
|
+
* bypassing the queue, call the {@link pageunlock} function with `force = true`.
|
|
5
|
+
*
|
|
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.
|
|
9
|
+
*/
|
|
10
|
+
export declare function pagelock(): () => void;
|
|
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.
|
|
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`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function pageunlock(force?: boolean): void;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ramstack/pagelock",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple utility for managing page scroll locking. No external dependencies.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/pagelock.esm.js",
|
|
7
|
+
"module": "dist/pagelock.esm.js",
|
|
8
|
+
"types": "dist/types/pagelock.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"author": "rameel <rameel-b@hotmail.com>",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "cross-env NODE_ENV=production rollup -c",
|
|
18
|
+
"prebuild": "npm run clean",
|
|
19
|
+
"clean": "rimraf dist"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/rameel/pagelock.git"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"pagelock",
|
|
27
|
+
"page-locking",
|
|
28
|
+
"scroll-locking",
|
|
29
|
+
"scroll-lock"
|
|
30
|
+
],
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
33
|
+
"@rollup/plugin-replace": "^5.0.5",
|
|
34
|
+
"@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",
|
|
39
|
+
"rollup-plugin-bundle-size": "^1.0.3",
|
|
40
|
+
"strip-comments": "^2.0.1",
|
|
41
|
+
"terser": "^5.27.0",
|
|
42
|
+
"tslib": "^2.6.2",
|
|
43
|
+
"typescript": "^5.3.3"
|
|
44
|
+
}
|
|
45
|
+
}
|