clear-react-router 2.1.5 → 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 CHANGED
@@ -30,12 +30,13 @@ var useGlobalState = ({ subscribe, getState, setState }) => {
30
30
  };
31
31
  //#endregion
32
32
  //#region utils/commitState.ts
33
- var createCommitState = ({ routeItemDataState, statusState, loaderStateRef, loaderState }) => (nextLocation, routeItem) => {
33
+ var createCommitState = ({ loaderStateRef, loaderState, routeItemDataState }) => (nextLocation, routeItem) => {
34
+ const isError = loaderStateRef.value.loaderError || loaderStateRef.value.beforeLoadError;
34
35
  routeItemDataState.setState({
35
36
  routeItem,
36
- location: nextLocation
37
+ location: nextLocation,
38
+ status: isError ? "error" : "active"
37
39
  });
38
- statusState.setState("active");
39
40
  loaderState.setState(loaderStateRef.value);
40
41
  if ((nextLocation.search ? `${nextLocation.pathname}${nextLocation.search}` : nextLocation.pathname) === window.location.pathname + window.location.search) return;
41
42
  history.pushState(null, "", `${nextLocation.pathname}${nextLocation.search}`);
@@ -105,8 +106,8 @@ var RouterConfig = class {
105
106
  var routerConfig = new RouterConfig();
106
107
  //#endregion
107
108
  //#region utils/commitNavigation.ts
108
- var commitNavigation = (callback) => {
109
- if (!routerConfig.isAnimated) return callback();
109
+ var commitNavigation = (callback, isCommited) => {
110
+ if (!routerConfig.isAnimated || isCommited) return callback();
110
111
  try {
111
112
  const transition = document.startViewTransition(callback);
112
113
  transition.ready.catch(() => {});
@@ -216,8 +217,8 @@ var createNavigate = (routerState, revalidateCache) => {
216
217
  let navigationSeq = 0;
217
218
  let interval = 0;
218
219
  let abortController = null;
219
- const { loaderState, loaderStateRef, statusState, contextState, loaderMap, routeItemDataState, scrollMapState } = routerState;
220
- const navigationExecutor = createCommitState(routerState);
220
+ const { loaderState, loaderStateRef, contextState, loaderMap, routeItemDataState, scrollMapState } = routerState;
221
+ const commitState = createCommitState(routerState);
221
222
  const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
222
223
  const createSignal = () => {
223
224
  abortController?.abort();
@@ -264,17 +265,20 @@ var createNavigate = (routerState, revalidateCache) => {
264
265
  updateScrollMap(routeItemDataState, scrollMapState);
265
266
  const path = getPath(location);
266
267
  if (routeItem?.optimistic && loaderMap.has(path)) {
267
- routeItemDataState.setState({
268
+ commitNavigation(() => routeItemDataState.setState({
268
269
  routeItem,
269
- location
270
- });
271
- statusState.setState("optimistic");
270
+ location,
271
+ status: "optimistic"
272
+ }));
272
273
  const currentLoaderState = loaderMap.get(path)?.state;
273
274
  if (currentLoaderState) loaderState.setState(currentLoaderState);
274
- } else {
275
- const pendingShouldExist = routeItem?.loader && !isCacheItemFresh(path);
276
- commitNavigation(() => statusState.setState(pendingShouldExist ? "pending" : "active"));
275
+ return;
277
276
  }
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;
@@ -330,7 +334,7 @@ var createNavigate = (routerState, revalidateCache) => {
330
334
  prepareNavigation(nextItem, nextLocation);
331
335
  await loader(nextItem, nextLocation, seq);
332
336
  if (seq !== navigationSeq) return;
333
- commitNavigation(() => navigationExecutor(nextLocation, nextItem));
337
+ commitNavigation(() => commitState(nextLocation, nextItem));
334
338
  await afterLoad(nextItem, params);
335
339
  };
336
340
  return navigate;
@@ -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: () => {
@@ -665,9 +668,6 @@ var useNavigation = () => {
665
668
  }, [navigate]);
666
669
  };
667
670
  //#endregion
668
- //#region hooks/useLoaderState.ts
669
- var useLoaderState = router.hooks.useLoaderState;
670
- //#endregion
671
671
  //#region hooks/useApplyCustomAnimation.ts
672
672
  var useApplyCustomAnimation = (animationDuration) => {
673
673
  useEffect(() => {
@@ -778,10 +778,9 @@ var IS_MOBILE = isMobile();
778
778
  var MOBILE_CACHE_SIZE = 60;
779
779
  var DESKTOP_CACHE_SIZE = 150;
780
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 }) => {
781
- const { useRouteItemData, useStatus } = router.hooks;
782
- const [routeItemData] = useRouteItemData();
783
- const [status] = useStatus();
784
- const loaderState = useLoaderState();
781
+ const { useRouteItemData } = router.hooks;
782
+ const [{ routeItem, status, location }] = useRouteItemData();
783
+ const isError = status === "error";
785
784
  const isLoading = status === "pending";
786
785
  const isOptimistic = status === "optimistic";
787
786
  useNavigation();
@@ -800,11 +799,9 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
800
799
  useApplyCustomAnimation(animationDuration);
801
800
  useSetInitialContext(initialContext);
802
801
  usePreserveScroll(defaultScrollRestorationBehavior);
803
- const { routeItem, location } = routeItemData;
804
- const showErrorElement = !isLoading && Boolean(loaderState.loaderError || loaderState.beforeLoadError);
805
- if (!showErrorElement && isLoading) return renderElement(routeItemData?.routeItem?.loaderFallback || defaultLoaderFallback);
802
+ if (isLoading && !isError) return renderElement(routeItem?.loaderFallback || defaultLoaderFallback);
806
803
  if (!routeItem) return null;
807
- if (showErrorElement) return renderElement(routeItem.errorElement || defaultErrorElement);
804
+ if (isError) return renderElement(routeItem.errorElement || defaultErrorElement);
808
805
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
809
806
  style: { viewTransitionName: "page" },
810
807
  children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, { children: renderElement(routeItem.element) }, location.pathname), optimisticSpinner && isOptimistic && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Spinner, {})]
@@ -813,10 +810,9 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
813
810
  //#endregion
814
811
  //#region hooks/useIsRoutePending.ts
815
812
  var useIsRoutePending = (routePath) => {
816
- const { useStatus, useRouteItemData } = router.hooks;
813
+ const { useRouteItemData } = router.hooks;
817
814
  const [routeItemData] = useRouteItemData();
818
- const [status] = useStatus();
819
- return status === "pending" && routeItemData?.location.pathname === routePath;
815
+ return routeItemData.status === "pending" && routeItemData?.location.pathname === routePath;
820
816
  };
821
817
  //#endregion
822
818
  //#region hooks/useNavigate.ts
@@ -923,6 +919,9 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
923
919
  //#region hooks/useParams.ts
924
920
  var useParams = router.hooks.useParams;
925
921
  //#endregion
922
+ //#region hooks/useLoaderState.ts
923
+ var useLoaderState = router.hooks.useLoaderState;
924
+ //#endregion
926
925
  //#region hooks/useInvalidate.ts
927
926
  var useInvalidate = () => router.runtime.invalidate;
928
927
  //#endregion
@@ -975,9 +974,9 @@ var useAction = router.hooks.useAction;
975
974
  //#endregion
976
975
  //#region hooks/useIsDataLoading.ts
977
976
  var useIsDataLoading = () => {
978
- const { useStatus } = router.hooks;
979
- const [status] = useStatus();
980
- return status === "pending";
977
+ const { useRouteItemData } = router.hooks;
978
+ const [routeData] = useRouteItemData();
979
+ return routeData.status === "pending";
981
980
  };
982
981
  //#endregion
983
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';
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 +1 @@
1
- export declare const commitNavigation: (callback: () => void) => void;
1
+ export declare const commitNavigation: (callback: () => void, isCommited?: boolean) => void;
@@ -1,2 +1,2 @@
1
1
  import { Location, RouteItem, RouterState } from '../types';
2
- export declare const createCommitState: ({ routeItemDataState, statusState, loaderStateRef, loaderState }: RouterState) => (nextLocation: Location, routeItem: RouteItem | undefined) => void;
2
+ export declare const createCommitState: ({ loaderStateRef, loaderState, routeItemDataState }: RouterState) => (nextLocation: Location, routeItem?: RouteItem) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "2.1.5",
3
+ "version": "2.1.7",
4
4
  "description": "A lightweight routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {