@nano_kit/router 1.0.0-alpha.4
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/LICENSE +21 -0
- package/README.md +114 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/di.d.ts +45 -0
- package/dist/di.d.ts.map +1 -0
- package/dist/diy.d.ts +51 -0
- package/dist/diy.d.ts.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +606 -0
- package/dist/index.js.map +1 -0
- package/dist/navigation.d.ts +26 -0
- package/dist/navigation.d.ts.map +1 -0
- package/dist/navigation.types.d.ts +50 -0
- package/dist/navigation.types.d.ts.map +1 -0
- package/dist/paths.d.ts +18 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.types.d.ts +7 -0
- package/dist/paths.types.d.ts.map +1 -0
- package/dist/router.d.ts +87 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.types.d.ts +54 -0
- package/dist/router.types.d.ts.map +1 -0
- package/dist/searchParams.d.ts +19 -0
- package/dist/searchParams.d.ts.map +1 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +62 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 - present, TrigenSoftware
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @nano_kit/router
|
|
2
|
+
|
|
3
|
+
[![ESM-only package][package]][package-url]
|
|
4
|
+
[![NPM version][npm]][npm-url]
|
|
5
|
+
[![Dependencies status][deps]][deps-url]
|
|
6
|
+
[![Install size][size]][size-url]
|
|
7
|
+
[![Build status][build]][build-url]
|
|
8
|
+
[![Coverage status][coverage]][coverage-url]
|
|
9
|
+
|
|
10
|
+
[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg
|
|
11
|
+
[package-url]: https://nodejs.org/api/esm.html
|
|
12
|
+
|
|
13
|
+
[npm]: https://img.shields.io/npm/v/@nano_kit/router.svg
|
|
14
|
+
[npm-url]: https://npmjs.com/package/@nano_kit/router
|
|
15
|
+
|
|
16
|
+
[deps]: https://img.shields.io/librariesio/release/npm/@nano_kit/router
|
|
17
|
+
[deps-url]: https://libraries.io/npm/@nano_kit/router/tree
|
|
18
|
+
|
|
19
|
+
[size]: https://deno.bundlejs.com/badge?q=@nano_kit/router
|
|
20
|
+
[size-url]: https://bundlejs.com/?q=@nano_kit/router
|
|
21
|
+
|
|
22
|
+
[build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/nano_kit/tests.yml?branch=main
|
|
23
|
+
[build-url]: https://github.com/TrigenSoftware/nano_kit/actions
|
|
24
|
+
|
|
25
|
+
[coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/nano_kit.svg
|
|
26
|
+
[coverage-url]: https://app.codecov.io/gh/TrigenSoftware/nano_kit
|
|
27
|
+
|
|
28
|
+
<picture>
|
|
29
|
+
<source media="(prefers-color-scheme: dark)" srcset="../../website/src/assets/ring-system_white.svg">
|
|
30
|
+
<img alt="Four-pointed ring system star logo" src="../../website/src/assets/ring-system_black.svg" width="100" height="100" align="right">
|
|
31
|
+
</picture>
|
|
32
|
+
|
|
33
|
+
A small and powerful router for [@nano_kit/store](../store) state manager.
|
|
34
|
+
|
|
35
|
+
- **Small**. Around 2 kB (minified & brotlied). Zero dependencies except [@nano_kit/store](../store).
|
|
36
|
+
- **Type-safe**. Full TypeScript support with type inference for routes and parameters.
|
|
37
|
+
- **Signal-based**. Built on top of [@nano_kit/store](../store)'s reactive signals for automatic UI updates.
|
|
38
|
+
- **Flexible**. Supports nested layouts, parameterized routes, optional parameters, wildcards, and query parameters.
|
|
39
|
+
- **SSR-ready**. Works seamlessly with server-side rendering.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pnpm add @nano_kit/store @nano_kit/router
|
|
45
|
+
# or
|
|
46
|
+
npm install @nano_kit/store @nano_kit/router
|
|
47
|
+
# or
|
|
48
|
+
yarn add @nano_kit/store @nano_kit/router
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
Here is a minimal example demonstrating navigation, routing, and reactive page rendering:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { effect } from '@nano_kit/store'
|
|
57
|
+
import { browserNavigation, router, page, layout, notFound } from '@nano_kit/router'
|
|
58
|
+
|
|
59
|
+
/* Define your routes */
|
|
60
|
+
const routes = {
|
|
61
|
+
home: '/',
|
|
62
|
+
user: '/users/:id',
|
|
63
|
+
userPosts: '/users/:id/posts',
|
|
64
|
+
admin: '/admin/*'
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/* Setup navigation with browser history */
|
|
68
|
+
const [$location, navigation] = browserNavigation(routes)
|
|
69
|
+
|
|
70
|
+
/* Define page components */
|
|
71
|
+
const HomePage = () => 'Welcome Home!'
|
|
72
|
+
const UserPage = () => `User ID: ${$location().params.id}`
|
|
73
|
+
const UserPostsPage = () => `Posts for User: ${$location().params.id}`
|
|
74
|
+
const AdminLayout = ($page) => `Admin Layout: ${$page()}`
|
|
75
|
+
const AdminPage = () => `Admin Page: ${$location().params.wildcard || 'dashboard'}`
|
|
76
|
+
const NotFoundPage = () => 'Page Not Found'
|
|
77
|
+
|
|
78
|
+
/* Create router with pages and layouts */
|
|
79
|
+
const [$page] = router($location, [
|
|
80
|
+
page('home', HomePage),
|
|
81
|
+
page('user', UserPage),
|
|
82
|
+
page('posts', UserPostsPage),
|
|
83
|
+
layout(AdminLayout, [
|
|
84
|
+
page('admin', AdminPage)
|
|
85
|
+
]),
|
|
86
|
+
notFound(NotFoundPage)
|
|
87
|
+
], composeLayoutFunction)
|
|
88
|
+
|
|
89
|
+
/* React to route changes (mounting $page triggers router) */
|
|
90
|
+
const unsub = effect(() => {
|
|
91
|
+
const PageComponent = $page()
|
|
92
|
+
|
|
93
|
+
console.log('Current page:', PageComponent())
|
|
94
|
+
// Render PageComponent in your app
|
|
95
|
+
})
|
|
96
|
+
// Output: Current page: Welcome Home!
|
|
97
|
+
|
|
98
|
+
/* Navigate programmatically */
|
|
99
|
+
navigation.push('/users/123')
|
|
100
|
+
// Output: Current page: User ID: 123
|
|
101
|
+
|
|
102
|
+
navigation.push('/admin/settings/profile')
|
|
103
|
+
// Output: Current page: Admin Layout: Admin Page: settings/profile
|
|
104
|
+
|
|
105
|
+
navigation.back()
|
|
106
|
+
// Output: Current page: User ID: 123
|
|
107
|
+
|
|
108
|
+
/* Cleanup */
|
|
109
|
+
unsub()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Documentation
|
|
113
|
+
|
|
114
|
+
For comprehensive guides, advanced patterns, and API reference, visit the [documentation website](https://nano_kit.js.org/router).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,SAAS,CAAA;AACvC,eAAO,MAAM,oBAAoB,YAAY,CAAA;AAC7C,eAAO,MAAM,gBAAgB,QAAQ,CAAA"}
|
package/dist/di.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type InjectionFactory, type ReadableSignal } from '@nano_kit/store';
|
|
2
|
+
import type { Routes } from './types.js';
|
|
3
|
+
import { type Navigation, type RouteLocationRecord } from './navigation.js';
|
|
4
|
+
import { type LayoutMatchRef, type PageMatchRef, type StoresPreload } from './router.js';
|
|
5
|
+
/**
|
|
6
|
+
* Create browser navigation and location injection factories.
|
|
7
|
+
* @param routes - Routes object defining path patterns.
|
|
8
|
+
* @returns Tuple of location and navigation injection factories.
|
|
9
|
+
*/
|
|
10
|
+
export declare function browserNavigation$<const R extends Routes = {}>(routes?: R): [
|
|
11
|
+
InjectionFactory<RouteLocationRecord<R>>,
|
|
12
|
+
InjectionFactory<Navigation>
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Create virtual navigation and location injection factories.
|
|
16
|
+
* @param initialPath - Initial path for the virtual navigation (default: '/').
|
|
17
|
+
* @param routes - Routes object defining path patterns.
|
|
18
|
+
* @returns Tuple of location and navigation injection factories.
|
|
19
|
+
*/
|
|
20
|
+
export declare function virtualNavigation$<const R extends Routes = {}>(initialPath?: string, routes?: R): [
|
|
21
|
+
InjectionFactory<RouteLocationRecord<R>>,
|
|
22
|
+
InjectionFactory<Navigation>
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Creates a current route matching page injection factory.
|
|
26
|
+
* @param Location$ - Injection factory for the route location record.
|
|
27
|
+
* @param pages - Array of page match references.
|
|
28
|
+
* @returns Tuple of page injection factory and stores preload injection factory.
|
|
29
|
+
*/
|
|
30
|
+
export declare function router$<R extends Routes, K extends keyof R & string, P>(Location$: InjectionFactory<RouteLocationRecord<R>>, pages: (PageMatchRef<NoInfer<K>, P> | PageMatchRef<null, P>)[]): [
|
|
31
|
+
InjectionFactory<ReadableSignal<P | null>>,
|
|
32
|
+
InjectionFactory<StoresPreload>
|
|
33
|
+
];
|
|
34
|
+
/**
|
|
35
|
+
* Creates a current route matching page and layout injection factory.
|
|
36
|
+
* @param Location$ - Injection factory for the route location record.
|
|
37
|
+
* @param pages - Array of page and layout match references.
|
|
38
|
+
* @param compose - Function to compose layouts with nested content.
|
|
39
|
+
* @returns Tuple of page injection factory and stores preload injection factory.
|
|
40
|
+
*/
|
|
41
|
+
export declare function router$<R extends Routes, K extends keyof R & string, P, N, L, C>(Location$: InjectionFactory<RouteLocationRecord<R>>, pages: (PageMatchRef<NoInfer<K>, P> | PageMatchRef<null, P> | LayoutMatchRef<NoInfer<K>, N, L>)[], compose: ($nested: ReadableSignal<N | null>, layout: L) => C): [
|
|
42
|
+
InjectionFactory<ReadableSignal<P | C | null>>,
|
|
43
|
+
InjectionFactory<StoresPreload>
|
|
44
|
+
];
|
|
45
|
+
//# sourceMappingURL=di.d.ts.map
|
package/dist/di.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"di.d.ts","sourceRoot":"","sources":["../src/di.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,cAAc,EAEpB,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACxC,OAAO,EACL,KAAK,UAAU,EACf,KAAK,mBAAmB,EAGzB,MAAM,iBAAiB,CAAA;AACxB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C,CAAC,CAAC,EAAE,CAAC,CAAC,GAC3B,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,GACrB,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CACnC,EAAE,EACH,OAAO,EAAE,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,KAAK,CAAC,GAC3D;IACD,gBAAgB,CAAC,cAAc,CAAC,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC;IAC9C,gBAAgB,CAAC,aAAa,CAAC;CAChC,CAAA"}
|
package/dist/diy.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Navigation } from './navigation.js';
|
|
2
|
+
/**
|
|
3
|
+
* Handle link click events to navigate using the router.
|
|
4
|
+
* @param this - Router navigation object
|
|
5
|
+
* @param event - MouseEvent from the click
|
|
6
|
+
*/
|
|
7
|
+
export declare function onLinkClick(this: Navigation, event: Pick<MouseEvent, 'target' | 'button' | 'defaultPrevented' | 'altKey' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'preventDefault'>): void;
|
|
8
|
+
/**
|
|
9
|
+
* Effect to listen for link clicks and navigate using the router.
|
|
10
|
+
* @param navigation - Router navigation object
|
|
11
|
+
* @returns Cleanup function to remove the event listener
|
|
12
|
+
*/
|
|
13
|
+
export declare function listenLinks(navigation: Navigation): () => void;
|
|
14
|
+
/**
|
|
15
|
+
* Reset scroll position to the top of the page.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resetScroll(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Scroll to an anchor on the page.
|
|
20
|
+
* @param hash - The anchor hash (e.g., #section1)
|
|
21
|
+
* @param options - `scrollIntoView` options
|
|
22
|
+
*/
|
|
23
|
+
export declare function scrollToAnchor(hash: string, options?: ScrollIntoViewOptions): void;
|
|
24
|
+
/**
|
|
25
|
+
* Utility class to save and restore scroll positions using session storage.
|
|
26
|
+
*/
|
|
27
|
+
export declare class ScrollRestorator {
|
|
28
|
+
#private;
|
|
29
|
+
/**
|
|
30
|
+
* Create a ScrollRestorator instance.
|
|
31
|
+
* @param storageKeyPrefix - Prefix for session storage keys
|
|
32
|
+
*/
|
|
33
|
+
constructor(storageKeyPrefix?: string);
|
|
34
|
+
/**
|
|
35
|
+
* Save the current scroll position for the given location.
|
|
36
|
+
* @param location - The location object
|
|
37
|
+
*/
|
|
38
|
+
save(location: Location): void;
|
|
39
|
+
/**
|
|
40
|
+
* Restore the scroll position for the given location.
|
|
41
|
+
* @param location - The location object
|
|
42
|
+
* @returns True if a saved position was restored, false otherwise
|
|
43
|
+
*/
|
|
44
|
+
restore(location: Location): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Clear the saved scroll position for the given location.
|
|
47
|
+
* @param location - The location object
|
|
48
|
+
*/
|
|
49
|
+
clear(location: Location): void;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=diy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diy.d.ts","sourceRoot":"","sources":["../src/diy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEjD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,QAAQ,GAAG,QAAQ,GAAG,kBAAkB,GAAG,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,UAAU,GAAG,gBAAgB,CAAC,QAqBrI;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,UAAU,EAAE,UAAU,cAQjD;AAED;;GAEG;AACH,wBAAgB,WAAW,SAE1B;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,qBAAqB,QAS3E;AAED;;GAEG;AACH,qBAAa,gBAAgB;;IAG3B;;;OAGG;gBACS,gBAAgB,SAAU;IAItC;;;OAGG;IACH,IAAI,CAAC,QAAQ,EAAE,QAAQ;IAIvB;;;;OAIG;IACH,OAAO,CAAC,QAAQ,EAAE,QAAQ;IAc1B;;;OAGG;IACH,KAAK,CAAC,QAAQ,EAAE,QAAQ;CAGzB"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './constants.js';
|
|
3
|
+
export * from './diy.js';
|
|
4
|
+
export * from './utils.js';
|
|
5
|
+
export * from './navigation.js';
|
|
6
|
+
export * from './searchParams.js';
|
|
7
|
+
export * from './paths.js';
|
|
8
|
+
export * from './router.js';
|
|
9
|
+
export * from './di.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA"}
|