@real-router/react 0.19.0 → 0.21.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 +34 -4
- package/dist/cjs/{Link-CFGcNhCQ.js → Link-CTo-n4Hr.js} +2 -2
- package/dist/cjs/{Link-CFGcNhCQ.js.map → Link-CTo-n4Hr.js.map} +1 -1
- package/dist/cjs/RouterProvider-Qu-8nMIX.js +2 -0
- package/dist/cjs/RouterProvider-Qu-8nMIX.js.map +1 -0
- package/dist/cjs/{RouterProvider-BFSblUxR.d.ts → RouterProvider-uxwfsTJq.d.ts} +2 -1
- package/dist/cjs/{RouterProvider-BFSblUxR.d.ts.map → RouterProvider-uxwfsTJq.d.ts.map} +1 -1
- package/dist/cjs/index.d.ts +231 -4
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/ink.js +1 -1
- package/dist/cjs/legacy.d.ts +1 -1
- package/dist/cjs/legacy.js +1 -1
- package/dist/esm/{Link-coFvvMgW.mjs → Link-59cXeHhr.mjs} +2 -2
- package/dist/esm/{Link-coFvvMgW.mjs.map → Link-59cXeHhr.mjs.map} +1 -1
- package/dist/esm/RouterProvider-B6vpi85i.mjs +2 -0
- package/dist/esm/RouterProvider-B6vpi85i.mjs.map +1 -0
- package/dist/esm/{RouterProvider-EtbwzAgd.d.mts → RouterProvider-CqJSW6CT.d.mts} +2 -1
- package/dist/esm/{RouterProvider-EtbwzAgd.d.mts.map → RouterProvider-CqJSW6CT.d.mts.map} +1 -1
- package/dist/esm/index.d.mts +231 -4
- package/dist/esm/index.d.mts.map +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/ink.mjs +1 -1
- package/dist/esm/legacy.d.mts +1 -1
- package/dist/esm/legacy.mjs +1 -1
- package/package.json +3 -3
- package/src/RouterProvider.tsx +19 -1
- package/src/components/modern/RouteView/RouteView.tsx +7 -2
- package/src/components/modern/RouteView/components.tsx +7 -1
- package/src/components/modern/RouteView/helpers.tsx +141 -52
- package/src/components/modern/RouteView/index.ts +1 -0
- package/src/components/modern/RouteView/types.ts +13 -1
- package/src/hooks/useRouteEnter.tsx +156 -0
- package/src/hooks/useRouteExit.tsx +159 -0
- package/src/index.ts +17 -0
- package/dist/cjs/RouterProvider-BOWw6Z_i.js +0 -2
- package/dist/cjs/RouterProvider-BOWw6Z_i.js.map +0 -1
- package/dist/esm/RouterProvider-Cx0t2IIF.mjs +0 -2
- package/dist/esm/RouterProvider-Cx0t2IIF.mjs.map +0 -1
package/src/RouterProvider.tsx
CHANGED
|
@@ -3,7 +3,11 @@ import { createRouteSource } from "@real-router/sources";
|
|
|
3
3
|
import { useEffect, useMemo, useSyncExternalStore } from "react";
|
|
4
4
|
|
|
5
5
|
import { NavigatorContext, RouteContext, RouterContext } from "./context";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
createRouteAnnouncer,
|
|
8
|
+
createScrollRestoration,
|
|
9
|
+
createViewTransitions,
|
|
10
|
+
} from "./dom-utils";
|
|
7
11
|
|
|
8
12
|
import type { ScrollRestorationOptions } from "./dom-utils";
|
|
9
13
|
import type { Router } from "@real-router/core";
|
|
@@ -14,6 +18,7 @@ export interface RouteProviderProps {
|
|
|
14
18
|
children: ReactNode;
|
|
15
19
|
announceNavigation?: boolean;
|
|
16
20
|
scrollRestoration?: ScrollRestorationOptions;
|
|
21
|
+
viewTransitions?: boolean;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
export const RouterProvider: FC<RouteProviderProps> = ({
|
|
@@ -21,6 +26,7 @@ export const RouterProvider: FC<RouteProviderProps> = ({
|
|
|
21
26
|
children,
|
|
22
27
|
announceNavigation,
|
|
23
28
|
scrollRestoration,
|
|
29
|
+
viewTransitions,
|
|
24
30
|
}) => {
|
|
25
31
|
useEffect(() => {
|
|
26
32
|
if (!announceNavigation) {
|
|
@@ -61,6 +67,18 @@ export const RouterProvider: FC<RouteProviderProps> = ({
|
|
|
61
67
|
// eslint-disable-next-line @eslint-react/exhaustive-deps
|
|
62
68
|
}, [router, srEnabled, srMode, srAnchor]);
|
|
63
69
|
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (!viewTransitions) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const vt = createViewTransitions(router);
|
|
76
|
+
|
|
77
|
+
return () => {
|
|
78
|
+
vt.destroy();
|
|
79
|
+
};
|
|
80
|
+
}, [router, viewTransitions]);
|
|
81
|
+
|
|
64
82
|
const navigator = useMemo(() => getNavigator(router), [router]);
|
|
65
83
|
|
|
66
84
|
// useSyncExternalStore manages the router subscription lifecycle:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useMemo, useRef } from "react";
|
|
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
|
|
|
@@ -49,10 +49,15 @@ function RouteViewRoot({
|
|
|
49
49
|
|
|
50
50
|
RouteViewRoot.displayName = "RouteView";
|
|
51
51
|
|
|
52
|
-
export const RouteView = Object.assign(RouteViewRoot, {
|
|
52
|
+
export const RouteView = Object.assign(RouteViewRoot, {
|
|
53
|
+
Match,
|
|
54
|
+
Self,
|
|
55
|
+
NotFound,
|
|
56
|
+
});
|
|
53
57
|
|
|
54
58
|
export type {
|
|
55
59
|
RouteViewProps,
|
|
56
60
|
MatchProps as RouteViewMatchProps,
|
|
61
|
+
SelfProps as RouteViewSelfProps,
|
|
57
62
|
NotFoundProps as RouteViewNotFoundProps,
|
|
58
63
|
} from "./types";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MatchProps, NotFoundProps } from "./types";
|
|
1
|
+
import type { MatchProps, NotFoundProps, SelfProps } from "./types";
|
|
2
2
|
|
|
3
3
|
export function Match(_props: MatchProps): null {
|
|
4
4
|
return null;
|
|
@@ -6,6 +6,12 @@ export function Match(_props: MatchProps): null {
|
|
|
6
6
|
|
|
7
7
|
Match.displayName = "RouteView.Match";
|
|
8
8
|
|
|
9
|
+
export function Self(_props: SelfProps): null {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
Self.displayName = "RouteView.Self";
|
|
14
|
+
|
|
9
15
|
export function NotFound(_props: NotFoundProps): null {
|
|
10
16
|
return null;
|
|
11
17
|
}
|
|
@@ -2,11 +2,18 @@ import { UNKNOWN_ROUTE } from "@real-router/core";
|
|
|
2
2
|
import { startsWithSegment } from "@real-router/route-utils";
|
|
3
3
|
import { Activity, Children, Fragment, Suspense, isValidElement } from "react";
|
|
4
4
|
|
|
5
|
-
import { Match, NotFound } from "./components";
|
|
5
|
+
import { Match, NotFound, Self } from "./components";
|
|
6
6
|
|
|
7
|
-
import type { MatchProps, NotFoundProps } from "./types";
|
|
7
|
+
import type { MatchProps, NotFoundProps, SelfProps } from "./types";
|
|
8
8
|
import type { ReactElement, ReactNode } from "react";
|
|
9
9
|
|
|
10
|
+
interface FallbackSlots {
|
|
11
|
+
selfChildren: ReactNode;
|
|
12
|
+
selfFallback: ReactNode | undefined;
|
|
13
|
+
selfFound: boolean;
|
|
14
|
+
notFoundChildren: ReactNode;
|
|
15
|
+
}
|
|
16
|
+
|
|
10
17
|
function isSegmentMatch(
|
|
11
18
|
routeName: string,
|
|
12
19
|
fullSegmentName: string,
|
|
@@ -33,7 +40,11 @@ export function collectElements(
|
|
|
33
40
|
continue;
|
|
34
41
|
}
|
|
35
42
|
|
|
36
|
-
if (
|
|
43
|
+
if (
|
|
44
|
+
child.type === Match ||
|
|
45
|
+
child.type === Self ||
|
|
46
|
+
child.type === NotFound
|
|
47
|
+
) {
|
|
37
48
|
result.push(child);
|
|
38
49
|
} else {
|
|
39
50
|
collectElements(
|
|
@@ -44,29 +55,127 @@ export function collectElements(
|
|
|
44
55
|
}
|
|
45
56
|
}
|
|
46
57
|
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
function renderSlotElement(
|
|
59
|
+
slotChildren: ReactNode,
|
|
60
|
+
key: string,
|
|
50
61
|
keepAlive: boolean,
|
|
51
62
|
mode: "visible" | "hidden",
|
|
52
63
|
fallback?: ReactNode,
|
|
53
64
|
): ReactElement {
|
|
54
65
|
const content =
|
|
55
66
|
fallback === undefined ? (
|
|
56
|
-
|
|
67
|
+
slotChildren
|
|
57
68
|
) : (
|
|
58
|
-
<Suspense fallback={fallback}>{
|
|
69
|
+
<Suspense fallback={fallback}>{slotChildren}</Suspense>
|
|
59
70
|
);
|
|
60
71
|
|
|
61
72
|
if (keepAlive) {
|
|
62
73
|
return (
|
|
63
|
-
<Activity mode={mode} key={
|
|
74
|
+
<Activity mode={mode} key={key}>
|
|
64
75
|
{content}
|
|
65
76
|
</Activity>
|
|
66
77
|
);
|
|
67
78
|
}
|
|
68
79
|
|
|
69
|
-
return <Fragment key={
|
|
80
|
+
return <Fragment key={key}>{content}</Fragment>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function recordFallback(child: ReactElement, slots: FallbackSlots): boolean {
|
|
84
|
+
if (child.type === NotFound) {
|
|
85
|
+
slots.notFoundChildren = (child.props as NotFoundProps).children;
|
|
86
|
+
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (child.type === Self) {
|
|
91
|
+
// First-wins: subsequent <Self> elements are ignored, mirroring NotFound.
|
|
92
|
+
if (!slots.selfFound) {
|
|
93
|
+
slots.selfChildren = (child.props as SelfProps).children;
|
|
94
|
+
slots.selfFallback = (child.props as SelfProps).fallback;
|
|
95
|
+
slots.selfFound = true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function processMatch(
|
|
105
|
+
child: ReactElement,
|
|
106
|
+
routeName: string,
|
|
107
|
+
nodeName: string,
|
|
108
|
+
hasBeenActivated: Set<string>,
|
|
109
|
+
alreadyActive: boolean,
|
|
110
|
+
): { rendered: ReactElement | null; matched: boolean } {
|
|
111
|
+
const {
|
|
112
|
+
segment,
|
|
113
|
+
exact = false,
|
|
114
|
+
keepAlive = false,
|
|
115
|
+
fallback,
|
|
116
|
+
} = child.props as MatchProps;
|
|
117
|
+
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
118
|
+
const isActive =
|
|
119
|
+
!alreadyActive && isSegmentMatch(routeName, fullSegmentName, exact);
|
|
120
|
+
|
|
121
|
+
if (isActive) {
|
|
122
|
+
hasBeenActivated.add(fullSegmentName);
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
rendered: renderSlotElement(
|
|
126
|
+
(child.props as MatchProps).children,
|
|
127
|
+
fullSegmentName,
|
|
128
|
+
keepAlive,
|
|
129
|
+
"visible",
|
|
130
|
+
fallback,
|
|
131
|
+
),
|
|
132
|
+
matched: true,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (keepAlive && hasBeenActivated.has(fullSegmentName)) {
|
|
137
|
+
return {
|
|
138
|
+
rendered: renderSlotElement(
|
|
139
|
+
(child.props as MatchProps).children,
|
|
140
|
+
fullSegmentName,
|
|
141
|
+
keepAlive,
|
|
142
|
+
"hidden",
|
|
143
|
+
fallback,
|
|
144
|
+
),
|
|
145
|
+
matched: false,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { rendered: null, matched: false };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function appendFallback(
|
|
153
|
+
rendered: ReactElement[],
|
|
154
|
+
routeName: string,
|
|
155
|
+
nodeName: string,
|
|
156
|
+
slots: FallbackSlots,
|
|
157
|
+
): void {
|
|
158
|
+
if (slots.selfFound && routeName === nodeName) {
|
|
159
|
+
rendered.push(
|
|
160
|
+
renderSlotElement(
|
|
161
|
+
slots.selfChildren,
|
|
162
|
+
"__route-view-self__",
|
|
163
|
+
false,
|
|
164
|
+
"visible",
|
|
165
|
+
slots.selfFallback,
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (routeName === UNKNOWN_ROUTE && slots.notFoundChildren !== null) {
|
|
173
|
+
rendered.push(
|
|
174
|
+
<Fragment key="__route-view-not-found__">
|
|
175
|
+
{slots.notFoundChildren}
|
|
176
|
+
</Fragment>,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
70
179
|
}
|
|
71
180
|
|
|
72
181
|
export function buildRenderList(
|
|
@@ -75,59 +184,39 @@ export function buildRenderList(
|
|
|
75
184
|
nodeName: string,
|
|
76
185
|
hasBeenActivated: Set<string>,
|
|
77
186
|
): { rendered: ReactElement[]; activeMatchFound: boolean } {
|
|
78
|
-
|
|
187
|
+
const slots: FallbackSlots = {
|
|
188
|
+
selfChildren: null,
|
|
189
|
+
selfFallback: undefined,
|
|
190
|
+
selfFound: false,
|
|
191
|
+
notFoundChildren: null,
|
|
192
|
+
};
|
|
79
193
|
let activeMatchFound = false;
|
|
80
194
|
const rendered: ReactElement[] = [];
|
|
81
195
|
|
|
82
196
|
for (const child of elements) {
|
|
83
|
-
if (child
|
|
84
|
-
notFoundChildren = (child.props as NotFoundProps).children;
|
|
197
|
+
if (recordFallback(child, slots)) {
|
|
85
198
|
continue;
|
|
86
199
|
}
|
|
87
200
|
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (isActive) {
|
|
201
|
+
const result = processMatch(
|
|
202
|
+
child,
|
|
203
|
+
routeName,
|
|
204
|
+
nodeName,
|
|
205
|
+
hasBeenActivated,
|
|
206
|
+
activeMatchFound,
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
if (result.matched) {
|
|
99
210
|
activeMatchFound = true;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
fullSegmentName,
|
|
105
|
-
keepAlive,
|
|
106
|
-
"visible",
|
|
107
|
-
fallback,
|
|
108
|
-
),
|
|
109
|
-
);
|
|
110
|
-
} else if (keepAlive && hasBeenActivated.has(fullSegmentName)) {
|
|
111
|
-
rendered.push(
|
|
112
|
-
renderMatchElement(
|
|
113
|
-
(child.props as MatchProps).children,
|
|
114
|
-
fullSegmentName,
|
|
115
|
-
keepAlive,
|
|
116
|
-
"hidden",
|
|
117
|
-
fallback,
|
|
118
|
-
),
|
|
119
|
-
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (result.rendered !== null) {
|
|
214
|
+
rendered.push(result.rendered);
|
|
120
215
|
}
|
|
121
216
|
}
|
|
122
217
|
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
routeName === UNKNOWN_ROUTE &&
|
|
126
|
-
notFoundChildren !== null
|
|
127
|
-
) {
|
|
128
|
-
rendered.push(
|
|
129
|
-
<Fragment key="__route-view-not-found__">{notFoundChildren}</Fragment>,
|
|
130
|
-
);
|
|
218
|
+
if (!activeMatchFound) {
|
|
219
|
+
appendFallback(rendered, routeName, nodeName, slots);
|
|
131
220
|
}
|
|
132
221
|
|
|
133
222
|
return { rendered, activeMatchFound };
|
|
@@ -3,7 +3,7 @@ import type { ReactNode } from "react";
|
|
|
3
3
|
export interface RouteViewProps {
|
|
4
4
|
/** Route tree node name to subscribe to. "" for root. */
|
|
5
5
|
readonly nodeName: string;
|
|
6
|
-
/** <RouteView.Match
|
|
6
|
+
/** <RouteView.Match>, <RouteView.Self>, and <RouteView.NotFound> elements. */
|
|
7
7
|
readonly children: ReactNode;
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -20,6 +20,18 @@ export interface MatchProps {
|
|
|
20
20
|
readonly children: ReactNode;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export interface SelfProps {
|
|
24
|
+
/**
|
|
25
|
+
* Fallback content to show while children are suspended.
|
|
26
|
+
*
|
|
27
|
+
* Symmetric with `<RouteView.Match fallback>` — wraps children in
|
|
28
|
+
* `<Suspense>` when defined.
|
|
29
|
+
*/
|
|
30
|
+
readonly fallback?: ReactNode;
|
|
31
|
+
/** Content to render when the active route name equals the parent RouteView's nodeName. */
|
|
32
|
+
readonly children: ReactNode;
|
|
33
|
+
}
|
|
34
|
+
|
|
23
35
|
export interface NotFoundProps {
|
|
24
36
|
/** Content to render on UNKNOWN_ROUTE. */
|
|
25
37
|
readonly children: ReactNode;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
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 ad-hoc `useEffect` + `useRoute()` doesn't:
|
|
30
|
+
*
|
|
31
|
+
* - **Skip-initial**: handler is skipped when there is no
|
|
32
|
+
* `previousRoute` (i.e. first-load mount). Most consumers want to
|
|
33
|
+
* fire side effects only on real navigations, not on hydration.
|
|
34
|
+
* - **Same-route skip** (default): handler is skipped when
|
|
35
|
+
* `route.name === previousRoute.name`. Sort/filter/query-only
|
|
36
|
+
* navigations re-run the effect (because `route` reference changes
|
|
37
|
+
* in `useRoute`'s snapshot), but they are not "entries" in the
|
|
38
|
+
* animation / analytics sense — the component instance has stayed
|
|
39
|
+
* mounted throughout. Opt out with `skipSameRoute: false` when
|
|
40
|
+
* the handler legitimately needs to fire on every navigation
|
|
41
|
+
* (e.g. analytics tracking each query-param flip).
|
|
42
|
+
* - **StrictMode double-mount immunity**: in dev, React's StrictMode
|
|
43
|
+
* runs every effect twice to surface bugs. Without a guard,
|
|
44
|
+
* analytics fire twice, animations restart, focus jumps. The hook
|
|
45
|
+
* tracks the last-handled `route` reference and short-circuits the
|
|
46
|
+
* second pass.
|
|
47
|
+
* - **Latest-handler ref**: the handler can change identity on every
|
|
48
|
+
* render without re-running the effect — the registered wrapper
|
|
49
|
+
* dispatches to whatever `handlerRef.current` points to.
|
|
50
|
+
* - **Mount-time `route` / `previousRoute` snapshot**: the handler
|
|
51
|
+
* receives the values that were live at the moment of mount, not
|
|
52
|
+
* the latest ones (which may have moved on if the user navigated
|
|
53
|
+
* again before the effect drained).
|
|
54
|
+
*
|
|
55
|
+
* Race-safety: `useRoute()` is wired through `useSyncExternalStore` from
|
|
56
|
+
* `@real-router/sources`, so by the time the new component's effect
|
|
57
|
+
* runs, the snapshot is the post-commit one. This is the reason we can
|
|
58
|
+
* read mount-time context from `useRoute()` instead of subscribing to
|
|
59
|
+
* `router.subscribe` directly (which fires before React schedules a
|
|
60
|
+
* re-render — the well-known race in distributed components).
|
|
61
|
+
*
|
|
62
|
+
* @example Direction-aware entry animation
|
|
63
|
+
* ```tsx
|
|
64
|
+
* useRouteEnter(({ route }) => {
|
|
65
|
+
* const direction = route.context.browser?.direction;
|
|
66
|
+
* ref.current?.classList.add(
|
|
67
|
+
* direction === "back" ? "slide-from-left" : "slide-from-right",
|
|
68
|
+
* );
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example Source-aware focus management
|
|
73
|
+
* ```tsx
|
|
74
|
+
* useRouteEnter(({ route }) => {
|
|
75
|
+
* if (route.context.browser?.source === "navigate") {
|
|
76
|
+
* headingRef.current?.focus();
|
|
77
|
+
* }
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Analytics page-enter event (skip-initial built-in)
|
|
82
|
+
* ```tsx
|
|
83
|
+
* useRouteEnter(({ route, previousRoute }) => {
|
|
84
|
+
* analytics.track("page_enter", {
|
|
85
|
+
* route: route.name,
|
|
86
|
+
* from: previousRoute.name,
|
|
87
|
+
* });
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example Reading rich transition metadata via `route.transition`
|
|
92
|
+
* ```tsx
|
|
93
|
+
* useRouteEnter(({ route }) => {
|
|
94
|
+
* // route.transition: TransitionMeta — populated by core for every state
|
|
95
|
+
* if (route.transition.redirected) {
|
|
96
|
+
* showToast(`Redirected from ${route.transition.from}`);
|
|
97
|
+
* }
|
|
98
|
+
* if (route.transition.segments.activated.includes("products")) {
|
|
99
|
+
* // products subtree just became active (could be products or
|
|
100
|
+
* // products.detail). Useful for subtree-scoped side effects.
|
|
101
|
+
* }
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export function useRouteEnter(
|
|
106
|
+
handler: RouteEnterHandler,
|
|
107
|
+
options?: UseRouteEnterOptions,
|
|
108
|
+
): void {
|
|
109
|
+
const { route, previousRoute } = useRoute();
|
|
110
|
+
const handlerRef = useRef(handler);
|
|
111
|
+
const lastHandledRouteRef = useRef<State | null>(null);
|
|
112
|
+
const skipSameRoute = options?.skipSameRoute ?? true;
|
|
113
|
+
|
|
114
|
+
// Keep the latest handler reference accessible without re-running
|
|
115
|
+
// the effect. useLayoutEffect (synchronous, post-render, pre-paint)
|
|
116
|
+
// updates the ref before the effect can read it.
|
|
117
|
+
useLayoutEffect(() => {
|
|
118
|
+
handlerRef.current = handler;
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
// Early-exit guards, top-down:
|
|
123
|
+
//
|
|
124
|
+
// - **Defensive**: `route` / `previousRoute` may be undefined
|
|
125
|
+
// during SSR or pre-start hydration. Not testable from vitest
|
|
126
|
+
// (tests start the router before render), so v8-ignored.
|
|
127
|
+
// - **Skip-initial**: `state.transition.from` is undefined only
|
|
128
|
+
// for the very first state committed by `router.start()`.
|
|
129
|
+
// - **Skip-same-route**: query-only navigations have
|
|
130
|
+
// `transition.from === route.name`. Opt-out via
|
|
131
|
+
// `skipSameRoute: false`.
|
|
132
|
+
// - **StrictMode dedupe**: same `route` ref between effect
|
|
133
|
+
// cleanup + re-run in dev's strict pass. Not testable from
|
|
134
|
+
// vitest (`NODE_ENV === "test"` disables React's strict-mode
|
|
135
|
+
// double-run), so v8-ignored.
|
|
136
|
+
/* v8 ignore start */
|
|
137
|
+
if (!route) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
/* v8 ignore stop */
|
|
141
|
+
if (!route.transition.from) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (skipSameRoute && route.transition.from === route.name) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
/* v8 ignore start */
|
|
148
|
+
if (lastHandledRouteRef.current === route || !previousRoute) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
/* v8 ignore stop */
|
|
152
|
+
|
|
153
|
+
lastHandledRouteRef.current = route;
|
|
154
|
+
handlerRef.current({ route, previousRoute });
|
|
155
|
+
}, [route, previousRoute, skipSameRoute]);
|
|
156
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef } from "react";
|
|
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
|
+
* - **Stable handler reference**: the handler can change identity on
|
|
49
|
+
* every render without causing resubscription — internal ref keeps
|
|
50
|
+
* the latest handler accessible to the long-lived subscription.
|
|
51
|
+
*
|
|
52
|
+
* Returns nothing — the subscription's lifecycle is bound to the
|
|
53
|
+
* component's mount.
|
|
54
|
+
*
|
|
55
|
+
* If the handler returns a Promise, the router blocks on it. If the
|
|
56
|
+
* Promise resolves, navigation proceeds. If it rejects, the router emits
|
|
57
|
+
* `TRANSITION_CANCELLED` (existing core behavior, no change here).
|
|
58
|
+
*
|
|
59
|
+
* @example Animation
|
|
60
|
+
* ```tsx
|
|
61
|
+
* const ref = useRef<HTMLDivElement>(null);
|
|
62
|
+
*
|
|
63
|
+
* useRouteExit(async ({ signal }) => {
|
|
64
|
+
* const el = ref.current;
|
|
65
|
+
* if (!el) return;
|
|
66
|
+
* el.classList.add("fade-out");
|
|
67
|
+
* const cleanup = () => el.classList.remove("fade-out");
|
|
68
|
+
* signal.addEventListener("abort", cleanup, { once: true });
|
|
69
|
+
* try {
|
|
70
|
+
* el.getBoundingClientRect(); // style flush
|
|
71
|
+
* await Promise.allSettled(el.getAnimations().map((a) => a.finished));
|
|
72
|
+
* } finally {
|
|
73
|
+
* cleanup();
|
|
74
|
+
* }
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @example Auto-save form draft
|
|
79
|
+
* ```tsx
|
|
80
|
+
* useRouteExit(async ({ signal }) => {
|
|
81
|
+
* if (formState.dirty) await api.saveDraft(formState, { signal });
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example Cancel inflight requests
|
|
86
|
+
* ```tsx
|
|
87
|
+
* useRouteExit(() => {
|
|
88
|
+
* inflightController.abort();
|
|
89
|
+
* });
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @example Library-coordinated exit (motion / framer-motion)
|
|
93
|
+
* ```tsx
|
|
94
|
+
* const exitResolverRef = useRef<(() => void) | null>(null);
|
|
95
|
+
*
|
|
96
|
+
* useRouteExit(({ signal }) => {
|
|
97
|
+
* return new Promise<void>((resolve) => {
|
|
98
|
+
* exitResolverRef.current = resolve;
|
|
99
|
+
* signal.addEventListener("abort", () => resolve(), { once: true });
|
|
100
|
+
* });
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* const onExitComplete = () => exitResolverRef.current?.();
|
|
104
|
+
* // pass onExitComplete to <AnimatePresence>
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @example Reading rich transition metadata via `nextRoute.transition`
|
|
108
|
+
* ```tsx
|
|
109
|
+
* useRouteExit(({ route, nextRoute }) => {
|
|
110
|
+
* // nextRoute.transition: TransitionMeta — preview of the upcoming nav
|
|
111
|
+
* if (nextRoute.transition.segments.deactivated.includes("products")) {
|
|
112
|
+
* // leaving the products subtree entirely — flush product-related caches
|
|
113
|
+
* productCache.clear();
|
|
114
|
+
* }
|
|
115
|
+
* if (nextRoute.transition.redirected) {
|
|
116
|
+
* // skip animation when navigation arrived via redirect
|
|
117
|
+
* return;
|
|
118
|
+
* }
|
|
119
|
+
* });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export function useRouteExit(
|
|
123
|
+
handler: RouteExitHandler,
|
|
124
|
+
options?: UseRouteExitOptions,
|
|
125
|
+
): void {
|
|
126
|
+
const router = useRouter();
|
|
127
|
+
const handlerRef = useRef(handler);
|
|
128
|
+
const skipSameRoute = options?.skipSameRoute ?? true;
|
|
129
|
+
|
|
130
|
+
// Keep the latest handler accessible to the subscription without
|
|
131
|
+
// resubscribing on every render — the subscription registers the
|
|
132
|
+
// wrapper once and dispatches to whatever ref points to.
|
|
133
|
+
// useLayoutEffect (synchronous, post-render, pre-paint) updates the
|
|
134
|
+
// ref before the browser can dispatch any router events that could
|
|
135
|
+
// observe a stale closure.
|
|
136
|
+
useLayoutEffect(() => {
|
|
137
|
+
handlerRef.current = handler;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
return router.subscribeLeave(({ route, nextRoute, signal }) => {
|
|
142
|
+
if (skipSameRoute && route.name === nextRoute.name) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Reentrant abort: signal is already aborted when listener fires
|
|
147
|
+
// (e.g. a newer navigate superseded this one before subscribeLeave
|
|
148
|
+
// even ran). addEventListener("abort", ...) does not fire
|
|
149
|
+
// retroactively, so we skip the handler entirely — any cleanup the
|
|
150
|
+
// handler would have wired via abort listener must not run because
|
|
151
|
+
// the corresponding `add` work never happened.
|
|
152
|
+
if (signal.aborted) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return handlerRef.current({ route, nextRoute, signal });
|
|
157
|
+
});
|
|
158
|
+
}, [router, skipSameRoute]);
|
|
159
|
+
}
|