sv-router 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/create-router.svelte.js +24 -0
- package/src/helpers/match-route.js +2 -2
- package/src/index.d.ts +9 -0
- package/src/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sv-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Modern Svelte Routing",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"svelte",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
49
49
|
"eslint-plugin-svelte": "^3.13.1",
|
|
50
50
|
"eslint-plugin-unicorn": "^62.0.0",
|
|
51
|
-
"globals": "^
|
|
51
|
+
"globals": "^17.0.0",
|
|
52
52
|
"happy-dom": "^20.0.11",
|
|
53
53
|
"prettier": "^3.7.4",
|
|
54
54
|
"prettier-plugin-jsdoc": "^1.8.0",
|
|
@@ -43,6 +43,9 @@ let params = $state({ value: {} });
|
|
|
43
43
|
|
|
44
44
|
let meta = $state({ value: {} });
|
|
45
45
|
|
|
46
|
+
/** @type {(() => boolean) | null} */
|
|
47
|
+
let navigationBlocker = null;
|
|
48
|
+
|
|
46
49
|
let navigationIndex = 0;
|
|
47
50
|
let pendingNavigationIndex = 0;
|
|
48
51
|
|
|
@@ -151,6 +154,22 @@ export async function onNavigate(path, options = {}) {
|
|
|
151
154
|
throw new Error('Router not initialized: `createRouter` was not called.');
|
|
152
155
|
}
|
|
153
156
|
|
|
157
|
+
if (navigationBlocker) {
|
|
158
|
+
if (!navigationBlocker()) {
|
|
159
|
+
const url = new URL(globalThis.location.toString());
|
|
160
|
+
url.search = location.search;
|
|
161
|
+
url.hash = location.hash;
|
|
162
|
+
if (base.name === '#') {
|
|
163
|
+
url.hash = location.pathname;
|
|
164
|
+
} else {
|
|
165
|
+
url.pathname = location.pathname;
|
|
166
|
+
}
|
|
167
|
+
globalThis.history.replaceState($state.snapshot(location.state) || {}, '', url.toString());
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
navigationBlocker = null;
|
|
171
|
+
}
|
|
172
|
+
|
|
154
173
|
navigationIndex++;
|
|
155
174
|
const currentNavigationIndex = navigationIndex;
|
|
156
175
|
|
|
@@ -292,3 +311,8 @@ export function onGlobalClick(event) {
|
|
|
292
311
|
viewTransition: viewTransition === '' || viewTransition === 'true',
|
|
293
312
|
});
|
|
294
313
|
}
|
|
314
|
+
|
|
315
|
+
/** @param {() => boolean} callback */
|
|
316
|
+
export function blockNavigation(callback) {
|
|
317
|
+
navigationBlocker = callback;
|
|
318
|
+
}
|
|
@@ -64,11 +64,11 @@ export function matchRoute(pathname, routes) {
|
|
|
64
64
|
|
|
65
65
|
const pathPart = pathParts[index];
|
|
66
66
|
if (routePart.startsWith(':')) {
|
|
67
|
-
params[routePart.slice(1)] = pathPart;
|
|
67
|
+
params[routePart.slice(1)] = decodeURIComponent(pathPart);
|
|
68
68
|
} else if (routePart.startsWith('*')) {
|
|
69
69
|
const param = routePart.slice(1);
|
|
70
70
|
if (param) {
|
|
71
|
-
params[param] = pathParts.slice(index).join('/');
|
|
71
|
+
params[param] = pathParts.slice(index).map(decodeURIComponent).join('/');
|
|
72
72
|
}
|
|
73
73
|
if (breakFromLayouts) {
|
|
74
74
|
routePart = `(${routePart})`;
|
package/src/index.d.ts
CHANGED
|
@@ -28,6 +28,15 @@ export function serializeSearch(search: Search): string | undefined;
|
|
|
28
28
|
*/
|
|
29
29
|
export function createRouter<T extends Routes>(r: T): RouterApi<T>;
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Block navigation until the callback returns `false`.
|
|
33
|
+
*
|
|
34
|
+
* ```js
|
|
35
|
+
* blockNavigation(() => confirm('Are you sure you want to leave?'));
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function blockNavigation(callback: () => boolean): void;
|
|
39
|
+
|
|
31
40
|
/**
|
|
32
41
|
* The component that will render the current route. You can pass a `base` prop to set the base path
|
|
33
42
|
* that is prepended to every url.
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { isActiveLink } from './actions.svelte.js';
|
|
2
|
-
export { createRouter } from './create-router.svelte.js';
|
|
2
|
+
export { blockNavigation, createRouter } from './create-router.svelte.js';
|
|
3
3
|
export { serializeSearch } from './helpers/utils.js';
|
|
4
4
|
export { Navigation } from './navigation.js';
|
|
5
5
|
export { default as Router } from './Router.svelte';
|