clear-react-router 2.1.6 → 2.1.7
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/index.js +26 -26
- package/dist/types.d.ts +1 -3
- package/dist/utils/commitState.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -30,13 +30,13 @@ var useGlobalState = ({ subscribe, getState, setState }) => {
|
|
|
30
30
|
};
|
|
31
31
|
//#endregion
|
|
32
32
|
//#region utils/commitState.ts
|
|
33
|
-
var createCommitState = ({
|
|
34
|
-
|
|
33
|
+
var createCommitState = ({ loaderStateRef, loaderState, routeItemDataState }) => (nextLocation, routeItem) => {
|
|
34
|
+
const isError = loaderStateRef.value.loaderError || loaderStateRef.value.beforeLoadError;
|
|
35
|
+
routeItemDataState.setState({
|
|
35
36
|
routeItem,
|
|
36
|
-
location: nextLocation
|
|
37
|
+
location: nextLocation,
|
|
38
|
+
status: isError ? "error" : "active"
|
|
37
39
|
});
|
|
38
|
-
const isError = loaderStateRef.value.loaderError || loaderStateRef.value.beforeLoadError;
|
|
39
|
-
statusState.setState(isError ? "error" : "active");
|
|
40
40
|
loaderState.setState(loaderStateRef.value);
|
|
41
41
|
if ((nextLocation.search ? `${nextLocation.pathname}${nextLocation.search}` : nextLocation.pathname) === window.location.pathname + window.location.search) return;
|
|
42
42
|
history.pushState(null, "", `${nextLocation.pathname}${nextLocation.search}`);
|
|
@@ -217,7 +217,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
217
217
|
let navigationSeq = 0;
|
|
218
218
|
let interval = 0;
|
|
219
219
|
let abortController = null;
|
|
220
|
-
const { loaderState, loaderStateRef,
|
|
220
|
+
const { loaderState, loaderStateRef, contextState, loaderMap, routeItemDataState, scrollMapState } = routerState;
|
|
221
221
|
const commitState = createCommitState(routerState);
|
|
222
222
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
223
223
|
const createSignal = () => {
|
|
@@ -263,18 +263,22 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
263
263
|
};
|
|
264
264
|
const prepareNavigation = (routeItem, location) => {
|
|
265
265
|
updateScrollMap(routeItemDataState, scrollMapState);
|
|
266
|
-
if (routeItem?.loader) routeItemDataState.setState({
|
|
267
|
-
routeItem,
|
|
268
|
-
location
|
|
269
|
-
});
|
|
270
266
|
const path = getPath(location);
|
|
271
267
|
if (routeItem?.optimistic && loaderMap.has(path)) {
|
|
272
|
-
|
|
268
|
+
commitNavigation(() => routeItemDataState.setState({
|
|
269
|
+
routeItem,
|
|
270
|
+
location,
|
|
271
|
+
status: "optimistic"
|
|
272
|
+
}));
|
|
273
273
|
const currentLoaderState = loaderMap.get(path)?.state;
|
|
274
274
|
if (currentLoaderState) loaderState.setState(currentLoaderState);
|
|
275
275
|
return;
|
|
276
276
|
}
|
|
277
|
-
if (routeItem?.loader && !isCacheItemFresh(path))
|
|
277
|
+
if (routeItem?.loader && !isCacheItemFresh(path)) commitNavigation(() => routeItemDataState.setState({
|
|
278
|
+
routeItem,
|
|
279
|
+
location,
|
|
280
|
+
status: "pending"
|
|
281
|
+
}));
|
|
278
282
|
};
|
|
279
283
|
const polling = (routeItem, nextLocation) => {
|
|
280
284
|
if (!routeItem?.pollingInterval) return;
|
|
@@ -534,9 +538,9 @@ var createRouterInstance = () => {
|
|
|
534
538
|
const routerState = {
|
|
535
539
|
routeItemDataState: create({
|
|
536
540
|
routeItem: void 0,
|
|
537
|
-
location: {}
|
|
541
|
+
location: {},
|
|
542
|
+
status: "idle"
|
|
538
543
|
}),
|
|
539
|
-
statusState: create("idle"),
|
|
540
544
|
scrollMapState: create({}),
|
|
541
545
|
contextState: create({}),
|
|
542
546
|
blockedRouteState: create({
|
|
@@ -581,7 +585,6 @@ var createRouterInstance = () => {
|
|
|
581
585
|
useBlockedRoute: () => useGlobalState(routerState.blockedRouteState),
|
|
582
586
|
useRouteItemData: () => useGlobalState(routerState.routeItemDataState),
|
|
583
587
|
useScrollMap: () => useGlobalState(routerState.scrollMapState),
|
|
584
|
-
useStatus: () => useGlobalState(routerState.statusState),
|
|
585
588
|
useContextState: () => useGlobalState(routerState.contextState),
|
|
586
589
|
useLoaderState: () => useGlobalState(routerState.loaderState)[0],
|
|
587
590
|
useParams: () => {
|
|
@@ -775,9 +778,8 @@ var IS_MOBILE = isMobile();
|
|
|
775
778
|
var MOBILE_CACHE_SIZE = 60;
|
|
776
779
|
var DESKTOP_CACHE_SIZE = 150;
|
|
777
780
|
var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration, defaultLoaderFallback, defaultErrorElement, defaultRetry, defaultStaleTime, context: initialContext, isAnimated = false, optimisticSpinner = true, defaultMinLoaderDuration = 0, maxCacheSize = IS_MOBILE ? MOBILE_CACHE_SIZE : DESKTOP_CACHE_SIZE, defaultPrefetch = IS_MOBILE ? "viewport" : "hover", defaultHoverPrefetchDelay = 150, defaultScrollRestorationBehavior = "auto", errorBoundary: ErrorBoundary = EmptyBoundary }) => {
|
|
778
|
-
const { useRouteItemData
|
|
779
|
-
const [
|
|
780
|
-
const [status] = useStatus();
|
|
781
|
+
const { useRouteItemData } = router.hooks;
|
|
782
|
+
const [{ routeItem, status, location }] = useRouteItemData();
|
|
781
783
|
const isError = status === "error";
|
|
782
784
|
const isLoading = status === "pending";
|
|
783
785
|
const isOptimistic = status === "optimistic";
|
|
@@ -797,8 +799,7 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
797
799
|
useApplyCustomAnimation(animationDuration);
|
|
798
800
|
useSetInitialContext(initialContext);
|
|
799
801
|
usePreserveScroll(defaultScrollRestorationBehavior);
|
|
800
|
-
|
|
801
|
-
if (isLoading && !isError) return renderElement(routeItemData?.routeItem?.loaderFallback || defaultLoaderFallback);
|
|
802
|
+
if (isLoading && !isError) return renderElement(routeItem?.loaderFallback || defaultLoaderFallback);
|
|
802
803
|
if (!routeItem) return null;
|
|
803
804
|
if (isError) return renderElement(routeItem.errorElement || defaultErrorElement);
|
|
804
805
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
|
|
@@ -809,10 +810,9 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
809
810
|
//#endregion
|
|
810
811
|
//#region hooks/useIsRoutePending.ts
|
|
811
812
|
var useIsRoutePending = (routePath) => {
|
|
812
|
-
const {
|
|
813
|
+
const { useRouteItemData } = router.hooks;
|
|
813
814
|
const [routeItemData] = useRouteItemData();
|
|
814
|
-
|
|
815
|
-
return status === "pending" && routeItemData?.location.pathname === routePath;
|
|
815
|
+
return routeItemData.status === "pending" && routeItemData?.location.pathname === routePath;
|
|
816
816
|
};
|
|
817
817
|
//#endregion
|
|
818
818
|
//#region hooks/useNavigate.ts
|
|
@@ -974,9 +974,9 @@ var useAction = router.hooks.useAction;
|
|
|
974
974
|
//#endregion
|
|
975
975
|
//#region hooks/useIsDataLoading.ts
|
|
976
976
|
var useIsDataLoading = () => {
|
|
977
|
-
const {
|
|
978
|
-
const [
|
|
979
|
-
return status === "pending";
|
|
977
|
+
const { useRouteItemData } = router.hooks;
|
|
978
|
+
const [routeData] = useRouteItemData();
|
|
979
|
+
return routeData.status === "pending";
|
|
980
980
|
};
|
|
981
981
|
//#endregion
|
|
982
982
|
//#region hooks/useRouterContext.ts
|
package/dist/types.d.ts
CHANGED
|
@@ -72,6 +72,7 @@ export type LoaderState<T = unknown> = {
|
|
|
72
72
|
export type RouteItemData = {
|
|
73
73
|
location: Location;
|
|
74
74
|
routeItem: RouteItem | undefined;
|
|
75
|
+
status: 'idle' | 'pending' | 'active' | 'optimistic' | 'error';
|
|
75
76
|
};
|
|
76
77
|
type ObjectRetry = {
|
|
77
78
|
count: number;
|
|
@@ -111,11 +112,9 @@ export type LoadingPromise = Promise<{
|
|
|
111
112
|
data: null;
|
|
112
113
|
error: unknown;
|
|
113
114
|
} | undefined>;
|
|
114
|
-
export type Status = 'idle' | 'pending' | 'active' | 'optimistic' | 'error';
|
|
115
115
|
export type ScrollMap = Record<string, [string, number][]>;
|
|
116
116
|
export type RouterState = {
|
|
117
117
|
routeItemDataState: Store<RouteItemData>;
|
|
118
|
-
statusState: Store<Status>;
|
|
119
118
|
scrollMapState: Store<ScrollMap>;
|
|
120
119
|
contextState: Store<Record<string, unknown>>;
|
|
121
120
|
blockedRouteState: Store<{
|
|
@@ -141,7 +140,6 @@ export type RouterType = {
|
|
|
141
140
|
}>>;
|
|
142
141
|
useRouteItemData(): ReturnType<typeof useGlobalState<RouteItemData>>;
|
|
143
142
|
useScrollMap(): ReturnType<typeof useGlobalState<ScrollMap>>;
|
|
144
|
-
useStatus(): ReturnType<typeof useGlobalState<Status>>;
|
|
145
143
|
useContextState(): ReturnType<typeof useGlobalState<Record<string, unknown>>>;
|
|
146
144
|
useLoaderState<T>(): LoaderState<T>;
|
|
147
145
|
useParams<T>(): T;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { Location, RouteItem, RouterState } from '../types';
|
|
2
|
-
export declare const createCommitState: ({
|
|
2
|
+
export declare const createCommitState: ({ loaderStateRef, loaderState, routeItemDataState }: RouterState) => (nextLocation: Location, routeItem?: RouteItem) => void;
|