@real-router/react 0.27.1 → 0.27.3
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/dist/cjs/Await-D2oCO8nH.d.ts.map +1 -1
- package/dist/cjs/HttpStatusProvider-DfsVxZba.d.ts.map +1 -1
- package/dist/cjs/{Link-CyDwbrFA.js → Link-D4K4T0Ef.js} +2 -2
- package/dist/cjs/{Link-CyDwbrFA.js.map → Link-D4K4T0Ef.js.map} +1 -1
- package/dist/cjs/RouterErrorBoundary-3UhUelqY.d.ts.map +1 -1
- package/dist/cjs/{RouterProvider-CPsCmrRw.js → RouterProvider-CAaKExt_.js} +2 -2
- package/dist/cjs/RouterProvider-CAaKExt_.js.map +1 -0
- package/dist/cjs/createHttpStatusSink-ChfBL87y.js.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/ink.js.map +1 -1
- package/dist/cjs/legacy.js +1 -1
- package/dist/cjs/ssr.js.map +1 -1
- package/dist/cjs/useDeferred-2fAqMTwr.d.ts.map +1 -1
- package/dist/cjs/useRoute-D2vAzKWM.js.map +1 -1
- package/dist/cjs/useRouteEnter-CqO9sPPD.d.ts.map +1 -1
- package/dist/cjs/useRouterTransition-D5nwRENd.d.ts.map +1 -1
- package/dist/esm/Await-D2oCO8nH.d.mts.map +1 -1
- package/dist/esm/HttpStatusProvider-DfsVxZba.d.mts.map +1 -1
- package/dist/esm/{Link-uERoM4yk.mjs → Link-PQIMnFd0.mjs} +2 -2
- package/dist/esm/{Link-uERoM4yk.mjs.map → Link-PQIMnFd0.mjs.map} +1 -1
- package/dist/esm/RouterErrorBoundary-3UhUelqY.d.mts.map +1 -1
- package/dist/esm/{RouterProvider-DgHypQTJ.mjs → RouterProvider-DhZoi6au.mjs} +2 -2
- package/dist/esm/RouterProvider-DhZoi6au.mjs.map +1 -0
- 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/ink.mjs.map +1 -1
- package/dist/esm/legacy.mjs +1 -1
- package/dist/esm/useDeferred-2fAqMTwr.d.mts.map +1 -1
- package/dist/esm/useRouteEnter-CqO9sPPD.d.mts.map +1 -1
- package/dist/esm/useRouterTransition-PCwA_qj1.d.mts.map +1 -1
- package/package.json +7 -8
- package/dist/cjs/RouterProvider-CPsCmrRw.js.map +0 -1
- package/dist/esm/RouterProvider-DgHypQTJ.mjs.map +0 -1
- package/src/RouterProvider.tsx +0 -148
- package/src/components/Await.tsx +0 -46
- package/src/components/ClientOnly.tsx +0 -25
- package/src/components/HttpStatusCode.tsx +0 -66
- package/src/components/HttpStatusProvider.tsx +0 -26
- package/src/components/InkLink.tsx +0 -134
- package/src/components/InkRouterProvider.tsx +0 -9
- package/src/components/Link.tsx +0 -117
- package/src/components/RouterErrorBoundary.tsx +0 -69
- package/src/components/ServerOnly.tsx +0 -26
- package/src/components/Streamed.tsx +0 -30
- package/src/components/modern/RouteView/RouteView.tsx +0 -69
- package/src/components/modern/RouteView/components.tsx +0 -19
- package/src/components/modern/RouteView/helpers.tsx +0 -233
- package/src/components/modern/RouteView/index.ts +0 -8
- package/src/components/modern/RouteView/types.ts +0 -38
- package/src/constants.ts +0 -10
- package/src/context.ts +0 -14
- package/src/hooks/useDeferred.tsx +0 -34
- package/src/hooks/useIsActiveRoute.tsx +0 -47
- package/src/hooks/useNavigator.tsx +0 -15
- package/src/hooks/useRoute.tsx +0 -32
- package/src/hooks/useRouteEnter.tsx +0 -148
- package/src/hooks/useRouteExit.tsx +0 -159
- package/src/hooks/useRouteNode.tsx +0 -37
- package/src/hooks/useRouteUtils.tsx +0 -35
- package/src/hooks/useRouter.tsx +0 -15
- package/src/hooks/useRouterTransition.tsx +0 -17
- package/src/index.react-server.ts +0 -33
- package/src/index.ts +0 -61
- package/src/ink-types.ts +0 -25
- package/src/ink.ts +0 -30
- package/src/legacy.ssr.ts +0 -35
- package/src/legacy.ts +0 -35
- package/src/ssr.react-server.ts +0 -21
- package/src/ssr.ts +0 -43
- package/src/types.ts +0 -40
- package/src/utils/createHttpStatusSink.ts +0 -27
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
import { UNKNOWN_ROUTE } from "@real-router/core";
|
|
2
|
-
import { startsWithSegment } from "@real-router/route-utils";
|
|
3
|
-
import { Activity, Children, Fragment, Suspense, isValidElement } from "react";
|
|
4
|
-
|
|
5
|
-
import { Match, NotFound, Self } from "./components";
|
|
6
|
-
|
|
7
|
-
import type { MatchProps, NotFoundProps, SelfProps } from "./types";
|
|
8
|
-
import type { ReactElement, ReactNode } from "react";
|
|
9
|
-
|
|
10
|
-
interface FallbackSlots {
|
|
11
|
-
selfChildren: ReactNode;
|
|
12
|
-
selfFallback: ReactNode | undefined;
|
|
13
|
-
selfFound: boolean;
|
|
14
|
-
notFoundChildren: ReactNode;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Fixed keys used by appendFallback to distinguish the Self / NotFound
|
|
18
|
-
// render slots from user-supplied <Match> children. Match render slots key
|
|
19
|
-
// off `fullSegmentName` instead — these two are the only synthetic keys.
|
|
20
|
-
const SELF_KEY = "__route-view-self__";
|
|
21
|
-
const NOT_FOUND_KEY = "__route-view-not-found__";
|
|
22
|
-
|
|
23
|
-
function isSegmentMatch(
|
|
24
|
-
routeName: string,
|
|
25
|
-
fullSegmentName: string,
|
|
26
|
-
exact: boolean,
|
|
27
|
-
): boolean {
|
|
28
|
-
if (fullSegmentName === "") {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (exact) {
|
|
33
|
-
return routeName === fullSegmentName;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return startsWithSegment(routeName, fullSegmentName);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function collectElements(
|
|
40
|
-
children: ReactNode,
|
|
41
|
-
result: ReactElement[],
|
|
42
|
-
): void {
|
|
43
|
-
// Recurses into Fragment-like wrappers (anything that isn't Match / Self /
|
|
44
|
-
// NotFound) to flatten the slot tree. No explicit depth guard: typical
|
|
45
|
-
// RouteView shape is `<RouteView><Match/>...<NotFound/></RouteView>` —
|
|
46
|
-
// depth ≤ 3 in real apps. A pathological hand-written tree of N Fragments
|
|
47
|
-
// recurses N times; the call stack, not this function, is the bound.
|
|
48
|
-
//
|
|
49
|
-
// `Children.forEach` iterates without `Children.toArray`'s array allocation
|
|
50
|
-
// and per-child clone-with-synthetic-key step. We don't read child.key here
|
|
51
|
-
// (Match/Self/NotFound carry their own segment-derived keys further down),
|
|
52
|
-
// so the cheaper iterator is functionally equivalent.
|
|
53
|
-
// eslint-disable-next-line @eslint-react/no-children-for-each -- intentional: collectElements is a render-hot pipeline; toArray's array+key clone is wasteful here
|
|
54
|
-
Children.forEach(children, (child) => {
|
|
55
|
-
if (!isValidElement(child)) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
child.type === Match ||
|
|
61
|
-
child.type === Self ||
|
|
62
|
-
child.type === NotFound
|
|
63
|
-
) {
|
|
64
|
-
result.push(child);
|
|
65
|
-
} else {
|
|
66
|
-
collectElements(
|
|
67
|
-
(child.props as { readonly children: ReactNode }).children,
|
|
68
|
-
result,
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function renderSlotElement(
|
|
75
|
-
slotChildren: ReactNode,
|
|
76
|
-
key: string,
|
|
77
|
-
keepAlive: boolean,
|
|
78
|
-
mode: "visible" | "hidden",
|
|
79
|
-
fallback?: ReactNode,
|
|
80
|
-
): ReactElement {
|
|
81
|
-
const content =
|
|
82
|
-
fallback === undefined ? (
|
|
83
|
-
slotChildren
|
|
84
|
-
) : (
|
|
85
|
-
<Suspense fallback={fallback}>{slotChildren}</Suspense>
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
if (keepAlive) {
|
|
89
|
-
return (
|
|
90
|
-
<Activity mode={mode} key={key}>
|
|
91
|
-
{content}
|
|
92
|
-
</Activity>
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return <Fragment key={key}>{content}</Fragment>;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function recordFallback(child: ReactElement, slots: FallbackSlots): boolean {
|
|
100
|
-
if (child.type === NotFound) {
|
|
101
|
-
slots.notFoundChildren = (child.props as NotFoundProps).children;
|
|
102
|
-
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (child.type === Self) {
|
|
107
|
-
// First-wins: subsequent <Self> elements are ignored, mirroring NotFound.
|
|
108
|
-
if (!slots.selfFound) {
|
|
109
|
-
slots.selfChildren = (child.props as SelfProps).children;
|
|
110
|
-
slots.selfFallback = (child.props as SelfProps).fallback;
|
|
111
|
-
slots.selfFound = true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function processMatch(
|
|
121
|
-
child: ReactElement,
|
|
122
|
-
routeName: string,
|
|
123
|
-
nodeName: string,
|
|
124
|
-
hasBeenActivated: Set<string>,
|
|
125
|
-
alreadyActive: boolean,
|
|
126
|
-
): { rendered: ReactElement | null; matched: boolean } {
|
|
127
|
-
const matchProps = child.props as MatchProps;
|
|
128
|
-
const { segment, exact = false, keepAlive = false, fallback } = matchProps;
|
|
129
|
-
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
130
|
-
const isActive =
|
|
131
|
-
!alreadyActive && isSegmentMatch(routeName, fullSegmentName, exact);
|
|
132
|
-
|
|
133
|
-
if (isActive) {
|
|
134
|
-
hasBeenActivated.add(fullSegmentName);
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
rendered: renderSlotElement(
|
|
138
|
-
matchProps.children,
|
|
139
|
-
fullSegmentName,
|
|
140
|
-
keepAlive,
|
|
141
|
-
"visible",
|
|
142
|
-
fallback,
|
|
143
|
-
),
|
|
144
|
-
matched: true,
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (keepAlive && hasBeenActivated.has(fullSegmentName)) {
|
|
149
|
-
return {
|
|
150
|
-
rendered: renderSlotElement(
|
|
151
|
-
matchProps.children,
|
|
152
|
-
fullSegmentName,
|
|
153
|
-
keepAlive,
|
|
154
|
-
"hidden",
|
|
155
|
-
fallback,
|
|
156
|
-
),
|
|
157
|
-
matched: false,
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return { rendered: null, matched: false };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function appendFallback(
|
|
165
|
-
rendered: ReactElement[],
|
|
166
|
-
routeName: string,
|
|
167
|
-
nodeName: string,
|
|
168
|
-
slots: FallbackSlots,
|
|
169
|
-
): void {
|
|
170
|
-
if (slots.selfFound && routeName === nodeName) {
|
|
171
|
-
rendered.push(
|
|
172
|
-
renderSlotElement(
|
|
173
|
-
slots.selfChildren,
|
|
174
|
-
SELF_KEY,
|
|
175
|
-
false,
|
|
176
|
-
"visible",
|
|
177
|
-
slots.selfFallback,
|
|
178
|
-
),
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (routeName === UNKNOWN_ROUTE && slots.notFoundChildren !== null) {
|
|
185
|
-
rendered.push(
|
|
186
|
-
<Fragment key={NOT_FOUND_KEY}>{slots.notFoundChildren}</Fragment>,
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export function buildRenderList(
|
|
192
|
-
elements: ReactElement[],
|
|
193
|
-
routeName: string,
|
|
194
|
-
nodeName: string,
|
|
195
|
-
hasBeenActivated: Set<string>,
|
|
196
|
-
): { rendered: ReactElement[]; activeMatchFound: boolean } {
|
|
197
|
-
const slots: FallbackSlots = {
|
|
198
|
-
selfChildren: null,
|
|
199
|
-
selfFallback: undefined,
|
|
200
|
-
selfFound: false,
|
|
201
|
-
notFoundChildren: null,
|
|
202
|
-
};
|
|
203
|
-
let activeMatchFound = false;
|
|
204
|
-
const rendered: ReactElement[] = [];
|
|
205
|
-
|
|
206
|
-
for (const child of elements) {
|
|
207
|
-
if (recordFallback(child, slots)) {
|
|
208
|
-
continue;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const result = processMatch(
|
|
212
|
-
child,
|
|
213
|
-
routeName,
|
|
214
|
-
nodeName,
|
|
215
|
-
hasBeenActivated,
|
|
216
|
-
activeMatchFound,
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
if (result.matched) {
|
|
220
|
-
activeMatchFound = true;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (result.rendered !== null) {
|
|
224
|
-
rendered.push(result.rendered);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (!activeMatchFound) {
|
|
229
|
-
appendFallback(rendered, routeName, nodeName, slots);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return { rendered, activeMatchFound };
|
|
233
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { ReactNode } from "react";
|
|
2
|
-
|
|
3
|
-
export interface RouteViewProps {
|
|
4
|
-
/** Route tree node name to subscribe to. "" for root. */
|
|
5
|
-
readonly nodeName: string;
|
|
6
|
-
/** <RouteView.Match>, <RouteView.Self>, and <RouteView.NotFound> elements. */
|
|
7
|
-
readonly children: ReactNode;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface MatchProps {
|
|
11
|
-
/** Route segment to match against. */
|
|
12
|
-
readonly segment: string;
|
|
13
|
-
/** Exact match only (no descendants). Defaults to false. */
|
|
14
|
-
readonly exact?: boolean;
|
|
15
|
-
/** Preserve component state when deactivated (React Activity). Defaults to false. */
|
|
16
|
-
readonly keepAlive?: boolean;
|
|
17
|
-
/** Fallback content to show while children are suspended. */
|
|
18
|
-
readonly fallback?: ReactNode;
|
|
19
|
-
/** Content to render when matched. */
|
|
20
|
-
readonly children: ReactNode;
|
|
21
|
-
}
|
|
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
|
-
|
|
35
|
-
export interface NotFoundProps {
|
|
36
|
-
/** Content to render on UNKNOWN_ROUTE. */
|
|
37
|
-
readonly children: ReactNode;
|
|
38
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const EMPTY_PARAMS = Object.freeze({});
|
|
2
|
-
|
|
3
|
-
export const EMPTY_OPTIONS = Object.freeze({});
|
|
4
|
-
|
|
5
|
-
// Singleton forever-pending promise — module-scope so `useDeferred(unknownKey)`
|
|
6
|
-
// returns a stable reference across calls (Suspense boundary doesn't retry).
|
|
7
|
-
export const NEVER_PROMISE = new Promise<never>(() => {
|
|
8
|
-
// Intentionally never resolves — surfaces a forever-pending Suspense boundary
|
|
9
|
-
// when a key is requested that the loader never declared.
|
|
10
|
-
});
|
package/src/context.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { createContext } from "react";
|
|
2
|
-
|
|
3
|
-
import type { RouteContext as RouteContextType } from "./types";
|
|
4
|
-
import type { Router, Navigator } from "@real-router/core";
|
|
5
|
-
|
|
6
|
-
// All three contexts use the `<T | null>` shape with a `null` default. Hooks
|
|
7
|
-
// (useRouter / useRoute / useNavigator) treat a `null` read as
|
|
8
|
-
// "RouterProvider missing" and throw — this surfaces wiring mistakes loudly
|
|
9
|
-
// instead of letting consumers chase an undefined router instance.
|
|
10
|
-
export const RouteContext = createContext<RouteContextType | null>(null);
|
|
11
|
-
|
|
12
|
-
export const RouterContext = createContext<Router | null>(null);
|
|
13
|
-
|
|
14
|
-
export const NavigatorContext = createContext<Navigator | null>(null);
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { NEVER_PROMISE } from "../constants";
|
|
2
|
-
import { useRoute } from "./useRoute";
|
|
3
|
-
|
|
4
|
-
interface DeferredContext {
|
|
5
|
-
ssrDataDeferred?: Record<string, Promise<unknown>>;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Read a deferred promise published by `defer({ deferred: { <key>: Promise } })`
|
|
10
|
-
* inside an SSR data loader.
|
|
11
|
-
*
|
|
12
|
-
* - **Server render**: returns the actual loader-returned promise. Combine
|
|
13
|
-
* with `<Suspense>` + `use(promise)` for native streaming via React 19's
|
|
14
|
-
* `renderToReadableStream`.
|
|
15
|
-
* - **Post-hydration**: returns a registry-backed promise; the inline
|
|
16
|
-
* `<script>__rrDefer__("key", json)</script>` tags emitted by the server
|
|
17
|
-
* stream resolve it. `use()` returns synchronously once the registry
|
|
18
|
-
* settles.
|
|
19
|
-
* - **Unknown key**: returns a never-resolving promise — Suspense boundary
|
|
20
|
-
* stays in fallback. This surfaces consumer/loader key drift as a visible
|
|
21
|
-
* loading state instead of a silent runtime error.
|
|
22
|
-
*
|
|
23
|
-
* The hook subscribes to `RouteContext`, so it re-runs on every navigation.
|
|
24
|
-
* Promise reference identity is stable across renders within the same
|
|
25
|
-
* navigation — `use()` will not re-suspend on rerenders.
|
|
26
|
-
*/
|
|
27
|
-
export function useDeferred<T = unknown>(key: string): Promise<T> {
|
|
28
|
-
const { route } = useRoute();
|
|
29
|
-
const context = route.context as DeferredContext;
|
|
30
|
-
const deferred = context.ssrDataDeferred;
|
|
31
|
-
const promise = deferred?.[key];
|
|
32
|
-
|
|
33
|
-
return (promise ?? NEVER_PROMISE) as Promise<T>;
|
|
34
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { createActiveRouteSource } from "@real-router/sources";
|
|
2
|
-
import { useMemo, useSyncExternalStore } from "react";
|
|
3
|
-
|
|
4
|
-
import { useRouter } from "./useRouter";
|
|
5
|
-
|
|
6
|
-
import type { Params } from "@real-router/core";
|
|
7
|
-
|
|
8
|
-
export function useIsActiveRoute(
|
|
9
|
-
routeName: string,
|
|
10
|
-
params?: Params,
|
|
11
|
-
strict = false,
|
|
12
|
-
ignoreQueryParams = true,
|
|
13
|
-
hash?: string,
|
|
14
|
-
): boolean {
|
|
15
|
-
const router = useRouter();
|
|
16
|
-
|
|
17
|
-
// createActiveRouteSource is per-router + canonical-args cached in
|
|
18
|
-
// @real-router/sources, so passing params by reference is safe — equivalent
|
|
19
|
-
// param shapes hit the same cache entry regardless of key order. The
|
|
20
|
-
// `hash` argument (#532) is part of the cache key when defined: a Link
|
|
21
|
-
// pointing to `/settings#account` shares its source only with other
|
|
22
|
-
// consumers using the same routeName + params + hash.
|
|
23
|
-
//
|
|
24
|
-
// The useMemo wrap skips `canonicalJson(params)` + cache lookup on every
|
|
25
|
-
// render when all primitive deps and the `params` reference are stable —
|
|
26
|
-
// the common case once memo()+shallowEqual has bailed out further up
|
|
27
|
-
// (or when the parent re-renders for a non-Link reason). For inline
|
|
28
|
-
// `params={{id:1}}` the dep changes per render and the lookup still
|
|
29
|
-
// runs, but that path was already the slow path before this memo.
|
|
30
|
-
// exactOptionalPropertyTypes forbids `{ hash: undefined }` literally, so
|
|
31
|
-
// we conditionally spread the key only when the caller passed a value.
|
|
32
|
-
const store = useMemo(
|
|
33
|
-
() =>
|
|
34
|
-
createActiveRouteSource(router, routeName, params, {
|
|
35
|
-
strict,
|
|
36
|
-
ignoreQueryParams,
|
|
37
|
-
...(hash !== undefined && { hash }),
|
|
38
|
-
}),
|
|
39
|
-
[router, routeName, params, strict, ignoreQueryParams, hash],
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
return useSyncExternalStore(
|
|
43
|
-
store.subscribe,
|
|
44
|
-
store.getSnapshot,
|
|
45
|
-
store.getSnapshot,
|
|
46
|
-
);
|
|
47
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
|
|
3
|
-
import { NavigatorContext } from "../context";
|
|
4
|
-
|
|
5
|
-
import type { Navigator } from "@real-router/core";
|
|
6
|
-
|
|
7
|
-
export const useNavigator = (): Navigator => {
|
|
8
|
-
const navigator = useContext(NavigatorContext);
|
|
9
|
-
|
|
10
|
-
if (!navigator) {
|
|
11
|
-
throw new Error("useNavigator must be used within a RouterProvider");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return navigator;
|
|
15
|
-
};
|
package/src/hooks/useRoute.tsx
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { useContext } from "react";
|
|
2
|
-
|
|
3
|
-
import { RouteContext } from "../context";
|
|
4
|
-
|
|
5
|
-
import type { RouteContext as RouteContextType } from "../types";
|
|
6
|
-
import type { Params, State } from "@real-router/core";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Return shape of `useRoute<P>()` — the context with `route` narrowed to
|
|
10
|
-
* `State<P>` (non-nullable). Promoting the intersection to a named alias
|
|
11
|
-
* keeps the function signature and the cast site in sync.
|
|
12
|
-
*/
|
|
13
|
-
type RouteHookResult<P extends Params = Params> = Omit<
|
|
14
|
-
RouteContextType<P>,
|
|
15
|
-
"route"
|
|
16
|
-
> & { route: State<P> };
|
|
17
|
-
|
|
18
|
-
export const useRoute = <P extends Params = Params>(): RouteHookResult<P> => {
|
|
19
|
-
const routeContext = useContext(RouteContext);
|
|
20
|
-
|
|
21
|
-
if (!routeContext) {
|
|
22
|
-
throw new Error("useRoute must be used within a RouterProvider");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (!routeContext.route) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
"useRoute called with no active route. Did you forget to await router.start() before rendering, or is the router stopped/disposed?",
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return routeContext as RouteHookResult<P>;
|
|
32
|
-
};
|
|
@@ -1,148 +0,0 @@
|
|
|
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
|
-
// - **Skip-initial**: `state.transition.from` is undefined only
|
|
125
|
-
// for the very first state committed by `router.start()`.
|
|
126
|
-
// - **Skip-same-route**: query-only navigations have
|
|
127
|
-
// `transition.from === route.name`. Opt-out via
|
|
128
|
-
// `skipSameRoute: false`.
|
|
129
|
-
// - **StrictMode dedupe**: same `route` ref between effect
|
|
130
|
-
// cleanup + re-run in dev's strict pass. Not testable from
|
|
131
|
-
// vitest (`NODE_ENV === "test"` disables React's strict-mode
|
|
132
|
-
// double-run), so v8-ignored.
|
|
133
|
-
if (!route.transition.from) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if (skipSameRoute && route.transition.from === route.name) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
/* v8 ignore start */
|
|
140
|
-
if (lastHandledRouteRef.current === route || !previousRoute) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
/* v8 ignore stop */
|
|
144
|
-
|
|
145
|
-
lastHandledRouteRef.current = route;
|
|
146
|
-
handlerRef.current({ route, previousRoute });
|
|
147
|
-
}, [route, previousRoute, skipSameRoute]);
|
|
148
|
-
}
|