@ramstack/pagelock 1.0.1 → 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/README.md +16 -14
- package/dist/pagelock.esm.js +16 -19
- package/dist/pagelock.esm.min.js +1 -1
- package/dist/pagelock.js +16 -19
- package/dist/pagelock.min.js +1 -1
- package/dist/types/pagelock.d.ts +7 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ramstack/pagelock
|
|
2
|
+
[](https://www.npmjs.com/package/@ramstack/pagelock)
|
|
3
|
+
[](https://github.com/rameel/pagelock/blob/main/LICENSE)
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
The library
|
|
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.
|
|
5
7
|
|
|
6
8
|
## Installation
|
|
7
9
|
|
|
8
|
-
###
|
|
10
|
+
### Using NPM
|
|
9
11
|
```sh
|
|
10
12
|
npm install --save @ramstack/pagelock
|
|
11
13
|
```
|
|
12
14
|
|
|
13
|
-
###
|
|
15
|
+
### Using CDN
|
|
14
16
|
```html
|
|
15
|
-
<script src="https://cdn.jsdelivr.net/npm/@ramstack/pagelock@1
|
|
17
|
+
<script src="https://cdn.jsdelivr.net/npm/@ramstack/pagelock@1/dist/pagelock.min.js"></script>
|
|
16
18
|
```
|
|
17
19
|
|
|
18
20
|
## Usage examples
|
|
@@ -26,7 +28,7 @@ npm install --save @ramstack/pagelock
|
|
|
26
28
|
let release;
|
|
27
29
|
|
|
28
30
|
async function show() {
|
|
29
|
-
// Lock the page scroll before
|
|
31
|
+
// Lock the page scroll before showing the modal dialog
|
|
30
32
|
release = pagelock();
|
|
31
33
|
|
|
32
34
|
await showModal();
|
|
@@ -76,23 +78,23 @@ watch(show, value => {
|
|
|
76
78
|
```js
|
|
77
79
|
/**
|
|
78
80
|
* Locks the page scroll. Subsequent calls to the function add to the queue of lock holders.
|
|
79
|
-
*
|
|
81
|
+
* The page scroll remains locked until the queue is empty. To forcibly release the page scroll,
|
|
80
82
|
* bypassing the queue, call the {@link pageunlock} function with `force = true`.
|
|
81
83
|
*
|
|
82
84
|
* @returns {() => void} - A function to unlock the page scroll.
|
|
83
|
-
* Subsequent calls to
|
|
84
|
-
* without affecting the
|
|
85
|
+
* Subsequent calls to this release function are safe: it only releases its own lock
|
|
86
|
+
* without affecting the others in the queue.
|
|
85
87
|
*/
|
|
86
88
|
function pagelock(): () => void;
|
|
87
89
|
```
|
|
88
90
|
|
|
89
91
|
```js
|
|
90
92
|
/**
|
|
91
|
-
* Unlocks the page scroll.
|
|
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).
|
|
93
95
|
*
|
|
94
|
-
* @param {boolean} [force] - If `true`, forcibly unlocks the page scroll,
|
|
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`.
|
|
96
98
|
*/
|
|
97
99
|
function pageunlock(force?: boolean): void;
|
|
98
100
|
```
|
package/dist/pagelock.esm.js
CHANGED
|
@@ -1,47 +1,44 @@
|
|
|
1
|
-
|
|
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 =
|
|
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
|
|
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 };
|
package/dist/pagelock.esm.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
|
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 || {});
|
package/dist/pagelock.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t){"use strict";
|
|
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||{});
|
package/dist/types/pagelock.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
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
|
|
8
|
-
* without affecting the
|
|
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.
|
|
13
|
-
*
|
|
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,
|
|
16
|
-
*
|
|
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