@alexcarpenter/scroll-lock 0.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/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/index.d.ts +7 -0
- package/index.js +186 -0
- package/package.json +68 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright 2026 Alex Carpenter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Scroll Lock
|
|
2
|
+
|
|
3
|
+
A tiny scroll lock for dialogs, drawers, popovers, and other overlays.
|
|
4
|
+
|
|
5
|
+
- **Small.** Less than 1 kB minified and Brotli-compressed.
|
|
6
|
+
- **Fast.** Does not scan descendants or read computed styles.
|
|
7
|
+
- **Focused.** One function and no runtime dependencies.
|
|
8
|
+
- **Composable.** Supports nested overlays and one allowed scroll surface.
|
|
9
|
+
- **Framework-free.** Works directly with React, Vue, Svelte, and vanilla DOM.
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import { scrollLock } from '@alexcarpenter/scroll-lock'
|
|
13
|
+
|
|
14
|
+
const release = scrollLock()
|
|
15
|
+
release()
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
npm install @alexcarpenter/scroll-lock
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
Call `scrollLock()` when an overlay opens. It returns an idempotent function
|
|
27
|
+
that restores the page.
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import { scrollLock } from '@alexcarpenter/scroll-lock'
|
|
31
|
+
|
|
32
|
+
const release = scrollLock()
|
|
33
|
+
|
|
34
|
+
// Close the overlay later.
|
|
35
|
+
release()
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Pass an element to keep that surface interactive and scrollable:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
const release = scrollLock(dialog)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Only the most recent lock is active. Releasing it resumes the previous lock,
|
|
45
|
+
which makes nested dialogs and popovers work naturally.
|
|
46
|
+
|
|
47
|
+
## React
|
|
48
|
+
|
|
49
|
+
The returned function is compatible with `useEffect` cleanup, so React support
|
|
50
|
+
does not require an adapter or React dependency.
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { useEffect, useRef } from 'react'
|
|
54
|
+
import { scrollLock } from '@alexcarpenter/scroll-lock'
|
|
55
|
+
|
|
56
|
+
export function Dialog({ open, children }) {
|
|
57
|
+
const dialogRef = useRef<HTMLDialogElement>(null)
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (!open) return
|
|
61
|
+
return scrollLock(dialogRef.current)
|
|
62
|
+
}, [open])
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<dialog ref={dialogRef} open={open}>
|
|
66
|
+
{children}
|
|
67
|
+
</dialog>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
For a page-wide lock, omit the reference:
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (open) return scrollLock()
|
|
77
|
+
}, [open])
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
This also behaves correctly under React Strict Mode because every release
|
|
81
|
+
function is safe to call more than once.
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
### `scrollLock(surface?)`
|
|
86
|
+
|
|
87
|
+
Freezes page movement and returns a release function.
|
|
88
|
+
|
|
89
|
+
- `surface?: HTMLElement | null` — element that remains interactive and
|
|
90
|
+
scrollable.
|
|
91
|
+
- Returns `() => void` — restores the previous lock or the original page.
|
|
92
|
+
|
|
93
|
+
## Behavior
|
|
94
|
+
|
|
95
|
+
- Preserves page coordinates and existing inline styles.
|
|
96
|
+
- Blocks wheel and scroll-key input outside the allowed surface.
|
|
97
|
+
- Contains wheel movement at the allowed surface boundary.
|
|
98
|
+
- Preserves pinch-to-zoom while restricting single-finger page movement.
|
|
99
|
+
- Prevents dragged scrollbars behind an overlay from moving.
|
|
100
|
+
- Rebuilds active listeners after orientation changes.
|
|
101
|
+
- Discards sessions whose allowed surface leaves the document.
|
|
102
|
+
- Creates one temporary stylesheet and listener lifecycle for the active lock.
|
|
103
|
+
|
|
104
|
+
## Size
|
|
105
|
+
|
|
106
|
+
The package uses [Size Limit](https://github.com/ai/size-limit) with a hard
|
|
107
|
+
1 kB budget:
|
|
108
|
+
|
|
109
|
+
```sh
|
|
110
|
+
pnpm test:size
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Current size: **964 B** minified and Brotli-compressed.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lock page scrolling and optionally leave one element scrollable.
|
|
3
|
+
*
|
|
4
|
+
* @param node Element to keep interactive and scrollable.
|
|
5
|
+
* @returns An idempotent function that releases this lock.
|
|
6
|
+
*/
|
|
7
|
+
export function scrollLock(surface?: HTMLElement | null): () => void
|
package/index.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/** @typedef {[HTMLElement | null, () => void, () => void]} Session */
|
|
2
|
+
|
|
3
|
+
const anchors = new WeakMap()
|
|
4
|
+
const touchActions = [
|
|
5
|
+
'pinch-zoom',
|
|
6
|
+
'pan-x pinch-zoom',
|
|
7
|
+
'pan-y pinch-zoom',
|
|
8
|
+
'auto'
|
|
9
|
+
]
|
|
10
|
+
const scrollKey = /^(Space|Page(Up|Down)|End|Home|Arrow(Left|Up|Right|Down))$/
|
|
11
|
+
|
|
12
|
+
/** @param {HTMLElement} element */
|
|
13
|
+
const pin = element => {
|
|
14
|
+
const anchor = anchors.get(element)
|
|
15
|
+
element.scrollLeft = anchor[0]
|
|
16
|
+
element.scrollTop = anchor[1]
|
|
17
|
+
anchor[2] = requestAnimationFrame(() => pin(element))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** @type {Session[]} */
|
|
21
|
+
let stack = []
|
|
22
|
+
let sheet
|
|
23
|
+
let viewport
|
|
24
|
+
|
|
25
|
+
const prune = () => {
|
|
26
|
+
for (let i = stack.length; i--; ) {
|
|
27
|
+
const session = stack[i]
|
|
28
|
+
if (session[0] && !document.body.contains(session[0])) {
|
|
29
|
+
session[2]()
|
|
30
|
+
stack.splice(i, 1)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Lock page scrolling and optionally leave one element scrollable.
|
|
37
|
+
* @param {HTMLElement | null} [surface]
|
|
38
|
+
* @returns {() => void}
|
|
39
|
+
*/
|
|
40
|
+
export function scrollLock(surface = null) {
|
|
41
|
+
if (!stack.length) {
|
|
42
|
+
viewport = [
|
|
43
|
+
document.body.style.position,
|
|
44
|
+
document.body.style.inset,
|
|
45
|
+
document.body.style.overflowY,
|
|
46
|
+
document.documentElement.style.scrollbarGutter,
|
|
47
|
+
window.scrollX,
|
|
48
|
+
window.scrollY
|
|
49
|
+
]
|
|
50
|
+
document.documentElement.style.scrollbarGutter = 'stable'
|
|
51
|
+
document.body.style.overflowY = 'hidden'
|
|
52
|
+
document.body.style.position = 'fixed'
|
|
53
|
+
document.body.style.inset = `-${viewport[5]}px 0 0 -${viewport[4]}px`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
stack.at(-1)?.[2]()
|
|
57
|
+
prune()
|
|
58
|
+
|
|
59
|
+
let events
|
|
60
|
+
let marker
|
|
61
|
+
let blocked = true
|
|
62
|
+
|
|
63
|
+
/** @param {WheelEvent} event */
|
|
64
|
+
const guardWheel = event => {
|
|
65
|
+
if (!surface || blocked) return event.preventDefault()
|
|
66
|
+
const distance = surface.scrollHeight - surface.offsetHeight
|
|
67
|
+
const target = event.target
|
|
68
|
+
if (
|
|
69
|
+
distance > 0 &&
|
|
70
|
+
target !== surface &&
|
|
71
|
+
target instanceof Node &&
|
|
72
|
+
surface.contains(target)
|
|
73
|
+
) {
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
;(target === surface ||
|
|
77
|
+
(event.deltaY > 0 && surface.scrollTop >= distance) ||
|
|
78
|
+
(event.deltaY < 0 && surface.scrollTop <= 0)) &&
|
|
79
|
+
event.preventDefault()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {KeyboardEvent} event */
|
|
83
|
+
const guardKey = event => {
|
|
84
|
+
blocked && scrollKey.test(event.key) && event.preventDefault()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @param {MouseEvent} event */
|
|
88
|
+
const hold = event => {
|
|
89
|
+
const target = event.target
|
|
90
|
+
if (!(target instanceof HTMLElement) || target === surface) return
|
|
91
|
+
const anchor = [target.scrollLeft, target.scrollTop]
|
|
92
|
+
anchors.set(target, anchor)
|
|
93
|
+
pin(target)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** @param {MouseEvent} event */
|
|
97
|
+
const release = event => {
|
|
98
|
+
const target = event.target
|
|
99
|
+
if (!(target instanceof HTMLElement)) return
|
|
100
|
+
const anchor = anchors.get(target)
|
|
101
|
+
if (anchor) {
|
|
102
|
+
requestAnimationFrame(() => {
|
|
103
|
+
cancelAnimationFrame(anchor[2])
|
|
104
|
+
anchors.delete(target)
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const mount = () => {
|
|
110
|
+
if (events) return
|
|
111
|
+
blocked = true
|
|
112
|
+
events = new AbortController()
|
|
113
|
+
const options = {
|
|
114
|
+
capture: true,
|
|
115
|
+
passive: false,
|
|
116
|
+
signal: events.signal
|
|
117
|
+
}
|
|
118
|
+
window.addEventListener('wheel', guardWheel, options)
|
|
119
|
+
window.addEventListener('keydown', guardKey, options)
|
|
120
|
+
window.addEventListener('mousedown', hold, options)
|
|
121
|
+
window.addEventListener('mouseup', release, options)
|
|
122
|
+
|
|
123
|
+
sheet ||= document.head.appendChild(document.createElement('style'))
|
|
124
|
+
const base = '*{pointer-events:none}*,::backdrop{touch-action:none}'
|
|
125
|
+
|
|
126
|
+
if (surface) {
|
|
127
|
+
marker = `n${stack.length}`
|
|
128
|
+
surface.classList.add(marker)
|
|
129
|
+
const x = surface.scrollWidth > surface.clientWidth
|
|
130
|
+
const y = surface.scrollHeight > surface.clientHeight
|
|
131
|
+
const touch = touchActions[+x + 2 * +y]
|
|
132
|
+
sheet.textContent = `${base}.${marker},.${marker} *{pointer-events:auto;overscroll-behavior:contain;touch-action:${touch}}`
|
|
133
|
+
surface.addEventListener('mouseover', () => (blocked = false), options)
|
|
134
|
+
surface.addEventListener('mouseout', () => (blocked = true), options)
|
|
135
|
+
} else {
|
|
136
|
+
sheet.textContent = base
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const unmount = () => {
|
|
141
|
+
if (!events) return
|
|
142
|
+
events = events.abort()
|
|
143
|
+
marker && surface.classList.remove(marker)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** @type {Session} */
|
|
147
|
+
const session = [surface, mount, unmount]
|
|
148
|
+
|
|
149
|
+
if (!surface || document.body.contains(surface)) {
|
|
150
|
+
stack.push(session)
|
|
151
|
+
mount()
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const orientation = window.matchMedia('(orientation:portrait)')
|
|
155
|
+
orientation.onchange = () => {
|
|
156
|
+
if (events) {
|
|
157
|
+
unmount()
|
|
158
|
+
mount()
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let released
|
|
163
|
+
return () => {
|
|
164
|
+
if (released) return
|
|
165
|
+
released = true
|
|
166
|
+
orientation.onchange = null
|
|
167
|
+
const index = stack.indexOf(session)
|
|
168
|
+
if (~index) {
|
|
169
|
+
const top = index === stack.length - 1
|
|
170
|
+
unmount()
|
|
171
|
+
stack.splice(index, 1)
|
|
172
|
+
prune()
|
|
173
|
+
top && stack.at(-1)?.[1]()
|
|
174
|
+
}
|
|
175
|
+
if (!stack.length) {
|
|
176
|
+
sheet = sheet?.remove()
|
|
177
|
+
document.body.style.position = viewport[0]
|
|
178
|
+
document.body.style.inset = viewport[1]
|
|
179
|
+
document.body.style.overflowY = viewport[2]
|
|
180
|
+
window.scroll(viewport[4], viewport[5])
|
|
181
|
+
requestAnimationFrame(() => {
|
|
182
|
+
document.documentElement.style.scrollbarGutter = viewport[3]
|
|
183
|
+
})
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alexcarpenter/scroll-lock",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sub-kilobyte scroll locking for overlays",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"scroll",
|
|
7
|
+
"lock",
|
|
8
|
+
"scrollbar",
|
|
9
|
+
"modal",
|
|
10
|
+
"dialog"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Alex Carpenter",
|
|
14
|
+
"repository": "alexcarpenter/scroll-lock",
|
|
15
|
+
"funding": [
|
|
16
|
+
{
|
|
17
|
+
"type": "github",
|
|
18
|
+
"url": "https://github.com/sponsors/alexcarpenter"
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"types": "./index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./index.js",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"index.js",
|
|
30
|
+
"index.d.ts",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"CHANGELOG.md"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test:lint": "oxlint",
|
|
37
|
+
"test:coverage": "vitest run --coverage",
|
|
38
|
+
"test:types": "check-dts",
|
|
39
|
+
"test:size": "size-limit",
|
|
40
|
+
"test": "pnpm run /^test:/"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@logux/oxc-configs": "^0.2.0",
|
|
44
|
+
"@size-limit/preset-small-lib": "^12.0.1",
|
|
45
|
+
"@vitest/browser": "^4.0.16",
|
|
46
|
+
"@vitest/browser-playwright": "^4.0.16",
|
|
47
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
48
|
+
"actions-up": "^1.12.0",
|
|
49
|
+
"check-dts": "^1.0.0",
|
|
50
|
+
"multiocular": "^0.8.2",
|
|
51
|
+
"oxfmt": "^0.42.0",
|
|
52
|
+
"oxlint": "1.58.0",
|
|
53
|
+
"oxlint-tsgolint": "0.18.1",
|
|
54
|
+
"playwright": "^1.57.0",
|
|
55
|
+
"size-limit": "^12.0.1",
|
|
56
|
+
"typescript": "^6.0.2",
|
|
57
|
+
"vitest": "^4.0.16"
|
|
58
|
+
},
|
|
59
|
+
"size-limit": [
|
|
60
|
+
{
|
|
61
|
+
"import": "{ scrollLock }",
|
|
62
|
+
"limit": "1 kB"
|
|
63
|
+
}
|
|
64
|
+
],
|
|
65
|
+
"engines": {
|
|
66
|
+
"node": "^22.0.0 || >=24.0.0"
|
|
67
|
+
}
|
|
68
|
+
}
|