Skip to content

Commit 977c464

Browse files
committed
update readme, test condition
1 parent 20b8795 commit 977c464

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
The [Intersection Observer](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) is a great API.
1515
But it may not be the one-size-fits-all solution to highlight menu/sidebar links.
1616

17-
You may noticed that last targets, may 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.
17+
You may noticed that last targets may 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.
1818

1919
But also, it's tricky to customize behavior according to different scroll interactions.
2020

@@ -26,7 +26,7 @@ For example, you want to immediately highlight targets when scroll is originated
2626

2727
- Precise and stable at any speed
2828
- CSS scroll-behavior and callback agnostic
29-
- Adaptive behavior on mount (hash), scroll, click, cancel.
29+
- Adaptive behavior on mount, back/forward navigation, scroll, click, cancel.
3030
- Customizable boundary offsets for each direction
3131
- Customizable behavior on top/bottom reached
3232
- Supports containers different than window
@@ -179,11 +179,14 @@ const { isActive, setActive } = useActive(targets, {
179179

180180
## 3. Create your sidebar
181181

182-
### **1.** Call `setActive` in your click handler by passing the anchor ID
182+
### **1.** Call _setActive_ in your click handler by passing the anchor ID
183183

184184
```vue
185+
<!-- Sidebar.vue -->
186+
185187
<script setup>
186188
// ...
189+
187190
const { isActive, setActive } = useActive(targets)
188191
</script>
189192
@@ -199,18 +202,37 @@ const { isActive, setActive } = useActive(targets)
199202
</a>
200203
</nav>
201204
</template>
205+
```
202206

203-
<style>
207+
<br />
208+
209+
### **2.** Define scroll behavior
210+
211+
You're free to choose between CSS (smooth or auto), [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) or even a library like [animated-scroll-to](https://github.com/Stanko/animated-scroll-to).
212+
213+
#### A. Using CSS scroll-behavior
214+
215+
- If content is scrolled by the window, add the following CSS rule to your `html` element:
216+
217+
```css
204218
html {
205-
/* or .container { */
206219
scroll-behavior: smooth; /* or 'auto' */
207220
}
208-
</style>
209221
```
210222

211-
Feel free to create your own click handler and to choose the scrolling strategy: CSS (smooth or auto), [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) or even a library like [animated-scroll-to](https://github.com/Stanko/animated-scroll-to) with custom easings will work. Just remember to call `setActive` in your handler.
223+
- If content is scrolled by a container:
212224

213-
<details><summary><strong>Custom Scroll Callback</strong></summary>
225+
```css
226+
html {
227+
scroll-behavior: auto; /* Keep it 'auto' */
228+
}
229+
230+
.container {
231+
scroll-behavior: smooth;
232+
}
233+
```
234+
235+
<details><summary><strong>B. Custom Scroll Animation</strong></summary>
214236

215237
<br />
216238

@@ -250,9 +272,11 @@ function scrollTo(event, id) {
250272

251273
</details>
252274

253-
### **2.** Use `isActive` to style the active link:
275+
<br />
276+
277+
### **3.** Use _isActive_ or _activeId_ to style the active link:
254278

255-
> :bulb: If you're playing with transitions or advanced styling rules simply leverage _activeIndex_ and _activeId_.
279+
> :bulb: If you're playing with transitions simply leverage _activeIndex_.
256280
257281
```vue
258282
<script setup>
@@ -345,9 +369,9 @@ useActive(targets, { overlayHeight: 100 })
345369

346370
<br />
347371

348-
## Vue Router onMount hash scroll
372+
## Vue Router scroll to hash on page mount
349373

350-
If using Nuxt, Vue Router is already configured to scroll to the URL hash on page load.
374+
If using Nuxt, Vue Router is already configured to scroll to the URL hash on page load, back/forward navigation.
351375

352376
If not using Nuxt and you're setting up Vue Router from scratch, you must enable the feature manually:
353377

@@ -358,23 +382,18 @@ const router = createRouter({
358382
if (to.hash) {
359383
return {
360384
el: to.hash
361-
// top: 100 // Eventual overlayHeight value
385+
// top: 100 // Eventual fixed header (overlayHeight)
362386
}
363387
}
364388
}
365389
})
366390
```
367391

368-
```css
369-
html {
370-
/* Or .container { */
371-
scroll-behavior: smooth; /* Or auto */
372-
}
373-
```
392+
> :bulb: If you using native CSS scroll-behavior, [adding the rule](#define-scroll-behavior) to your CSS is enough. No need to set it again in Vue Router.
374393
375394
### Containers
376395

377-
The above rule will work if your content is scrolled by the window. If you want to scroll to a target inside a container, you must use `scrollIntoView`:
396+
If you want to scroll to a target inside a container, you must use `scrollIntoView`:
378397

379398
```js
380399
const router = createRouter({

src/useActive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export function useActive(
246246
});
247247

248248
watch(activeId, (newId) => {
249-
if (!replaceHash) {
249+
if (replaceHash) {
250250
const start = jumpToFirst ? 0 : -1;
251251
const newHash = `${location.pathname}${activeIndex.value > start ? `#${newId}` : ''}`;
252252
history.replaceState(history.state, '', newHash);

0 commit comments

Comments
 (0)