@sigx/router 0.1.6

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andreas Ekdahl
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/dist/Link.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Link component - declarative navigation
3
+ *
4
+ * @example
5
+ * ```tsx
6
+ * <Link to="/about">About</Link>
7
+ *
8
+ * <Link to={{ path: '/blog', query: { page: '2' } }}>Blog</Link>
9
+ *
10
+ * <Link to="/contact" replace>Contact</Link>
11
+ * ```
12
+ */
13
+ import { type DefineProp, type DefineEvent, type DefineSlot } from 'sigx';
14
+ import type { RouteLocationRaw } from './types.js';
15
+ type LinkProps = DefineProp<'to', RouteLocationRaw, true> & DefineProp<'replace', boolean> & DefineProp<'activeClass', string> & DefineProp<'exactActiveClass', string> & DefineProp<'ariaCurrentValue', 'page' | 'step' | 'location' | 'date' | 'time' | 'true' | 'false'> & DefineEvent<'click', MouseEvent> & DefineSlot<'default'>;
16
+ export declare const Link: import("@sigx/runtime-core").ComponentFactory<LinkProps, void, {
17
+ default: () => import("@sigx/runtime-core").JSXElement | import("@sigx/runtime-core").JSXElement[] | null;
18
+ }>;
19
+ /**
20
+ * Alias for Link component
21
+ */
22
+ export declare const RouterLink: import("@sigx/runtime-core").ComponentFactory<LinkProps, void, {
23
+ default: () => import("@sigx/runtime-core").JSXElement | import("@sigx/runtime-core").JSXElement[] | null;
24
+ }>;
25
+ export {};
26
+ //# sourceMappingURL=Link.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Link.d.ts","sourceRoot":"","sources":["../src/Link.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAa,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,MAAM,CAAC;AAErF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,KAAK,SAAS,GACR,UAAU,CAAC,IAAI,EAAE,gBAAgB,EAAE,IAAI,CAAC,GACxC,UAAU,CAAC,SAAS,EAAE,OAAO,CAAC,GAC9B,UAAU,CAAC,aAAa,EAAE,MAAM,CAAC,GACjC,UAAU,CAAC,kBAAkB,EAAE,MAAM,CAAC,GACtC,UAAU,CAAC,kBAAkB,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GACjG,WAAW,CAAC,OAAO,EAAE,UAAU,CAAC,GAChC,UAAU,CAAC,SAAS,CAAC,CAAC;AAE5B,eAAO,MAAM,IAAI;;EAmEG,CAAC;AAErB;;GAEG;AACH,eAAO,MAAM,UAAU;;EAAO,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * RouterView component - renders the matched route component
3
+ *
4
+ * @example
5
+ * ```tsx
6
+ * <RouterView />
7
+ *
8
+ * // With named views (for nested routes)
9
+ * <RouterView name="sidebar" />
10
+ *
11
+ * // With page props (from SSG getStaticProps)
12
+ * <RouterView pageProps={{ posts: [...] }} />
13
+ * ```
14
+ */
15
+ import { type DefineProp, type ComponentFactory } from 'sigx';
16
+ type RouterViewProps = DefineProp<'name', string> & DefineProp<'pageProps', Record<string, unknown>>;
17
+ export declare const RouterView: ComponentFactory<RouterViewProps, void, {}>;
18
+ export {};
19
+ //# sourceMappingURL=RouterView.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RouterView.d.ts","sourceRoot":"","sources":["../src/RouterView.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAa,KAAK,UAAU,EAAE,KAAK,gBAAgB,EAAmC,MAAM,MAAM,CAAC;AAG1G,KAAK,eAAe,GAAG,UAAU,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAerG,eAAO,MAAM,UAAU,6CAuDG,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * History implementations for @sigx/router
3
+ */
4
+ export { createWebHistory, type BrowserHistoryOptions } from './web.js';
5
+ export { createMemoryHistory, type MemoryHistoryOptions } from './memory.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/history/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,gBAAgB,EAAE,KAAK,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,KAAK,oBAAoB,EAAE,MAAM,aAAa,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Memory history implementation for SSR
3
+ *
4
+ * This history doesn't interact with the browser History API,
5
+ * making it suitable for server-side rendering.
6
+ */
7
+ import type { RouterHistory } from '../types.js';
8
+ export interface MemoryHistoryOptions {
9
+ /** Base path for the application */
10
+ base?: string;
11
+ /** Initial location */
12
+ initialLocation?: string;
13
+ }
14
+ /**
15
+ * Create a memory history instance for SSR
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * // Server-side
20
+ * const history = createMemoryHistory({ initialLocation: req.url });
21
+ * const router = createRouter({ routes, history });
22
+ * ```
23
+ */
24
+ export declare function createMemoryHistory(options?: MemoryHistoryOptions): RouterHistory;
25
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/history/memory.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAgB,MAAM,aAAa,CAAC;AAE/D,MAAM,WAAW,oBAAoB;IACjC,oCAAoC;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,uBAAuB;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAOD;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,oBAAyB,GAAG,aAAa,CA+FrF"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Browser history implementation using the History API
3
+ *
4
+ * This is the default history for client-side navigation.
5
+ */
6
+ import type { RouterHistory } from '../types.js';
7
+ export interface BrowserHistoryOptions {
8
+ /** Base path for the application */
9
+ base?: string;
10
+ }
11
+ /**
12
+ * Create a browser history instance that uses the HTML5 History API
13
+ */
14
+ export declare function createWebHistory(options?: BrowserHistoryOptions): RouterHistory;
15
+ //# sourceMappingURL=web.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/history/web.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAgB,MAAM,aAAa,CAAC;AAE/D,MAAM,WAAW,qBAAqB;IAClC,oCAAoC;IACpC,IAAI,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,qBAA0B,GAAG,aAAa,CAwGnF"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Router hooks for @sigx/router
3
+ *
4
+ * These hooks provide access to router state and navigation within components.
5
+ */
6
+ import type { RouteLocation, RouteParams, RouteQuery } from './types.js';
7
+ import { useRouter, useRoute } from './router.js';
8
+ export { useRouter, useRoute };
9
+ /**
10
+ * Get route parameters (reactive)
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * const params = useParams();
15
+ * console.log(params.id);
16
+ * ```
17
+ */
18
+ export declare function useParams(): RouteParams;
19
+ /**
20
+ * Get query parameters (reactive)
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * const query = useQuery();
25
+ * console.log(query.search);
26
+ * ```
27
+ */
28
+ export declare function useQuery(): RouteQuery;
29
+ /**
30
+ * Get a navigate function for programmatic navigation
31
+ *
32
+ * @example
33
+ * ```tsx
34
+ * const navigate = useNavigate();
35
+ * navigate('/about');
36
+ * navigate({ path: '/blog', query: { page: '2' } });
37
+ * ```
38
+ */
39
+ export declare function useNavigate(): (to: import("./types.js").RouteLocationRaw) => Promise<RouteLocation | void>;
40
+ /**
41
+ * Guard type for route enter
42
+ */
43
+ type RouteEnterGuard = (to: RouteLocation, from: RouteLocation | null, next: (to?: string | false | RouteLocation) => void) => void;
44
+ /**
45
+ * Guard type for route leave
46
+ */
47
+ type RouteLeaveGuard = (to: RouteLocation, from: RouteLocation, next: (to?: string | false | RouteLocation) => void) => void;
48
+ /**
49
+ * Register a navigation guard that runs before leaving the current route
50
+ *
51
+ * @example
52
+ * ```tsx
53
+ * onBeforeRouteLeave((to, from, next) => {
54
+ * if (hasUnsavedChanges) {
55
+ * const answer = confirm('You have unsaved changes. Leave anyway?');
56
+ * if (!answer) {
57
+ * next(false);
58
+ * return;
59
+ * }
60
+ * }
61
+ * next();
62
+ * });
63
+ * ```
64
+ */
65
+ export declare function onBeforeRouteLeave(guard: RouteLeaveGuard): void;
66
+ /**
67
+ * Register a navigation guard that runs when entering the current route
68
+ *
69
+ * @example
70
+ * ```tsx
71
+ * onBeforeRouteUpdate((to, from, next) => {
72
+ * // Called when route params change but component is reused
73
+ * next();
74
+ * });
75
+ * ```
76
+ */
77
+ export declare function onBeforeRouteUpdate(guard: RouteEnterGuard): void;
78
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAU,aAAa,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAGlD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;AAE/B;;;;;;;;GAQG;AACH,wBAAgB,SAAS,IAAI,WAAW,CAGvC;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,IAAI,UAAU,CAGrC;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,iFAG1B;AAED;;GAEG;AACH,KAAK,eAAe,GAAG,CACnB,EAAE,EAAE,aAAa,EACjB,IAAI,EAAE,aAAa,GAAG,IAAI,EAC1B,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,aAAa,KAAK,IAAI,KAClD,IAAI,CAAC;AAEV;;GAEG;AACH,KAAK,eAAe,GAAG,CACnB,EAAE,EAAE,aAAa,EACjB,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,aAAa,KAAK,IAAI,KAClD,IAAI,CAAC;AAEV;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI,CAoC/D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI,CAkChE"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @sigx/router
3
+ *
4
+ * A robust router for SignalX with SSR support, hooks, and plugin architecture.
5
+ *
6
+ * ## Features
7
+ * - 🔌 **Pluggable History** - Use browser history for SPA, memory history for SSR
8
+ * - 🪝 **Hooks** - useRouter, useRoute, useParams, useNavigate
9
+ * - 🛡️ **Navigation Guards** - beforeEach, beforeResolve, afterEach
10
+ * - 🎯 **Route Matching** - Path patterns with params (e.g., `/blog/:slug`)
11
+ * - 🎭 **Pattern Matching** - Declarative `router.match()` for type-safe routing
12
+ * - 📦 **Components** - RouterView, Link for declarative routing
13
+ *
14
+ * ## Quick Start
15
+ *
16
+ * ```tsx
17
+ * import { defineApp } from '@sigx/runtime-core';
18
+ * import { createRouter, createWebHistory, RouterView, Link } from '@sigx/router';
19
+ *
20
+ * const router = createRouter({
21
+ * history: createWebHistory(),
22
+ * routes: [
23
+ * { path: '/', name: 'home', component: Home },
24
+ * { path: '/about', name: 'about', component: About },
25
+ * { path: '/blog/:slug', name: 'blog-post', component: BlogPost }
26
+ * ]
27
+ * });
28
+ *
29
+ * const App = component(() => {
30
+ * return () => (
31
+ * <div>
32
+ * <nav>
33
+ * <Link to="/">Home</Link>
34
+ * <Link to="/about">About</Link>
35
+ * </nav>
36
+ * <RouterView />
37
+ * </div>
38
+ * );
39
+ * });
40
+ *
41
+ * const app = defineApp(<App />);
42
+ * app.use(router);
43
+ * app.mount('#app');
44
+ * ```
45
+ *
46
+ * ## Pattern Matching
47
+ *
48
+ * Use `router.match()` for declarative, type-safe route rendering:
49
+ *
50
+ * ```tsx
51
+ * const App = component(() => {
52
+ * const router = useRouter();
53
+ *
54
+ * return () => router.match({
55
+ * home: () => <HomePage />,
56
+ * about: () => <AboutPage />,
57
+ * 'blog-post': ({ slug }) => <BlogPost slug={slug} />,
58
+ * _: () => <NotFound /> // Fallback for unmatched routes
59
+ * });
60
+ * });
61
+ * ```
62
+ *
63
+ * ## SSR Usage
64
+ *
65
+ * ```tsx
66
+ * import { createRouter, createMemoryHistory } from '@sigx/router';
67
+ *
68
+ * // Server-side
69
+ * const router = createRouter({
70
+ * history: createMemoryHistory({ initialLocation: req.url }),
71
+ * routes
72
+ * });
73
+ *
74
+ * const html = await renderToString(<App />, { router });
75
+ * ```
76
+ *
77
+ * @module
78
+ */
79
+ export type { Router, RouterOptions, RouterHistory, RouteLocation, RouteLocationRaw, RouteRecordRaw, RouteParams, RouteQuery, RouteMeta, MatchedRouteRecord, NavigationGuard, NavigationHookAfter, HistoryState, RouteMatchPatterns } from './types.js';
80
+ export { createRouter, useRouter, useRoute } from './router.js';
81
+ export { createWebHistory, type BrowserHistoryOptions } from './history/web.js';
82
+ export { createMemoryHistory, type MemoryHistoryOptions } from './history/memory.js';
83
+ export { useParams, useQuery, useNavigate, onBeforeRouteLeave, onBeforeRouteUpdate } from './hooks.js';
84
+ export { RouterView } from './RouterView.js';
85
+ export { Link, RouterLink } from './Link.js';
86
+ export { createRouterPlugin, type RouterPluginOptions } from './plugin.js';
87
+ export { parseQuery, stringifyQuery, parseURL, buildPath } from './matcher.js';
88
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6EG;AAGH,YAAY,EACR,MAAM,EACN,aAAa,EACb,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,WAAW,EACX,UAAU,EACV,SAAS,EACT,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,YAAY,EACZ,kBAAkB,EACrB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAGhE,OAAO,EAAE,gBAAgB,EAAE,KAAK,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAE,mBAAmB,EAAE,KAAK,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAGrF,OAAO,EACH,SAAS,EACT,QAAQ,EACR,WAAW,EACX,kBAAkB,EAClB,mBAAmB,EACtB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAG7C,OAAO,EAAE,kBAAkB,EAAE,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAG3E,OAAO,EACH,UAAU,EACV,cAAc,EACd,QAAQ,EACR,SAAS,EACZ,MAAM,cAAc,CAAC"}