clear-react-router 1.5.5 → 1.5.6

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.
@@ -0,0 +1,7 @@
1
+ declare class RouterConfig {
2
+ isAnimated: boolean;
3
+ showFallbackIfAnimated: boolean;
4
+ configure(config: Partial<RouterConfig>): void;
5
+ }
6
+ export declare const routerConfig: RouterConfig;
7
+ export {};
@@ -3,6 +3,7 @@ export type NavigationContextValue = {
3
3
  blockerState: BlockerState;
4
4
  routeItemData: RouteItemData;
5
5
  currentLoaderFallback: RouteItem['loaderFallback'];
6
+ isLoading: boolean;
6
7
  };
7
8
  export type ActionsContextValue = {
8
9
  setSearch(arg: string): void;
@@ -9,8 +9,9 @@ type UseHandleNavigation = {
9
9
  routeItem?: RouteItem;
10
10
  pathname: string;
11
11
  }): boolean;
12
+ setIsLoading(arg: boolean): void;
12
13
  };
13
- export declare const useHandleNavigation: ({ routeList, context, revalidateCache, setContext, isCacheItemFresh, }: UseHandleNavigation) => {
14
+ export declare const useHandleNavigation: ({ routeList, context, revalidateCache, setContext, isCacheItemFresh, setIsLoading, }: UseHandleNavigation) => {
14
15
  blockerState: BlockerState;
15
16
  updateLocation: (nextLocation: Location) => Promise<void>;
16
17
  updateBlockedRoute: ({ type, payload }: UpdateBlockedRouteProps) => void;
@@ -12,5 +12,7 @@ export declare const useLoader: ({ routeList, context, setContext }: UseLoaderPa
12
12
  routeItem?: RouteItem;
13
13
  pathname: string;
14
14
  }) => boolean;
15
+ isLoading: boolean;
16
+ setIsLoading: Dispatch<SetStateAction<boolean>>;
15
17
  };
16
18
  export {};
@@ -1 +1 @@
1
- export declare const useSetIsAnimated: (isAnimated?: boolean) => void;
1
+ export declare const useSetIsAnimated: (isAnimated: boolean | undefined, showFallbackIfAnimated: boolean) => void;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Suspense, createContext, lazy, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
1
+ import { Suspense, createContext, lazy, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
2
2
  //#region \0rolldown/runtime.js
3
3
  var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
4
4
  //#endregion
@@ -45,7 +45,7 @@ var require_react_jsx_runtime_production = /* @__PURE__ */ __commonJSMin(((expor
45
45
  var import_jsx_runtime = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
46
46
  module.exports = require_react_jsx_runtime_production();
47
47
  })))();
