@real-router/solid 0.7.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 +47 -2
- package/dist/cjs/index.d.ts +167 -2
- package/dist/cjs/index.js +337 -4
- package/dist/esm/index.d.mts +167 -2
- package/dist/esm/index.mjs +336 -5
- package/dist/types/RouterProvider.d.ts +1 -0
- package/dist/types/RouterProvider.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 +4 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/RouterProvider.tsx +18 -1
- package/src/hooks/useRouteEnter.tsx +129 -0
- package/src/hooks/useRouteExit.tsx +123 -0
- package/src/index.tsx +16 -0
|
@@ -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";
|
|
@@ -18,6 +20,8 @@ export type { LinkProps, RouteState } from "./types";
|
|
|
18
20
|
export type { RouterErrorBoundaryProps } from "./components/RouterErrorBoundary";
|
|
19
21
|
export type { LinkDirectiveOptions } from "./directives/link";
|
|
20
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,kBAAkB,EAClB,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);
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { onCleanup } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import { useRouter } from "./useRouter";
|
|
4
|
+
|
|
5
|
+
import type { State } from "@real-router/core";
|
|
6
|
+
|
|
7
|
+
export interface RouteExitContext {
|
|
8
|
+
/** The route being left. */
|
|
9
|
+
route: State;
|
|
10
|
+
/** The route being navigated to. */
|
|
11
|
+
nextRoute: State;
|
|
12
|
+
/**
|
|
13
|
+
* AbortSignal that fires when this navigation is superseded by a later
|
|
14
|
+
* one (rapid clicks). Already filtered: when the handler runs,
|
|
15
|
+
* `signal.aborted` is guaranteed to be `false`. Use
|
|
16
|
+
* `signal.addEventListener("abort", cleanup, { once: true })` for
|
|
17
|
+
* cleanup that must run on cancellation.
|
|
18
|
+
*/
|
|
19
|
+
signal: AbortSignal;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface UseRouteExitOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Skip the handler when `route.name === nextRoute.name`
|
|
25
|
+
* (sort/filter/query-only navigations on the same route). Default:
|
|
26
|
+
* `true`.
|
|
27
|
+
*/
|
|
28
|
+
skipSameRoute?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type RouteExitHandler = (
|
|
32
|
+
context: RouteExitContext,
|
|
33
|
+
) => void | Promise<void>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Subscribe to the router's leave-window with the universal guards baked
|
|
37
|
+
* in. Wraps `router.subscribeLeave` so consumers don't repeat the same
|
|
38
|
+
* boilerplate every time:
|
|
39
|
+
*
|
|
40
|
+
* - **Reentrant abort pre-check**: if `signal.aborted` is already `true`
|
|
41
|
+
* when the handler would run (rapid navigation superseded a slower
|
|
42
|
+
* one), the handler is skipped entirely. `signal.addEventListener(
|
|
43
|
+
* "abort", ...)` does not fire retroactively, so without this guard
|
|
44
|
+
* downstream cleanup would never trigger.
|
|
45
|
+
* - **Same-route skip**: by default, `route.name === nextRoute.name`
|
|
46
|
+
* short-circuits the handler — query-only navigations (sort, filter,
|
|
47
|
+
* pagination) skip the work. Opt out with `skipSameRoute: false`.
|
|
48
|
+
*
|
|
49
|
+
* Returns nothing — the subscription's lifecycle is bound to the
|
|
50
|
+
* component via `onCleanup`.
|
|
51
|
+
*
|
|
52
|
+
* If the handler returns a Promise, the router blocks on it. If the
|
|
53
|
+
* Promise resolves, navigation proceeds. If it rejects, the router emits
|
|
54
|
+
* `TRANSITION_CANCELLED`.
|
|
55
|
+
*
|
|
56
|
+
* **Handler reactivity (Solid)**: Solid components run **once** at mount;
|
|
57
|
+
* `handler` is captured in closure at the call site. If you need
|
|
58
|
+
* different behavior across renders, derive it from a signal inside the
|
|
59
|
+
* handler body — do not rely on swapping the handler reference.
|
|
60
|
+
*
|
|
61
|
+
* @example Animation
|
|
62
|
+
* ```tsx
|
|
63
|
+
* let ref: HTMLDivElement | undefined;
|
|
64
|
+
*
|
|
65
|
+
* useRouteExit(async ({ signal }) => {
|
|
66
|
+
* const el = ref;
|
|
67
|
+
* if (!el) return;
|
|
68
|
+
* el.classList.add("fade-out");
|
|
69
|
+
* const cleanup = () => el.classList.remove("fade-out");
|
|
70
|
+
* signal.addEventListener("abort", cleanup, { once: true });
|
|
71
|
+
* try {
|
|
72
|
+
* el.getBoundingClientRect(); // style flush
|
|
73
|
+
* await Promise.allSettled(el.getAnimations().map((a) => a.finished));
|
|
74
|
+
* } finally {
|
|
75
|
+
* cleanup();
|
|
76
|
+
* }
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example Auto-save form draft
|
|
81
|
+
* ```tsx
|
|
82
|
+
* useRouteExit(async ({ signal }) => {
|
|
83
|
+
* if (formState.dirty) await api.saveDraft(formState, { signal });
|
|
84
|
+
* });
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @example Reading rich transition metadata via `nextRoute.transition`
|
|
88
|
+
* ```tsx
|
|
89
|
+
* useRouteExit(({ route, nextRoute }) => {
|
|
90
|
+
* if (nextRoute.transition.segments.deactivated.includes("products")) {
|
|
91
|
+
* productCache.clear();
|
|
92
|
+
* }
|
|
93
|
+
* if (nextRoute.transition.redirected) {
|
|
94
|
+
* return;
|
|
95
|
+
* }
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export function useRouteExit(
|
|
100
|
+
handler: RouteExitHandler,
|
|
101
|
+
options?: UseRouteExitOptions,
|
|
102
|
+
): void {
|
|
103
|
+
const router = useRouter();
|
|
104
|
+
const skipSameRoute = options?.skipSameRoute ?? true;
|
|
105
|
+
|
|
106
|
+
const off = router.subscribeLeave(({ route, nextRoute, signal }) => {
|
|
107
|
+
if (skipSameRoute && route.name === nextRoute.name) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Reentrant abort: signal is already aborted when listener fires
|
|
112
|
+
// (e.g. a newer navigate superseded this one before subscribeLeave
|
|
113
|
+
// even ran). addEventListener("abort", ...) does not fire
|
|
114
|
+
// retroactively, so we skip the handler entirely.
|
|
115
|
+
if (signal.aborted) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return handler({ route, nextRoute, signal });
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
onCleanup(off);
|
|
123
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -22,6 +22,10 @@ export { useRouteNodeStore } from "./hooks/useRouteNodeStore";
|
|
|
22
22
|
|
|
23
23
|
export { useRouterTransition } from "./hooks/useRouterTransition";
|
|
24
24
|
|
|
25
|
+
export { useRouteExit } from "./hooks/useRouteExit";
|
|
26
|
+
|
|
27
|
+
export { useRouteEnter } from "./hooks/useRouteEnter";
|
|
28
|
+
|
|
25
29
|
export { RouterProvider } from "./RouterProvider";
|
|
26
30
|
|
|
27
31
|
export { RouterContext, RouteContext } from "./context";
|
|
@@ -43,6 +47,18 @@ export type {
|
|
|
43
47
|
RouteViewNotFoundProps,
|
|
44
48
|
} from "./components/RouteView";
|
|
45
49
|
|
|
50
|
+
export type {
|
|
51
|
+
RouteExitContext,
|
|
52
|
+
RouteExitHandler,
|
|
53
|
+
UseRouteExitOptions,
|
|
54
|
+
} from "./hooks/useRouteExit";
|
|
55
|
+
|
|
56
|
+
export type {
|
|
57
|
+
RouteEnterContext,
|
|
58
|
+
RouteEnterHandler,
|
|
59
|
+
UseRouteEnterOptions,
|
|
60
|
+
} from "./hooks/useRouteEnter";
|
|
61
|
+
|
|
46
62
|
export type { Navigator } from "@real-router/core";
|
|
47
63
|
|
|
48
64
|
export type { RouterTransitionSnapshot } from "@real-router/sources";
|