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
> :warning: In case you setup `vue-router` from scratch (e.g. Vite SPA), please make sure that you have configured [scroll behavior](#vue-router---scroll-to-and-from-hash) in your router instance. This is not required if using Nuxt.
93
+
> :warning: In case you setup Vue Router from scratch (e.g. Vite SPA), please make sure that you have configured [scroll behavior](#vue-router---scroll-to-and-from-hash) in your router instance.
94
+
>
95
+
> This is not required if using Nuxt as it's already configured by the framework.
88
96
89
97
---
90
98
@@ -143,7 +151,7 @@ Since the object is reactive and kept in sync with the content, you can directly
143
151
144
152
```vue
145
153
<script setup lang="ts">
146
-
import { useActive } from 'vue-use-active-scroll'
154
+
import { useActiveScroll } from 'vue-use-active-scroll'
In this case, you must query the DOM in an `onMounted` hook or a watcher in order to get the targets.
182
190
183
-
Many CMSs already append IDs to the markup headings. In case yours doesn't, you can add them manually.
191
+
Many CMSs already append IDs to markup headings. In case yours doesn't, you can add them manually.
184
192
185
-
The below example shows also how to collect the links to render in the sidebar.
193
+
The below example also shows how to compute the sidebar links in case you are not able to retrieve them in advance in order to cover the worst case scenario.
@@ -336,11 +344,11 @@ function scrollTo(event, id) {
336
344
337
345
<br />
338
346
339
-
## Vue Router - Scroll to and from hash
347
+
## Vue Router - Scroll to and from anchors
340
348
341
-
> :warning: If using Nuxt, Vue Router is already configured to scroll to and from URL hash on page load or back/forward navigation. **So you don't need to do follow the steps below**. Otherwise rules must be defined manually.
349
+
> :warning: If using Nuxt, Vue Router is already configured to scroll to and from anchors on page load or back/forward navigation. **So you don't need to do follow the steps below**. Otherwise rules must be defined manually.
342
350
343
-
### Scrolling to hash
351
+
### Scrolling to anchors
344
352
345
353
For content scrolled by the window, simply return the target element. To scroll to a target scrolled by a container, use _scrollIntoView_ method.
346
354
@@ -368,9 +376,9 @@ const router = createRouter({
368
376
369
377
> :bulb: There's no need need to set overlayHeight if using `scrollIntoView` as the method is aware of target's `scroll-margin-top` property.
370
378
371
-
### Scrolling from hash back to the top of the page
379
+
### Scrolling from anchor back to the top of the page
372
380
373
-
To navigate back to the top of the same page (e.g. clicking on browser back button from a hash to the page root), use the _scroll_ method for containers and return _top_ for content scrolled by the window.
381
+
To navigate back to the top of the same page (e.g. clicking on browser's back button from hash to the page root), use the _scroll_ method for containers and return _top_ for content scrolled by the window.
374
382
375
383
```js
376
384
constrouter=createRouter({
@@ -414,12 +422,40 @@ If you don't like that, choose to replace instead of pushing the hash:
414
422
415
423
<br />
416
424
425
+
## Server-side rendering
426
+
427
+
Since `useActiveScroll` won't kick in until the page is hydrated, if you're using Nuxt, you might want to render the first link as active if on the server.
428
+
429
+
```vue
430
+
<script setup>
431
+
const isSSR = ref(true)
432
+
433
+
onMounted(() => (isSSR.value = false))
434
+
</script>
435
+
436
+
<template>
437
+
<nav>
438
+
<RouterLink
439
+
v-for="(link, idx) in links"
440
+
:class="{
441
+
'sidebar-link--active':
442
+
(isSSR && idx === 0) || link.href === activeId,
443
+
}"
444
+
>
445
+
{{ link.label }}
446
+
</RouterLink>
447
+
</nav>
448
+
</template>
449
+
```
450
+
451
+
<br />
452
+
417
453
## Setting scroll-margin-top for fixed headers
418
454
419
455
You might noticed that if you have a fixed header and defined an `overlayHeight`, once clicked to scroll, the target may be underneath the header. You must add `scroll-margin-top` to your targets:
0 commit comments