You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 062fa6d
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: packages/docs/experimental/scroll-restoration.md
+24-1Lines changed: 24 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,30 @@ useScrollRestoration()
93
93
</script>
94
94
```
95
95
96
-
The router _captures_ the position **when you leave the page**, and when the browser tab is hidden or closed. It _restores_ the position after a navigation, when the page is rendered. It uses `onRouteRendered()` under the hood.
96
+
The router _captures_ the position **when you leave the page**, and when the browser tab becomes hidden. It _restores_ the position after a navigation, when the page is rendered. It uses `onRouteRendered()` under the hood.
97
+
98
+
## Capture events
99
+
100
+
The plugin captures positions after each successful navigation and when the document becomes hidden. It does not listen to `pagehide`.
101
+
102
+
Use `setupListeners(capture, signal)` to replace the default `visibilitychange` listener. The supplied `capture()` saves all active scroll registrations for the current route. Navigation capture remains enabled.
103
+
104
+
For example, capture on scroll:
105
+
106
+
```ts
107
+
app.use(ScrollRestoration, {
108
+
router,
109
+
capture: SCROLL_RESTORATION_CAPTURE_DEFAULT,
110
+
restore: SCROLL_RESTORATION_RESTORE_DEFAULT,
111
+
setupListeners(capture, signal) {
112
+
window.addEventListener('scroll', capture, { passive: true, signal })
113
+
},
114
+
})
115
+
```
116
+
117
+
Pass `signal` to your listeners so they are removed when the app is unmounted. You can listen to your own event instead, or save the supplied `capture` function and call it manually. To disable additional capture events, use `setupListeners: () => {}`.
118
+
119
+
A [`scroll` event cannot be canceled](https://developer.chrome.com/blog/passive-event-listeners), so `passive: true` does not improve scroll performance. Each capture calls all active capture functions and writes to `sessionStorage` synchronously. For frequent events, throttle captures and use the signal to cancel pending work on unmount.
0 commit comments