@real-router/solid 0.6.0 → 0.8.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/README.md +49 -4
- package/dist/cjs/index.d.ts +178 -2
- package/dist/cjs/index.js +407 -22
- package/dist/esm/index.d.mts +178 -2
- package/dist/esm/index.mjs +406 -23
- package/dist/types/RouterProvider.d.ts +1 -0
- package/dist/types/RouterProvider.d.ts.map +1 -1
- package/dist/types/components/RouteView/RouteView.d.ts +3 -2
- package/dist/types/components/RouteView/RouteView.d.ts.map +1 -1
- package/dist/types/components/RouteView/components.d.ts +12 -2
- package/dist/types/components/RouteView/components.d.ts.map +1 -1
- package/dist/types/components/RouteView/helpers.d.ts.map +1 -1
- package/dist/types/components/RouteView/index.d.ts +1 -1
- package/dist/types/components/RouteView/index.d.ts.map +1 -1
- package/dist/types/components/RouteView/types.d.ts +6 -0
- package/dist/types/components/RouteView/types.d.ts.map +1 -1
- package/dist/types/dom-utils/direction-tracker.d.ts +27 -0
- package/dist/types/dom-utils/direction-tracker.d.ts.map +1 -0
- package/dist/types/dom-utils/index.d.ts +4 -0
- package/dist/types/dom-utils/index.d.ts.map +1 -1
- package/dist/types/dom-utils/view-transitions.d.ts +6 -0
- package/dist/types/dom-utils/view-transitions.d.ts.map +1 -0
- package/dist/types/hooks/useRouteEnter.d.ts +76 -0
- package/dist/types/hooks/useRouteEnter.d.ts.map +1 -0
- package/dist/types/hooks/useRouteExit.d.ts +90 -0
- package/dist/types/hooks/useRouteExit.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/RouterProvider.tsx +18 -1
- package/src/components/RouteView/RouteView.tsx +7 -2
- package/src/components/RouteView/components.tsx +25 -2
- package/src/components/RouteView/helpers.tsx +67 -21
- package/src/components/RouteView/index.ts +1 -0
- package/src/components/RouteView/types.ts +7 -0
- package/src/hooks/useRouteEnter.tsx +129 -0
- package/src/hooks/useRouteExit.tsx +123 -0
- package/src/index.tsx +17 -0
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
export { createDirectionTracker } from "./direction-tracker.js";
|
|
1
2
|
export { createRouteAnnouncer } from "./route-announcer.js";
|
|
2
3
|
export { createScrollRestoration } from "./scroll-restore.js";
|
|
4
|
+
export { createViewTransitions } from "./view-transitions.js";
|
|
3
5
|
export { shouldNavigate, buildHref, buildActiveClassName, shallowEqual, applyLinkA11y, } from "./link-utils.js";
|
|
4
6
|
export type { RouteAnnouncerOptions } from "./route-announcer.js";
|
|
5
7
|
export type { ScrollRestorationOptions, ScrollRestorationMode, } from "./scroll-restore.js";
|
|
8
|
+
export type { DirectionTracker } from "./direction-tracker.js";
|
|
9
|
+
export type { ViewTransitions } from "./view-transitions.js";
|
|
6
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/dom-utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,OAAO,EACL,cAAc,EACd,SAAS,EACT,oBAAoB,EACpB,YAAY,EACZ,aAAa,GACd,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAElE,YAAY,EACV,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/dom-utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAEhE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAE9D,OAAO,EACL,cAAc,EACd,SAAS,EACT,oBAAoB,EACpB,YAAY,EACZ,aAAa,GACd,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAElE,YAAY,EACV,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE/D,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"view-transitions.d.ts","sourceRoot":"","sources":["../../../src/dom-utils/view-transitions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAQD,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,CAiIrE"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { State } from "@real-router/core";
|
|
2
|
+
export interface RouteEnterContext {
|
|
3
|
+
/** The route that was just activated. */
|
|
4
|
+
route: State;
|
|
5
|
+
/** The route that was active immediately before this navigation. */
|
|
6
|
+
previousRoute: State;
|
|
7
|
+
}
|
|
8
|
+
export type RouteEnterHandler = (context: RouteEnterContext) => void;
|
|
9
|
+
export interface UseRouteEnterOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Skip the handler when `route.name === previousRoute.name`
|
|
12
|
+
* (sort/filter/query-only navigations on the same route). Default:
|
|
13
|
+
* `true`. Symmetric with `useRouteExit`'s same-name option.
|
|
14
|
+
*/
|
|
15
|
+
skipSameRoute?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Fire `handler` once when the component mounts as a result of a
|
|
19
|
+
* navigation. Mirror of `useRouteExit` for the entry side.
|
|
20
|
+
*
|
|
21
|
+
* What this hook covers that an ad-hoc `createEffect` + `useRoute()`
|
|
22
|
+
* doesn't:
|
|
23
|
+
*
|
|
24
|
+
* - **Skip-initial**: handler is skipped when there is no
|
|
25
|
+
* `transition.from` (i.e. first-load mount). Most consumers want to
|
|
26
|
+
* fire side effects only on real navigations, not on hydration.
|
|
27
|
+
* - **Same-route skip** (default): handler is skipped when
|
|
28
|
+
* `route.transition.from === route.name`. Sort/filter/query-only
|
|
29
|
+
* navigations re-trigger the effect (because the `route` reference
|
|
30
|
+
* changes), but they are not "entries" in the animation / analytics
|
|
31
|
+
* sense — the component instance has stayed mounted throughout.
|
|
32
|
+
* Opt out with `skipSameRoute: false`.
|
|
33
|
+
* - **Mount-time `route` / `previousRoute` snapshot**: the handler
|
|
34
|
+
* receives the values that were live at the moment of effect
|
|
35
|
+
* activation, not the latest ones (which may have moved on if the
|
|
36
|
+
* user navigated again before the effect drained).
|
|
37
|
+
*
|
|
38
|
+
* **Handler reactivity (Solid)**: Solid components run **once** at mount;
|
|
39
|
+
* `handler` is captured in closure when the hook is called. If you need
|
|
40
|
+
* different behavior across renders, derive it from a signal inside the
|
|
41
|
+
* handler body — do not rely on swapping the handler reference.
|
|
42
|
+
*
|
|
43
|
+
* @example Direction-aware entry animation
|
|
44
|
+
* ```tsx
|
|
45
|
+
* useRouteEnter(({ route }) => {
|
|
46
|
+
* const direction = route.context.browser?.direction;
|
|
47
|
+
* ref?.classList.add(
|
|
48
|
+
* direction === "back" ? "slide-from-left" : "slide-from-right",
|
|
49
|
+
* );
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Analytics page-enter event (skip-initial built-in)
|
|
54
|
+
* ```tsx
|
|
55
|
+
* useRouteEnter(({ route, previousRoute }) => {
|
|
56
|
+
* analytics.track("page_enter", {
|
|
57
|
+
* route: route.name,
|
|
58
|
+
* from: previousRoute.name,
|
|
59
|
+
* });
|
|
60
|
+
* });
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @example Reading rich transition metadata via `route.transition`
|
|
64
|
+
* ```tsx
|
|
65
|
+
* useRouteEnter(({ route }) => {
|
|
66
|
+
* if (route.transition.redirected) {
|
|
67
|
+
* showToast(`Redirected from ${route.transition.from}`);
|
|
68
|
+
* }
|
|
69
|
+
* if (route.transition.segments.activated.includes("products")) {
|
|
70
|
+
* // products subtree just became active
|
|
71
|
+
* }
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function useRouteEnter(handler: RouteEnterHandler, options?: UseRouteEnterOptions): void;
|
|
76
|
+
//# sourceMappingURL=useRouteEnter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRouteEnter.d.ts","sourceRoot":"","sources":["../../../src/hooks/useRouteEnter.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE/C,MAAM,WAAW,iBAAiB;IAChC,yCAAyC;IACzC,KAAK,EAAE,KAAK,CAAC;IACb,oEAAoE;IACpE,aAAa,EAAE,KAAK,CAAC;CACtB;AAED,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,CAAC;AAErE,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,iBAAiB,EAC1B,OAAO,CAAC,EAAE,oBAAoB,GAC7B,IAAI,CA2CN"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { State } from "@real-router/core";
|
|
2
|
+
export interface RouteExitContext {
|
|
3
|
+
/** The route being left. */
|
|
4
|
+
route: State;
|
|
5
|
+
/** The route being navigated to. */
|
|
6
|
+
nextRoute: State;
|
|
7
|
+
/**
|
|
8
|
+
* AbortSignal that fires when this navigation is superseded by a later
|
|
9
|
+
* one (rapid clicks). Already filtered: when the handler runs,
|
|
10
|
+
* `signal.aborted` is guaranteed to be `false`. Use
|
|
11
|
+
* `signal.addEventListener("abort", cleanup, { once: true })` for
|
|
12
|
+
* cleanup that must run on cancellation.
|
|
13
|
+
*/
|
|
14
|
+
signal: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
export interface UseRouteExitOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Skip the handler when `route.name === nextRoute.name`
|
|
19
|
+
* (sort/filter/query-only navigations on the same route). Default:
|
|
20
|
+
* `true`.
|
|
21
|
+
*/
|
|
22
|
+
skipSameRoute?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export type RouteExitHandler = (context: RouteExitContext) => void | Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Subscribe to the router's leave-window with the universal guards baked
|
|
27
|
+
* in. Wraps `router.subscribeLeave` so consumers don't repeat the same
|
|
28
|
+
* boilerplate every time:
|
|
29
|
+
*
|
|
30
|
+
* - **Reentrant abort pre-check**: if `signal.aborted` is already `true`
|
|
31
|
+
* when the handler would run (rapid navigation superseded a slower
|
|
32
|
+
* one), the handler is skipped entirely. `signal.addEventListener(
|
|
33
|
+
* "abort", ...)` does not fire retroactively, so without this guard
|
|
34
|
+
* downstream cleanup would never trigger.
|
|
35
|
+
* - **Same-route skip**: by default, `route.name === nextRoute.name`
|
|
36
|
+
* short-circuits the handler — query-only navigations (sort, filter,
|
|
37
|
+
* pagination) skip the work. Opt out with `skipSameRoute: false`.
|
|
38
|
+
*
|
|
39
|
+
* Returns nothing — the subscription's lifecycle is bound to the
|
|
40
|
+
* component via `onCleanup`.
|
|
41
|
+
*
|
|
42
|
+
* If the handler returns a Promise, the router blocks on it. If the
|
|
43
|
+
* Promise resolves, navigation proceeds. If it rejects, the router emits
|
|
44
|
+
* `TRANSITION_CANCELLED`.
|
|
45
|
+
*
|
|
46
|
+
* **Handler reactivity (Solid)**: Solid components run **once** at mount;
|
|
47
|
+
* `handler` is captured in closure at the call site. If you need
|
|
48
|
+
* different behavior across renders, derive it from a signal inside the
|
|
49
|
+
* handler body — do not rely on swapping the handler reference.
|
|
50
|
+
*
|
|
51
|
+
* @example Animation
|
|
52
|
+
* ```tsx
|
|
53
|
+
* let ref: HTMLDivElement | undefined;
|
|
54
|
+
*
|
|
55
|
+
* useRouteExit(async ({ signal }) => {
|
|
56
|
+
* const el = ref;
|
|
57
|
+
* if (!el) return;
|
|
58
|
+
* el.classList.add("fade-out");
|
|
59
|
+
* const cleanup = () => el.classList.remove("fade-out");
|
|
60
|
+
* signal.addEventListener("abort", cleanup, { once: true });
|
|
61
|
+
* try {
|
|
62
|
+
* el.getBoundingClientRect(); // style flush
|
|
63
|
+
* await Promise.allSettled(el.getAnimations().map((a) => a.finished));
|
|
64
|
+
* } finally {
|
|
65
|
+
* cleanup();
|
|
66
|
+
* }
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @example Auto-save form draft
|
|
71
|
+
* ```tsx
|
|
72
|
+
* useRouteExit(async ({ signal }) => {
|
|
73
|
+
* if (formState.dirty) await api.saveDraft(formState, { signal });
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @example Reading rich transition metadata via `nextRoute.transition`
|
|
78
|
+
* ```tsx
|
|
79
|
+
* useRouteExit(({ route, nextRoute }) => {
|
|
80
|
+
* if (nextRoute.transition.segments.deactivated.includes("products")) {
|
|
81
|
+
* productCache.clear();
|
|
82
|
+
* }
|
|
83
|
+
* if (nextRoute.transition.redirected) {
|
|
84
|
+
* return;
|
|
85
|
+
* }
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function useRouteExit(handler: RouteExitHandler, options?: UseRouteExitOptions): void;
|
|
90
|
+
//# sourceMappingURL=useRouteExit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRouteExit.d.ts","sourceRoot":"","sources":["../../../src/hooks/useRouteExit.tsx"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE/C,MAAM,WAAW,gBAAgB;IAC/B,4BAA4B;IAC5B,KAAK,EAAE,KAAK,CAAC;IACb,oCAAoC;IACpC,SAAS,EAAE,KAAK,CAAC;IACjB;;;;;;OAMG;IACH,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,MAAM,gBAAgB,GAAG,CAC7B,OAAO,EAAE,gBAAgB,KACtB,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,gBAAgB,EACzB,OAAO,CAAC,EAAE,mBAAmB,GAC5B,IAAI,CAqBN"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export { useRouteNode } from "./hooks/useRouteNode";
|
|
|
10
10
|
export { useRouteStore } from "./hooks/useRouteStore";
|
|
11
11
|
export { useRouteNodeStore } from "./hooks/useRouteNodeStore";
|
|
12
12
|
export { useRouterTransition } from "./hooks/useRouterTransition";
|
|
13
|
+
export { useRouteExit } from "./hooks/useRouteExit";
|
|
14
|
+
export { useRouteEnter } from "./hooks/useRouteEnter";
|
|
13
15
|
export { RouterProvider } from "./RouterProvider";
|
|
14
16
|
export { RouterContext, RouteContext } from "./context";
|
|
15
17
|
export { createSignalFromSource } from "./createSignalFromSource";
|
|
@@ -17,7 +19,9 @@ export { createStoreFromSource } from "./createStoreFromSource";
|
|
|
17
19
|
export type { LinkProps, RouteState } from "./types";
|
|
18
20
|
export type { RouterErrorBoundaryProps } from "./components/RouterErrorBoundary";
|
|
19
21
|
export type { LinkDirectiveOptions } from "./directives/link";
|
|
20
|
-
export type { RouteViewProps, RouteViewMatchProps, RouteViewNotFoundProps, } from "./components/RouteView";
|
|
22
|
+
export type { RouteViewProps, RouteViewMatchProps, RouteViewSelfProps, RouteViewNotFoundProps, } from "./components/RouteView";
|
|
23
|
+
export type { RouteExitContext, RouteExitHandler, UseRouteExitOptions, } from "./hooks/useRouteExit";
|
|
24
|
+
export type { RouteEnterContext, RouteEnterHandler, UseRouteEnterOptions, } from "./hooks/useRouteEnter";
|
|
21
25
|
export type { Navigator } from "@real-router/core";
|
|
22
26
|
export type { RouterTransitionSnapshot } from "@real-router/sources";
|
|
23
27
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAEvE,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAE9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAElE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAExD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErD,YAAY,EAAE,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AAEjF,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,YAAY,EACV,cAAc,EACd,mBAAmB,EACnB,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAEhC,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,YAAY,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAEvE,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAE9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAElE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAExD,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErD,YAAY,EAAE,wBAAwB,EAAE,MAAM,kCAAkC,CAAC;AAEjF,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,YAAY,EACV,cAAc,EACd,mBAAmB,EACnB,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,wBAAwB,CAAC;AAEhC,YAAY,EACV,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,sBAAsB,CAAC;AAE9B,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAE/B,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAEnD,YAAY,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/solid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "Solid.js integration for Real-Router",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"license": "MIT",
|
|
52
52
|
"sideEffects": false,
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@real-router/core": "^0.50.
|
|
54
|
+
"@real-router/core": "^0.50.2",
|
|
55
55
|
"@real-router/route-utils": "^0.2.1",
|
|
56
56
|
"@real-router/sources": "^0.7.2"
|
|
57
57
|
},
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"solid-js": "1.9.12",
|
|
72
72
|
"vite-plugin-solid": "2.11.11",
|
|
73
73
|
"vitest": "4.1.0",
|
|
74
|
-
"@real-router/browser-plugin": "^0.
|
|
74
|
+
"@real-router/browser-plugin": "^0.16.0"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
77
77
|
"solid-js": ">=1.7.0"
|
package/src/RouterProvider.tsx
CHANGED
|
@@ -4,7 +4,11 @@ import { createSelector, onCleanup, onMount } from "solid-js";
|
|
|
4
4
|
|
|
5
5
|
import { RouterContext, RouteContext } from "./context";
|
|
6
6
|
import { createSignalFromSource } from "./createSignalFromSource";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
createRouteAnnouncer,
|
|
9
|
+
createScrollRestoration,
|
|
10
|
+
createViewTransitions,
|
|
11
|
+
} from "./dom-utils";
|
|
8
12
|
|
|
9
13
|
import type { ScrollRestorationOptions } from "./dom-utils";
|
|
10
14
|
import type { Router } from "@real-router/core";
|
|
@@ -14,6 +18,7 @@ export interface RouteProviderProps {
|
|
|
14
18
|
router: Router;
|
|
15
19
|
announceNavigation?: boolean;
|
|
16
20
|
scrollRestoration?: ScrollRestorationOptions;
|
|
21
|
+
viewTransitions?: boolean;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
export function isRouteActive(
|
|
@@ -53,6 +58,18 @@ export function RouterProvider(
|
|
|
53
58
|
});
|
|
54
59
|
});
|
|
55
60
|
|
|
61
|
+
onMount(() => {
|
|
62
|
+
if (!props.viewTransitions) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const vt = createViewTransitions(props.router);
|
|
67
|
+
|
|
68
|
+
onCleanup(() => {
|
|
69
|
+
vt.destroy();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
56
73
|
const navigator = getNavigator(props.router);
|
|
57
74
|
const routeSource = createRouteSource(props.router);
|
|
58
75
|
const routeSignal = createSignalFromSource(routeSource);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { children as resolveChildren, createMemo } from "solid-js";
|
|
2
2
|
|
|
3
|
-
import { Match, NotFound } from "./components";
|
|
3
|
+
import { Match, NotFound, Self } from "./components";
|
|
4
4
|
import { buildRenderList, collectElements } from "./helpers";
|
|
5
5
|
import { useRouteNode } from "../../hooks/useRouteNode";
|
|
6
6
|
|
|
@@ -44,10 +44,15 @@ function RouteViewRoot(props: Readonly<RouteViewProps>): JSX.Element {
|
|
|
44
44
|
|
|
45
45
|
RouteViewRoot.displayName = "RouteView";
|
|
46
46
|
|
|
47
|
-
export const RouteView = Object.assign(RouteViewRoot, {
|
|
47
|
+
export const RouteView = Object.assign(RouteViewRoot, {
|
|
48
|
+
Match,
|
|
49
|
+
Self,
|
|
50
|
+
NotFound,
|
|
51
|
+
});
|
|
48
52
|
|
|
49
53
|
export type {
|
|
50
54
|
RouteViewProps,
|
|
51
55
|
MatchProps as RouteViewMatchProps,
|
|
56
|
+
SelfProps as RouteViewSelfProps,
|
|
52
57
|
NotFoundProps as RouteViewNotFoundProps,
|
|
53
58
|
} from "./types";
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import type { MatchProps, NotFoundProps } from "./types";
|
|
1
|
+
import type { MatchProps, NotFoundProps, SelfProps } from "./types";
|
|
2
2
|
import type { JSX } from "solid-js";
|
|
3
3
|
|
|
4
4
|
// Local (non-global) Symbols — Symbol.for() would expose markers to spoofing
|
|
5
5
|
// via the global Symbol registry. See Gotchas section "RouteView Marker Objects".
|
|
6
6
|
export const MATCH_MARKER = Symbol("RouteView.Match");
|
|
7
7
|
|
|
8
|
+
export const SELF_MARKER = Symbol("RouteView.Self");
|
|
9
|
+
|
|
8
10
|
export const NOT_FOUND_MARKER = Symbol("RouteView.NotFound");
|
|
9
11
|
|
|
10
12
|
export interface MatchMarker {
|
|
@@ -15,12 +17,18 @@ export interface MatchMarker {
|
|
|
15
17
|
children: JSX.Element;
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
export interface SelfMarker {
|
|
21
|
+
$$type: typeof SELF_MARKER;
|
|
22
|
+
fallback?: JSX.Element;
|
|
23
|
+
children: JSX.Element;
|
|
24
|
+
}
|
|
25
|
+
|
|
18
26
|
export interface NotFoundMarker {
|
|
19
27
|
$$type: typeof NOT_FOUND_MARKER;
|
|
20
28
|
children: JSX.Element;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
|
-
export type RouteViewMarker = MatchMarker | NotFoundMarker;
|
|
31
|
+
export type RouteViewMarker = MatchMarker | SelfMarker | NotFoundMarker;
|
|
24
32
|
|
|
25
33
|
export function Match(props: MatchProps): JSX.Element {
|
|
26
34
|
const result: MatchMarker = {
|
|
@@ -41,6 +49,21 @@ export function Match(props: MatchProps): JSX.Element {
|
|
|
41
49
|
|
|
42
50
|
Match.displayName = "RouteView.Match";
|
|
43
51
|
|
|
52
|
+
export function Self(props: SelfProps): JSX.Element {
|
|
53
|
+
const result: SelfMarker = {
|
|
54
|
+
$$type: SELF_MARKER,
|
|
55
|
+
fallback: props.fallback,
|
|
56
|
+
get children(): JSX.Element {
|
|
57
|
+
return props.children;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// See Match for the marker-pattern rationale.
|
|
62
|
+
return result as unknown as JSX.Element;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
Self.displayName = "RouteView.Self";
|
|
66
|
+
|
|
44
67
|
export function NotFound(props: NotFoundProps): JSX.Element {
|
|
45
68
|
const result: NotFoundMarker = {
|
|
46
69
|
$$type: NOT_FOUND_MARKER,
|
|
@@ -2,12 +2,13 @@ import { UNKNOWN_ROUTE } from "@real-router/core";
|
|
|
2
2
|
import { startsWithSegment } from "@real-router/route-utils";
|
|
3
3
|
import { Suspense } from "solid-js";
|
|
4
4
|
|
|
5
|
-
import { MATCH_MARKER, NOT_FOUND_MARKER } from "./components";
|
|
5
|
+
import { MATCH_MARKER, NOT_FOUND_MARKER, SELF_MARKER } from "./components";
|
|
6
6
|
|
|
7
7
|
import type {
|
|
8
8
|
MatchMarker,
|
|
9
9
|
NotFoundMarker,
|
|
10
10
|
RouteViewMarker,
|
|
11
|
+
SelfMarker,
|
|
11
12
|
} from "./components";
|
|
12
13
|
import type { JSX } from "solid-js";
|
|
13
14
|
|
|
@@ -32,6 +33,15 @@ function isMatchMarker(value: unknown): value is MatchMarker {
|
|
|
32
33
|
);
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
function isSelfMarker(value: unknown): value is SelfMarker {
|
|
37
|
+
return (
|
|
38
|
+
value != null &&
|
|
39
|
+
typeof value === "object" &&
|
|
40
|
+
"$$type" in value &&
|
|
41
|
+
value.$$type === SELF_MARKER
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
35
45
|
function isNotFoundMarker(value: unknown): value is NotFoundMarker {
|
|
36
46
|
return (
|
|
37
47
|
value != null &&
|
|
@@ -57,16 +67,56 @@ export function collectElements(
|
|
|
57
67
|
return;
|
|
58
68
|
}
|
|
59
69
|
|
|
60
|
-
if (
|
|
70
|
+
if (
|
|
71
|
+
isMatchMarker(children) ||
|
|
72
|
+
isSelfMarker(children) ||
|
|
73
|
+
isNotFoundMarker(children)
|
|
74
|
+
) {
|
|
61
75
|
result.push(children);
|
|
62
76
|
}
|
|
63
77
|
}
|
|
64
78
|
|
|
79
|
+
// child.children is a getter — read it INSIDE the JSX expression so Solid
|
|
80
|
+
// creates a reactive dependency. Pulling it into a variable freezes the
|
|
81
|
+
// value at template-build time and breaks Suspense fallback transitions
|
|
82
|
+
// (lazy() resolution).
|
|
83
|
+
function renderMatch(child: MatchMarker): JSX.Element {
|
|
84
|
+
return child.fallback === undefined ? (
|
|
85
|
+
child.children
|
|
86
|
+
) : (
|
|
87
|
+
<Suspense fallback={child.fallback}>{child.children}</Suspense>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function renderSelf(self: SelfMarker): JSX.Element {
|
|
92
|
+
return self.fallback === undefined ? (
|
|
93
|
+
self.children
|
|
94
|
+
) : (
|
|
95
|
+
<Suspense fallback={self.fallback}>{self.children}</Suspense>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function processMatchChild(
|
|
100
|
+
child: MatchMarker,
|
|
101
|
+
routeName: string,
|
|
102
|
+
nodeName: string,
|
|
103
|
+
): JSX.Element | null {
|
|
104
|
+
const { segment, exact } = child;
|
|
105
|
+
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
106
|
+
|
|
107
|
+
if (!isSegmentMatch(routeName, fullSegmentName, exact)) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return renderMatch(child);
|
|
112
|
+
}
|
|
113
|
+
|
|
65
114
|
export function buildRenderList(
|
|
66
115
|
elements: RouteViewMarker[],
|
|
67
116
|
routeName: string,
|
|
68
117
|
nodeName: string,
|
|
69
118
|
): JSX.Element[] {
|
|
119
|
+
let selfMarker: SelfMarker | null = null;
|
|
70
120
|
let notFoundChildren: JSX.Element | null = null;
|
|
71
121
|
let activeMatchFound = false;
|
|
72
122
|
const rendered: JSX.Element[] = [];
|
|
@@ -77,33 +127,29 @@ export function buildRenderList(
|
|
|
77
127
|
continue;
|
|
78
128
|
}
|
|
79
129
|
|
|
80
|
-
if (
|
|
130
|
+
if (isSelfMarker(child)) {
|
|
131
|
+
selfMarker ??= child;
|
|
81
132
|
continue;
|
|
82
133
|
}
|
|
83
134
|
|
|
84
|
-
|
|
85
|
-
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
86
|
-
|
|
87
|
-
if (!isSegmentMatch(routeName, fullSegmentName, exact)) {
|
|
135
|
+
if (activeMatchFound) {
|
|
88
136
|
continue;
|
|
89
137
|
}
|
|
90
138
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
),
|
|
98
|
-
);
|
|
139
|
+
const matchRendered = processMatchChild(child, routeName, nodeName);
|
|
140
|
+
|
|
141
|
+
if (matchRendered !== null) {
|
|
142
|
+
activeMatchFound = true;
|
|
143
|
+
rendered.push(matchRendered);
|
|
144
|
+
}
|
|
99
145
|
}
|
|
100
146
|
|
|
101
|
-
if (
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
notFoundChildren !== null
|
|
105
|
-
|
|
106
|
-
|
|
147
|
+
if (!activeMatchFound) {
|
|
148
|
+
if (selfMarker !== null && routeName === nodeName) {
|
|
149
|
+
rendered.push(renderSelf(selfMarker));
|
|
150
|
+
} else if (routeName === UNKNOWN_ROUTE && notFoundChildren !== null) {
|
|
151
|
+
rendered.push(notFoundChildren);
|
|
152
|
+
}
|
|
107
153
|
}
|
|
108
154
|
|
|
109
155
|
return rendered;
|
|
@@ -12,6 +12,13 @@ export interface MatchProps {
|
|
|
12
12
|
readonly children: JSX.Element;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
export interface SelfProps {
|
|
16
|
+
/** Fallback content while children are suspended. */
|
|
17
|
+
readonly fallback?: JSX.Element;
|
|
18
|
+
/** Content to render when the active route name equals the parent RouteView's nodeName. */
|
|
19
|
+
readonly children: JSX.Element;
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
export interface NotFoundProps {
|
|
16
23
|
readonly children: JSX.Element;
|
|
17
24
|
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { createEffect } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import { useRoute } from "./useRoute";
|
|
4
|
+
|
|
5
|
+
import type { State } from "@real-router/core";
|
|
6
|
+
|
|
7
|
+
export interface RouteEnterContext {
|
|
8
|
+
/** The route that was just activated. */
|
|
9
|
+
route: State;
|
|
10
|
+
/** The route that was active immediately before this navigation. */
|
|
11
|
+
previousRoute: State;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type RouteEnterHandler = (context: RouteEnterContext) => void;
|
|
15
|
+
|
|
16
|
+
export interface UseRouteEnterOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Skip the handler when `route.name === previousRoute.name`
|
|
19
|
+
* (sort/filter/query-only navigations on the same route). Default:
|
|
20
|
+
* `true`. Symmetric with `useRouteExit`'s same-name option.
|
|
21
|
+
*/
|
|
22
|
+
skipSameRoute?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Fire `handler` once when the component mounts as a result of a
|
|
27
|
+
* navigation. Mirror of `useRouteExit` for the entry side.
|
|
28
|
+
*
|
|
29
|
+
* What this hook covers that an ad-hoc `createEffect` + `useRoute()`
|
|
30
|
+
* doesn't:
|
|
31
|
+
*
|
|
32
|
+
* - **Skip-initial**: handler is skipped when there is no
|
|
33
|
+
* `transition.from` (i.e. first-load mount). Most consumers want to
|
|
34
|
+
* fire side effects only on real navigations, not on hydration.
|
|
35
|
+
* - **Same-route skip** (default): handler is skipped when
|
|
36
|
+
* `route.transition.from === route.name`. Sort/filter/query-only
|
|
37
|
+
* navigations re-trigger the effect (because the `route` reference
|
|
38
|
+
* changes), but they are not "entries" in the animation / analytics
|
|
39
|
+
* sense — the component instance has stayed mounted throughout.
|
|
40
|
+
* Opt out with `skipSameRoute: false`.
|
|
41
|
+
* - **Mount-time `route` / `previousRoute` snapshot**: the handler
|
|
42
|
+
* receives the values that were live at the moment of effect
|
|
43
|
+
* activation, not the latest ones (which may have moved on if the
|
|
44
|
+
* user navigated again before the effect drained).
|
|
45
|
+
*
|
|
46
|
+
* **Handler reactivity (Solid)**: Solid components run **once** at mount;
|
|
47
|
+
* `handler` is captured in closure when the hook is called. If you need
|
|
48
|
+
* different behavior across renders, derive it from a signal inside the
|
|
49
|
+
* handler body — do not rely on swapping the handler reference.
|
|
50
|
+
*
|
|
51
|
+
* @example Direction-aware entry animation
|
|
52
|
+
* ```tsx
|
|
53
|
+
* useRouteEnter(({ route }) => {
|
|
54
|
+
* const direction = route.context.browser?.direction;
|
|
55
|
+
* ref?.classList.add(
|
|
56
|
+
* direction === "back" ? "slide-from-left" : "slide-from-right",
|
|
57
|
+
* );
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example Analytics page-enter event (skip-initial built-in)
|
|
62
|
+
* ```tsx
|
|
63
|
+
* useRouteEnter(({ route, previousRoute }) => {
|
|
64
|
+
* analytics.track("page_enter", {
|
|
65
|
+
* route: route.name,
|
|
66
|
+
* from: previousRoute.name,
|
|
67
|
+
* });
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* @example Reading rich transition metadata via `route.transition`
|
|
72
|
+
* ```tsx
|
|
73
|
+
* useRouteEnter(({ route }) => {
|
|
74
|
+
* if (route.transition.redirected) {
|
|
75
|
+
* showToast(`Redirected from ${route.transition.from}`);
|
|
76
|
+
* }
|
|
77
|
+
* if (route.transition.segments.activated.includes("products")) {
|
|
78
|
+
* // products subtree just became active
|
|
79
|
+
* }
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export function useRouteEnter(
|
|
84
|
+
handler: RouteEnterHandler,
|
|
85
|
+
options?: UseRouteEnterOptions,
|
|
86
|
+
): void {
|
|
87
|
+
const routeState = useRoute();
|
|
88
|
+
const skipSameRoute = options?.skipSameRoute ?? true;
|
|
89
|
+
let lastHandledRoute: State | null = null;
|
|
90
|
+
|
|
91
|
+
createEffect(() => {
|
|
92
|
+
const { route, previousRoute } = routeState();
|
|
93
|
+
|
|
94
|
+
// Early-exit guards, top-down:
|
|
95
|
+
//
|
|
96
|
+
// - **Defensive**: `route` may be undefined during SSR or
|
|
97
|
+
// pre-start hydration. Not testable from vitest (tests start
|
|
98
|
+
// the router before render), so v8-ignored.
|
|
99
|
+
// - **Skip-initial**: `state.transition.from` is undefined only
|
|
100
|
+
// for the very first state committed by `router.start()`.
|
|
101
|
+
// - **Skip-same-route**: query-only navigations have
|
|
102
|
+
// `transition.from === route.name`. Opt-out via
|
|
103
|
+
// `skipSameRoute: false`.
|
|
104
|
+
// - **Defensive dedupe + missing `previousRoute`**: same `route`
|
|
105
|
+
// ref between effect activations is unexpected on Solid (effects
|
|
106
|
+
// run once per dependency change); `!previousRoute` is unreachable
|
|
107
|
+
// once `transition.from` is set (the two are populated together by
|
|
108
|
+
// core). Both kept for parity with React; v8-ignored.
|
|
109
|
+
/* v8 ignore start */
|
|
110
|
+
if (!route) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
/* v8 ignore stop */
|
|
114
|
+
if (!route.transition.from) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (skipSameRoute && route.transition.from === route.name) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
/* v8 ignore start */
|
|
121
|
+
if (lastHandledRoute === route || !previousRoute) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
/* v8 ignore stop */
|
|
125
|
+
|
|
126
|
+
lastHandledRoute = route;
|
|
127
|
+
handler({ route, previousRoute });
|
|
128
|
+
});
|
|
129
|
+
}
|