clear-react-router 1.5.6 → 1.5.8
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.
|
@@ -6,7 +6,6 @@ export type NavigationContextValue = {
|
|
|
6
6
|
isLoading: boolean;
|
|
7
7
|
};
|
|
8
8
|
export type ActionsContextValue = {
|
|
9
|
-
setSearch(arg: string): void;
|
|
10
9
|
updateLocation(route: Location): Promise<void>;
|
|
11
10
|
updateBlockedRoute(arg: UpdateBlockedRouteProps): void;
|
|
12
11
|
prefetchLoader(arg: string): Promise<void>;
|
|
@@ -16,7 +16,6 @@ export declare const useHandleNavigation: ({ routeList, context, revalidateCache
|
|
|
16
16
|
updateLocation: (nextLocation: Location) => Promise<void>;
|
|
17
17
|
updateBlockedRoute: ({ type, payload }: UpdateBlockedRouteProps) => void;
|
|
18
18
|
routeItemData: RouteItemData;
|
|
19
|
-
setSearch: (search: string) => void;
|
|
20
19
|
restoreScroll: () => void;
|
|
21
20
|
currentLoaderFallback: import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | (() => import("react").ReactElement) | undefined;
|
|
22
21
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useSearch: () => string;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Suspense, createContext, lazy, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
1
|
+
import { Suspense, createContext, lazy, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } 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,10 +45,9 @@ 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,
|
|
48
|
+
var Provider = ({ children, setContext, context, updateBlockedRoute, updateLocation, prefetchLoader, blockerState, routeItemData, restoreScroll, currentLoaderFallback, isLoading }) => {
|
|
49
49
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ActionsContext.Provider, {
|
|
50
50
|
value: {
|
|
51
|
-
setSearch,
|
|
52
51
|
updateLocation,
|
|
53
52
|
updateBlockedRoute,
|
|
54
53
|
prefetchLoader,
|
|
@@ -199,13 +198,6 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
|
|
|
199
198
|
const prevPathname = useRef("");
|
|
200
199
|
const navigationSeq = useRef(0);
|
|
201
200
|
const scrollMapRef = useLatest(scrollMap);
|
|
202
|
-
const setSearch = useCallback((search) => setRouteItemData((prevState) => ({
|
|
203
|
-
...prevState,
|
|
204
|
-
location: {
|
|
205
|
-
...prevState.location,
|
|
206
|
-
search
|
|
207
|
-
}
|
|
208
|
-
})), []);
|
|
209
201
|
const restoreScroll = useCallback(() => {
|
|
210
202
|
if (!prevPathname.current || !scrollMapRef.current[prevPathname.current]) return;
|
|
211
203
|
requestAnimationFrame(() => {
|
|
@@ -229,14 +221,6 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
|
|
|
229
221
|
history.pushState(null, "", fullPath);
|
|
230
222
|
}, []);
|
|
231
223
|
const transitionedNavigation = useCallback((nextLocation, routeItem) => {
|
|
232
|
-
setScrollMap((prevState) => {
|
|
233
|
-
const scrollPosition = document.scrollingElement?.scrollTop ?? 0;
|
|
234
|
-
if (!scrollPosition || prevState[prevPathname.current] === scrollPosition) return prevState;
|
|
235
|
-
return {
|
|
236
|
-
...prevState,
|
|
237
|
-
[prevPathname.current]: scrollPosition
|
|
238
|
-
};
|
|
239
|
-
});
|
|
240
224
|
if (!isAnimated) {
|
|
241
225
|
navigation(nextLocation, routeItem);
|
|
242
226
|
return;
|
|
@@ -276,6 +260,14 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
|
|
|
276
260
|
return transitionedNavigation(nextLocation, nextItem);
|
|
277
261
|
}
|
|
278
262
|
if (seq !== navigationSeq.current) return;
|
|
263
|
+
setScrollMap((prevState) => {
|
|
264
|
+
const scrollPosition = document.scrollingElement?.scrollTop ?? 0;
|
|
265
|
+
if (!scrollPosition || prevState[prevPathname.current] === scrollPosition) return prevState;
|
|
266
|
+
return {
|
|
267
|
+
...prevState,
|
|
268
|
+
[prevPathname.current]: scrollPosition
|
|
269
|
+
};
|
|
270
|
+
});
|
|
279
271
|
setCurrentLoaderFallback(isCacheItemFresh({
|
|
280
272
|
routeItem: nextItem,
|
|
281
273
|
pathname: nextLocation.pathname
|
|
@@ -355,7 +347,6 @@ var useHandleNavigation = ({ routeList, context, revalidateCache, setContext, is
|
|
|
355
347
|
updateLocation,
|
|
356
348
|
updateBlockedRoute,
|
|
357
349
|
routeItemData,
|
|
358
|
-
setSearch,
|
|
359
350
|
restoreScroll,
|
|
360
351
|
currentLoaderFallback
|
|
361
352
|
};
|
|
@@ -446,7 +437,7 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
|
|
|
446
437
|
context,
|
|
447
438
|
setContext
|
|
448
439
|
});
|
|
449
|
-
const { blockerState, updateLocation, updateBlockedRoute, routeItemData,
|
|
440
|
+
const { blockerState, updateLocation, updateBlockedRoute, routeItemData, restoreScroll, currentLoaderFallback } = useHandleNavigation({
|
|
450
441
|
routeList,
|
|
451
442
|
context,
|
|
452
443
|
setContext,
|
|
@@ -456,7 +447,6 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
|
|
|
456
447
|
});
|
|
457
448
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Provider, {
|
|
458
449
|
...useMemo(() => ({
|
|
459
|
-
setSearch,
|
|
460
450
|
updateLocation,
|
|
461
451
|
prefetchLoader,
|
|
462
452
|
updateBlockedRoute,
|
|
@@ -472,7 +462,6 @@ var RouterProvider = ({ children, routeList, context: initialContext = {} }) =>
|
|
|
472
462
|
context,
|
|
473
463
|
prefetchLoader,
|
|
474
464
|
routeItemData,
|
|
475
|
-
setSearch,
|
|
476
465
|
updateBlockedRoute,
|
|
477
466
|
updateLocation,
|
|
478
467
|
currentLoaderFallback,
|
|
@@ -687,12 +676,40 @@ var useRouterContext = () => {
|
|
|
687
676
|
};
|
|
688
677
|
};
|
|
689
678
|
//#endregion
|
|
679
|
+
//#region hooks/useSearch.ts
|
|
680
|
+
var notify = () => {
|
|
681
|
+
window.dispatchEvent(new Event("locationChange"));
|
|
682
|
+
};
|
|
683
|
+
var patchHistory = () => {
|
|
684
|
+
const push = history.pushState;
|
|
685
|
+
const replace = history.replaceState;
|
|
686
|
+
history.pushState = function(...args) {
|
|
687
|
+
const result = push.apply(this, args);
|
|
688
|
+
notify();
|
|
689
|
+
return result;
|
|
690
|
+
};
|
|
691
|
+
history.replaceState = function(...args) {
|
|
692
|
+
const result = replace.apply(this, args);
|
|
693
|
+
notify();
|
|
694
|
+
return result;
|
|
695
|
+
};
|
|
696
|
+
window.addEventListener("popstate", notify);
|
|
697
|
+
};
|
|
698
|
+
var subscribe = (onChange) => {
|
|
699
|
+
window.addEventListener("locationChange", onChange);
|
|
700
|
+
return () => window.removeEventListener("locationChange", onChange);
|
|
701
|
+
};
|
|
702
|
+
var getSnapshot = () => window.location.search;
|
|
703
|
+
var useSearch = () => {
|
|
704
|
+
useEffect(patchHistory, []);
|
|
705
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
706
|
+
};
|
|
707
|
+
//#endregion
|
|
690
708
|
//#region hooks/useSearchParams.ts
|
|
691
709
|
var useSearchParams = () => {
|
|
692
|
-
const
|
|
693
|
-
const
|
|
694
|
-
const
|
|
695
|
-
const searchString = useMemo(() => location.search ? location.search.replace("?", "") : location.pathname.split("?")?.[1] ?? "", [location.pathname, location.search]);
|
|
710
|
+
const search = useSearch();
|
|
711
|
+
const searchRef = useLatest(search);
|
|
712
|
+
const searchString = search ? search.replace("?", "") : window.location.pathname.split("?")?.[1] ?? "";
|
|
696
713
|
const searchParams = useMemo(() => new URLSearchParams(searchString), [searchString]);
|
|
697
714
|
const getSearchParams = useCallback((param) => {
|
|
698
715
|
const allValues = searchParams.getAll(param);
|
|
@@ -700,24 +717,22 @@ var useSearchParams = () => {
|
|
|
700
717
|
}, [searchParams]);
|
|
701
718
|
const navigateWithSearchParams = useCallback((params) => {
|
|
702
719
|
const newSearch = params.toString();
|
|
703
|
-
const { pathname } =
|
|
720
|
+
const { pathname } = window.location;
|
|
704
721
|
const search = newSearch ? `?${newSearch}` : "";
|
|
705
|
-
setSearch(search);
|
|
706
722
|
history.replaceState(null, "", pathname + search);
|
|
707
|
-
}, [
|
|
723
|
+
}, []);
|
|
708
724
|
return {
|
|
709
725
|
searchParams,
|
|
710
726
|
getSearchParams,
|
|
711
727
|
setSearchParams: useCallback((param, value) => {
|
|
712
|
-
const
|
|
713
|
-
const currentParams = new URLSearchParams(search);
|
|
728
|
+
const currentParams = new URLSearchParams(searchRef.current);
|
|
714
729
|
if (typeof param === "string" && value !== void 0) {
|
|
715
730
|
currentParams.delete(param);
|
|
716
731
|
(Array.isArray(value) ? value : [value]).forEach((v) => currentParams.append(param, v));
|
|
717
732
|
navigateWithSearchParams(currentParams);
|
|
718
733
|
} else if (typeof param === "function") navigateWithSearchParams(param(currentParams));
|
|
719
734
|
else throw new Error("useSearchParams first argument must be either function or string");
|
|
720
|
-
}, [
|
|
735
|
+
}, [navigateWithSearchParams, searchRef])
|
|
721
736
|
};
|
|
722
737
|
};
|
|
723
738
|
//#endregion
|
|
@@ -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,
|
|
6
|
+
export declare const Provider: ({ children, setContext, context, updateBlockedRoute, updateLocation, prefetchLoader, blockerState, routeItemData, restoreScroll, currentLoaderFallback, isLoading, }: ProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
7
7
|
export {};
|