|
| 1 | +# Scroll Restoration |
| 2 | + |
| 3 | +::: warning Experimental |
| 4 | +This API is experimental and might have breaking changes. |
| 5 | +::: |
| 6 | + |
| 7 | +Scroll restoration lets components save and restore their own scroll positions. It is the **upcoming replacement** for the `scrollBehavior` router option. |
| 8 | + |
| 9 | +With it, you can: |
| 10 | + |
| 11 | +- Save multiple positions for one page |
| 12 | +- Use custom capture and restore functions, for example with `scrollIntoView()` |
| 13 | +- Control how pages share scroll restoration rather than relying on history entries |
| 14 | + |
| 15 | +[[toc]] |
| 16 | + |
| 17 | +## Setup |
| 18 | + |
| 19 | +Install `ScrollRestoration` before the router. Give it the router, and the default capture and restore functions: |
| 20 | + |
| 21 | +```ts [main.ts] |
| 22 | +import { createApp } from 'vue' |
| 23 | +import { |
| 24 | + ScrollRestoration, |
| 25 | + SCROLL_RESTORATION_CAPTURE_DEFAULT, |
| 26 | + SCROLL_RESTORATION_RESTORE_DEFAULT, |
| 27 | +} from 'vue-router/experimental' |
| 28 | +import App from './App.vue' |
| 29 | +import { router } from './router' |
| 30 | + |
| 31 | +const app = createApp(App) |
| 32 | + |
| 33 | +app.use(ScrollRestoration, { |
| 34 | + router, |
| 35 | + capture: SCROLL_RESTORATION_CAPTURE_DEFAULT, |
| 36 | + restore: SCROLL_RESTORATION_RESTORE_DEFAULT, |
| 37 | +}) |
| 38 | +app.use(router) |
| 39 | + |
| 40 | +app.mount('#app') |
| 41 | +``` |
| 42 | + |
| 43 | +The default functions save and restore the window scroll position and imitate the current mechanism combined with `scrollBehavior()` and some smart defaults. They are automatically inherited by nested components and can be overridden anywhere. Call `useScrollRestoration()` in a component to use them. |
| 44 | + |
| 45 | +Installing the plugin sets `history.scrollRestoration` to `manual`. Setting it to auto can with scroll restoration, especially in the context of _anchor links_. Set it back to `auto` if this is not an issue for you. |
| 46 | + |
| 47 | +## Restore a page |
| 48 | + |
| 49 | +Call `useScrollRestoration()` in any page component that needs to restore its scroll position: |
| 50 | + |
| 51 | +```vue [pages/Articles.vue] |
| 52 | +<script setup lang="ts"> |
| 53 | +import { useScrollRestoration } from 'vue-router/experimental' |
| 54 | +
|
| 55 | +useScrollRestoration() |
| 56 | +</script> |
| 57 | +``` |
| 58 | + |
| 59 | +The router _captures_ the position **when you leave the page** and _restores_ it after a navigation, when the component is mounted or updated using `onRouteRendered()`. |
| 60 | + |
| 61 | +## Multiple positions |
| 62 | + |
| 63 | +`capture()` returns an object. Each property is one saved position. Use `default` for the main position and add other names for other scroll containers: |
| 64 | + |
| 65 | +```vue [pages/Docs.vue] |
| 66 | +<script setup lang="ts"> |
| 67 | +import { useTemplateRef } from 'vue' |
| 68 | +import { useScrollRestoration } from 'vue-router/experimental' |
| 69 | +
|
| 70 | +const sidebar = useTemplateRef('sidebar') |
| 71 | +
|
| 72 | +useScrollRestoration({ |
| 73 | + capture: () => ({ |
| 74 | + default: { top: window.scrollY }, |
| 75 | + sidebar: { top: sidebar.value?.scrollTop }, |
| 76 | + }), |
| 77 | + restore: entry => { |
| 78 | + // scroll to top if no entry is found (new visit) |
| 79 | + window.scrollTo({ top: entry?.default?.top ?? 0 }) |
| 80 | + sidebar.value!.scrollTop = entry?.sidebar?.top ?? 0 |
| 81 | + }, |
| 82 | +}) |
| 83 | +</script> |
| 84 | +
|
| 85 | +<template> |
| 86 | + <aside ref="sidebar">...</aside> |
| 87 | + <main>...</main> |
| 88 | +</template> |
| 89 | +``` |
| 90 | + |
| 91 | +`capture()` and `restore()` must be synchronous. |
| 92 | + |
| 93 | +## Custom restore |
| 94 | + |
| 95 | +You can save an element selector and scroll to it with a saved offset: |
| 96 | + |
| 97 | +```vue [pages/Products.vue] |
| 98 | +<script setup lang="ts"> |
| 99 | +import { ref } from 'vue' |
| 100 | +import { useScrollRestoration } from 'vue-router/experimental' |
| 101 | +
|
| 102 | +const selectedId = ref<string>() |
| 103 | +
|
| 104 | +useScrollRestoration({ |
| 105 | + capture: () => |
| 106 | + selectedId.value |
| 107 | + ? { |
| 108 | + default: { |
| 109 | + el: `#${CSS.escape(selectedId.value)}`, |
| 110 | + // here you can add an offset to scroll a bit above the element |
| 111 | + top: 100, |
| 112 | + }, |
| 113 | + } |
| 114 | + : null, |
| 115 | + restore: entry => { |
| 116 | + if (!entry?.default?.el) return |
| 117 | +
|
| 118 | + const element = document.querySelector(entry.default.el) |
| 119 | + if (!element) return |
| 120 | +
|
| 121 | + window.scrollTo({ |
| 122 | + top: |
| 123 | + element.getBoundingClientRect().top + |
| 124 | + window.scrollY - |
| 125 | + (entry.default.top ?? 0), |
| 126 | + }) |
| 127 | + }, |
| 128 | +}) |
| 129 | +</script> |
| 130 | +``` |
| 131 | + |
| 132 | +Return `null` from `capture()` to remove the saved entry. |
| 133 | + |
| 134 | +## Keys |
| 135 | + |
| 136 | +By default, the key is `to.path + to.hash`. The same path and hash always use the same saved entry, including a new visit from a link. For example, if the users navigates to `/articles/42#comments`, scrolls down, and then navigates to `/articles/43`, navigating back **or** navigating directly again to `/articles/42#comments` will restore the previous position. |
| 137 | + |
| 138 | +Use a different key when pages must share or split entries: |
| 139 | + |
| 140 | +```ts |
| 141 | +// same position for all hashes on a page |
| 142 | +useScrollRestoration({ |
| 143 | + key: to => to.path, |
| 144 | +}) |
| 145 | + |
| 146 | +// different positions for different queries |
| 147 | +useScrollRestoration({ |
| 148 | + key: to => to.fullPath, |
| 149 | +}) |
| 150 | + |
| 151 | +// same position for different pages |
| 152 | +useScrollRestoration({ |
| 153 | + key: 'products', |
| 154 | +}) |
| 155 | +``` |
| 156 | + |
| 157 | +Components that are active at the same time must use different keys. |
| 158 | + |
| 159 | +## Manual restore |
| 160 | + |
| 161 | +Set `manual: true` when the content is not ready after navigation, like animations or virtualized list. Then call `scroll()` when the content is displayed: |
| 162 | + |
| 163 | +```vue [pages/Feed.vue] |
| 164 | +<script setup lang="ts"> |
| 165 | +import { nextTick, onMounted, ref } from 'vue' |
| 166 | +import { useScrollRestoration } from 'vue-router/experimental' |
| 167 | +
|
| 168 | +const posts = ref<Post[]>([]) |
| 169 | +const { scroll } = useScrollRestoration({ manual: true }) |
| 170 | +
|
| 171 | +onMounted(async () => { |
| 172 | + posts.value = await fetchPosts() |
| 173 | + await nextTick() |
| 174 | + scroll() |
| 175 | +}) |
| 176 | +</script> |
| 177 | +``` |
| 178 | + |
| 179 | +The router still captures the position automatically. |
0 commit comments