48
- var Provider = ({ children, setContext, context, updateBlockedRoute, updateLocation, setSearch, prefetchLoader, blockerState, routeItemData, restoreScroll, currentLoaderFallback }) => {
48
+ var Provider = ({ children, setContext, context, updateBlockedRoute, updateLocation, setSearch, prefetchLoader, blockerState, routeItemData, restoreScroll, currentLoaderFallback, isLoading }) => {
49
49
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ActionsContext.Provider, {
50
50
  value: {
51
51
  setSearch,
@@ -61,7 +61,8 @@ var Provider = ({ children, setContext, context, updateBlockedRoute, updateLocat
61
61
  value: {
62
62
  blockerState,
63
63
  routeItemData,
64
- currentLoaderFallback
64
+ currentLoaderFallback,
65
+ isLoading
65
66
  },
66
67
  children
67
68
  })
@@ -78,40 +79,55 @@ var useLatest = (value) => {
78
79
  return ref;
79
80
  };
80
81
  //#endregion
81
- //#region store/createStore.ts
82
- var createStore = (storeCreator) => {
83
- let store;
84
- const subscribers = /* @__PURE__ */ new Set();
85
- const get = () => store;
86
- const set = (action) => {
87
- const nextState = typeof action === "function" ? action(store) : action;
88
- if (!Object.is(store, nextState)) {
89
- const prevState = store;
90
- store = typeof nextState !== "object" || nextState === null ? nextState : Object.assign({}, store, nextState);
91
- subscribers.forEach((listener) => listener(store, prevState));
92
- }
93
- };
94
- store = storeCreator(set, get);
95
- const subscribe = (listener) => {
96
- subscribers.add(listener);
97
- return () => subscribers.delete(listener);
98
- };
99
- function use(selector) {
100
- return useSyncExternalStore(subscribe, () => selector ? selector(get()) : get());
82
+ //#region \0@oxc-project+runtime@0.133.0/helpers/esm/typeof.js
83
+ function _typeof(o) {
84
+ "@babel/helpers - typeof";
85
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
86
+ return typeof o;
87
+ } : function(o) {
88
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
89
+ }, _typeof(o);
90
+ }
91
+ //#endregion
92
+ //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPrimitive.js
93
+ function toPrimitive(t, r) {
94
+ if ("object" != _typeof(t) || !t) return t;
95
+ var e = t[Symbol.toPrimitive];
96
+ if (void 0 !== e) {
97
+ var i = e.call(t, r || "default");
98
+ if ("object" != _typeof(i)) return i;
99
+ throw new TypeError("@@toPrimitive must return a primitive value.");
101
100
  }
102
- return {
103
- get,
104
- set,
105
- subscribe,
106
- use
107
- };
108
- };
101
+ return ("string" === r ? String : Number)(t);
102
+ }
109
103
  //#endregion
110
- //#region store/isAnimatedStore.ts
111
- var isAnimatedStore = createStore((set) => ({
112
- isAnimated: false,
113
- setIsAnimated: (isAnimated) => set({ isAnimated })
114
- }));
104
+ //#region \0@oxc-project+runtime@0.133.0/helpers/esm/toPropertyKey.js
105
+ function toPropertyKey(t) {
106
+ var i = toPrimitive(t, "string");
107
+ return "symbol" == _typeof(i) ? i : i + "";
108
+ }
109
+ //#endregion
110
+ //#region \0@oxc-project+runtime@0.133.0/helpers/esm/defineProperty.js
111
+ function _defineProperty(e, r, t) {
112
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
113
+ value: t,
114
+ enumerable: !0,
115
+ configurable: !0,
116
+ writable: !0
117
+ }) : e[r] = t, e;
118
+ }
119
+ //#endregion
120
+ //#region config/routerConfig.ts
121
+ var RouterConfig = class {
122
+ constructor() {
123
+ _defineProperty(this, "isAnimated", false);
124
+ _defineProperty(this, "showFallbackIfAnimated", false);
125
+ }
126
+ configure(config) {
127
+ Object.assign(this, config);
128
+ }
129
+ };
130
+ var routerConfig = new RouterConfig();
115
131
  //#endregion
116
132
  //#region utils/createLazyComponent.tsx
117
133
  var createLazyComponent = (importFn, fallback) => {
@@ -166,8 +182,8 @@ var comparePaths = (el, pathname) => {
166
182
  //#endregion
167
183
  //#region hooks/useHandleNavigation.ts
168
184
  var ALL_LOCATIONS = "*";
169
- var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, isCacheItemFresh }) => {
170
- const isAnimated = isAnimatedStore.use((state) => state.isAnimated);
185
+ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, isCacheItemFresh, setIsLoading }) => {
186
+ const { isAnimated, showFallbackIfAnimated: showFallback } = routerConfig;
171
187
  const [blockedRoute, setBlockedRoute] = useState({
172
188
  from: "",
173
189
  to: ""
@@ -205,6 +221,7 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
205
221
  routeItem,
206
222
  loaderState: loaderState.current
207
223
  });
224
+ setIsLoading(false);
208
225
  setCurrentLoaderFallback(void 0);
209
226
  prevPathname.current = nextLocation.pathname;
210
227
  const fullPath = nextLocation.search ? `${nextLocation.pathname}${nextLocation.search}` : nextLocation.pathname;
@@ -262,7 +279,7 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
262
279
  setCurrentLoaderFallback(isCacheItemFresh({
263
280
  routeItem: nextItem,
264
281
  pathname: nextLocation.pathname
265
- }) ? void 0 : nextItem?.loaderFallback);
282
+ }) || isAnimated && !showFallback ? void 0 : nextItem?.loaderFallback);
266
283
  await revalidateCache({
267
284
  routeItem: nextItem,
268
285
  loaderState,
@@ -281,7 +298,9 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
281
298
  routeList,
282
299
  transitionedNavigation,
283
300
  setContext,
284
- isCacheItemFresh
301
+ isCacheItemFresh,
302
+ isAnimated,
303
+ showFallback
285
304
  ]);
