clear-react-router 2.1.4 → 2.1.5
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 +0 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +55 -55
- package/dist/runtime/invalidate.d.ts +1 -1
- package/dist/types.d.ts +10 -9
- package/dist/utils/utils.d.ts +6 -4
- package/package.json +2 -2
- /package/dist/{utils → creators}/createRouter.d.ts +0 -0
- /package/dist/{utils → creators}/createRouterInstance.d.ts +0 -0
- /package/dist/{utils → runtime}/revalidateCache.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
[](https://www.npmjs.com/package/clear-react-router)
|
|
2
|
-
|
|
3
1
|
# Clear Router
|
|
4
2
|
|
|
5
3
|
A lightweight, type-safe routing library for client side React applications with nested routes, data loading, navigation blocking, prefetching, and route actions.
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,6 @@ export { useIsDataLoading } from './hooks/useIsDataLoading';
|
|
|
11
11
|
export { useRouterContext } from './hooks/useRouterContext';
|
|
12
12
|
export { useSearchParams } from './hooks/useSearchParams';
|
|
13
13
|
export { useSubmitAction } from './hooks/useSubmitAction';
|
|
14
|
-
export { createRouter } from './
|
|
14
|
+
export { createRouter } from './creators/createRouter';
|
|
15
15
|
export { lazy } from './utils/lazy';
|
|
16
16
|
export type { RouteItem, BlockerState, Location, RouterProps, ElementProps } from './types';
|
package/dist/index.js
CHANGED
|
@@ -126,12 +126,9 @@ var createIsCacheItemFresh = (loaderMap) => (path) => {
|
|
|
126
126
|
};
|
|
127
127
|
//#endregion
|
|
128
128
|
//#region utils/utils.ts
|
|
129
|
-
var
|
|
130
|
-
const { routeItem: stateItem, location: { pathname: statePathname } } = router.state.routeItemDataState.getState();
|
|
131
|
-
const routeItem = nextItem || stateItem;
|
|
132
|
-
const pathname = nextPathname || statePathname;
|
|
129
|
+
var getParams = (location, routeItem) => {
|
|
133
130
|
if (!routeItem) return {};
|
|
134
|
-
const pathnameSegments = pathname.split("/");
|
|
131
|
+
const pathnameSegments = location.pathname.split("/");
|
|
135
132
|
return routeItem.pattern.split("/").reduce((acc, segment, index) => {
|
|
136
133
|
if (segment.startsWith(":")) acc[segment.slice(1)] = pathnameSegments[index];
|
|
137
134
|
return acc;
|
|
@@ -152,11 +149,9 @@ var isMobile = () => {
|
|
|
152
149
|
return hasCoarsePointer && isSmallScreen;
|
|
153
150
|
};
|
|
154
151
|
var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
155
|
-
var getPartialLoaderArgs = (location) => {
|
|
156
|
-
const params = getParamsObject();
|
|
157
|
-
const { contextState } = router.state;
|
|
152
|
+
var getPartialLoaderArgs = (contextState, location, routeItem) => {
|
|
158
153
|
return {
|
|
159
|
-
params,
|
|
154
|
+
params: getParams(location, routeItem),
|
|
160
155
|
context: contextState.getState(),
|
|
161
156
|
setContext: contextState.setState,
|
|
162
157
|
searchParams: Object.fromEntries(new URLSearchParams(location.search).entries()),
|
|
@@ -164,8 +159,7 @@ var getPartialLoaderArgs = (location) => {
|
|
|
164
159
|
};
|
|
165
160
|
};
|
|
166
161
|
var isVerticalScroll = (el) => !!el && el.scrollHeight > el.clientHeight;
|
|
167
|
-
var updateScrollMap = () => {
|
|
168
|
-
const { routeItemDataState, scrollMapState } = router.state;
|
|
162
|
+
var updateScrollMap = (routeItemDataState, scrollMapState) => {
|
|
169
163
|
const { routeItem, location: { pathname } } = routeItemDataState.getState();
|
|
170
164
|
scrollMapState.setState((prevState) => {
|
|
171
165
|
if (Array.isArray(routeItem?.scrollRestoration)) return {
|
|
@@ -190,6 +184,28 @@ var updateScrollMap = () => {
|
|
|
190
184
|
};
|
|
191
185
|
});
|
|
192
186
|
};
|
|
187
|
+
var restoreScroll = (scrollMap, pathname, behavior) => {
|
|
188
|
+
scrollMap[pathname].forEach(([key, scrollPosition]) => {
|
|
189
|
+
if (key === "__window_top__" || key === "__window_left__") {
|
|
190
|
+
requestAnimationFrame(() => {
|
|
191
|
+
window.scrollTo({
|
|
192
|
+
[key === "__window_top__" ? "top" : "left"]: scrollPosition,
|
|
193
|
+
behavior
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const element = document.getElementById(key);
|
|
199
|
+
if (!element) return;
|
|
200
|
+
const axis = isVerticalScroll(element) ? "top" : "left";
|
|
201
|
+
requestAnimationFrame(() => {
|
|
202
|
+
element.scrollTo({
|
|
203
|
+
[axis]: scrollPosition,
|
|
204
|
+
behavior
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
};
|
|
193
209
|
var findRoute = (pathname, includeAll) => {
|
|
194
210
|
if (includeAll) return routerConfig.routes.find((el) => el.path === "*" || comparePaths(el, pathname));
|
|
195
211
|
return routerConfig.routes.find((el) => comparePaths(el, pathname));
|
|
@@ -200,7 +216,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
200
216
|
let navigationSeq = 0;
|
|
201
217
|
let interval = 0;
|
|
202
218
|
let abortController = null;
|
|
203
|
-
const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState } = routerState;
|
|
219
|
+
const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState, scrollMapState } = routerState;
|
|
204
220
|
const navigationExecutor = createCommitState(routerState);
|
|
205
221
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
206
222
|
const createSignal = () => {
|
|
@@ -218,7 +234,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
218
234
|
const nextItem = findRoute(location.pathname, true);
|
|
219
235
|
return {
|
|
220
236
|
nextItem,
|
|
221
|
-
params:
|
|
237
|
+
params: getParams(location, nextItem)
|
|
222
238
|
};
|
|
223
239
|
};
|
|
224
240
|
const beforeLoad = async (routeItem, nextLocation) => {
|
|
@@ -228,7 +244,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
228
244
|
try {
|
|
229
245
|
await loaderFn({
|
|
230
246
|
redirect,
|
|
231
|
-
...getPartialLoaderArgs(nextLocation)
|
|
247
|
+
...getPartialLoaderArgs(contextState, nextLocation, routeItem)
|
|
232
248
|
});
|
|
233
249
|
loaderStateRef.set((prev) => ({
|
|
234
250
|
...prev,
|
|
@@ -245,7 +261,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
245
261
|
if (routeItem?.beforeLoad) await runBeforeLoad(routeItem?.beforeLoad);
|
|
246
262
|
};
|
|
247
263
|
const prepareNavigation = (routeItem, location) => {
|
|
248
|
-
updateScrollMap();
|
|
264
|
+
updateScrollMap(routeItemDataState, scrollMapState);
|
|
249
265
|
const path = getPath(location);
|
|
250
266
|
if (routeItem?.optimistic && loaderMap.has(path)) {
|
|
251
267
|
routeItemDataState.setState({
|
|
@@ -322,7 +338,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
322
338
|
//#endregion
|
|
323
339
|
//#region runtime/invalidate.ts
|
|
324
340
|
var redirect = Promise.resolve;
|
|
325
|
-
var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalidateCache) => {
|
|
341
|
+
var createInvalidate = ({ routeItemDataState, loaderState, loaderMap, contextState }, revalidateCache) => {
|
|
326
342
|
const invalidatePath = async (routeItem, pathname, options) => {
|
|
327
343
|
const routePathname = routeItemDataState.getState().location.pathname;
|
|
328
344
|
loaderMap.delete(pathname);
|
|
@@ -334,7 +350,7 @@ var createInvalidate = ({ routeItemDataState, loaderState, loaderMap }, revalida
|
|
|
334
350
|
if (routeItem?.beforeLoad && options?.withBeforeLoad) try {
|
|
335
351
|
await routeItem.beforeLoad({
|
|
336
352
|
redirect,
|
|
337
|
-
...getPartialLoaderArgs(location)
|
|
353
|
+
...getPartialLoaderArgs(contextState, location, routeItem)
|
|
338
354
|
});
|
|
339
355
|
loaderState.setState((prev) => ({
|
|
340
356
|
...prev,
|
|
@@ -392,7 +408,7 @@ var createPrefetch = ({ routeItemDataState }, revalidateCache) => async (locatio
|
|
|
392
408
|
}
|
|
393
409
|
};
|
|
394
410
|
//#endregion
|
|
395
|
-
//#region
|
|
411
|
+
//#region runtime/revalidateCache.ts
|
|
396
412
|
var isObjectRetry = (arg) => typeof arg === "object";
|
|
397
413
|
var createRetry = (arg) => {
|
|
398
414
|
if (arg === void 0) return null;
|
|
@@ -411,7 +427,7 @@ var getRetry = (routeItem) => {
|
|
|
411
427
|
};
|
|
412
428
|
};
|
|
413
429
|
var createRevalidateCache = (routerState) => {
|
|
414
|
-
const { loaderMap, loadingPromises } = routerState;
|
|
430
|
+
const { loaderMap, loadingPromises, contextState } = routerState;
|
|
415
431
|
const evict = () => {
|
|
416
432
|
if (loaderMap.size <= routerConfig.maxCacheSize) return;
|
|
417
433
|
const oldestKey = loaderMap.keys().next().value;
|
|
@@ -450,7 +466,7 @@ var createRevalidateCache = (routerState) => {
|
|
|
450
466
|
const effectiveSignal = signal ?? new AbortController().signal;
|
|
451
467
|
try {
|
|
452
468
|
const result = await routeItem?.loader({
|
|
453
|
-
...getPartialLoaderArgs(location),
|
|
469
|
+
...getPartialLoaderArgs(contextState, location, routeItem),
|
|
454
470
|
signal: effectiveSignal
|
|
455
471
|
});
|
|
456
472
|
loaderMap.set(path, {
|
|
@@ -513,7 +529,7 @@ var Cell = class {
|
|
|
513
529
|
}
|
|
514
530
|
};
|
|
515
531
|
//#endregion
|
|
516
|
-
//#region
|
|
532
|
+
//#region creators/createRouterInstance.ts
|
|
517
533
|
var createRouterInstance = () => {
|
|
518
534
|
const routerState = {
|
|
519
535
|
routeItemDataState: create({
|
|
@@ -536,13 +552,13 @@ var createRouterInstance = () => {
|
|
|
536
552
|
const invalidate = createInvalidate(routerState, revalidateCache);
|
|
537
553
|
const navigate = createNavigate(routerState, revalidateCache);
|
|
538
554
|
const prefetch = createPrefetch(routerState, revalidateCache);
|
|
539
|
-
const
|
|
555
|
+
const getCurrentAction = (actionKey) => {
|
|
540
556
|
const { routeItem, location } = routerState.routeItemDataState.getState();
|
|
541
557
|
if (!routeItem) throw new Error("Route not found");
|
|
542
558
|
if (!routeItem.actions) throw new Error("Route action creator not found");
|
|
543
559
|
const context = routerState.contextState.getState();
|
|
544
560
|
const setContext = routerState.contextState.setState;
|
|
545
|
-
const params =
|
|
561
|
+
const params = getParams(location, routeItem);
|
|
546
562
|
const searchParams = Object.fromEntries(new URLSearchParams(location.search).entries());
|
|
547
563
|
const action = routeItem.actions({
|
|
548
564
|
context,
|
|
@@ -568,7 +584,10 @@ var createRouterInstance = () => {
|
|
|
568
584
|
useStatus: () => useGlobalState(routerState.statusState),
|
|
569
585
|
useContextState: () => useGlobalState(routerState.contextState),
|
|
570
586
|
useLoaderState: () => useGlobalState(routerState.loaderState)[0],
|
|
571
|
-
useParams: () =>
|
|
587
|
+
useParams: () => {
|
|
588
|
+
const { routeItem, location } = routerState.routeItemDataState.getState();
|
|
589
|
+
return getParams(location, routeItem);
|
|
590
|
+
},
|
|
572
591
|
useNavigate: () => {
|
|
573
592
|
const { blockedRouteState } = routerState;
|
|
574
593
|
const { location } = routerState.routeItemDataState.getState();
|
|
@@ -588,7 +607,7 @@ var createRouterInstance = () => {
|
|
|
588
607
|
};
|
|
589
608
|
},
|
|
590
609
|
useAction: (action, options = {}) => {
|
|
591
|
-
const currentAction =
|
|
610
|
+
const currentAction = getCurrentAction(action);
|
|
592
611
|
return async (formData) => {
|
|
593
612
|
try {
|
|
594
613
|
const data = await currentAction(formData);
|
|
@@ -606,6 +625,12 @@ var createRouterInstance = () => {
|
|
|
606
625
|
};
|
|
607
626
|
}
|
|
608
627
|
};
|
|
628
|
+
},
|
|
629
|
+
useRestoreScroll: (restorationBehavior) => {
|
|
630
|
+
const { routeItem, location: { pathname } } = routerState.routeItemDataState.getState();
|
|
631
|
+
const scrollMap = routerState.scrollMapState.getState();
|
|
632
|
+
if (!routeItem || routeItem.scrollRestoration === false || !scrollMap[pathname]) return;
|
|
633
|
+
return () => restoreScroll(scrollMap, pathname, routeItem.scrollRestorationBehavior ?? restorationBehavior);
|
|
609
634
|
}
|
|
610
635
|
}
|
|
611
636
|
};
|
|
@@ -682,36 +707,11 @@ var useApplyCustomAnimation = (animationDuration) => {
|
|
|
682
707
|
//#endregion
|
|
683
708
|
//#region hooks/usePreserveScroll.ts
|
|
684
709
|
var usePreserveScroll = (restorationBehavior) => {
|
|
685
|
-
const {
|
|
686
|
-
const
|
|
710
|
+
const { useRestoreScroll } = router.hooks;
|
|
711
|
+
const restoreScroll = useRestoreScroll(restorationBehavior);
|
|
687
712
|
useEffect(() => {
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
scrollMap[pathname].forEach(([key, scrollPosition]) => {
|
|
691
|
-
if (key === "__window_top__" || key === "__window_left__") {
|
|
692
|
-
requestAnimationFrame(() => {
|
|
693
|
-
window.scrollTo({
|
|
694
|
-
[key === "__window_top__" ? "top" : "left"]: scrollPosition,
|
|
695
|
-
behavior
|
|
696
|
-
});
|
|
697
|
-
});
|
|
698
|
-
return;
|
|
699
|
-
}
|
|
700
|
-
const element = document.getElementById(key);
|
|
701
|
-
if (!element) return;
|
|
702
|
-
const axis = isVerticalScroll(element) ? "top" : "left";
|
|
703
|
-
requestAnimationFrame(() => {
|
|
704
|
-
element.scrollTo({
|
|
705
|
-
[axis]: scrollPosition,
|
|
706
|
-
behavior
|
|
707
|
-
});
|
|
708
|
-
});
|
|
709
|
-
});
|
|
710
|
-
}, [
|
|
711
|
-
pathname,
|
|
712
|
-
routeItem?.scrollRestoration,
|
|
713
|
-
scrollMap
|
|
714
|
-
]);
|
|
713
|
+
restoreScroll?.();
|
|
714
|
+
}, [restoreScroll]);
|
|
715
715
|
};
|
|
716
716
|
//#endregion
|
|
717
717
|
//#region hooks/useSetRouterConfig.ts
|
|
@@ -1093,7 +1093,7 @@ var createLazyComponent = (importFn, fallback) => {
|
|
|
1093
1093
|
//#region types.ts
|
|
1094
1094
|
var LAZY_MARKER = Symbol("clear-router-lazy");
|
|
1095
1095
|
//#endregion
|
|
1096
|
-
//#region
|
|
1096
|
+
//#region creators/createRouter.ts
|
|
1097
1097
|
var isLazy = (el) => typeof el.element === "object" && el.element !== null && LAZY_MARKER in el.element;
|
|
1098
1098
|
var parseClientRouteItem = (el, parentPattern = "") => {
|
|
1099
1099
|
const pattern = `${parentPattern}/${el.path}`.replace(/\/+/g, "/");
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { type InvalidateOptions, InvalidateResult, RevalidateCache, RouterState } from '../types';
|
|
2
|
-
export declare const createInvalidate: ({ routeItemDataState, loaderState, loaderMap }: RouterState, revalidateCache: RevalidateCache) => (pathList?: string | string[], options?: InvalidateOptions) => Promise<InvalidateResult[]>;
|
|
2
|
+
export declare const createInvalidate: ({ routeItemDataState, loaderState, loaderMap, contextState }: RouterState, revalidateCache: RevalidateCache) => (pathList?: string | string[], options?: InvalidateOptions) => Promise<InvalidateResult[]>;
|
package/dist/types.d.ts
CHANGED
|
@@ -135,21 +135,22 @@ export type RouterType = {
|
|
|
135
135
|
prefetch(location: Location): Promise<void>;
|
|
136
136
|
};
|
|
137
137
|
hooks: {
|
|
138
|
-
useBlockedRoute
|
|
138
|
+
useBlockedRoute(): ReturnType<typeof useGlobalState<{
|
|
139
139
|
from: string;
|
|
140
140
|
to: string;
|
|
141
141
|
}>>;
|
|
142
|
-
useRouteItemData
|
|
143
|
-
useScrollMap
|
|
144
|
-
useStatus
|
|
145
|
-
useContextState
|
|
146
|
-
useLoaderState
|
|
147
|
-
useParams
|
|
148
|
-
useNavigate
|
|
149
|
-
useAction
|
|
142
|
+
useRouteItemData(): ReturnType<typeof useGlobalState<RouteItemData>>;
|
|
143
|
+
useScrollMap(): ReturnType<typeof useGlobalState<ScrollMap>>;
|
|
144
|
+
useStatus(): ReturnType<typeof useGlobalState<Status>>;
|
|
145
|
+
useContextState(): ReturnType<typeof useGlobalState<Record<string, unknown>>>;
|
|
146
|
+
useLoaderState<T>(): LoaderState<T>;
|
|
147
|
+
useParams<T>(): T;
|
|
148
|
+
useNavigate(): (arg: Location | string | -1) => Promise<void>;
|
|
149
|
+
useAction(action: string, options?: Options): (arg: FormData) => Promise<{
|
|
150
150
|
data: unknown;
|
|
151
151
|
error: Error | null;
|
|
152
152
|
}>;
|
|
153
|
+
useRestoreScroll(restorationBehavior: ScrollRestorationBehavior): (() => void) | undefined;
|
|
153
154
|
};
|
|
154
155
|
};
|
|
155
156
|
export type InvalidateOptions = {
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { Location, RouteItem } from '../types';
|
|
2
|
-
|
|
1
|
+
import { Location, RouteItem, RouteItemData, RouterState, ScrollMap, ScrollRestorationBehavior } from '../types';
|
|
2
|
+
import { Store } from '../create';
|
|
3
|
+
export declare const getParams: (location: Location, routeItem?: RouteItem) => Record<string, string>;
|
|
3
4
|
export declare const parseWindowLocation: (location: typeof window.location) => Location;
|
|
4
5
|
export declare const comparePaths: (route: RouteItem, pathname: string) => boolean;
|
|
5
6
|
export declare const isMobile: () => boolean;
|
|
6
7
|
export declare const sleep: (ms: number) => Promise<unknown>;
|
|
7
|
-
export declare const getPartialLoaderArgs: (location: Location) => {
|
|
8
|
+
export declare const getPartialLoaderArgs: (contextState: RouterState["contextState"], location: Location, routeItem?: RouteItem) => {
|
|
8
9
|
params: Record<string, string>;
|
|
9
10
|
context: Record<string, unknown>;
|
|
10
11
|
setContext: (action: Record<string, unknown> | ((prevState: Record<string, unknown>) => Record<string, unknown>)) => void;
|
|
@@ -12,4 +13,5 @@ export declare const getPartialLoaderArgs: (location: Location) => {
|
|
|
12
13
|
location: Location;
|
|
13
14
|
};
|
|
14
15
|
export declare const isVerticalScroll: (el: Element | null) => boolean;
|
|
15
|
-
export declare const updateScrollMap: () => void;
|
|
16
|
+
export declare const updateScrollMap: (routeItemDataState: Store<RouteItemData>, scrollMapState: Store<ScrollMap>) => void;
|
|
17
|
+
export declare const restoreScroll: (scrollMap: ScrollMap, pathname: string, behavior: ScrollRestorationBehavior) => void;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clear-react-router",
|
|
3
|
-
"version": "2.1.
|
|
4
|
-
"description": "A lightweight
|
|
3
|
+
"version": "2.1.5",
|
|
4
|
+
"description": "A lightweight routing library for React applications",
|
|
5
5
|
"author": "Andrew Bubnov",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "vite build && tsc --emitDeclarationOnly"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|