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
The [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) is a great API.
12
12
But it may not be the one-size-fits-all solution to highlight menu/sidebar links.
13
13
14
-
In some cases, you may noticed that last targets never intersect and that by clicking on the correspondent links highlights the wrong ones (or does nothing). Same may happen with the URL hash, which in some cases may not reflect the actual active target.
14
+
You may noticed that last targets, never intersect if entirely visible in the viewport. Clicking on their links highlights other links or does nothing. In addition to that, the URL hashmay not reflect the active link.
15
15
16
-
But most important, you noticed that's tricky to customize behavior according to different scroll interactions.
16
+
But also, it's tricky to customize behavior according to different scroll interactions.
17
17
18
18
For example, you want to immediately highlight targets when scroll is originated from click but not when scroll is originated from wheel/touch.
> :bulb: For a TOC, you want to target (and scroll) the headings of your sections (instead of the whole section) to ensure results better-aligned with users' reading flow.
Nuxt Content automatically applies IDs to your headings. You can get the TOC links by accessing `data.body.toc.links` via [queryContent](https://content.nuxtjs.org/api/composables/query-pages/).
99
+
Nuxt Content automatically applies IDs to your headings. If enabled the [document-driven mode](https://content.nuxtjs.org/guide/writing/document-driven/) you can directly query the TOC in your sidebar component:
100
100
101
101
```js
102
-
const { data } =awaituseAsyncData('about', () =>
103
-
queryContent('/about').findOne()
104
-
)
102
+
const { toc } =useContent()
105
103
```
106
104
105
+
Then just compute the array of the IDs to observe (assuming max depth is 3):
| setActive |`(id: string) => void`| Function to include in your click handlers in order to ensure proper behavior between any interaction which may trigger or cancel a scroll event. |
169
-
| isActive |`(id: string) => boolean`| Whether the given Id is active or not |
170
-
| activeId |`Ref<string>`| Id of the active target |
171
-
| activeIndex |`Ref<number>`| Index of the active target in offset order, `0` for the first target and so on. |
| setActive |`(id: string) => void`| Function to include in your click handlers to ensure proper behavior between any interaction which may trigger or alter highlighting. |
170
+
| isActive |`(id: string) => boolean`| Whether the given Id is active or not |
171
+
| activeId |`Ref<string>`| Id of the active target |
172
+
| activeIndex |`Ref<number>`| Index of the active target in offset order, `0` for the first target and so on. |
0 commit comments