286
305
  const setNextLocationRef = useLatest(navigationHandler);
287
306
  const updateBlockedRoute = useCallback(({ type, payload = "" }) => setBlockedRoute((prevState) => {
@@ -344,6 +363,7 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
344
363
  //#endregion
345
364
  //#region hooks/useLoader.ts
346
365
  var useLoader = ({ routeList, context, setContext }) => {
366
+ const [isLoading, setIsLoading] = useState(false);
347
367
  const cacheTimestampsRef = useRef({});
348
368
  const loaderCacheRef = useRef({});
349
369
  const isCacheItemFresh = useCallback(({ routeItem, pathname }) => {
@@ -366,6 +386,7 @@ var useLoader = ({ routeList, context, setContext }) => {
366
386
  if (loaderState) loaderState.current = loaderCacheRef.current[pathname];
367
387
  return;
368
388
  }
389
+ if (loaderState) setIsLoading(true);
369
390
  try {
370
391
  const params = getParamsObject({
371
392
  params: routeItem.params,
@@ -411,14 +432,16 @@ var useLoader = ({ routeList, context, setContext }) => {
411
432
  });
412
433
  }, [revalidateCache, routeList]),
413
434
  revalidateCache,
414
- isCacheItemFresh
435
+ isCacheItemFresh,
436
+ isLoading,
437
+ setIsLoading
415
438
  };
416
439
  };
417
440
  //#endregion
418
441
  //#region components/RouterProvider.tsx
419
442
  var RouterProvider = ({ children, routeList, context: initialContext = {} }) => {
420
443
  const [context, setContext] = useState(initialContext);
421
- const { prefetchLoader, revalidateCache, isCacheItemFresh } = useLoader({
444
+ const { prefetchLoader, revalidateCache, isCacheItemFresh, setIsLoading, isLoading } = useLoader({
422
445
  routeList,
423
446
  context,
424
447
  setContext
@@ -428,7 +451,8 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
428
451
  context,
429
452
  setContext,
430
453
  revalidateCache,
431
- isCacheItemFresh
454
+ isCacheItemFresh,
455
+ setIsLoading
432
456
  });
433
457
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Provider, {
434
458
  ...useMemo(() => ({
@@ -441,7 +465,8 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
441
465
  setContext,
442
466
  routeItemData,
443
467
  restoreScroll,
444
- currentLoaderFallback
468
+ currentLoaderFallback,
469
+ isLoading
445
470
  }), [
446
471
  blockerState,
447
472
  context,
@@ -451,7 +476,8 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
451
476
  updateBlockedRoute,
452
477
  updateLocation,
453
478
  currentLoaderFallback,
454
- restoreScroll
479
+ restoreScroll,
480
+ isLoading
455
481
  ]),
456
482
  children
457
483
  });
@@ -517,6 +543,14 @@ var usePreserveScroll = (preserveScroll) => {
517
543
  ]);
518
544
  };
519
545
  //#endregion
546
+ //#region hooks/useSetIsAnimated.ts
547
+ var useSetIsAnimated = (isAnimated = false, showFallbackIfAnimated) => {
548
+ useEffect(() => routerConfig.configure({
549
+ isAnimated,
550
+ showFallbackIfAnimated
551
+ }), [isAnimated]);
552
+ };
553
+ //#endregion
520
554
  //#region components/Spinner.tsx
521
555
  var Spinner = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "cr-spinner" });
522
556
  //#endregion
@@ -528,10 +562,10 @@ var renderElement = (Component) => {
528
562
  //#endregion
529
563
  //#region components/Router.tsx
530
564
  var Router = ({ isAnimated, animationDuration, spinner = true, preserveScroll = true, showFallbackIfAnimated = false }) => {
531
- const { routeItemData: { routeItem, loaderState }, currentLoaderFallback } = useNavigationState();
565
+ const { routeItemData: { routeItem, loaderState }, currentLoaderFallback, isLoading } = useNavigationState();
532
566
  usePreserveScroll(preserveScroll);
533
567
  useApplyCustomAnimation(animationDuration);
534
- const isLoading = Boolean(currentLoaderFallback);
568
+ useSetIsAnimated(isAnimated, showFallbackIfAnimated);
535
569
  const showErrorElement = !isLoading && Boolean(loaderState.loaderError || loaderState.beforeLoadError);
536
570
  const showSpinner = spinner && isAnimated && isLoading;
537
571
  const loadingContent = !showErrorElement && isLoading;
@@ -3,5 +3,5 @@ import { type ActionsContextValue, type DataContextValue, type NavigationContext
3
3
  type ProviderProps = NavigationContextValue & ActionsContextValue & DataContextValue & {
4
4
  children: ReactNode;
5
5
  };
6
- export declare const Provider: ({ children, setContext, context, updateBlockedRoute, updateLocation, setSearch, prefetchLoader, blockerState, routeItemData, restoreScroll, currentLoaderFallback, }: ProviderProps) => import("react/jsx-runtime").JSX.Element;
6
+ export declare const Provider: ({ children, setContext, context, updateBlockedRoute, updateLocation, setSearch, prefetchLoader, blockerState, routeItemData, restoreScroll, currentLoaderFallback, isLoading, }: ProviderProps) => import("react/jsx-runtime").JSX.Element;
7
7
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.5.5",
3
+ "version": "1.5.6",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "scripts": {
@@ -1,13 +0,0 @@
1
- type SetStateAction<T> = ((prevState: T) => Partial<T>) | Partial<T>;
2
- type Listener<T> = (state: T, prevState: T) => void;
3
- type Store<T> = {
4
- get: () => T;
5
- set: (action: SetStateAction<T>) => void;
6
- subscribe: (listener: Listener<T>) => () => void;
7
- use(): T;
8
- use<S>(selector: (state: T) => S): S;
9
- use<S>(selector?: (state: T) => S): T | S;
10
- };
11
- type StoreCreator<T> = (set: (action: SetStateAction<T>) => void, get: () => T) => T;
12
- export declare const createStore: <T>(storeCreator: StoreCreator<T>) => Store<T>;
13
- export {};
@@ -1,13 +0,0 @@
1
- type IsAnimatedStore = {
2
- isAnimated: boolean;
3
- setIsAnimated(arg: boolean): void;
4
- };
5
- export declare const isAnimatedStore: {
6
- get: () => IsAnimatedStore;
7
- set: (action: Partial<IsAnimatedStore> | ((prevState: IsAnimatedStore) => Partial<IsAnimatedStore>)) => void;
8
- subscribe: (listener: (state: IsAnimatedStore, prevState: IsAnimatedStore) => void) => () => void;
9
- use(): IsAnimatedStore;
10
- use<S>(selector: (state: IsAnimatedStore) => S): S;
11
- use<S>(selector?: ((state: IsAnimatedStore) => S) | undefined): IsAnimatedStore | S;
12
- };
13
- export {};