glide-scroll 0.0.0-stage → 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 ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ First release.
6
+
7
+ - `glideScroll()` smooths mouse-wheel and trackpad scrolling for the whole page, and returns a
8
+ function that turns it off.
9
+ - `<GlideScroll />` from `glide-scroll/react` does the same as a component.
10
+ - Pauses hover while the page glides under a still mouse, sleeps when the page is still, and
11
+ switches itself off on devices that can't keep it smooth.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cameron Spears
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, 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 CHANGED
@@ -1,3 +1,69 @@
1
- # Temporary Holding Version
1
+ # glide-scroll
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Smooth mouse-wheel scrolling for a whole page, in about 200 lines and 1.2 KB gzipped. No
4
+ dependencies.
5
+
6
+ [Demo](https://cameronspears.github.io/glide-scroll/demo/) (try it with a mouse wheel, then turn it off and compare)
7
+
8
+ ```bash
9
+ npm install glide-scroll
10
+ ```
11
+
12
+ ```js
13
+ import { glideScroll } from 'glide-scroll';
14
+
15
+ const stop = glideScroll(); // call stop() to turn it off again
16
+ ```
17
+
18
+ Or in React, once near the root:
19
+
20
+ ```jsx
21
+ import { GlideScroll } from 'glide-scroll/react';
22
+
23
+ <GlideScroll />;
24
+ ```
25
+
26
+ ## Why not Lenis
27
+
28
+ I was using [Lenis](https://github.com/darkroomengineering/lenis) on a site with long tables. With
29
+ the CPU slowed down in Chrome's dev tools, it dropped frames. Two things cost the most: a frame loop
30
+ that ran all the time, and every row under a still mouse getting hovered and repainted as the page
31
+ moved past it.
32
+
33
+ glide-scroll only runs while the page is moving. During a glide it puts an invisible layer over the
34
+ page, so nothing underneath gets hovered until you move the mouse. On that site it cut the
35
+ main-thread work per scroll by about 60%, and hit testing went from about 67 ms to 4 ms.
36
+
37
+ It does less than Lenis. It only smooths the page itself, not elements that scroll on their own, and
38
+ it doesn't hand out scroll events for GSAP and friends. If you need either, use Lenis.
39
+
40
+ ## How it moves
41
+
42
+ <picture>
43
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/cameronspears/glide-scroll/main/.github/assets/glide-dark.svg" />
44
+ <img alt="Page position after three quick wheel ticks: the browser jumps 100px per tick, glide-scroll eases to the same place in about half a second." src="https://raw.githubusercontent.com/cameronspears/glide-scroll/main/.github/assets/glide-light.svg" />
45
+ </picture>
46
+
47
+ Each wheel tick moves a target, and every frame the page closes part of the gap. How much depends on
48
+ how long the frame took, so it moves at the same speed at 60 Hz and 120 Hz.
49
+
50
+ The browser keeps everything else: touch, keys, the scrollbar, sideways swipes, pinch zoom, dialogs,
51
+ and boxes with their own scrollbar (until they reach the end). A click or key press stops a glide.
52
+ People with reduced motion turned on get no glide at all.
53
+
54
+ If a device can't keep up (more than a quarter of frames late, twice in a row), it turns itself off
55
+ for the rest of the visit.
56
+
57
+ ## Options
58
+
59
+ `glideScroll(options)` and `<GlideScroll {...options} />` take the same two:
60
+
61
+ - `catchUpMs` (default `120`): how quickly the page catches up. Lower is snappier.
62
+ - `storageKey` (default `'glide-scroll-off'`): the `sessionStorage` key that remembers a slow
63
+ device. Pass `false` to never remember.
64
+
65
+ On the server it does nothing, so it's safe in SSR frameworks.
66
+
67
+ ## License
68
+
69
+ MIT
@@ -0,0 +1,45 @@
1
+ export interface GlideScrollOptions {
2
+ /**
3
+ * How quickly the page catches up with the wheel, in milliseconds. Each period closes about two
4
+ * thirds of the gap left, so the default of 120 settles a flick in about half a second. Lower
5
+ * feels snappier, higher feels floatier.
6
+ */
7
+ catchUpMs?: number;
8
+ /**
9
+ * The sessionStorage key that remembers this device couldn't keep the glide smooth, so later
10
+ * pages in the same visit skip it. Pass `false` to never remember.
11
+ */
12
+ storageKey?: string | false;
13
+ }
14
+ /**
15
+ * Makes the page glide to a soft stop under a mouse wheel or trackpad, instead of moving in
16
+ * steps. Returns a function that turns it off again.
17
+ *
18
+ * Built to keep every frame cheap:
19
+ * - It only runs while the page is gliding, and sleeps the rest of the time.
20
+ * - Its pace is set in time, not frames, so a 120Hz screen gets twice the frames, not twice the
21
+ * speed.
22
+ * - While the page glides under a mouse held still, hover is paused, as on a phone. Otherwise
23
+ * every row passing under the pointer would light up and redraw. Moving the mouse brings hover
24
+ * back at once.
25
+ *
26
+ * Touch, the keyboard and the scrollbar stay the browser's own, and so do sideways swipes, pinch
27
+ * zoom, dialogs and boxes that scroll on their own. A click or key press ends the glide, and if
28
+ * anything else moves the page (a link to a section, the scrollbar), the glide lets go. Visitors
29
+ * who ask for reduced motion get no glide.
30
+ *
31
+ * If the glide stutters twice in a row, it switches itself off for the rest of the visit and the
32
+ * browser scrolls the page again.
33
+ *
34
+ * On the server, or under reduced motion, it does nothing and returns a function that does nothing.
35
+ *
36
+ * @param options How quickly the glide catches up, and where it remembers a slow device.
37
+ * @returns A function that turns the glide off and removes everything it added.
38
+ *
39
+ * @example
40
+ * import { glideScroll } from 'glide-scroll';
41
+ *
42
+ * const stop = glideScroll();
43
+ * // later: stop();
44
+ */
45
+ export declare function glideScroll(options?: GlideScrollOptions): () => void;
package/dist/index.js ADDED
@@ -0,0 +1,216 @@
1
+ // Closer than this to where it's heading, the glide is done.
2
+ const SETTLED_PX = 0.5;
3
+ // The mouse moved this far during a glide: someone is pointing at something, so hover returns.
4
+ const POINTING_PX = 4;
5
+ // A frame slower than this counts as a stutter. It sits clear of 33ms, the normal frame on a
6
+ // browser that runs at 30 frames a second (Safari in Low Power Mode).
7
+ const STUTTER_MS = 40;
8
+ // Frames per sample: a second and a half of gliding on a 60Hz screen, half that on 120Hz.
9
+ const SAMPLE_FRAMES = 90;
10
+ function readFlag(key) {
11
+ if (!key)
12
+ return false;
13
+ try {
14
+ return sessionStorage.getItem(key) !== null;
15
+ }
16
+ catch {
17
+ return false;
18
+ }
19
+ }
20
+ function writeFlag(key) {
21
+ if (!key)
22
+ return;
23
+ try {
24
+ sessionStorage.setItem(key, '1');
25
+ }
26
+ catch {
27
+ // Private windows may refuse storage: the next page finds out for itself.
28
+ }
29
+ }
30
+ /** Wheel distance in pixels, whatever unit the browser reports it in. */
31
+ function wheelPixels(event) {
32
+ if (event.deltaMode === WheelEvent.DOM_DELTA_LINE)
33
+ return event.deltaY * 40;
34
+ if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE)
35
+ return event.deltaY * window.innerHeight;
36
+ return event.deltaY;
37
+ }
38
+ /**
39
+ * True when the wheel belongs to something other than the page: a dialog, or a box under the
40
+ * pointer that scrolls on its own (a menu, a popover, a table) and can still move that way, or
41
+ * that keeps its scroll to itself.
42
+ */
43
+ function belongsElsewhere(target, delta) {
44
+ // Something has locked the page in place (an open menu or panel): leave it locked.
45
+ for (const page of [document.documentElement, document.body]) {
46
+ if (/hidden|clip/.test(getComputedStyle(page).overflowY))
47
+ return true;
48
+ }
49
+ for (let node = target instanceof Element ? target : null; node; node = node.parentElement) {
50
+ if (node === document.body)
51
+ return false;
52
+ if (node.tagName === 'DIALOG')
53
+ return true;
54
+ if (node.scrollHeight <= node.clientHeight + 1)
55
+ continue;
56
+ const style = getComputedStyle(node);
57
+ if (!/auto|scroll|overlay/.test(style.overflowY))
58
+ continue;
59
+ if (style.overscrollBehaviorY !== 'auto')
60
+ return true;
61
+ const canMove = delta > 0 ? node.scrollTop + node.clientHeight < node.scrollHeight - 1 : node.scrollTop > 0;
62
+ if (canMove)
63
+ return true;
64
+ }
65
+ return false;
66
+ }
67
+ /**
68
+ * Makes the page glide to a soft stop under a mouse wheel or trackpad, instead of moving in
69
+ * steps. Returns a function that turns it off again.
70
+ *
71
+ * Built to keep every frame cheap:
72
+ * - It only runs while the page is gliding, and sleeps the rest of the time.
73
+ * - Its pace is set in time, not frames, so a 120Hz screen gets twice the frames, not twice the
74
+ * speed.
75
+ * - While the page glides under a mouse held still, hover is paused, as on a phone. Otherwise
76
+ * every row passing under the pointer would light up and redraw. Moving the mouse brings hover
77
+ * back at once.
78
+ *
79
+ * Touch, the keyboard and the scrollbar stay the browser's own, and so do sideways swipes, pinch
80
+ * zoom, dialogs and boxes that scroll on their own. A click or key press ends the glide, and if
81
+ * anything else moves the page (a link to a section, the scrollbar), the glide lets go. Visitors
82
+ * who ask for reduced motion get no glide.
83
+ *
84
+ * If the glide stutters twice in a row, it switches itself off for the rest of the visit and the
85
+ * browser scrolls the page again.
86
+ *
87
+ * On the server, or under reduced motion, it does nothing and returns a function that does nothing.
88
+ *
89
+ * @param options How quickly the glide catches up, and where it remembers a slow device.
90
+ * @returns A function that turns the glide off and removes everything it added.
91
+ *
92
+ * @example
93
+ * import { glideScroll } from 'glide-scroll';
94
+ *
95
+ * const stop = glideScroll();
96
+ * // later: stop();
97
+ */
98
+ export function glideScroll(options = {}) {
99
+ const { catchUpMs = 120, storageKey = 'glide-scroll-off' } = options;
100
+ if (typeof window === 'undefined')
101
+ return () => { };
102
+ if (readFlag(storageKey) || matchMedia('(prefers-reduced-motion: reduce)').matches) {
103
+ return () => { };
104
+ }
105
+ const root = document.documentElement;
106
+ // Where the page is now and where it's heading. Both are only meaningful while gliding.
107
+ let current = 0;
108
+ let target = 0;
109
+ // The furthest the page can go, measured once per glide: measuring it on every wheel tick would
110
+ // make the browser lay out the page again each time.
111
+ let max = 0;
112
+ let frame = 0;
113
+ let lastTime = 0;
114
+ let pointerTravel = 0;
115
+ // The stutter check.
116
+ let frames = 0;
117
+ let stutters = 0;
118
+ let badSamples = 0;
119
+ // An invisible sheet over the page while it glides: the pointer rests on it, so nothing
120
+ // underneath lights up as it passes. Showing one element is far cheaper than switching hover off
121
+ // across the whole page, which would restyle every element on it.
122
+ const sheet = document.createElement('div');
123
+ sheet.setAttribute('aria-hidden', 'true');
124
+ sheet.dataset.glideScroll = '';
125
+ sheet.style.cssText = 'position:fixed;inset:0;z-index:2147483647;display:none';
126
+ document.body.appendChild(sheet);
127
+ const pauseHover = (paused) => {
128
+ sheet.style.display = paused ? 'block' : 'none';
129
+ };
130
+ const stop = () => {
131
+ if (frame)
132
+ cancelAnimationFrame(frame);
133
+ frame = 0;
134
+ pauseHover(false);
135
+ };
136
+ const step = (now) => {
137
+ // Something else moved the page (the scrollbar, a jump to a section): let it.
138
+ if (Math.abs(window.scrollY - Math.round(current)) > 1)
139
+ return stop();
140
+ // A frame's time is when the frame began, which can be just before the wheel tick that
141
+ // started the glide: never step backwards.
142
+ const gap = Math.max(0, now - lastTime);
143
+ lastTime = now;
144
+ current += (target - current) * (1 - Math.exp(-Math.min(gap, 100) / catchUpMs));
145
+ const done = Math.abs(target - current) < SETTLED_PX;
146
+ if (done)
147
+ current = target;
148
+ window.scrollTo({ top: current, behavior: 'instant' });
149
+ if (done)
150
+ return stop();
151
+ frames += 1;
152
+ if (gap > STUTTER_MS)
153
+ stutters += 1;
154
+ if (frames >= SAMPLE_FRAMES) {
155
+ badSamples = stutters > frames / 4 ? badSamples + 1 : 0;
156
+ frames = 0;
157
+ stutters = 0;
158
+ if (badSamples >= 2) {
159
+ writeFlag(storageKey);
160
+ return cleanup();
161
+ }
162
+ }
163
+ frame = requestAnimationFrame(step);
164
+ };
165
+ const onWheel = (event) => {
166
+ if (event.ctrlKey || Math.abs(event.deltaX) > Math.abs(event.deltaY))
167
+ return;
168
+ const delta = wheelPixels(event);
169
+ // Mid-glide the page is latched, as the browser's own scroll is: no need to look for boxes.
170
+ if (!frame && belongsElsewhere(event.target, delta))
171
+ return;
172
+ if (!frame) {
173
+ const from = window.scrollY;
174
+ const edge = root.scrollHeight - window.innerHeight;
175
+ // Already at the top or bottom: nothing to glide to, so the browser keeps the wheel (and its
176
+ // edge bounce), rather than a glide starting and stopping on every tick.
177
+ if (delta > 0 ? from >= edge - 1 : from <= 0)
178
+ return;
179
+ current = from;
180
+ target = from;
181
+ max = edge;
182
+ lastTime = performance.now();
183
+ pointerTravel = 0;
184
+ pauseHover(true);
185
+ frame = requestAnimationFrame(step);
186
+ }
187
+ event.preventDefault();
188
+ target = Math.max(0, Math.min(max, target + delta));
189
+ };
190
+ const onPointerMove = (event) => {
191
+ if (!frame || sheet.style.display === 'none')
192
+ return;
193
+ pointerTravel += Math.abs(event.movementX) + Math.abs(event.movementY);
194
+ if (pointerTravel > POINTING_PX)
195
+ pauseHover(false);
196
+ };
197
+ let cleaned = false;
198
+ const cleanup = () => {
199
+ if (cleaned)
200
+ return;
201
+ cleaned = true;
202
+ stop();
203
+ window.removeEventListener('wheel', onWheel);
204
+ window.removeEventListener('pointermove', onPointerMove);
205
+ window.removeEventListener('pointerdown', stop, true);
206
+ window.removeEventListener('keydown', stop, true);
207
+ sheet.remove();
208
+ };
209
+ window.addEventListener('wheel', onWheel, { passive: false });
210
+ window.addEventListener('pointermove', onPointerMove, { passive: true });
211
+ // A click or key press ends the glide. Otherwise the glide would carry on over whatever the
212
+ // press does next: opening a page, jumping to a section, or scrolling by keyboard.
213
+ window.addEventListener('pointerdown', stop, true);
214
+ window.addEventListener('keydown', stop, true);
215
+ return cleanup;
216
+ }
@@ -0,0 +1,12 @@
1
+ import { type GlideScrollOptions } from './index.js';
2
+ export type { GlideScrollOptions };
3
+ /**
4
+ * Turns on glide scrolling while mounted, and off when unmounted. Put it once near the root of the
5
+ * app. Renders nothing. Options are read once, when it mounts.
6
+ *
7
+ * @example
8
+ * import { GlideScroll } from 'glide-scroll/react';
9
+ *
10
+ * <GlideScroll catchUpMs={160} />
11
+ */
12
+ export declare function GlideScroll({ catchUpMs, storageKey }?: GlideScrollOptions): null;
package/dist/react.js ADDED
@@ -0,0 +1,18 @@
1
+ 'use client';
2
+ import { useEffect } from 'react';
3
+ import { glideScroll } from './index.js';
4
+ /**
5
+ * Turns on glide scrolling while mounted, and off when unmounted. Put it once near the root of the
6
+ * app. Renders nothing. Options are read once, when it mounts.
7
+ *
8
+ * @example
9
+ * import { GlideScroll } from 'glide-scroll/react';
10
+ *
11
+ * <GlideScroll catchUpMs={160} />
12
+ */
13
+ export function GlideScroll({ catchUpMs, storageKey } = {}) {
14
+ useEffect(() => glideScroll({ catchUpMs, storageKey }),
15
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- options are read once, on mount
16
+ []);
17
+ return null;
18
+ }
package/package.json CHANGED
@@ -1,6 +1,65 @@
1
1
  {
2
2
  "name": "glide-scroll",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Smooth mouse-wheel scrolling for the whole page that keeps every frame cheap. No dependencies, about 1.2 KB gzipped.",
5
+ "license": "MIT",
6
+ "author": "Cameron Spears",
7
+ "homepage": "https://github.com/cameronspears/glide-scroll#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/cameronspears/glide-scroll.git"
11
+ },
12
+ "bugs": "https://github.com/cameronspears/glide-scroll/issues",
13
+ "type": "module",
14
+ "sideEffects": false,
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./react": {
21
+ "types": "./dist/react.d.ts",
22
+ "default": "./dist/react.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "CHANGELOG.md"
28
+ ],
29
+ "scripts": {
30
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
31
+ "test": "vitest run",
32
+ "demo": "pnpm build && python3 -m http.server 4173"
33
+ },
34
+ "keywords": [
35
+ "smooth-scroll",
36
+ "scroll",
37
+ "wheel",
38
+ "trackpad",
39
+ "lenis",
40
+ "react"
41
+ ],
42
+ "peerDependencies": {
43
+ "react": ">=17"
44
+ },
45
+ "peerDependenciesMeta": {
46
+ "react": {
47
+ "optional": true
48
+ }
49
+ },
50
+ "devDependencies": {
51
+ "@types/react": "^19.1.0",
52
+ "jsdom": "^26.1.0",
53
+ "react": "^19.1.0",
54
+ "typescript": "^5.8.3",
55
+ "vitest": "^3.2.4"
56
+ },
57
+ "engines": {
58
+ "node": ">=20"
59
+ },
60
+ "publishConfig": {
61
+ "access": "public",
62
+ "provenance": true
63
+ },
64
+ "packageManager": "pnpm@10.20.0"
65
+ }