Skip to content

Commit a05eadf

Browse files
committed
release 1.1.1
1 parent e515178 commit a05eadf

File tree

9 files changed

+426
-102
lines changed

9 files changed

+426
-102
lines changed

README.md

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ Moreover, this package is meant for highlighting links in vertical sidebars and
5656

5757
<br />
5858

59+
## Requirements
60+
61+
If scrolling to anchors, Vue Router (RouterLink / NuxtLink) is required.
62+
63+
<br />
64+
5965
## Installation
6066

6167
```bash
@@ -84,7 +90,9 @@ The composable returns an object with properties to react to the active link and
8490
const { setActive, activeId, activeIndex /*, ... */ } = useActiveScroll(targets)
8591
```
8692

87-
> :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.
8896
8997
---
9098

@@ -143,7 +151,7 @@ Since the object is reactive and kept in sync with the content, you can directly
143151

144152
```vue
145153
<script setup lang="ts">
146-
import { useActive } from 'vue-use-active-scroll'
154+
import { useActiveScroll } from 'vue-use-active-scroll'
147155
148156
const { toc } = useContent()
149157
@@ -155,7 +163,7 @@ const ids = computed(() =>
155163
])
156164
)
157165
158-
const { setActive, activeId } = useActive(ids)
166+
const { setActive, activeId } = useActiveScroll(ids)
159167
</script>
160168
161169
<template>
@@ -180,9 +188,9 @@ const { setActive, activeId } = useActive(ids)
180188

181189
In this case, you must query the DOM in an `onMounted` hook or a watcher in order to get the targets.
182190

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.
184192

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.
186194

187195
```vue
188196
<script setup>
@@ -221,7 +229,7 @@ watch(container, (c) => (c ? setTargets(c) : resetTargets()), {
221229
flush: 'post',
222230
})
223231
224-
const { setActive, activeId } = useActive(targets)
232+
const { setActive, activeId } = useActiveScroll(targets)
225233
</script>
226234
227235
<template>
@@ -248,10 +256,10 @@ const { setActive, activeId } = useActive(targets)
248256

249257
## Customization
250258

251-
`useActive` accepts an optional configuration object as last argument:
259+
`useActiveScroll` accepts an optional configuration object as last argument:
252260

253261
```js
254-
const { activeId, setActive } = useActive(targets, {
262+
const { activeId, setActive } = useActiveScroll(targets, {
255263
// ...
256264
})
257265
```
@@ -305,12 +313,12 @@ html {
305313

306314
```vue
307315
<script setup>
308-
import { useActive } from 'vue-use-active-scroll'
316+
import { useActiveScroll } from 'vue-use-active-scroll'
309317
import animateScrollTo from 'animated-scroll-to'
310318
311319
// ...
312320
313-
const { setActive, activeId } = useActive(targets)
321+
const { setActive, activeId } = useActiveScroll(targets)
314322
315323
function scrollTo(event, id) {
316324
// ...
@@ -336,11 +344,11 @@ function scrollTo(event, id) {
336344

337345
<br />
338346

339-
## Vue Router - Scroll to and from hash
347+
## Vue Router - Scroll to and from anchors
340348

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.
342350
343-
### Scrolling to hash
351+
### Scrolling to anchors
344352

345353
For content scrolled by the window, simply return the target element. To scroll to a target scrolled by a container, use _scrollIntoView_ method.
346354

@@ -368,9 +376,9 @@ const router = createRouter({
368376
369377
> :bulb: There's no need need to set overlayHeight if using `scrollIntoView` as the method is aware of target's `scroll-margin-top` property.
370378
371-
### Scrolling from hash back to the top of the page
379+
### Scrolling from anchor back to the top of the page
372380

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.
374382

375383
```js
376384
const router = createRouter({
@@ -414,12 +422,40 @@ If you don't like that, choose to replace instead of pushing the hash:
414422

415423
<br />
416424

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+
417453
## Setting scroll-margin-top for fixed headers
418454

419455
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:
420456

421457
```js
422-
useActive(targets, { overlayHeight: 100 })
458+
useActiveScroll(targets, { overlayHeight: 100 })
423459
```
424460

425461
```css
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<link rel="icon" href="/favicon.ico" />
6+
<link rel="icon" href="demo/public/favicon.ico" />
77
<title>Vue Use Active Scroll</title>
88
<meta
99
name="description"
@@ -12,6 +12,6 @@
1212
</head>
1313
<body>
1414
<div id="app"></div>
15-
<script type="module" src="/main.ts"></script>
15+
<script type="module" src="demo/main.ts"></script>
1616
</body>
1717
</html>

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-use-active-scroll",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"private": false,
55
"description": "Accurate TOC/sidebar links without compromises for Vue 3.",
66
"keywords": [
@@ -40,7 +40,6 @@
4040
"scripts": {
4141
"build": "rm -rf dist && vue-tsc && vite build && rm -rf dist/_redirects dist/favicon.ico",
4242
"build:app": "vue-tsc && vite build --mode app",
43-
"postbuild": "cp -R src/types.ts dist/index.d.ts",
4443
"dev": "vite",
4544
"prepare": "husky install",
4645
"test": "cypress run --component",
@@ -51,16 +50,17 @@
5150
},
5251
"devDependencies": {
5352
"@rollup/plugin-terser": "^0.4.3",
54-
"@types/node": "^20.6.2",
53+
"@types/node": "^20.6.3",
5554
"@vitejs/plugin-vue": "^4.3.4",
5655
"animated-scroll-to": "^2.3.0",
5756
"cypress": "^13.2.0",
5857
"husky": "^8.0.3",
5958
"prettier": "^3.0.3",
6059
"typescript": "^5.2.2",
6160
"vite": "4.4.9",
61+
"vite-plugin-dts": "^3.5.4",
6262
"vue": "^3.3.4",
6363
"vue-router": "^4.2.4",
64-
"vue-tsc": "^1.8.11"
64+
"vue-tsc": "^1.8.13"
6565
}
6666
}

0 commit comments

Comments
 (0)