use-smooth-scroll-restore 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 +21 -0
- package/README.md +58 -0
- package/dist/index.cjs +65 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.mjs +63 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 UdhayaKumar
|
|
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
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# 🌊 use-smooth-scroll-restore
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/use-smooth-scroll-restore)
|
|
4
|
+
[](https://bundlephobia.com/package/use-smooth-scroll-restore)
|
|
5
|
+
[](https://github.com/udhayavirat18/use-smooth-scroll-restore/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
The ultimate React hook for seamless scroll position restoration. Engineered to fix the "jump-to-top" bug across routes, tabs, and browser back/forward buttons.
|
|
8
|
+
|
|
9
|
+
## ✨ Why Every App Needs This
|
|
10
|
+
|
|
11
|
+
Most SPAs (Single Page Apps) lose the user's scroll position when they navigate away or hit the back button. `use-smooth-scroll-restore` provides a **privacy-first, flicker-free** solution.
|
|
12
|
+
|
|
13
|
+
- **✅ Browser Back/Forward:** Lands exactly where the user left off.
|
|
14
|
+
- **✅ Refresh Persistence:** Remembers position even after a hard refresh.
|
|
15
|
+
- **✅ Mobile Safari Fix:** Optimized for iOS scroll-jumping edge cases.
|
|
16
|
+
- **✅ Performance First:** Throttled event listeners + `requestAnimationFrame`.
|
|
17
|
+
- **✅ Zero Dependencies:** Tiny footprint (< 1kB gzipped).
|
|
18
|
+
|
|
19
|
+
## 📦 Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install use-smooth-scroll-restore
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 🚀 Usage
|
|
26
|
+
|
|
27
|
+
Simply call the hook in your top-level page components or a layout wrapper.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { useSmoothScrollRestore } from 'use-smooth-scroll-restore';
|
|
31
|
+
|
|
32
|
+
function BlogPage() {
|
|
33
|
+
// Remembers scroll position automatically
|
|
34
|
+
useSmoothScrollRestore();
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<main>
|
|
38
|
+
<h1>Long Content...</h1>
|
|
39
|
+
</main>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## ⚙️ Advanced Options
|
|
45
|
+
|
|
46
|
+
| Option | Type | Default | Description |
|
|
47
|
+
|--------|------|---------|-------------|
|
|
48
|
+
| `throttle` | `number` | `100` | Delay (ms) between scroll tracking updates. |
|
|
49
|
+
| `key` | `string` | `'scroll-restore'` | Custom prefix for sessionStorage keys. |
|
|
50
|
+
| `isEnabled` | `boolean` | `true` | Programmatically enable/disable tracking. |
|
|
51
|
+
|
|
52
|
+
## 🛡️ Privacy
|
|
53
|
+
|
|
54
|
+
This hook uses sessionStorage, meaning all scroll data is wiped when the tab is closed. No cookies, no local storage, and zero tracking.
|
|
55
|
+
|
|
56
|
+
## 🧠 How it Works
|
|
57
|
+
|
|
58
|
+
By leveraging `useLayoutEffect` and `requestAnimationFrame`, we ensure the scroll restoration happens before the browser paints. This eliminates the "flash of top-page" content, making navigation feel native and instantaneous.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const react = require('react');
|
|
4
|
+
|
|
5
|
+
const useSmoothScrollRestore = (options = {}) => {
|
|
6
|
+
const {
|
|
7
|
+
throttle = 100,
|
|
8
|
+
key = "scroll-restore",
|
|
9
|
+
isEnabled = true
|
|
10
|
+
} = options;
|
|
11
|
+
const scrollTimer = react.useRef(null);
|
|
12
|
+
const saveScroll = react.useCallback(() => {
|
|
13
|
+
if (!isEnabled) return;
|
|
14
|
+
const path = window.location.pathname;
|
|
15
|
+
const scrollData = {
|
|
16
|
+
x: window.scrollX,
|
|
17
|
+
y: window.scrollY,
|
|
18
|
+
timestamp: Date.now()
|
|
19
|
+
};
|
|
20
|
+
const storageKey = `${key}:${path}`;
|
|
21
|
+
sessionStorage.setItem(storageKey, JSON.stringify(scrollData));
|
|
22
|
+
}, [isEnabled, key]);
|
|
23
|
+
const restoreScroll = react.useCallback(() => {
|
|
24
|
+
if (!isEnabled) return;
|
|
25
|
+
const path = window.location.pathname;
|
|
26
|
+
const storageKey = `${key}:${path}`;
|
|
27
|
+
const saved = sessionStorage.getItem(storageKey);
|
|
28
|
+
if (saved) {
|
|
29
|
+
try {
|
|
30
|
+
const { x, y } = JSON.parse(saved);
|
|
31
|
+
requestAnimationFrame(() => {
|
|
32
|
+
window.scrollTo({
|
|
33
|
+
top: y,
|
|
34
|
+
left: x,
|
|
35
|
+
behavior: "auto"
|
|
36
|
+
// 'auto' is faster and more reliable for restoration
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.warn("Failed to restore scroll position", e);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}, [isEnabled, key]);
|
|
44
|
+
react.useEffect(() => {
|
|
45
|
+
if (!isEnabled) return;
|
|
46
|
+
const handleScroll = () => {
|
|
47
|
+
if (scrollTimer.current) return;
|
|
48
|
+
scrollTimer.current = setTimeout(() => {
|
|
49
|
+
saveScroll();
|
|
50
|
+
scrollTimer.current = null;
|
|
51
|
+
}, throttle);
|
|
52
|
+
};
|
|
53
|
+
window.addEventListener("scroll", handleScroll, { passive: true });
|
|
54
|
+
return () => {
|
|
55
|
+
window.removeEventListener("scroll", handleScroll);
|
|
56
|
+
if (scrollTimer.current) clearTimeout(scrollTimer.current);
|
|
57
|
+
};
|
|
58
|
+
}, [saveScroll, throttle, isEnabled]);
|
|
59
|
+
react.useLayoutEffect(() => {
|
|
60
|
+
restoreScroll();
|
|
61
|
+
}, [restoreScroll]);
|
|
62
|
+
return { saveScroll, restoreScroll };
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
exports.useSmoothScrollRestore = useSmoothScrollRestore;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface ScrollOptions {
|
|
2
|
+
throttle?: number;
|
|
3
|
+
key?: string;
|
|
4
|
+
isEnabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
declare const useSmoothScrollRestore: (options?: ScrollOptions) => {
|
|
7
|
+
saveScroll: () => void;
|
|
8
|
+
restoreScroll: () => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { useSmoothScrollRestore };
|
|
12
|
+
export type { ScrollOptions };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface ScrollOptions {
|
|
2
|
+
throttle?: number;
|
|
3
|
+
key?: string;
|
|
4
|
+
isEnabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
declare const useSmoothScrollRestore: (options?: ScrollOptions) => {
|
|
7
|
+
saveScroll: () => void;
|
|
8
|
+
restoreScroll: () => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { useSmoothScrollRestore };
|
|
12
|
+
export type { ScrollOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface ScrollOptions {
|
|
2
|
+
throttle?: number;
|
|
3
|
+
key?: string;
|
|
4
|
+
isEnabled?: boolean;
|
|
5
|
+
}
|
|
6
|
+
declare const useSmoothScrollRestore: (options?: ScrollOptions) => {
|
|
7
|
+
saveScroll: () => void;
|
|
8
|
+
restoreScroll: () => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { useSmoothScrollRestore };
|
|
12
|
+
export type { ScrollOptions };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useRef, useCallback, useEffect, useLayoutEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const useSmoothScrollRestore = (options = {}) => {
|
|
4
|
+
const {
|
|
5
|
+
throttle = 100,
|
|
6
|
+
key = "scroll-restore",
|
|
7
|
+
isEnabled = true
|
|
8
|
+
} = options;
|
|
9
|
+
const scrollTimer = useRef(null);
|
|
10
|
+
const saveScroll = useCallback(() => {
|
|
11
|
+
if (!isEnabled) return;
|
|
12
|
+
const path = window.location.pathname;
|
|
13
|
+
const scrollData = {
|
|
14
|
+
x: window.scrollX,
|
|
15
|
+
y: window.scrollY,
|
|
16
|
+
timestamp: Date.now()
|
|
17
|
+
};
|
|
18
|
+
const storageKey = `${key}:${path}`;
|
|
19
|
+
sessionStorage.setItem(storageKey, JSON.stringify(scrollData));
|
|
20
|
+
}, [isEnabled, key]);
|
|
21
|
+
const restoreScroll = useCallback(() => {
|
|
22
|
+
if (!isEnabled) return;
|
|
23
|
+
const path = window.location.pathname;
|
|
24
|
+
const storageKey = `${key}:${path}`;
|
|
25
|
+
const saved = sessionStorage.getItem(storageKey);
|
|
26
|
+
if (saved) {
|
|
27
|
+
try {
|
|
28
|
+
const { x, y } = JSON.parse(saved);
|
|
29
|
+
requestAnimationFrame(() => {
|
|
30
|
+
window.scrollTo({
|
|
31
|
+
top: y,
|
|
32
|
+
left: x,
|
|
33
|
+
behavior: "auto"
|
|
34
|
+
// 'auto' is faster and more reliable for restoration
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
} catch (e) {
|
|
38
|
+
console.warn("Failed to restore scroll position", e);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}, [isEnabled, key]);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (!isEnabled) return;
|
|
44
|
+
const handleScroll = () => {
|
|
45
|
+
if (scrollTimer.current) return;
|
|
46
|
+
scrollTimer.current = setTimeout(() => {
|
|
47
|
+
saveScroll();
|
|
48
|
+
scrollTimer.current = null;
|
|
49
|
+
}, throttle);
|
|
50
|
+
};
|
|
51
|
+
window.addEventListener("scroll", handleScroll, { passive: true });
|
|
52
|
+
return () => {
|
|
53
|
+
window.removeEventListener("scroll", handleScroll);
|
|
54
|
+
if (scrollTimer.current) clearTimeout(scrollTimer.current);
|
|
55
|
+
};
|
|
56
|
+
}, [saveScroll, throttle, isEnabled]);
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
restoreScroll();
|
|
59
|
+
}, [restoreScroll]);
|
|
60
|
+
return { saveScroll, restoreScroll };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { useSmoothScrollRestore };
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-smooth-scroll-restore",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "The ultimate React hook for seamless scroll position restoration across routes, tabs, and sessions. Zero-config, lightweight, and flicker-free.",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "unbuild",
|
|
23
|
+
"dev": "unbuild --stub",
|
|
24
|
+
"lint": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"react",
|
|
28
|
+
"react-hook",
|
|
29
|
+
"scroll-restoration",
|
|
30
|
+
"scroll-restore",
|
|
31
|
+
"nextjs",
|
|
32
|
+
"react-router",
|
|
33
|
+
"ux",
|
|
34
|
+
"scroll-position",
|
|
35
|
+
"persistence",
|
|
36
|
+
"flicker-free",
|
|
37
|
+
"smooth-scroll"
|
|
38
|
+
],
|
|
39
|
+
"author": "UdhayaKumar",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "https://github.com/udhayavirat18/use-smooth-scroll-restore"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/udhayavirat18/use-smooth-scroll-restore/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/udhayavirat18/use-smooth-scroll-restore#readme",
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/react": "^19.2.14",
|
|
51
|
+
"typescript": "^5.9.3",
|
|
52
|
+
"unbuild": "^3.6.1"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"react": ">=18.0.0"
|
|
56
|
+
}
|
|
57
|
+
}
|