clear-react-router 1.5.4 → 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,41 +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
- console.log({ store });
92
- subscribers.forEach((listener) => listener(store, prevState));
93
- }
94
- };
95
- store = storeCreator(set, get);
96
- const subscribe = (listener) => {
97
- subscribers.add(listener);
98
- return () => subscribers.delete(listener);
99
- };
100
- function use(selector) {
101
- 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.");
102
100
  }
103
- return {
104
- get,
105
- set,
106
- subscribe,
107
- use
108
- };
109
- };
101
+ return ("string" === r ? String : Number)(t);
102
+ }
110
103
  //#endregion
111
- //#region store/isAnimatedStore.ts
112
- var isAnimatedStore = createStore((set) => ({
113
- isAnimated: false,
114
- setIsAnimated: (isAnimated) => set({ isAnimated })
115
- }));
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();
116
131
  //#endregion
117
132
  //#region utils/createLazyComponent.tsx
118
133
  var createLazyComponent = (importFn, fallback) => {
@@ -167,8 +182,8 @@ var comparePaths = (el, pathname) => {
167
182
  //#endregion
168
183
  //#region hooks/useHandleNavigation.ts
169
184
  var ALL_LOCATIONS = "*";
170
- var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, isCacheItemFresh }) => {
171
- const isAnimated = isAnimatedStore.use((state) => state.isAnimated);
185
+ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, isCacheItemFresh, setIsLoading }) => {
186
+ const { isAnimated, showFallbackIfAnimated: showFallback } = routerConfig;
172
187
  const [blockedRoute, setBlockedRoute] = useState({
173
188
  from: "",
174
189
  to: ""
@@ -206,6 +221,7 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
206
221
  routeItem,
207
222
  loaderState: loaderState.current
208
223
  });
224
+ setIsLoading(false);
209
225
  setCurrentLoaderFallback(void 0);
210
226
  prevPathname.current = nextLocation.pathname;
211
227
  const fullPath = nextLocation.search ? `${nextLocation.pathname}${nextLocation.search}` : nextLocation.pathname;
@@ -263,7 +279,7 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
263
279
  setCurrentLoaderFallback(isCacheItemFresh({
264
280
  routeItem: nextItem,
265
281
  pathname: nextLocation.pathname
266
- }) ? void 0 : nextItem?.loaderFallback);
282
+ }) || isAnimated && !showFallback ? void 0 : nextItem?.loaderFallback);
267
283
  await revalidateCache({
268
284
  routeItem: nextItem,
269
285
  loaderState,
@@ -282,7 +298,9 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
282
298
  routeList,
283
299
  transitionedNavigation,
284
300
  setContext,
285
- isCacheItemFresh
301
+ isCacheItemFresh,
302
+ isAnimated,
303
+ showFallback
286
304
  ]);
287
305
  const setNextLocationRef = useLatest(navigationHandler);
288
306
  const updateBlockedRoute = useCallback(({ type, payload = "" }) => setBlockedRoute((prevState) => {
@@ -345,6 +363,7 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
345
363
  //#endregion
346
364
  //#region hooks/useLoader.ts
347
365
  var useLoader = ({ routeList, context, setContext }) => {
366
+ const [isLoading, setIsLoading] = useState(false);
348
367
  const cacheTimestampsRef = useRef({});
349
368
  const loaderCacheRef = useRef({});
350
369
  const isCacheItemFresh = useCallback(({ routeItem, pathname }) => {
@@ -367,6 +386,7 @@ var useLoader = ({ routeList, context, setContext }) => {
367
386
  if (loaderState) loaderState.current = loaderCacheRef.current[pathname];
368
387
  return;
369
388
  }
389
+ if (loaderState) setIsLoading(true);
370
390
  try {
371
391
  const params = getParamsObject({
372
392
  params: routeItem.params,
@@ -412,14 +432,16 @@ var useLoader = ({ routeList, context, setContext }) => {
412
432
  });
413
433
  }, [revalidateCache, routeList]),
414
434
  revalidateCache,
415
- isCacheItemFresh
435
+ isCacheItemFresh,
436
+ isLoading,
437
+ setIsLoading
416
438
  };
417
439
  };
418
440
  //#endregion
419
441
  //#region components/RouterProvider.tsx
420
442
  var RouterProvider = ({ children, routeList, context: initialContext = {} }) => {
421
443
  const [context, setContext] = useState(initialContext);
422
- const { prefetchLoader, revalidateCache, isCacheItemFresh } = useLoader({
444
+ const { prefetchLoader, revalidateCache, isCacheItemFresh, setIsLoading, isLoading } = useLoader({
423
445
  routeList,
424
446
  context,
425
447
  setContext
@@ -429,7 +451,8 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
429
451
  context,
430
452
  setContext,
431
453
  revalidateCache,
432
- isCacheItemFresh
454
+ isCacheItemFresh,
455
+ setIsLoading
433
456
  });
434
457
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Provider, {
435
458
  ...useMemo(() => ({
@@ -442,7 +465,8 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
442
465
  setContext,
443
466
  routeItemData,
444
467
  restoreScroll,
445
- currentLoaderFallback
468
+ currentLoaderFallback,
469
+ isLoading
446
470
  }), [
447
471
  blockerState,
448
472
  context,
@@ -452,7 +476,8 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
452
476
  updateBlockedRoute,
453
477
  updateLocation,
454
478
  currentLoaderFallback,
455
- restoreScroll
479
+ restoreScroll,
480
+ isLoading
456
481
  ]),
457
482
  children
458
483
  });
@@ -518,6 +543,14 @@ var usePreserveScroll = (preserveScroll) => {
518
543
  ]);
519
544
  };
520
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
521
554
  //#region components/Spinner.tsx
522
555
  var Spinner = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "cr-spinner" });
523
556
  //#endregion
@@ -529,10 +562,10 @@ var renderElement = (Component) => {
529
562
  //#endregion
530
563
  //#region components/Router.tsx
531
564
  var Router = ({ isAnimated, animationDuration, spinner = true, preserveScroll = true, showFallbackIfAnimated = false }) => {
532
- const { routeItemData: { routeItem, loaderState }, currentLoaderFallback } = useNavigationState();
565
+ const { routeItemData: { routeItem, loaderState }, currentLoaderFallback, isLoading } = useNavigationState();
533
566
  usePreserveScroll(preserveScroll);
534
567
  useApplyCustomAnimation(animationDuration);
535
- const isLoading = Boolean(currentLoaderFallback);
568
+ useSetIsAnimated(isAnimated, showFallbackIfAnimated);
536
569
  const showErrorElement = !isLoading && Boolean(loaderState.loaderError || loaderState.beforeLoadError);
537
570
  const showSpinner = spinner && isAnimated && isLoading;
538
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.4",
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 {};