Skip to content

Commit 3a233a1

Browse files
authored
feat: improve navigate traversing api (#37)
1 parent 67ccdef commit 3a233a1

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

.changeset/clear-sites-attend.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+
Improve navigate traversing api

docs/guide/common/navigation.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,15 @@ navigate('/post/:slug', {
8080
});
8181
```
8282

83-
For browser history navigation, use:
83+
The `navigate` function also supports traversing through browser history, similar to the native [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API):
8484

8585
```ts
86-
navigate.back();
87-
navigate.forward();
86+
// Go back one page (equivalent to the browser's back button)
87+
navigate(-1);
88+
89+
// Go forward one page (equivalent to the browser's forward button)
90+
navigate(1);
91+
92+
// Go back two pages
93+
navigate(-2);
8894
```

docs/reference/index.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,16 @@ Programmatically navigate to a route.
3838

3939
**Parameters:**
4040

41-
- `path` - The route to navigate to
41+
- `path | number` - Either:
42+
- A string path to navigate to, or
43+
- A number representing steps to navigate in history (negative for back, positive for forward)
4244
- `options` - (Optional) Navigation options
4345
- `replace` - Replace current history entry instead of pushing
4446
- `search` - Query string
4547
- `state` - History state to save
4648
- `hash` - URL hash fragment
4749
- `params` - Parameters to substitute in the path
4850

49-
**Methods:**
50-
51-
- `navigate.back()` - Navigate back in history
52-
- `navigate.forward()` - Navigate forward in history
53-
5451
#### `isActive(path, params?)`
5552

5653
Checks if a given path is currently active.

src/create-router.svelte.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ export function createRouter(r) {
5757
}
5858

5959
/**
60-
* @param {string} path
60+
* @param {string | number} path
6161
* @param {import('./index.d.ts').NavigateOptions & { params?: Record<string, string> }} options
6262
*/
6363
function navigate(path, options = {}) {
64+
if (typeof path === 'number') {
65+
globalThis.history.go(path);
66+
return;
67+
}
6468
if (options.params) {
6569
path = constructPath(path, options.params);
6670
}
@@ -72,8 +76,6 @@ function navigate(path, options = {}) {
7276
}
7377
onNavigate(path, options);
7478
}
75-
navigate.back = () => globalThis.history.back();
76-
navigate.forward = () => globalThis.history.forward();
7779

7880
/**
7981
* @param {string} [path]

src/index.d.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,15 @@ export type RouterApi<T extends Routes> = {
9696
* },
9797
* });
9898
* // Back and forward
99-
* navigate.back();
100-
* navigate.forward();
99+
* navigate(-1);
100+
* navigate(2);
101101
* ```
102102
*
103103
* @param route The route to navigate to.
104104
* @param options The navigation options.
105105
*/
106-
navigate: {
107-
<U extends Path<T>>(...args: NavigateArgs<U>): void;
108-
back: () => void;
109-
forward: () => void;
110-
};
106+
navigate<U extends Path<T>>(...args: NavigateArgs<U>): void;
107+
111108
/**
112109
* Will return `true` if the given path is active.
113110
*
@@ -163,9 +160,10 @@ export type NavigateOptions =
163160
| undefined;
164161

165162
type NavigateArgs<T extends string> =
166-
PathParams<T> extends never
167-
? [T] | [T, NavigateOptions]
168-
: [T, NavigateOptions & { params: PathParams<T> }];
163+
| (PathParams<T> extends never
164+
? [T] | [T, NavigateOptions]
165+
: [T, NavigateOptions & { params: PathParams<T> }])
166+
| [number];
169167

170168
type StripNonRoutes<T extends Routes> = {
171169
[K in keyof T as K extends `*${string}`

0 commit comments

Comments
 (0)