Skip to content

Commit 2051484

Browse files
committed
fix(docs): clarify some important things in the docs
1 parent b3c62d1 commit 2051484

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

README.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,47 @@ page you choose. It does this by storing the scroll position of the element in
4848
session storage and then restoring it when the user navigates back to the page
4949
(very similar to how Remix handles scroll restoration for the `<body>`).
5050

51-
This depends on Remix's `useBeforeUnload`, `useNavigation`, and `useLocation`
52-
hooks. There's probably an easy way we could make this support React Router in
53-
general. PRs welcome!
51+
This depends on React Router's `useBeforeUnload`, `useNavigation`, and
52+
`useLocation` hooks. It could probably be generalized to work with other
53+
routers. PRs welcome.
5454

5555
## Usage
5656

5757
```tsx
5858
import { ElementScrollRestoration } from '@epic-web/restore-scroll'
5959

60-
// ... Stick this in your root component somewhere:
6160
return (
62-
<html>
63-
<body>
61+
<div>
62+
<ul id="christmas-gifts">
63+
<li>🎁</li>
64+
<li>🎂</li>
65+
<li>🎉</li>
6466
{/* ... */}
65-
<ElementScrollRestoration elementQuery="[data-restore-scroll='true']" />
66-
{/* ... */}
67-
</body>
68-
</html>
67+
</ul>
68+
<ElementScrollRestoration elementQuery="#christmas-gifts" />
69+
</div>
6970
)
7071
```
7172

72-
Then, for any element for which you wish to restore scroll position, simply add
73-
the `data-restore-scroll="true"` attribute:
74-
75-
```tsx
76-
<ul data-restore-scroll="true">
77-
<li>...</li>
78-
<li>...</li>
79-
<li>...</li>
80-
</ul>
81-
```
82-
8373
And that's it! Now when the user navigates away from the page and then back to
8474
it, the list will be scrolled to the position it was at when the user left the
8575
page.
8676

77+
## Tips:
78+
79+
1. This requires an inline script, so you'll need to pass a `nonce` if you're
80+
using a Content Security Policy that requires this.
81+
2. Make certain to place the `ElementScrollRestoration` component _after_ the
82+
element you want to restore the scroll position of. This is because the
83+
component will render a `<script>` tag immediately after the element, and
84+
that script will run immediately, so the element needs to be in the DOM
85+
before the script runs.
86+
3. If you're computing the `id` and that value can change between navigations,
87+
you may need to specify a `key` on `ElementScrollRestoration` to trigger the
88+
inline script to be evaluated again and set the scroll position correctly.
89+
4. You'll want one of these components for each scrollable element you want to
90+
restore the scroll position for.
91+
8792
## License
8893

8994
MIT

0 commit comments

Comments
 (0)