Skip to content

Commit 7cc079d

Browse files
committed
update readme
1 parent e30c74a commit 7cc079d

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

README.md

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Vue Use Active Scroll
22

3-
**Examples:** Vite: [Demo App]() — Nuxt Content: [TOC](https://stackblitz.com/edit/github-tlaeiq?file=pages%2F[...slug].vue) - [Nested TOC](https://stackblitz.com/edit/github-oh85gq?file=pages%2F[...slug].vue)
3+
**Examples:** Vite: [Demo App]() — Nuxt Content: [Nested TOC](https://stackblitz.com/edit/github-oh85gq?file=components%2FSidebar.vue)
44

55
:bulb: Requires Vue 3 or above.
66

@@ -11,9 +11,9 @@
1111
The [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) is a great API.
1212
But it may not be the one-size-fits-all solution to highlight menu/sidebar links.
1313

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 hash may not reflect the active link.
1515

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

1818
For example, you want to immediately highlight targets when scroll is originated from click but not when scroll is originated from wheel/touch.
1919

@@ -92,37 +92,38 @@ const { isActive } = useActive(targets)
9292

9393
> :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.
9494
95-
<details><summary><strong>Nuxt Content</strong></summary>
95+
<details><summary><strong>Nuxt Content 2</strong></summary>
9696

9797
<br />
9898

99-
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:
100100

101101
```js
102-
const { data } = await useAsyncData('about', () =>
103-
queryContent('/about').findOne()
104-
)
102+
const { toc } = useContent()
105103
```
106104

105+
Then just compute the array of the IDs to observe (assuming max depth is 3):
106+
107107
```js
108-
// data.body.toc.links
109-
110-
;[
111-
{
112-
id: 'title-1',
113-
depth: 2,
114-
text: 'Title 1',
115-
children: [{ id: 'subtitle-1', depth: 3, text: 'Subtitle 1' }] // Nested
116-
},
117-
{ id: 'title-2', depth: 2, text: 'Title 2' },
118-
{ id: 'title-3', depth: 2, text: 'Title 3' },
119-
{ id: 'title-4', depth: 2, text: 'Title 4' }
120-
]
108+
const targets = computed(() =>
109+
toc.value.links.flatMap(({ id, children = [] }) => [
110+
id,
111+
...children.map(({ id }) => id)
112+
])
113+
)
114+
115+
const { setActive, isActive } = useActive(targets)
121116
```
122117

123-
You can compute a flat array of the IDs to observe by mapping `data.body.toc.links`:
118+
<details><summary><strong>Without Document-driven</strong></summary>
119+
120+
<br />
124121

125122
```js
123+
const { data } = await useAsyncData('about', () =>
124+
queryContent('/about').findOne()
125+
)
126+
126127
const targets = computed(() =>
127128
data.value
128129
? data.value.body.toc.links.flatMap(({ id, children = [] }) => [
@@ -132,13 +133,13 @@ const targets = computed(() =>
132133
: []
133134
)
134135

135-
// console.log(targets.value) => ['title-1', 'subtitle-1', 'title-2', 'title-3', 'title-4']
136-
137136
const { isActive } = useActive(targets)
138137
```
139138

140139
</details>
141140

141+
</details>
142+
142143
<br />
143144

144145
## 2. Configure the composable (optional)
@@ -163,12 +164,12 @@ const { isActive, setActive } = useActive(targets, {
163164

164165
### Return object
165166

166-
| Name | Type | Description |
167-
| ----------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
168-
| 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. |
167+
| Name | Type | Description |
168+
| ----------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
169+
| 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. |
172173

173174
<br />
174175

0 commit comments

Comments
 (0)