@surgijs/router 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/angular.d.ts +45 -0
- package/dist/angular.js +52 -0
- package/dist/angular.js.map +1 -0
- package/dist/chunk-FRJIJ3K7.js +104 -0
- package/dist/chunk-FRJIJ3K7.js.map +1 -0
- package/dist/chunk-LY6JNBHA.js +20 -0
- package/dist/chunk-LY6JNBHA.js.map +1 -0
- package/dist/chunk-OQUB6NVM.js +53 -0
- package/dist/chunk-OQUB6NVM.js.map +1 -0
- package/dist/chunk-W2SIXU6O.js +44 -0
- package/dist/chunk-W2SIXU6O.js.map +1 -0
- package/dist/core.d.ts +73 -0
- package/dist/core.js +4 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +1123 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +80 -0
- package/dist/react.js +120 -0
- package/dist/react.js.map +1 -0
- package/dist/router-ClmQESkC.d.ts +497 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.js +25 -0
- package/dist/server.js.map +1 -0
- package/dist/svelte.d.ts +50 -0
- package/dist/svelte.js +60 -0
- package/dist/svelte.js.map +1 -0
- package/dist/vue.d.ts +80 -0
- package/dist/vue.js +115 -0
- package/dist/vue.js.map +1 -0
- package/package.json +112 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SurgiJS
|
|
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
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @surgijs/router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A small router that does not depend on a UI library: typed path and query parameters, nested and lazy routes, guards and redirects, data loading, slash, hash and memory history, SSR with a hydration snapshot, and integration with [`@surgijs/store`](../store). Adapters for SurgiJS, React, Vue, Svelte and Angular.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm i @surgijs/router
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { createRouter, defineRoute, p } from '@surgijs/router';
|
|
11
|
+
import { Outlet, RouterProvider } from '@surgijs/router/core';
|
|
12
|
+
|
|
13
|
+
const user = defineRoute({
|
|
14
|
+
path: 'users/:id',
|
|
15
|
+
params: { id: p.int() },
|
|
16
|
+
query: { tab: p.enum(['posts', 'likes'], { default: 'posts' }) },
|
|
17
|
+
lazy: () => import('./user'),
|
|
18
|
+
load: ({ params, signal }) => fetchUser(params.id, signal),
|
|
19
|
+
preload: ['posts'], // store remotes loaded before the page shows
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const router = createRouter({
|
|
23
|
+
store,
|
|
24
|
+
routes: [{ path: '/', component: Layout, children: [{ path: '', component: Home }, user] }],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
router.navigate(user, { params: { id: 7 }, query: { tab: 'likes' } }); // /users/7?tab=likes
|
|
28
|
+
|
|
29
|
+
export const App = () => <RouterProvider router={router} />;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
| Entry | |
|
|
33
|
+
| --- | --- |
|
|
34
|
+
| `@surgijs/router` | `createRouter`, `defineRoute`, `redirect`, `p`, `slashHistory`, `hashHistory`, `memoryHistory`, `interceptLinks`, `autoScroll`, `readSnapshot` |
|
|
35
|
+
| `@surgijs/router/core` | SurgiJS: `RouterProvider`, `Outlet`, `useParams`, `useQuery`, `useLoaderData`, `fromRouter`, `hydrateApp`, … |
|
|
36
|
+
| `@surgijs/router/server` | `withRouter`, `stateScript`, `routerResponse` |
|
|
37
|
+
| `@surgijs/router/react` | `RouterProvider`, `Outlet`, `Link`, hooks |
|
|
38
|
+
| `@surgijs/router/vue` | `createRouterPlugin`, `RouterView`, `RouterLink`, composables |
|
|
39
|
+
| `@surgijs/router/svelte` | stores and the `link` action |
|
|
40
|
+
| `@surgijs/router/angular` | signals and `provideRouter` |
|
|
41
|
+
|
|
42
|
+
`@surgijs/core`, `@surgijs/store`, `react`, `vue` and `@angular/core` are optional peers: install what you use. The [guide](../../apps/docs/guide/router.md) covers routes, navigation, URL modes, typed parameters, guards, loading, SSR and every adapter.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { InjectionToken, DestroyRef, Signal, Provider } from '@angular/core';
|
|
2
|
+
import { R as Router, a as RouterState, L as LeaveGuard, A as AnyRoute, b as RouteParams, c as RouteQuery } from './router-ClmQESkC.js';
|
|
3
|
+
|
|
4
|
+
declare const ROUTER: InjectionToken<Router>;
|
|
5
|
+
/** `providers: [provideRouter(router)]` makes the router injectable. */
|
|
6
|
+
declare function provideRouter(router: Router): Provider;
|
|
7
|
+
declare function injectRouter(): Router;
|
|
8
|
+
interface RouterSignalOptions<T> {
|
|
9
|
+
/** Defaults to structural `deepEqual`. */
|
|
10
|
+
equals?: (a: T, b: T) => boolean;
|
|
11
|
+
/** Ends the subscription when it is destroyed. Defaults to `inject(DestroyRef)`, so the call runs in an injection context. */
|
|
12
|
+
destroyRef?: DestroyRef;
|
|
13
|
+
}
|
|
14
|
+
/** A router selector as a read-only Angular signal, updated until the injection context is destroyed. */
|
|
15
|
+
declare function fromRouter<T>(router: Router, selector: (state: RouterState) => T, options?: RouterSignalOptions<T>): Signal<T>;
|
|
16
|
+
declare const injectLocation: () => Signal<{
|
|
17
|
+
url: string;
|
|
18
|
+
path: string;
|
|
19
|
+
search: string;
|
|
20
|
+
hash: string;
|
|
21
|
+
}>;
|
|
22
|
+
declare const injectParams: <R extends AnyRoute = AnyRoute>(_route?: R) => Signal<RouteParams<R>>;
|
|
23
|
+
declare const injectQuery: <R extends AnyRoute = AnyRoute>(_route?: R) => Signal<RouteQuery<R>>;
|
|
24
|
+
declare const injectIsPending: () => Signal<boolean>;
|
|
25
|
+
/** What `load` returned for the route at `level` (the leaf when omitted). */
|
|
26
|
+
declare const injectLoaderData: <T = unknown>(level?: number) => Signal<T | undefined>;
|
|
27
|
+
interface OutletView {
|
|
28
|
+
/** The component to render, for `NgComponentOutlet`. */
|
|
29
|
+
component: unknown;
|
|
30
|
+
/** Inputs for it: `{ error }` for an error component. */
|
|
31
|
+
inputs: Record<string, unknown>;
|
|
32
|
+
/** The depth for the outlet inside it. */
|
|
33
|
+
next: number;
|
|
34
|
+
/** The failure this outlet shows; `component` is `null` when the route has no error component. */
|
|
35
|
+
error?: unknown;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* What the outlet at `level` renders: the component of the route at that depth, or its error component.
|
|
39
|
+
* Use it with `NgComponentOutlet`. On a failure without an error component `component` is `null` and `error` is set.
|
|
40
|
+
*/
|
|
41
|
+
declare function injectOutlet(level?: number): Signal<OutletView | null>;
|
|
42
|
+
/** Registers a leave guard until the injection context is destroyed. */
|
|
43
|
+
declare function injectBeforeLeave(guard: LeaveGuard, destroyRef?: DestroyRef): void;
|
|
44
|
+
|
|
45
|
+
export { type OutletView, ROUTER, type RouterSignalOptions, fromRouter, injectBeforeLeave, injectIsPending, injectLoaderData, injectLocation, injectOutlet, injectParams, injectQuery, injectRouter, provideRouter };
|
package/dist/angular.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { deepEqual } from './chunk-W2SIXU6O.js';
|
|
2
|
+
import { InjectionToken, inject, DestroyRef, signal } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
var ROUTER = new InjectionToken("@surgijs/router");
|
|
5
|
+
function provideRouter(router) {
|
|
6
|
+
return { provide: ROUTER, useValue: router };
|
|
7
|
+
}
|
|
8
|
+
function injectRouter() {
|
|
9
|
+
return inject(ROUTER);
|
|
10
|
+
}
|
|
11
|
+
function fromRouter(router, selector, options = {}) {
|
|
12
|
+
const destroyRef = options.destroyRef ?? inject(DestroyRef);
|
|
13
|
+
const value = signal(router.get(selector));
|
|
14
|
+
const stop = router.subscribe(selector, (next) => value.set(next), { equals: options.equals ?? deepEqual });
|
|
15
|
+
try {
|
|
16
|
+
destroyRef.onDestroy(stop);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
stop();
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
return value.asReadonly();
|
|
22
|
+
}
|
|
23
|
+
var injectLocation = () => fromRouter(injectRouter(), (s) => ({ url: s.url, path: s.path, search: s.search, hash: s.hash }));
|
|
24
|
+
var injectParams = (_route) => fromRouter(injectRouter(), (s) => s.params);
|
|
25
|
+
var injectQuery = (_route) => fromRouter(injectRouter(), (s) => s.query);
|
|
26
|
+
var injectIsPending = () => fromRouter(injectRouter(), (s) => s.isPending);
|
|
27
|
+
var injectLoaderData = (level) => fromRouter(injectRouter(), (s) => s.data[level ?? s.data.length - 1]);
|
|
28
|
+
function injectOutlet(level = 0) {
|
|
29
|
+
return fromRouter(
|
|
30
|
+
injectRouter(),
|
|
31
|
+
(s) => {
|
|
32
|
+
const rec = s.matches[level]?.route;
|
|
33
|
+
if (!rec) return null;
|
|
34
|
+
const err = s.error && s.error.at === level ? s.error : null;
|
|
35
|
+
return {
|
|
36
|
+
component: (err ? rec.errorComponent : rec.component) ?? null,
|
|
37
|
+
inputs: err ? { error: err.error } : {},
|
|
38
|
+
next: level + 1,
|
|
39
|
+
...err && { error: err.error }
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
{ equals: (a, b) => a === null || b === null ? a === b : a.component === b.component && a.next === b.next && a.inputs.error === b.inputs.error }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
function injectBeforeLeave(guard, destroyRef = inject(DestroyRef)) {
|
|
46
|
+
const stop = injectRouter().beforeLeave(guard);
|
|
47
|
+
destroyRef.onDestroy(stop);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { ROUTER, fromRouter, injectBeforeLeave, injectIsPending, injectLoaderData, injectLocation, injectOutlet, injectParams, injectQuery, injectRouter, provideRouter };
|
|
51
|
+
//# sourceMappingURL=angular.js.map
|
|
52
|
+
//# sourceMappingURL=angular.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/angular.ts"],"names":[],"mappings":";;;AAKO,IAAM,MAAA,GAAS,IAAI,cAAA,CAAuB,iBAAiB;AAG3D,SAAS,cAAc,MAAA,EAA0B;AACtD,EAAA,OAAO,EAAE,OAAA,EAAS,MAAA,EAAQ,QAAA,EAAU,MAAA,EAAO;AAC7C;AAEO,SAAS,YAAA,GAAuB;AACrC,EAAA,OAAO,OAAO,MAAM,CAAA;AACtB;AAUO,SAAS,UAAA,CAAc,MAAA,EAAgB,QAAA,EAAqC,OAAA,GAAkC,EAAC,EAAc;AAClI,EAAA,MAAM,UAAA,GAAa,OAAA,CAA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AAO,UAAA;AAAA,IACL,YAAA,EAAa;AAAA,IACb,CAAC,CAAA,KAAyB;AACxB,MAAA,MAAM,GAAA,GAAM,CAAA,CAAE,OAAA,CAAQ,KAAK,CAAA,EAAG,KAAA;AAC9B,MAAA,IAAI,CAAC,KAAK,OAAO,IAAA;AACjB,MAAA,MAAM,GAAA,GAAM,EAAE,KAAA,IAAS,CAAA,CAAE,MAAM,EAAA,KAAO,KAAA,GAAQ,EAAE,KAAA,GAAQ,IAAA;AACxD,MAAA,OAAO;AAAA,QACL,SAAA,EAAA,CAAY,GAAA,GAAM,GAAA,CAAI,cAAA,GAAiB,IAAI,SAAA,KAAc,IAAA;AAAA,QACzD,QAAQ,GAAA,GAAM,EAAE,OAAO,GAAA,CAAI,KAAA,KAAU,EAAC;AAAA,QACtC,MAAM,KAAA,GAAQ,CAAA;AAAA,QACd,GAAI,GAAA,IAAO,EAAE,KAAA,EAAO,IAAI,KAAA;AAAM,OAChC;AAAA,IACF,CAAA;AAAA,IACA,EAAE,MAAA,EAAQ,CAAC,CAAA,EAAG,CAAA,KAAO,MAAM,IAAA,IAAQ,CAAA,KAAM,IAAA,GAAO,CAAA,KAAM,CAAA,GAAI,CAAA,CAAE,cAAc,CAAA,CAAE,SAAA,IAAa,CAAA,CAAE,IAAA,KAAS,CAAA,CAAE,IAAA,IAAQ,EAAE,MAAA,CAAO,KAAA,KAAU,CAAA,CAAE,MAAA,CAAO,KAAA;AAAO,GACnJ;AACF;AAGO,SAAS,iBAAA,CAAkB,KAAA,EAAmB,UAAA,GAAyB,MAAA,CAAO,UAAU,CAAA,EAAS;AACtG,EAAA,MAAM,IAAA,GAAO,YAAA,EAAa,CAAE,WAAA,CAAY,KAAK,CAAA;AAC7C,EAAA,UAAA,CAAW,UAAU,IAAI,CAAA;AAC3B","file":"angular.js","sourcesContent":["import { DestroyRef, InjectionToken, inject, signal, type Provider, type Signal } from '@angular/core';\nimport type { LeaveGuard, Router, RouterState } from './router';\nimport type { AnyRoute, RouteParams, RouteQuery } from './routes';\nimport { deepEqual } from './util';\n\nexport const ROUTER = new InjectionToken<Router>('@surgijs/router');\n\n/** `providers: [provideRouter(router)]` makes the router injectable. */\nexport function provideRouter(router: Router): Provider {\n return { provide: ROUTER, useValue: router };\n}\n\nexport function injectRouter(): Router {\n return inject(ROUTER);\n}\n\nexport interface RouterSignalOptions<T> {\n /** Defaults to structural `deepEqual`. */\n equals?: (a: T, b: T) => boolean;\n /** Ends the subscription when it is destroyed. Defaults to `inject(DestroyRef)`, so the call runs in an injection context. */\n destroyRef?: DestroyRef;\n}\n\n/** A router selector as a read-only Angular signal, updated until the injection context is destroyed. */\nexport function fromRouter<T>(router: Router, selector: (state: RouterState) => T, options: RouterSignalOptions<T> = {}): Signal<T> {\n const destroyRef = options.destroyRef ?? inject(DestroyRef);\n const value = signal<T>(router.get(selector));\n const stop = router.subscribe(selector, (next) => value.set(next), { equals: options.equals ?? deepEqual });\n try {\n destroyRef.onDestroy(stop);\n } catch (error) {\n stop();\n throw error;\n }\n return value.asReadonly();\n}\n\nexport const injectLocation = () => fromRouter(injectRouter(), (s) => ({ url: s.url, path: s.path, search: s.search, hash: s.hash }));\nexport const injectParams = <R extends AnyRoute = AnyRoute>(_route?: R) => fromRouter(injectRouter(), (s) => s.params as RouteParams<R>);\nexport const injectQuery = <R extends AnyRoute = AnyRoute>(_route?: R) => fromRouter(injectRouter(), (s) => s.query as RouteQuery<R>);\nexport const injectIsPending = () => fromRouter(injectRouter(), (s) => s.isPending);\n\n/** What `load` returned for the route at `level` (the leaf when omitted). */\nexport const injectLoaderData = <T = unknown>(level?: number) =>\n fromRouter(injectRouter(), (s) => s.data[level ?? s.data.length - 1] as T | undefined);\n\nexport interface OutletView {\n /** The component to render, for `NgComponentOutlet`. */\n component: unknown;\n /** Inputs for it: `{ error }` for an error component. */\n inputs: Record<string, unknown>;\n /** The depth for the outlet inside it. */\n next: number;\n /** The failure this outlet shows; `component` is `null` when the route has no error component. */\n error?: unknown;\n}\n\n/**\n * What the outlet at `level` renders: the component of the route at that depth, or its error component.\n * Use it with `NgComponentOutlet`. On a failure without an error component `component` is `null` and `error` is set.\n */\nexport function injectOutlet(level = 0): Signal<OutletView | null> {\n return fromRouter(\n injectRouter(),\n (s): OutletView | null => {\n const rec = s.matches[level]?.route;\n if (!rec) return null;\n const err = s.error && s.error.at === level ? s.error : null;\n return {\n component: (err ? rec.errorComponent : rec.component) ?? null,\n inputs: err ? { error: err.error } : {},\n next: level + 1,\n ...(err && { error: err.error }),\n };\n },\n { equals: (a, b) => (a === null || b === null ? a === b : a.component === b.component && a.next === b.next && a.inputs.error === b.inputs.error) },\n );\n}\n\n/** Registers a leave guard until the injection context is destroyed. */\nexport function injectBeforeLeave(guard: LeaveGuard, destroyRef: DestroyRef = inject(DestroyRef)): void {\n const stop = injectRouter().beforeLeave(guard);\n destroyRef.onDestroy(stop);\n}\n"]}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { deepEqual } from './chunk-W2SIXU6O.js';
|
|
2
|
+
import { createContext, useContext, createSignal, getOwner, onCleanup, createMemo, createRoot, untrack } from '@surgijs/core';
|
|
3
|
+
|
|
4
|
+
var RouterContext = createContext(null);
|
|
5
|
+
var LevelContext = createContext(0);
|
|
6
|
+
function useRouter() {
|
|
7
|
+
const router = useContext(RouterContext)();
|
|
8
|
+
if (!router) throw new Error("@surgijs/router: no router; render inside <RouterProvider router={router}>");
|
|
9
|
+
return router;
|
|
10
|
+
}
|
|
11
|
+
function fromRouter(router, selector, equals = deepEqual) {
|
|
12
|
+
const [value, setValue] = createSignal(router.get(selector));
|
|
13
|
+
const stop = router.subscribe(selector, (next) => setValue(() => next), { equals });
|
|
14
|
+
if (getOwner()) onCleanup(stop);
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function RouterProvider(props) {
|
|
18
|
+
return RouterContext.Provider({ value: props.router, children: props.children ?? (() => Outlet({})) });
|
|
19
|
+
}
|
|
20
|
+
var NONE = /* @__PURE__ */ Symbol("none");
|
|
21
|
+
function Outlet(props = {}) {
|
|
22
|
+
const router = useRouter();
|
|
23
|
+
const level = useContext(LevelContext)();
|
|
24
|
+
const route = fromRouter(router, (s) => s.matches[level]?.route ?? null, Object.is);
|
|
25
|
+
const failure = fromRouter(router, (s) => s.error && s.error.at === level ? s.error : null, Object.is);
|
|
26
|
+
let dispose;
|
|
27
|
+
let current = NONE;
|
|
28
|
+
let last;
|
|
29
|
+
onCleanup(() => dispose?.());
|
|
30
|
+
return createMemo(() => {
|
|
31
|
+
const rec = route();
|
|
32
|
+
const err = failure();
|
|
33
|
+
const key = err ?? rec;
|
|
34
|
+
if (key === current) return last;
|
|
35
|
+
current = key;
|
|
36
|
+
dispose?.();
|
|
37
|
+
last = createRoot((d) => {
|
|
38
|
+
dispose = d;
|
|
39
|
+
return untrack(() => {
|
|
40
|
+
if (!rec) return typeof props.fallback === "function" ? props.fallback() : props.fallback;
|
|
41
|
+
if (err && !rec.errorComponent) throw err.error;
|
|
42
|
+
const component = err ? rec.errorComponent : rec.component;
|
|
43
|
+
return LevelContext.Provider({
|
|
44
|
+
value: level + 1,
|
|
45
|
+
children: () => typeof component === "function" ? component(err ? { error: err.error } : {}) : Outlet({})
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
return last;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function useLocation() {
|
|
53
|
+
return fromRouter(useRouter(), (s) => ({ url: s.url, path: s.path, search: s.search, hash: s.hash }));
|
|
54
|
+
}
|
|
55
|
+
function useParams(_route) {
|
|
56
|
+
return fromRouter(useRouter(), (s) => s.params);
|
|
57
|
+
}
|
|
58
|
+
function useQuery(_route) {
|
|
59
|
+
return fromRouter(useRouter(), (s) => s.query);
|
|
60
|
+
}
|
|
61
|
+
function useMatch() {
|
|
62
|
+
const level = depth();
|
|
63
|
+
return fromRouter(
|
|
64
|
+
useRouter(),
|
|
65
|
+
(s) => s.matches[level < 0 ? s.matches.length - 1 : level],
|
|
66
|
+
(a, b) => a?.route === b?.route && a?.params === b?.params
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
function useLoaderData() {
|
|
70
|
+
const level = depth();
|
|
71
|
+
return fromRouter(useRouter(), (s) => s.data[level < 0 ? s.data.length - 1 : level]);
|
|
72
|
+
}
|
|
73
|
+
function useIsPending() {
|
|
74
|
+
return fromRouter(useRouter(), (s) => s.isPending);
|
|
75
|
+
}
|
|
76
|
+
function useIsActive(to, options) {
|
|
77
|
+
const router = useRouter();
|
|
78
|
+
return fromRouter(router, () => router.isActive(to, options));
|
|
79
|
+
}
|
|
80
|
+
function useHref(to, options) {
|
|
81
|
+
const router = useRouter();
|
|
82
|
+
return fromRouter(router, () => router.href(to, options));
|
|
83
|
+
}
|
|
84
|
+
function useBeforeLeave(guard) {
|
|
85
|
+
const stop = useRouter().beforeLeave(guard);
|
|
86
|
+
if (getOwner()) onCleanup(stop);
|
|
87
|
+
}
|
|
88
|
+
function useBeforeEach(guard) {
|
|
89
|
+
const stop = useRouter().beforeEach(guard);
|
|
90
|
+
if (getOwner()) onCleanup(stop);
|
|
91
|
+
}
|
|
92
|
+
function depth() {
|
|
93
|
+
return useContext(LevelContext)() - 1;
|
|
94
|
+
}
|
|
95
|
+
async function hydrateApp(options) {
|
|
96
|
+
await options.router.ready();
|
|
97
|
+
const { enableHydration } = await import('@surgijs/core/hydrate');
|
|
98
|
+
enableHydration();
|
|
99
|
+
await options.app();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { Outlet, RouterProvider, fromRouter, hydrateApp, useBeforeEach, useBeforeLeave, useHref, useIsActive, useIsPending, useLoaderData, useLocation, useMatch, useParams, useQuery, useRouter };
|
|
103
|
+
//# sourceMappingURL=chunk-FRJIJ3K7.js.map
|
|
104
|
+
//# sourceMappingURL=chunk-FRJIJ3K7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core.ts"],"names":[],"mappings":";;;AAKA,IAAM,aAA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A,KAAM,EAAE,SAAS,CAAA;AACnD;AAGO,SAAS,WAAA,CAAY,IAAuB,OAAA,EAAoF;AACrI,EAAA,MAAM,SAAS,SAAA,EAAU;AACzB,EAAA,OAAO,WAAW,MAAA,EAAQ,MAAM,OAAO,QAAA,CAAS,EAAA,EAAI,OAAO,CAAC,CAAA;AAC9D;AAGO,SAAS,OAAA,CAAQ,IAAuB,OAAA,EAA+E;AAC5H,EAAA,MAAM,SAAS,SAAA,EAAU;AACzB,EAAA,OAAO,WAAW,MAAA,EAAQ,MAAM,OAAO,IAAA,CAAK,EAAA,EAAa,OAAgB,CAAC,CAAA;AAC5E;AAGO,SAAS,eAAe,KAAA,EAAyB;AACtD,EAAA,MAAM,IAAA,GAAO,SAAA,EAAU,CAAE,WAAA,CAAY,KAAK,CAAA;AAC1C,EAAA,IAAI,QAAA,EAAS,EAAG,SAAA,CAAU,IAAI,CAAA;AAChC;AAGO,SAAS,cAAc,KAAA,EAAoB;AAChD,EAAA,MAAM,IAAA,GAAO,SAAA,EAAU,CAAE,UAAA,CAAW,KAAK,CAAA;AACzC,EAAA,IAAI,QAAA,EAAS,EAAG,SAAA,CAAU,IAAI,CAAA;AAChC;AAEA,SAAS,KAAA,GAAgB;AACvB,EAAA,OAAO,UAAA,CAAW,YAAY,CAAA,EAAE,GAAI,CAAA;AACtC;AAYA,eAAsB,WAAW,OAAA,EAAgE;AAC/F,EAAA,MAAM,OAAA,CAAQ,OAAO,KAAA,EAAM;AAC3B,EAAA,MAAM,EAAE,eAAA,EAAgB,GAAI,MAAM,OAAO,uBAAuB,CAAA;AAChE,EAAA,eAAA,EAAgB;AAChB,EAAA,MAAM,QAAQ,GAAA,EAAI;AACpB","file":"chunk-FRJIJ3K7.js","sourcesContent":["import { createContext, createMemo, createRoot, createSignal, getOwner, onCleanup, untrack, useContext, type Accessor, type JSX } from '@surgijs/core';\nimport type { Guard, LeaveGuard, Match, Router, RouterState, UrlOptions } from './index';\nimport type { AnyRoute, RouteParams, RouteQuery } from './routes';\nimport { deepEqual } from './util';\n\nconst RouterContext = createContext<Router | null>(null);\n/** The depth of the next `Outlet`: 0 outside any routed component. */\nconst LevelContext = createContext(0);\n\n/** The router a `RouterProvider` above provides. */\nexport function useRouter(): Router {\n const router = useContext(RouterContext)();\n if (!router) throw new Error('@surgijs/router: no router; render inside <RouterProvider router={router}>');\n return router;\n}\n\n/**\n * A router selector as a `@surgijs/core` signal. The subscription ends with the current owner, for\n * example the component that called it.\n */\nexport function fromRouter<T>(router: Router, selector: (state: RouterState) => T, equals: (a: T, b: T) => boolean = deepEqual): Accessor<T> {\n const [value, setValue] = createSignal<T>(router.get(selector));\n const stop = router.subscribe(selector, (next) => setValue(() => next), { equals });\n if (getOwner()) onCleanup(stop);\n return value;\n}\n\nexport interface RouterProviderProps {\n router: Router;\n /** What to render; the routes by default. */\n children?: JSX.Element | (() => JSX.Element);\n}\n\n/** Puts `router` on context for `Outlet` and the hooks. Without children it renders the matched routes. */\nexport function RouterProvider(props: RouterProviderProps): JSX.Element {\n return RouterContext.Provider({ value: props.router, children: props.children ?? (() => Outlet({})) });\n}\n\nexport interface OutletProps {\n /** Rendered when no route matches at this depth. */\n fallback?: JSX.Element | (() => JSX.Element);\n}\n\nconst NONE = Symbol('none');\n\n/**\n * Renders the route at this depth: the component of the matched route, below the layouts of its\n * parents. A failed `load` or `lazy` renders the `errorComponent` of the nearest route that has\n * one, with an `error` prop; without any, the error is thrown to the nearest `ErrorBoundary`.\n */\nexport function Outlet(props: OutletProps = {}): JSX.Element {\n const router = useRouter();\n const level = useContext(LevelContext)();\n const route = fromRouter(router, (s) => s.matches[level]?.route ?? null, Object.is);\n const failure = fromRouter(router, (s) => (s.error && s.error.at === level ? s.error : null), Object.is);\n let dispose: (() => void) | undefined;\n let current: unknown = NONE;\n let last: unknown;\n onCleanup(() => dispose?.());\n return createMemo(() => {\n const rec = route();\n const err = failure();\n const key = err ?? rec;\n if (key === current) return last;\n current = key;\n dispose?.();\n last = createRoot((d) => {\n dispose = d;\n return untrack(() => {\n if (!rec) return typeof props.fallback === 'function' ? (props.fallback as () => JSX.Element)() : props.fallback;\n if (err && !rec.errorComponent) throw err.error;\n const component = (err ? rec.errorComponent : rec.component) as ((p: object) => JSX.Element) | undefined;\n return LevelContext.Provider({\n value: level + 1,\n children: () => (typeof component === 'function' ? component(err ? { error: err.error } : {}) : Outlet({})),\n });\n });\n });\n return last;\n }) as unknown as JSX.Element;\n}\n\n/** The current location: `url`, `path`, `search` and `hash`. */\nexport function useLocation(): Accessor<{ url: string; path: string; search: string; hash: string }> {\n return fromRouter(useRouter(), (s) => ({ url: s.url, path: s.path, search: s.search, hash: s.hash }));\n}\n\n/** The path parameters of the current location; pass the route for their types. */\nexport function useParams<R extends AnyRoute = AnyRoute>(_route?: R): Accessor<RouteParams<R>> {\n return fromRouter(useRouter(), (s) => s.params as RouteParams<R>);\n}\n\n/** The validated query of the current location; pass the route for its type. */\nexport function useQuery<R extends AnyRoute = AnyRoute>(_route?: R): Accessor<RouteQuery<R>> {\n return fromRouter(useRouter(), (s) => s.query as RouteQuery<R>);\n}\n\n/** The match at the depth of the component that calls it (the leaf outside routed components). */\nexport function useMatch(): Accessor<Match | undefined> {\n const level = depth();\n return fromRouter(\n useRouter(),\n (s) => s.matches[level < 0 ? s.matches.length - 1 : level],\n (a, b) => a?.route === b?.route && a?.params === b?.params,\n );\n}\n\n/** What `load` returned for the route of the component that calls it. */\nexport function useLoaderData<T = unknown>(): Accessor<T | undefined> {\n const level = depth();\n return fromRouter(useRouter(), (s) => s.data[level < 0 ? s.data.length - 1 : level] as T | undefined);\n}\n\n/** Whether a navigation is waiting for `lazy`, `load` or a guard. */\nexport function useIsPending(): Accessor<boolean> {\n return fromRouter(useRouter(), (s) => s.isPending);\n}\n\n/** Whether the current path is `to` or below it (`exact`: is `to`). */\nexport function useIsActive(to: string | AnyRoute, options?: { exact?: boolean; params?: Record<string, unknown> }): Accessor<boolean> {\n const router = useRouter();\n return fromRouter(router, () => router.isActive(to, options));\n}\n\n/** The `href` of a target, for an `<a>`: relative targets resolve against the location they are read at. */\nexport function useHref(to: string | AnyRoute, options?: UrlOptions & { params?: Record<string, unknown> }): Accessor<string> {\n const router = useRouter();\n return fromRouter(router, () => router.href(to as never, options as never));\n}\n\n/** Registers a leave guard for the lifetime of the component. */\nexport function useBeforeLeave(guard: LeaveGuard): void {\n const stop = useRouter().beforeLeave(guard);\n if (getOwner()) onCleanup(stop);\n}\n\n/** Registers a navigation guard for the lifetime of the component. */\nexport function useBeforeEach(guard: Guard): void {\n const stop = useRouter().beforeEach(guard);\n if (getOwner()) onCleanup(stop);\n}\n\nfunction depth(): number {\n return useContext(LevelContext)() - 1;\n}\n\n/**\n * The client entry of a server-rendered app. It waits for the first navigation, which reuses the\n * server's data, turns hydration on and loads the module that defines the components, so they adopt\n * the server markup. `app` is a dynamic import, for the components must not run before hydration is on.\n *\n * ```ts\n * const router = createRouter({ routes, store, snapshot: readSnapshot() });\n * await hydrateApp({ router, app: () => import('./app') });\n * ```\n */\nexport async function hydrateApp(options: { router: Router; app: () => unknown }): Promise<void> {\n await options.router.ready();\n const { enableHydration } = await import('@surgijs/core/hydrate');\n enableHydration();\n await options.app();\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/snapshot.ts
|
|
2
|
+
var SNAPSHOT_ID = "__SURGI_DATA__";
|
|
3
|
+
function readSnapshot(id = SNAPSHOT_ID) {
|
|
4
|
+
if (typeof document === "undefined") return void 0;
|
|
5
|
+
const text = document.getElementById(id)?.textContent;
|
|
6
|
+
if (!text) return void 0;
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(text);
|
|
9
|
+
} catch {
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function serializeSnapshot(snapshot, id = SNAPSHOT_ID) {
|
|
14
|
+
const json = JSON.stringify(snapshot).replace(/</g, "\\u003c").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
15
|
+
return `<script type="application/json" id="${id.replace(/[^\w-]/g, "")}">${json}</script>`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { readSnapshot, serializeSnapshot };
|
|
19
|
+
//# sourceMappingURL=chunk-LY6JNBHA.js.map
|
|
20
|
+
//# sourceMappingURL=chunk-LY6JNBHA.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/snapshot.ts"],"names":[],"mappings":";AAGO,IAAM,WAAA,GAAc,gBAAA;AAGpB,SAAS,YAAA,CAAa,KAAa,WAAA,EAAyC;AACjF,EAAA,IAAI,OAAO,QAAA,KAAa,WAAA,EAAa,OAAO,MAAA;AAC5C,EAAA,MAAM,IAAA,GAAO,QAAA,CAAS,cAAA,CAAe,EAAE,CAAA,EAAG,WAAA;AAC1C,EAAA,IAAI,CAAC,MAAM,OAAO,MAAA;AAClB,EAAA,IAAI;AACF,IAAA,OAAO,IAAA,CAAK,MAAM,IAAI,CAAA;AAAA,EACxB,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,MAAA;AAAA,EACT;AACF;AAMO,SAAS,iBAAA,CAAkB,QAAA,EAA0B,EAAA,GAAa,WAAA,EAAqB;AAC5F,EAAA,MAAM,IAAA,GAAO,IAAA,CAAK,SAAA,CAAU,QAAQ,EAAE,OAAA,CAAQ,IAAA,EAAM,SAAS,CAAA,CAAE,QAAQ,SAAA,EAAW,SAAS,CAAA,CAAE,OAAA,CAAQ,WAAW,SAAS,CAAA;AACzH,EAAA,OAAO,uCAAuC,EAAA,CAAG,OAAA,CAAQ,WAAW,EAAE,CAAC,KAAK,IAAI,CAAA,SAAA,CAAA;AAClF","file":"chunk-LY6JNBHA.js","sourcesContent":["import type { RouterSnapshot } from './router';\n\n/** The id of the `<script type=\"application/json\">` the server writes and `readSnapshot` reads. */\nexport const SNAPSHOT_ID = '__SURGI_DATA__';\n\n/** Reads what `serializeSnapshot` wrote into the page; `undefined` when the page has none. */\nexport function readSnapshot(id: string = SNAPSHOT_ID): RouterSnapshot | undefined {\n if (typeof document === 'undefined') return undefined;\n const text = document.getElementById(id)?.textContent;\n if (!text) return undefined;\n try {\n return JSON.parse(text) as RouterSnapshot;\n } catch {\n return undefined;\n }\n}\n\n/**\n * `<script type=\"application/json\">` markup for a snapshot, safe inside HTML: `<` is escaped, so\n * no value can close the element or open a comment.\n */\nexport function serializeSnapshot(snapshot: RouterSnapshot, id: string = SNAPSHOT_ID): string {\n const json = JSON.stringify(snapshot).replace(/</g, '\\\\u003c').replace(/\\u2028/g, '\\\\u2028').replace(/\\u2029/g, '\\\\u2029');\n return `<script type=\"application/json\" id=\"${id.replace(/[^\\w-]/g, '')}\">${json}</script>`;\n}\n"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/links.ts
|
|
2
|
+
function isPlainClick(e) {
|
|
3
|
+
return !e.defaultPrevented && e.button === 0 && !e.metaKey && !e.ctrlKey && !e.shiftKey && !e.altKey;
|
|
4
|
+
}
|
|
5
|
+
function isNativeAnchor(a) {
|
|
6
|
+
const target = a.getAttribute("target");
|
|
7
|
+
return target !== null && target !== "" && target !== "_self" || a.hasAttribute("download") || a.hasAttribute("data-native") || /(^|\s)external(\s|$)/.test(a.getAttribute("rel") ?? "");
|
|
8
|
+
}
|
|
9
|
+
function interceptLinks(router, target = document) {
|
|
10
|
+
const onClick = (e) => {
|
|
11
|
+
if (!isPlainClick(e)) return;
|
|
12
|
+
let anchor;
|
|
13
|
+
for (const node of e.composedPath()) {
|
|
14
|
+
const el = node;
|
|
15
|
+
if (el.localName === "a" && typeof el.href === "string" && el.hasAttribute("href")) {
|
|
16
|
+
anchor = el;
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (!anchor || isNativeAnchor(anchor)) return;
|
|
21
|
+
let url;
|
|
22
|
+
try {
|
|
23
|
+
url = router.history.fromHref(new URL(anchor.href, location.href));
|
|
24
|
+
} catch {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (url === null) return;
|
|
28
|
+
e.preventDefault();
|
|
29
|
+
router.navigate(url, { replace: anchor.hasAttribute("data-replace") }).catch((err) => console.error("[@surgijs/router]", err));
|
|
30
|
+
};
|
|
31
|
+
target.addEventListener("click", onClick);
|
|
32
|
+
return () => target.removeEventListener("click", onClick);
|
|
33
|
+
}
|
|
34
|
+
function autoScroll(to, _from, info) {
|
|
35
|
+
if (info.pop) return;
|
|
36
|
+
if (to.hash) {
|
|
37
|
+
let id = to.hash.slice(1);
|
|
38
|
+
try {
|
|
39
|
+
id = decodeURIComponent(id);
|
|
40
|
+
} catch {
|
|
41
|
+
}
|
|
42
|
+
const el = document.getElementById(id);
|
|
43
|
+
if (el) {
|
|
44
|
+
el.scrollIntoView();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
scrollTo(0, 0);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { autoScroll, interceptLinks, isNativeAnchor, isPlainClick };
|
|
52
|
+
//# sourceMappingURL=chunk-OQUB6NVM.js.map
|
|
53
|
+
//# sourceMappingURL=chunk-OQUB6NVM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/links.ts"],"names":[],"mappings":";AAGO,SAAS,aAAa,CAAA,EAAwB;AACnD,EAAA,OAAO,CAAC,CAAA,CAAE,gBAAA,IAAoB,CAAA,CAAE,MAAA,KAAW,KAAK,CAAC,CAAA,CAAE,OAAA,IAAW,CAAC,EAAE,OAAA,IAAW,CAAC,CAAA,CAAE,QAAA,IAAY,CAAC,CAAA,CAAE,MAAA;AAChG;AAGO,SAAS,eAAe,CAAA,EAAqB;AAClD,EAAA,MAAM,MAAA,GAAS,CAAA,CAAE,YAAA,CAAa,QAAQ,CAAA;AACtC,EAAA,OACG,MAAA,KAAW,QAAQ,MAAA,KAAW,EAAA,IAAM,WAAW,OAAA,IAChD,CAAA,CAAE,aAAa,UAAU,CAAA,IACzB,EAAE,YA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AO,mBAAA,CAAoB,OAAA,EAAS,OAAO,CAAA;AAC1D;AAMO,SAAS,UAAA,CAAW,EAAA,EAAiB,KAAA,EAA2B,IAAA,EAA8B;AACnG,EAAA,IAAI,KAAK,GAAA,EAAK;AACd,EAAA,IAAI,GAAG,IAAA,EAAM;AACX,IAAA,IAAI,EAAA,GAAK,EAAA,CAAG,IAAA,CAAK,KAAA,CAAM,CAAC,CAAA;AACxB,IAAA,IAAI;AACF,MAAA,EAAA,GAAK,mBAAmB,EAAE,CAAA;AAAA,IAC5B,CAAA,CAAA,MAAQ;AAAA,IAER;AACA,IAAA,MAAM,EAAA,GAAK,QAAA,CAAS,cAAA,CAAe,EAAE,CAAA;AACrC,IAAA,IAAI,EAAA,EAAI;AACN,MAAA,EAAA,CAAG,cAAA,EAAe;AAClB,MAAA;AAAA,IACF;AAAA,EACF;AACA,EAAA,QAAA,CAAS,GAAG,CAAC,CAAA;AACf","file":"chunk-OQUB6NVM.js","sourcesContent":["import type { Router, RouterState } from './router';\n\n/** A primary-button click with no modifier that nothing has handled. */\nexport function isPlainClick(e: MouseEvent): boolean {\n return !e.defaultPrevented && e.button === 0 && !e.metaKey && !e.ctrlKey && !e.shiftKey && !e.altKey;\n}\n\n/** An anchor the browser must handle itself: another window, a download, `rel=\"external\"` or `data-native`. */\nexport function isNativeAnchor(a: Element): boolean {\n const target = a.getAttribute('target');\n return (\n (target !== null && target !== '' && target !== '_self') ||\n a.hasAttribute('download') ||\n a.hasAttribute('data-native') ||\n /(^|\\s)external(\\s|$)/.test(a.getAttribute('rel') ?? '')\n );\n}\n\n/**\n * Turns clicks on `<a href>` into router navigations. The anchor is found in the event's composed\n * path, so links inside shadow roots work (an anchor in a closed root is not visible to the document).\n * Modified clicks, other targets, downloads, `rel=\"external\"`, `data-native` and URLs outside the\n * router are left to the browser. `data-replace` on the anchor rewrites the current entry.\n */\nexport function interceptLinks(router: Router, target: EventTarget = document): () => void {\n const onClick = (e: Event): void => {\n if (!isPlainClick(e as MouseEvent)) return;\n let anchor: HTMLAnchorElement | undefined;\n for (const node of e.composedPath()) {\n const el = node as HTMLAnchorElement;\n if (el.localName === 'a' && typeof el.href === 'string' && el.hasAttribute('href')) {\n anchor = el;\n break;\n }\n }\n if (!anchor || isNativeAnchor(anchor)) return;\n let url: string | null;\n try {\n url = router.history.fromHref(new URL(anchor.href, location.href));\n } catch {\n return;\n }\n if (url === null) return;\n e.preventDefault();\n router.navigate(url, { replace: anchor.hasAttribute('data-replace') }).catch((err) => console.error('[@surgijs/router]', err));\n };\n target.addEventListener('click', onClick);\n return () => target.removeEventListener('click', onClick);\n}\n\n/**\n * Scrolls to the element a hash names, else to the top. Pass it as the `scroll` option;\n * back and forward are left to the browser.\n */\nexport function autoScroll(to: RouterState, _from: RouterState | null, info: { pop: boolean }): void {\n if (info.pop) return;\n if (to.hash) {\n let id = to.hash.slice(1);\n try {\n id = decodeURIComponent(id);\n } catch {\n // Keep the raw id.\n }\n const el = document.getElementById(id);\n if (el) {\n el.scrollIntoView();\n return;\n }\n }\n scrollTo(0, 0);\n}\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/util.ts
|
|
2
|
+
function deepEqual(a, b) {
|
|
3
|
+
if (Object.is(a, b)) return true;
|
|
4
|
+
if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;
|
|
5
|
+
if (Array.isArray(a)) {
|
|
6
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
7
|
+
for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
const proto = Object.getPrototypeOf(a);
|
|
11
|
+
if (Array.isArray(b) || proto !== Object.prototype && proto !== null || Object.getPrototypeOf(b) !== proto) return false;
|
|
12
|
+
const x = a;
|
|
13
|
+
const y = b;
|
|
14
|
+
const keys = Object.keys(x);
|
|
15
|
+
if (keys.length !== Object.keys(y).length) return false;
|
|
16
|
+
for (const k of keys) if (!Object.hasOwn(y, k) || !deepEqual(x[k], y[k])) return false;
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
var isThenable = (v) => v != null && typeof v.then === "function";
|
|
20
|
+
function drive(gen, onWait) {
|
|
21
|
+
let waited = false;
|
|
22
|
+
const loop = (step) => {
|
|
23
|
+
while (!step.done) {
|
|
24
|
+
const value = step.value;
|
|
25
|
+
if (isThenable(value)) {
|
|
26
|
+
if (!waited) {
|
|
27
|
+
waited = true;
|
|
28
|
+
onWait?.();
|
|
29
|
+
}
|
|
30
|
+
return Promise.resolve(value).then(
|
|
31
|
+
(v) => loop(gen.next(v)),
|
|
32
|
+
(e) => loop(gen.throw(e))
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
step = gen.next(value);
|
|
36
|
+
}
|
|
37
|
+
return step.value;
|
|
38
|
+
};
|
|
39
|
+
return loop(gen.next());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { deepEqual, drive, isThenable };
|
|
43
|
+
//# sourceMappingURL=chunk-W2SIXU6O.js.map
|
|
44
|
+
//# sourceMappingURL=chunk-W2SIXU6O.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/util.ts"],"names":[],"mappings":";AAIO,SAAS,SAAA,CAAU,GAAY,CAAA,EAAqB;AACzD,EAAA,IAAI,MAAA,CAAO,EAAA,CAAG,CAAA,EAAG,CAAC,GAAG,OAAO,IAAA;AAC5B,EAAA,IAAI,OAAO,CAAA,KAAM,QAAA,IAAY,OAAO,CAAA,KAAM,YAAY,CAAC,CAAA,IAAK,CAAC,CAAA,EAAG,OAAO,KAAA;AACvE,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,CAAC,CAAA,EAAG;AACpB,IAAA,IAAI,CAAC,MAAM,OAAA,CAAQ,CAAC,KAAK,CAAA,CAAE,MAAA,KAAW,CAAA,CAAE,MAAA,EAAQ,OAAO,KAAA;AACvD,IAAA,KAAA,IAAS,IAAI,CAAA,EAAG,CA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Q;AACX,UAAA,MAAA,GAAS,IAAA;AACT,UAAA,MAAA,IAAS;AAAA,QACX;AACA,QAAA,OAAO,OAAA,CAAQ,OAAA,CAAQ,KAAK,CAAA,CAAE,IAAA;AAAA,UAC5B,CAAC,CAAA,KAAM,IAAA,CAAK,GAAA,CAAI,IAAA,CAAK,CAAC,CAAC,CAAA;AAAA,UACvB,CAAC,CAAA,KAAM,IAAA,CAAK,GAAA,CAAI,KAAA,CAAM,CAAC,CAAC;AAAA,SAC1B;AAAA,MACF;AACA,MAAA,IAAA,GAAO,GAAA,CAAI,KAAK,KAAK,CAAA;AAAA,IACvB;AACA,IAAA,OAAO,IAAA,CAAK,KAAA;AAAA,EACd,CAAA;AACA,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,IAAA,EAAM,CAAA;AACxB","file":"chunk-W2SIXU6O.js","sourcesContent":["/**\n * Structural equality for router state. Instances of classes (route records, `Date`, errors) compare by\n * identity, which also keeps the walk out of the route graph, whose records point at each other.\n */\nexport function deepEqual(a: unknown, b: unknown): boolean {\n if (Object.is(a, b)) return true;\n if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) return false;\n if (Array.isArray(a)) {\n if (!Array.isArray(b) || a.length !== b.length) return false;\n for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;\n return true;\n }\n const proto = Object.getPrototypeOf(a);\n if (Array.isArray(b) || (proto !== Object.prototype && proto !== null) || Object.getPrototypeOf(b) !== proto) return false;\n const x = a as Record<string, unknown>;\n const y = b as Record<string, unknown>;\n const keys = Object.keys(x);\n if (keys.length !== Object.keys(y).length) return false;\n for (const k of keys) if (!Object.hasOwn(y, k) || !deepEqual(x[k], y[k])) return false;\n return true;\n}\n\nexport const isThenable = (v: unknown): v is PromiseLike<unknown> =>\n v != null && typeof (v as { then?: unknown }).then === 'function';\n\n/**\n * Runs a generator that yields values to wait for. A yielded value that is not a promise is\n * passed straight back, so a pipeline whose steps are all synchronous finishes synchronously.\n * `onWait` runs once, before the first wait.\n */\nexport function drive<T>(gen: Generator<unknown, T, any>, onWait?: () => void): T | Promise<T> {\n let waited = false;\n const loop = (step: IteratorResult<unknown, T>): T | Promise<T> => {\n while (!step.done) {\n const value = step.value;\n if (isThenable(value)) {\n if (!waited) {\n waited = true;\n onWait?.();\n }\n return Promise.resolve(value).then(\n (v) => loop(gen.next(v)),\n (e) => loop(gen.throw(e)),\n );\n }\n step = gen.next(value);\n }\n return step.value;\n };\n return loop(gen.next());\n}\n"]}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { JSX, Accessor } from '@surgijs/core';
|
|
2
|
+
import { R as Router, a as RouterState, G as Guard, L as LeaveGuard, A as AnyRoute, U as UrlOptions, M as Match, b as RouteParams, c as RouteQuery } from './router-ClmQESkC.js';
|
|
3
|
+
|
|
4
|
+
/** The router a `RouterProvider` above provides. */
|
|
5
|
+
declare function useRouter(): Router;
|
|
6
|
+
/**
|
|
7
|
+
* A router selector as a `@surgijs/core` signal. The subscription ends with the current owner, for
|
|
8
|
+
* example the component that called it.
|
|
9
|
+
*/
|
|
10
|
+
declare function fromRouter<T>(router: Router, selector: (state: RouterState) => T, equals?: (a: T, b: T) => boolean): Accessor<T>;
|
|
11
|
+
interface RouterProviderProps {
|
|
12
|
+
router: Router;
|
|
13
|
+
/** What to render; the routes by default. */
|
|
14
|
+
children?: JSX.Element | (() => JSX.Element);
|
|
15
|
+
}
|
|
16
|
+
/** Puts `router` on context for `Outlet` and the hooks. Without children it renders the matched routes. */
|
|
17
|
+
declare function RouterProvider(props: RouterProviderProps): JSX.Element;
|
|
18
|
+
interface OutletProps {
|
|
19
|
+
/** Rendered when no route matches at this depth. */
|
|
20
|
+
fallback?: JSX.Element | (() => JSX.Element);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Renders the route at this depth: the component of the matched route, below the layouts of its
|
|
24
|
+
* parents. A failed `load` or `lazy` renders the `errorComponent` of the nearest route that has
|
|
25
|
+
* one, with an `error` prop; without any, the error is thrown to the nearest `ErrorBoundary`.
|
|
26
|
+
*/
|
|
27
|
+
declare function Outlet(props?: OutletProps): JSX.Element;
|
|
28
|
+
/** The current location: `url`, `path`, `search` and `hash`. */
|
|
29
|
+
declare function useLocation(): Accessor<{
|
|
30
|
+
url: string;
|
|
31
|
+
path: string;
|
|
32
|
+
search: string;
|
|
33
|
+
hash: string;
|
|
34
|
+
}>;
|
|
35
|
+
/** The path parameters of the current location; pass the route for their types. */
|
|
36
|
+
declare function useParams<R extends AnyRoute = AnyRoute>(_route?: R): Accessor<RouteParams<R>>;
|
|
37
|
+
/** The validated query of the current location; pass the route for its type. */
|
|
38
|
+
declare function useQuery<R extends AnyRoute = AnyRoute>(_route?: R): Accessor<RouteQuery<R>>;
|
|
39
|
+
/** The match at the depth of the component that calls it (the leaf outside routed components). */
|
|
40
|
+
declare function useMatch(): Accessor<Match | undefined>;
|
|
41
|
+
/** What `load` returned for the route of the component that calls it. */
|
|
42
|
+
declare function useLoaderData<T = unknown>(): Accessor<T | undefined>;
|
|
43
|
+
/** Whether a navigation is waiting for `lazy`, `load` or a guard. */
|
|
44
|
+
declare function useIsPending(): Accessor<boolean>;
|
|
45
|
+
/** Whether the current path is `to` or below it (`exact`: is `to`). */
|
|
46
|
+
declare function useIsActive(to: string | AnyRoute, options?: {
|
|
47
|
+
exact?: boolean;
|
|
48
|
+
params?: Record<string, unknown>;
|
|
49
|
+
}): Accessor<boolean>;
|
|
50
|
+
/** The `href` of a target, for an `<a>`: relative targets resolve against the location they are read at. */
|
|
51
|
+
declare function useHref(to: string | AnyRoute, options?: UrlOptions & {
|
|
52
|
+
params?: Record<string, unknown>;
|
|
53
|
+
}): Accessor<string>;
|
|
54
|
+
/** Registers a leave guard for the lifetime of the component. */
|
|
55
|
+
declare function useBeforeLeave(guard: LeaveGuard): void;
|
|
56
|
+
/** Registers a navigation guard for the lifetime of the component. */
|
|
57
|
+
declare function useBeforeEach(guard: Guard): void;
|
|
58
|
+
/**
|
|
59
|
+
* The client entry of a server-rendered app. It waits for the first navigation, which reuses the
|
|
60
|
+
* server's data, turns hydration on and loads the module that defines the components, so they adopt
|
|
61
|
+
* the server markup. `app` is a dynamic import, for the components must not run before hydration is on.
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const router = createRouter({ routes, store, snapshot: readSnapshot() });
|
|
65
|
+
* await hydrateApp({ router, app: () => import('./app') });
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
declare function hydrateApp(options: {
|
|
69
|
+
router: Router;
|
|
70
|
+
app: () => unknown;
|
|
71
|
+
}): Promise<void>;
|
|
72
|
+
|
|
73
|
+
export { Outlet, type OutletProps, RouterProvider, type RouterProviderProps, fromRouter, hydrateApp, useBeforeEach, useBeforeLeave, useHref, useIsActive, useIsPending, useLoaderData, useLocation, useMatch, useParams, useQuery, useRouter };
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Outlet, RouterProvider, fromRouter, hydrateApp, useBeforeEach, useBeforeLeave, useHref, useIsActive, useIsPending, useLoaderData, useLocation, useMatch, useParams, useQuery, useRouter } from './chunk-FRJIJ3K7.js';
|
|
2
|
+
import './chunk-W2SIXU6O.js';
|
|
3
|
+
//# sourceMappingURL=core.js.map
|
|
4
|
+
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"core.js"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { a as RouterState, R as Router, d as RouterSnapshot } from './router-ClmQESkC.js';
|
|
2
|
+
export { A as AnyRoute, e as ArrayOptions, F as Field, G as Guard, f as GuardResult, H as HashCodec, g as HashHistoryOptions, h as HistoryDriver, i as HistoryLocation, j as LeaveContext, L as LeaveGuard, M as Match, N as NavFn, k as NavigateOptions, l as NavigationResult, m as NumberOptions, P as Params, n as Parsed, o as PathParams, p as Raw, q as Redirect, r as Route, s as RouteArgs, t as RouteContext, u as RouteDef, v as RouteError, w as RouteLocation, b as RouteParams, c as RouteQuery, x as RouteRecord, y as RouteSlice, z as RouterOptions, S as SchemaSpec, B as SlashHistoryOptions, C as SpecOut, D as StandardSchemaV1, E as StoreLike, I as StringOptions, J as SubscribeOptions, U as UrlOptions, K as createRouter, O as defineRoute, Q as defineRoutes, T as hashHistory, V as matchPath, W as memoryHistory, X as p, Y as parseQuery, Z as redirect, _ as slashHistory, $ as stringifyQuery } from './router-ClmQESkC.js';
|
|
3
|
+
|
|
4
|
+
/** A primary-button click with no modifier that nothing has handled. */
|
|
5
|
+
declare function isPlainClick(e: MouseEvent): boolean;
|
|
6
|
+
/** An anchor the browser must handle itself: another window, a download, `rel="external"` or `data-native`. */
|
|
7
|
+
declare function isNativeAnchor(a: Element): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Turns clicks on `<a href>` into router navigations. The anchor is found in the event's composed
|
|
10
|
+
* path, so links inside shadow roots work (an anchor in a closed root is not visible to the document).
|
|
11
|
+
* Modified clicks, other targets, downloads, `rel="external"`, `data-native` and URLs outside the
|
|
12
|
+
* router are left to the browser. `data-replace` on the anchor rewrites the current entry.
|
|
13
|
+
*/
|
|
14
|
+
declare function interceptLinks(router: Router, target?: EventTarget): () => void;
|
|
15
|
+
/**
|
|
16
|
+
* Scrolls to the element a hash names, else to the top. Pass it as the `scroll` option;
|
|
17
|
+
* back and forward are left to the browser.
|
|
18
|
+
*/
|
|
19
|
+
declare function autoScroll(to: RouterState, _from: RouterState | null, info: {
|
|
20
|
+
pop: boolean;
|
|
21
|
+
}): void;
|
|
22
|
+
|
|
23
|
+
/** Reads what `serializeSnapshot` wrote into the page; `undefined` when the page has none. */
|
|
24
|
+
declare function readSnapshot(id?: string): RouterSnapshot | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* `<script type="application/json">` markup for a snapshot, safe inside HTML: `<` is escaped, so
|
|
27
|
+
* no value can close the element or open a comment.
|
|
28
|
+
*/
|
|
29
|
+
declare function serializeSnapshot(snapshot: RouterSnapshot, id?: string): string;
|
|
30
|
+
|
|
31
|
+
export { Router, RouterSnapshot, RouterState, autoScroll, interceptLinks, isNativeAnchor, isPlainClick, readSnapshot, serializeSnapshot };
|