clear-react-router 1.4.5 → 1.4.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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Dispatch, SetStateAction } from 'react';
|
|
2
|
-
import { BlockerState, LoaderState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global';
|
|
2
|
+
import { BlockerState, LoaderState, Location, NextItemData, RouteItem, UpdateBlockedRouteProps } from '../types/global';
|
|
3
3
|
export type PropsContextValue = {
|
|
4
4
|
routeList: RouteItem[];
|
|
5
5
|
};
|
|
@@ -7,7 +7,7 @@ export type NavigationContextValue = {
|
|
|
7
7
|
location: Location;
|
|
8
8
|
blockerState: BlockerState;
|
|
9
9
|
isLoading: boolean;
|
|
10
|
-
|
|
10
|
+
nextItemData: NextItemData;
|
|
11
11
|
};
|
|
12
12
|
export type ActionsContextValue = {
|
|
13
13
|
setLocation: Dispatch<SetStateAction<Location>>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
|
-
import
|
|
2
|
+
import { BlockerState, LoaderState, Location, NextItemData, RevalidateCacheArgs, RouteItem, UpdateBlockedRouteProps } from '../types/global';
|
|
3
3
|
type UseHandleNavigation = {
|
|
4
4
|
routeList: RouteItem[];
|
|
5
5
|
setLocation: (arg: Location) => void;
|
|
@@ -12,6 +12,6 @@ export declare const useHandleNavigation: ({ setLocation, routeList, context, re
|
|
|
12
12
|
blockerState: BlockerState;
|
|
13
13
|
updateLocation: (nextLocation: Location) => Promise<void>;
|
|
14
14
|
updateBlockedRoute: ({ type, payload }: UpdateBlockedRouteProps) => void;
|
|
15
|
-
|
|
15
|
+
nextItemData: NextItemData;
|
|
16
16
|
};
|
|
17
17
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -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, location, setLocation, prefetchLoader, loaderState, blockerState, routeList, isLoading,
|
|
48
|
+
var Provider = ({ children, setContext, context, updateBlockedRoute, updateLocation, location, setLocation, prefetchLoader, loaderState, blockerState, routeList, isLoading, nextItemData }) => {
|
|
49
49
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PropsContext.Provider, {
|
|
50
50
|
value: { routeList },
|
|
51
51
|
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ActionsContext.Provider, {
|
|
@@ -66,7 +66,7 @@ var Provider = ({ children, setContext, context, updateBlockedRoute, updateLocat
|
|
|
66
66
|
blockerState,
|
|
67
67
|
location,
|
|
68
68
|
isLoading,
|
|
69
|
-
|
|
69
|
+
nextItemData
|
|
70
70
|
},
|
|
71
71
|
children
|
|
72
72
|
})
|
|
@@ -113,10 +113,10 @@ var parseClientRouteItem = (el, parentParams = [], parentPath = "") => {
|
|
|
113
113
|
}, ...el.children?.flatMap((child) => parseClientRouteItem(child, currentParams, path)) || []];
|
|
114
114
|
};
|
|
115
115
|
var createRouter = (clientList) => clientList.flatMap((el) => parseClientRouteItem(el, []));
|
|
116
|
-
var getParamsObject = ({
|
|
117
|
-
if (!
|
|
116
|
+
var getParamsObject = ({ params, pathname }) => {
|
|
117
|
+
if (!params) return {};
|
|
118
118
|
const split = pathname.split("/");
|
|
119
|
-
return (
|
|
119
|
+
return (params || []).map((el) => ({
|
|
120
120
|
index: split.findIndex((item) => item === el.key),
|
|
121
121
|
value: el.value
|
|
122
122
|
})).reduce((acc, cur) => ({
|
|
@@ -141,7 +141,7 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache, s
|
|
|
141
141
|
from: "",
|
|
142
142
|
to: ""
|
|
143
143
|
});
|
|
144
|
-
const [
|
|
144
|
+
const [nextItemData, setNextItemData] = useState({});
|
|
145
145
|
const prevPathname = useRef("");
|
|
146
146
|
const navigationSeq = useRef(0);
|
|
147
147
|
const navigation = useCallback((nextLocation) => {
|
|
@@ -162,9 +162,13 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache, s
|
|
|
162
162
|
navigationSeq.current = navigationSeq.current + 1;
|
|
163
163
|
const seq = navigationSeq.current;
|
|
164
164
|
const nextItem = routeList.find((el) => comparePaths(el, nextLocation.pathname));
|
|
165
|
-
|
|
165
|
+
setNextItemData({
|
|
166
|
+
loaderFallback: nextItem?.loaderFallback,
|
|
167
|
+
params: nextItem?.params,
|
|
168
|
+
pathname: nextLocation.pathname
|
|
169
|
+
});
|
|
166
170
|
const params = getParamsObject({
|
|
167
|
-
|
|
171
|
+
params: nextItem?.params,
|
|
168
172
|
pathname: nextLocation.pathname
|
|
169
173
|
});
|
|
170
174
|
if (nextItem?.beforeLoad) try {
|
|
@@ -266,7 +270,7 @@ var useHandleNavigation = ({ setLocation, routeList, context, revalidateCache, s
|
|
|
266
270
|
}, [blockedRoute]),
|
|
267
271
|
updateLocation,
|
|
268
272
|
updateBlockedRoute,
|
|
269
|
-
|
|
273
|
+
nextItemData
|
|
270
274
|
};
|
|
271
275
|
};
|
|
272
276
|
//#endregion
|
|
@@ -290,7 +294,7 @@ var useLoader = ({ routeList, context, setContext, setLoaderState }) => {
|
|
|
290
294
|
if (isCurrentRoute) setIsLoading(true);
|
|
291
295
|
try {
|
|
292
296
|
const params = getParamsObject({
|
|
293
|
-
routeItem,
|
|
297
|
+
params: routeItem.params,
|
|
294
298
|
pathname
|
|
295
299
|
});
|
|
296
300
|
const result = await routeItem?.loader({
|
|
@@ -352,7 +356,7 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
|
|
|
352
356
|
setContext,
|
|
353
357
|
setLoaderState
|
|
354
358
|
});
|
|
355
|
-
const { blockerState, updateLocation, updateBlockedRoute,
|
|
359
|
+
const { blockerState, updateLocation, updateBlockedRoute, nextItemData } = useHandleNavigation({
|
|
356
360
|
setLocation,
|
|
357
361
|
routeList,
|
|
358
362
|
context,
|
|
@@ -372,7 +376,7 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
|
|
|
372
376
|
context,
|
|
373
377
|
setContext,
|
|
374
378
|
routeList,
|
|
375
|
-
|
|
379
|
+
nextItemData,
|
|
376
380
|
isLoading
|
|
377
381
|
}), [
|
|
378
382
|
blockerState,
|
|
@@ -384,7 +388,7 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
|
|
|
384
388
|
routeList,
|
|
385
389
|
updateBlockedRoute,
|
|
386
390
|
updateLocation,
|
|
387
|
-
|
|
391
|
+
nextItemData
|
|
388
392
|
]),
|
|
389
393
|
children
|
|
390
394
|
});
|
|
@@ -455,9 +459,11 @@ var usePreserveScroll = ({ pathname, preserveScroll, nextPathname }) => {
|
|
|
455
459
|
}, [pathname, nextPathname]);
|
|
456
460
|
useEffect(() => {
|
|
457
461
|
if (!preserveScroll || !pathname || !scrollMap[pathname]) return;
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
462
|
+
requestAnimationFrame(() => {
|
|
463
|
+
window.scrollTo({
|
|
464
|
+
top: scrollMap[pathname],
|
|
465
|
+
behavior: "smooth"
|
|
466
|
+
});
|
|
461
467
|
});
|
|
462
468
|
}, [
|
|
463
469
|
pathname,
|
|
@@ -489,11 +495,11 @@ var renderElement = (Component) => {
|
|
|
489
495
|
//#region components/Router.tsx
|
|
490
496
|
var ALL_LOCATIONS = "*";
|
|
491
497
|
var Router = ({ isAnimated, animationDuration, spinner = true, preserveScroll = true }) => {
|
|
492
|
-
const { location, isLoading,
|
|
498
|
+
const { location, isLoading, nextItemData } = useNavigationState();
|
|
493
499
|
const { routeList } = usePropsData();
|
|
494
500
|
const { loaderState } = useRouterData();
|
|
495
501
|
usePreserveScroll({
|
|
496
|
-
nextPathname:
|
|
502
|
+
nextPathname: nextItemData.pathname,
|
|
497
503
|
pathname: location.pathname,
|
|
498
504
|
preserveScroll
|
|
499
505
|
});
|
|
@@ -502,25 +508,23 @@ var Router = ({ isAnimated, animationDuration, spinner = true, preserveScroll =
|
|
|
502
508
|
if (!location.pathname) return void 0;
|
|
503
509
|
return routeList.find((el) => el.path === ALL_LOCATIONS || comparePaths(el, location.pathname));
|
|
504
510
|
}, [location.pathname, routeList]);
|
|
505
|
-
const params = useMemo(() => {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
});
|
|
510
|
-
}, [
|
|
511
|
+
const params = useMemo(() => getParamsObject({
|
|
512
|
+
params: nextItemData.params,
|
|
513
|
+
pathname: nextItemData.pathname || location.pathname
|
|
514
|
+
}), [
|
|
511
515
|
location.pathname,
|
|
512
|
-
|
|
513
|
-
|
|
516
|
+
nextItemData.params,
|
|
517
|
+
nextItemData.pathname
|
|
514
518
|
]);
|
|
515
519
|
const shouldErrorElementShown = useMemo(() => Boolean(loaderState[location.pathname]?.loaderError || loaderState[location.pathname]?.beforeLoadError), [loaderState, location.pathname]);
|
|
516
|
-
if (!routeItem && !
|
|
517
|
-
if (!routeItem &&
|
|
520
|
+
if (!routeItem && !nextItemData.loaderFallback) return null;
|
|
521
|
+
if (!routeItem && nextItemData.loaderFallback) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ViewProvider, {
|
|
518
522
|
params,
|
|
519
|
-
children: renderElement(
|
|
523
|
+
children: renderElement(nextItemData.loaderFallback)
|
|
520
524
|
});
|
|
521
|
-
if (!isAnimated && !shouldErrorElementShown && isLoading) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ViewProvider, {
|
|
525
|
+
if (!isAnimated && !shouldErrorElementShown && isLoading && nextItemData.loaderFallback) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ViewProvider, {
|
|
522
526
|
params,
|
|
523
|
-
children: renderElement(
|
|
527
|
+
children: renderElement(nextItemData.loaderFallback)
|
|
524
528
|
});
|
|
525
529
|
if (shouldErrorElementShown) return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ViewProvider, {
|
|
526
530
|
params,
|
|
@@ -3,5 +3,5 @@ import { type ActionsContextValue, type DataContextValue, type NavigationContext
|
|
|
3
3
|
type ProviderProps = PropsContextValue & NavigationContextValue & ActionsContextValue & DataContextValue & {
|
|
4
4
|
children: ReactNode;
|
|
5
5
|
};
|
|
6
|
-
export declare const Provider: ({ children, setContext, context, updateBlockedRoute, updateLocation, location, setLocation, prefetchLoader, loaderState, blockerState, routeList, isLoading,
|
|
6
|
+
export declare const Provider: ({ children, setContext, context, updateBlockedRoute, updateLocation, location, setLocation, prefetchLoader, loaderState, blockerState, routeList, isLoading, nextItemData, }: ProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
7
7
|
export {};
|
package/dist/types/global.d.ts
CHANGED
package/dist/utils/utils.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ClientRouteItem, Location, RouteItem } from '../types/global';
|
|
2
2
|
export declare const createRouter: (clientList: ClientRouteItem[]) => RouteItem[];
|
|
3
|
-
export declare const getParamsObject: ({
|
|
4
|
-
|
|
3
|
+
export declare const getParamsObject: ({ params, pathname }: {
|
|
4
|
+
params?: RouteItem["params"];
|
|
5
5
|
pathname: string;
|
|
6
6
|
}) => {};
|
|
7
7
|
export declare const parseWindowLocation: (location: typeof window.location) => Location;
|