use-element-scroll-restoration 1.0.1
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 +50 -0
- package/dist/hooks/useElementScrollRestoration.d.ts +12 -0
- package/dist/hooks/useElementScrollRestoration.test.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +82 -0
- package/dist/index.umd.cjs +1 -0
- package/dist/utils/debounce.d.ts +3 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
## React custom hook for scroll restoration
|
|
2
|
+
|
|
3
|
+
This is a custom React hook that facilitates scroll restoration for a specific element. It allows you to save and restore the scroll position of an element, optionally persisting it to `localStorage` or `sessionStorage`. This can be useful in scenarios where you want to maintain scroll positions across navigation or page refreshes.<br>
|
|
4
|
+
It differs from [ScrollRestoration](https://reactrouter.com/en/main/components/scroll-restoration) in that it can be attached to a specific dom element.
|
|
5
|
+
|
|
6
|
+
### Demo
|
|
7
|
+
|
|
8
|
+
https://codesandbox.io/p/sandbox/aged-tree-kr7y6w
|
|
9
|
+
|
|
10
|
+
### Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install use-scroll-restoration
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Usage
|
|
17
|
+
|
|
18
|
+
Here's how you can use the `useScrollRestoration` hook in your React components:
|
|
19
|
+
|
|
20
|
+
```jsx
|
|
21
|
+
import React from 'react';
|
|
22
|
+
import { useScrollRestoration } from 'use-scroll-restoration';
|
|
23
|
+
|
|
24
|
+
function ScrollRestorationExample() {
|
|
25
|
+
const { ref, setScroll } = useScrollRestoration('exampleKey', {
|
|
26
|
+
debounceTime: 200,
|
|
27
|
+
persist: 'localStorage',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div ref={ref} style={{ height: '500px', overflow: 'auto' }}>
|
|
32
|
+
{/* Your scrollable content */}
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### API
|
|
39
|
+
|
|
40
|
+
#### `useScrollRestoration(key: string, options?: ScrollRestorationOptions)`
|
|
41
|
+
|
|
42
|
+
- `key` (required): A unique key to identify the scroll restoration context.
|
|
43
|
+
- `options` (optional): An object containing additional options:
|
|
44
|
+
- `debounceTime` (default: `100`): The debounce time (in milliseconds) before saving scroll position.
|
|
45
|
+
- `persist` (default: `false`): Specifies where to persist scroll position. Can be `false`, `'localStorage'`, or `'sessionStorage'`.
|
|
46
|
+
|
|
47
|
+
Returns an object with the following properties:
|
|
48
|
+
|
|
49
|
+
- `ref`: A callback that sets the DOM element reference. Attach this to the `ref` prop of the scrollable element you want to manage.
|
|
50
|
+
- `setScroll({ x, y })`: A function to manually set the scroll position, where `x` and `y` are optional scroll coordinates.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface ScrollRestorationOptions {
|
|
2
|
+
debounceTime?: number;
|
|
3
|
+
persist?: false | "localStorage" | "sessionStorage";
|
|
4
|
+
}
|
|
5
|
+
export declare function useElementScrollRestoration<U extends HTMLElement>(key: string, { debounceTime, persist }?: ScrollRestorationOptions): {
|
|
6
|
+
ref: (element: U | null) => void;
|
|
7
|
+
setScroll: ({ x, y }: {
|
|
8
|
+
x?: number;
|
|
9
|
+
y?: number;
|
|
10
|
+
}) => void;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useElementScrollRestoration } from './hooks/useElementScrollRestoration';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useState as u, useCallback as L, useEffect as f } from "react";
|
|
2
|
+
const T = (o, a) => {
|
|
3
|
+
let l;
|
|
4
|
+
return (...i) => {
|
|
5
|
+
l && clearTimeout(l), l = setTimeout(() => {
|
|
6
|
+
o(...i), l = null;
|
|
7
|
+
}, a);
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
function E(o, { debounceTime: a = 100, persist: l = !1 } = {}) {
|
|
11
|
+
const [i, c] = u({}), [r, g] = u(null), m = L((t) => {
|
|
12
|
+
t && g(t);
|
|
13
|
+
}, []), e = i[o], S = o in i;
|
|
14
|
+
return f(() => {
|
|
15
|
+
if (!r) return;
|
|
16
|
+
const t = T(() => {
|
|
17
|
+
const s = r.scrollTop, n = r.scrollLeft;
|
|
18
|
+
c((R) => ({
|
|
19
|
+
...R,
|
|
20
|
+
[o]: { scrollTop: s, scrollLeft: n }
|
|
21
|
+
}));
|
|
22
|
+
}, a);
|
|
23
|
+
return r.addEventListener("scroll", t), () => {
|
|
24
|
+
r.removeEventListener("scroll", t);
|
|
25
|
+
};
|
|
26
|
+
}, [a, o, r, l, c]), f(() => {
|
|
27
|
+
if (r)
|
|
28
|
+
if (S)
|
|
29
|
+
r.scrollTo(
|
|
30
|
+
e.scrollLeft,
|
|
31
|
+
e.scrollTop
|
|
32
|
+
);
|
|
33
|
+
else {
|
|
34
|
+
let t = {
|
|
35
|
+
scrollTop: r.scrollTop,
|
|
36
|
+
scrollLeft: r.scrollLeft
|
|
37
|
+
};
|
|
38
|
+
if (l === "localStorage") {
|
|
39
|
+
const s = localStorage.getItem(
|
|
40
|
+
`scrollRestoration-${o}`
|
|
41
|
+
);
|
|
42
|
+
s && (t = JSON.parse(s));
|
|
43
|
+
}
|
|
44
|
+
if (l === "sessionStorage") {
|
|
45
|
+
const s = sessionStorage.getItem(
|
|
46
|
+
`scrollRestoration-${o}`
|
|
47
|
+
);
|
|
48
|
+
s && (t = JSON.parse(s));
|
|
49
|
+
}
|
|
50
|
+
c((s) => ({
|
|
51
|
+
...s,
|
|
52
|
+
[o]: t
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
}, [
|
|
56
|
+
e,
|
|
57
|
+
r,
|
|
58
|
+
o,
|
|
59
|
+
l,
|
|
60
|
+
S,
|
|
61
|
+
c
|
|
62
|
+
]), f(() => {
|
|
63
|
+
!l || !e || (l === "localStorage" ? localStorage.setItem(
|
|
64
|
+
`scrollRestoration-${o}`,
|
|
65
|
+
JSON.stringify(e)
|
|
66
|
+
) : l === "sessionStorage" && sessionStorage.setItem(
|
|
67
|
+
`scrollRestoration-${o}`,
|
|
68
|
+
JSON.stringify(e)
|
|
69
|
+
));
|
|
70
|
+
}, [o, l, e]), { ref: m, setScroll: ({ x: t, y: s }) => {
|
|
71
|
+
c((n) => ({
|
|
72
|
+
...n,
|
|
73
|
+
[o]: {
|
|
74
|
+
scrollLeft: t !== void 0 ? t : n[o].scrollLeft,
|
|
75
|
+
scrollTop: s !== void 0 ? s : n[o].scrollTop
|
|
76
|
+
}
|
|
77
|
+
}));
|
|
78
|
+
} };
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
E as useElementScrollRestoration
|
|
82
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.index={},r.React))})(this,(function(r,n){"use strict";const d=(o,a)=>{let t;return(...u)=>{t&&clearTimeout(t),t=setTimeout(()=>{o(...u),t=null},a)}};function g(o,{debounceTime:a=100,persist:t=!1}={}){const[u,i]=n.useState({}),[s,m]=n.useState(null),R=n.useCallback(e=>{e&&m(e)},[]),c=u[o],S=o in u;return n.useEffect(()=>{if(!s)return;const e=d(()=>{const l=s.scrollTop,f=s.scrollLeft;i(T=>({...T,[o]:{scrollTop:l,scrollLeft:f}}))},a);return s.addEventListener("scroll",e),()=>{s.removeEventListener("scroll",e)}},[a,o,s,t,i]),n.useEffect(()=>{if(s)if(S)s.scrollTo(c.scrollLeft,c.scrollTop);else{let e={scrollTop:s.scrollTop,scrollLeft:s.scrollLeft};if(t==="localStorage"){const l=localStorage.getItem(`scrollRestoration-${o}`);l&&(e=JSON.parse(l))}if(t==="sessionStorage"){const l=sessionStorage.getItem(`scrollRestoration-${o}`);l&&(e=JSON.parse(l))}i(l=>({...l,[o]:e}))}},[c,s,o,t,S,i]),n.useEffect(()=>{!t||!c||(t==="localStorage"?localStorage.setItem(`scrollRestoration-${o}`,JSON.stringify(c)):t==="sessionStorage"&&sessionStorage.setItem(`scrollRestoration-${o}`,JSON.stringify(c)))},[o,t,c]),{ref:R,setScroll:({x:e,y:l})=>{i(f=>({...f,[o]:{scrollLeft:e!==void 0?e:f[o].scrollLeft,scrollTop:l!==void 0?l:f[o].scrollTop}}))}}}r.useElementScrollRestoration=g,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-element-scroll-restoration",
|
|
3
|
+
"author": "Jacob Cutshall",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"version": "1.0.1",
|
|
6
|
+
"description": "React hook for rstoring scroll position on specific elements",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/index.umd.cjs",
|
|
9
|
+
"moudle": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.umd.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/jpcutshall/use-element-scroll-restoration"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/jpcutshall/use-element-scroll-restoration",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"react",
|
|
27
|
+
"hook",
|
|
28
|
+
"scroll-restoration",
|
|
29
|
+
"use-scroll-restoration",
|
|
30
|
+
"use-element-scroll-restoration"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"react": "^19.2.4"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@eslint/js": "^10.0.1",
|
|
37
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
38
|
+
"@testing-library/react": "^16.3.2",
|
|
39
|
+
"@types/node": "^22.19.12",
|
|
40
|
+
"@types/react": "^19.2.14",
|
|
41
|
+
"@types/react-dom": "^19.2.3",
|
|
42
|
+
"eslint": "^9.39.3",
|
|
43
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
44
|
+
"globals": "^17.3.0",
|
|
45
|
+
"happy-dom": "^20.7.0",
|
|
46
|
+
"typescript": "^5.0.2",
|
|
47
|
+
"typescript-eslint": "^8.56.1",
|
|
48
|
+
"vite": "^7.3.1",
|
|
49
|
+
"vite-plugin-dts": "^4.5.4",
|
|
50
|
+
"vitest": "^4.0.18"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "vite build",
|
|
54
|
+
"lint": "eslint .",
|
|
55
|
+
"test": "vitest run"
|
|
56
|
+
}
|
|
57
|
+
}
|