Skip to content

Commit 4740b1c

Browse files
authored
feat: scroll behavior (#50)
1 parent 58e4b70 commit 4740b1c

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

.changeset/empty-donkeys-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv-router': patch
3+
---
4+
5+
Scroll behavior features

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export default defineConfig({
5757
{ text: 'Search Params', link: '/guide/common/search-params' },
5858
{ text: 'Active Route', link: '/guide/common/active-route' },
5959
{ text: 'Preloading', link: '/guide/common/preloading' },
60+
{ text: 'Scroll Behavior', link: '/guide/common/scroll-behavior' },
6061
],
6162
},
6263
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Scroll Behavior
2+
3+
## Scroll To Top
4+
5+
By default, the router automatically scrolls to the top of the page when navigating to a new route. You can customize this behavior on a per-navigation basis by providing a `scrollToTop` option:
6+
7+
```ts
8+
// Default behavior - scroll to top instantly
9+
navigate('/about');
10+
11+
// Scroll to top with smooth animation
12+
navigate('/about', { scrollToTop: 'smooth' });
13+
14+
// Prevent scrolling to top
15+
navigate('/about', { scrollToTop: false });
16+
```
17+
18+
This property accepts the same values as the `behavior` parameter of the browser's [`scrollTo` method](https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo#parameters).
19+
20+
For standard anchor tags, control scroll behavior with the `data-scroll-to-top` attribute:
21+
22+
```svelte
23+
<!-- Default behavior - scroll to top instantly -->
24+
<a href={p('/about')}>About</a>
25+
26+
<!-- Scroll to top with smooth animation -->
27+
<a href={p('/about')} data-scroll-to-top="smooth">About</a>
28+
29+
<!-- Prevent scrolling to top -->
30+
<a href={p('/about')} data-scroll-to-top="false">About</a>
31+
```
32+
33+
## Scroll Position Restoration
34+
35+
sv-router leverages the browser's native scroll restoration mechanism by default. This means that when using the browser back/forward buttons, scroll positions are automatically restored.
36+
37+
If you want to implement your own scroll restoration logic or disable it entirely, you can opt-out of the browser's native behavior through the [`scrollRestoration` property of the History API](https://developer.mozilla.org/en-US/docs/Web/API/History/scrollRestoration):
38+
39+
```ts
40+
history.scrollRestoration = 'manual';
41+
```

docs/reference/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Programmatically navigate to a route.
4646
- `search` - Query string
4747
- `state` - History state to save
4848
- `hash` - URL hash fragment
49+
- `scrollToTop` - Scroll behavior (`"auto" | "instant" | "smooth" | false`)
4950
- `params` - Parameters to substitute in the path
5051

5152
#### `isActive(path, params?)`

src/create-router.svelte.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export async function onNavigate(path, options = {}) {
113113
syncSearchParams();
114114
Object.assign(location, updatedLocation());
115115

116+
if (options.scrollToTop !== false) {
117+
window.scrollTo({ top: 0, left: 0, behavior: options.scrollToTop });
118+
}
119+
116120
for (const { afterLoad } of hooks) {
117121
afterLoad?.();
118122
}
@@ -130,12 +134,13 @@ export function onGlobalClick(event) {
130134
if (url.origin !== currentOrigin) return;
131135

132136
event.preventDefault();
133-
const { replace, state } = anchor.dataset;
137+
const { replace, state, scrollToTop } = anchor.dataset;
134138
onNavigate(url.pathname, {
135139
replace: replace === '' || replace === 'true',
136140
search: url.search,
137141
state,
138142
hash: url.hash,
143+
scrollToTop: scrollToTop === 'false' ? false : /** @type ScrollBehavior */ (scrollToTop),
139144
});
140145
}
141146

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export type NavigateOptions =
162162
search?: string;
163163
state?: string;
164164
hash?: string;
165+
scrollToTop?: ScrollBehavior | false;
165166
}
166167
| undefined;
167168

0 commit comments

Comments
 (0)