react-router-dom-v5-compat 6.8.2 → 6.9.0
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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +346 -24
- package/dist/index.js.map +1 -1
- package/dist/main.js +1 -1
- package/dist/react-router-dom/index.d.ts +3 -3
- package/dist/umd/react-router-dom-v5-compat.development.js +438 -19
- package/dist/umd/react-router-dom-v5-compat.development.js.map +1 -1
- package/dist/umd/react-router-dom-v5-compat.production.min.js +2 -2
- package/dist/umd/react-router-dom-v5-compat.production.min.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# `react-router-dom-v5-compat`
|
|
2
2
|
|
|
3
|
+
## 6.9.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies:
|
|
8
|
+
- `react-router@6.9.0`
|
|
9
|
+
- `react-router-dom@6.9.0`
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Add missed data router API re-exports ([#10171](https://github.com/remix-run/react-router/pull/10171))
|
|
14
|
+
|
|
3
15
|
## 6.8.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
* would break. We could stop doing two bundles in v6 "react-router-dom" and
|
|
47
47
|
* deprecate the deep require if we wanted to avoid the duplication here.
|
|
48
48
|
*/
|
|
49
|
-
export type { Hash, Location, Path, To, MemoryRouterProps, NavigateFunction, NavigateOptions, NavigateProps, Navigator, OutletProps, Params, ParamParseKey, PathMatch, RouteMatch, RouteObject, RouteProps, PathRouteProps, LayoutRouteProps, IndexRouteProps, RouterProps, Pathname, Search, RoutesProps, } from "./react-router-dom";
|
|
50
|
-
export { BrowserRouter, HashRouter, Link, MemoryRouter, NavLink, Navigate, NavigationType, Outlet, Route, Router, Routes, UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext, createPath, createRoutesFromChildren, createSearchParams, generatePath, matchPath, matchRoutes, parsePath, renderMatches, resolvePath, unstable_HistoryRouter, useHref, useInRouterContext, useLinkClickHandler, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams, } from "./react-router-dom";
|
|
49
|
+
export type { BrowserRouterProps, Hash, HashRouterProps, HistoryRouterProps, LinkProps, Location, Path, To, MemoryRouterProps, NavigateFunction, NavigateOptions, NavigateProps, Navigator, NavLinkProps, OutletProps, Params, ParamParseKey, PathMatch, RouteMatch, RouteObject, RouteProps, PathRouteProps, LayoutRouteProps, IndexRouteProps, RouterProps, Pathname, Search, RoutesProps, ParamKeyValuePair, URLSearchParamsInit, ActionFunction, ActionFunctionArgs, AwaitProps, unstable_Blocker, unstable_BlockerFunction, DataRouteMatch, DataRouteObject, Fetcher, FetcherWithComponents, FormEncType, FormMethod, FormProps, GetScrollRestorationKeyFunction, IndexRouteObject, JsonFunction, LoaderFunction, LoaderFunctionArgs, Navigation, NonIndexRouteObject, PathPattern, RedirectFunction, RelativeRoutingType, RouterProviderProps, ScrollRestorationProps, ShouldRevalidateFunction, SubmitFunction, SubmitOptions, } from "./react-router-dom";
|
|
50
|
+
export { BrowserRouter, HashRouter, Link, MemoryRouter, NavLink, Navigate, NavigationType, Outlet, Route, Router, Routes, UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext, createPath, createRoutesFromChildren, createSearchParams, generatePath, matchPath, matchRoutes, parsePath, renderMatches, resolvePath, unstable_HistoryRouter, useHref, useInRouterContext, useLinkClickHandler, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes, useSearchParams, AbortedDeferredError, Await, RouterProvider, ScrollRestoration, createBrowserRouter, createHashRouter, createMemoryRouter, createRoutesFromElements, defer, isRouteErrorResponse, Form, json, redirect, useActionData, useAsyncError, useAsyncValue, useBeforeUnload, unstable_useBlocker, useFetcher, useFetchers, useFormAction, useLoaderData, useMatches, useNavigation, unstable_usePrompt, useRevalidator, useRouteError, useRouteLoaderData, useSubmit, UNSAFE_DataRouterContext, UNSAFE_DataRouterStateContext, UNSAFE_useScrollRestoration, } from "./react-router-dom";
|
|
51
51
|
export type { StaticRouterProps } from "./lib/components";
|
|
52
52
|
export { CompatRouter, CompatRoute, StaticRouter } from "./lib/components";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* React Router DOM v5 Compat v6.
|
|
2
|
+
* React Router DOM v5 Compat v6.9.0
|
|
3
3
|
*
|
|
4
4
|
* Copyright (c) Remix Software Inc.
|
|
5
5
|
*
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
11
|
import * as React from 'react';
|
|
12
|
-
import { Router, UNSAFE_NavigationContext, useHref, useResolvedPath, useLocation, UNSAFE_DataRouterStateContext, useNavigate, createPath, UNSAFE_RouteContext, UNSAFE_DataRouterContext, Routes, Route } from 'react-router';
|
|
13
|
-
export { MemoryRouter, Navigate, NavigationType, Outlet, Route, Router, Routes, UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext, createPath, createRoutesFromChildren, generatePath, matchPath, matchRoutes, parsePath, renderMatches, resolvePath, useHref, useInRouterContext, useLocation, useMatch, useNavigate, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRoutes } from 'react-router';
|
|
14
|
-
import { createBrowserHistory, createHashHistory, stripBasename, UNSAFE_invariant, joinPaths } from '@remix-run/router';
|
|
12
|
+
import { UNSAFE_detectErrorBoundary, Router, UNSAFE_NavigationContext, useHref, useResolvedPath, useLocation, UNSAFE_DataRouterStateContext, useNavigate, createPath, UNSAFE_RouteContext, useMatches, useNavigation, unstable_useBlocker, UNSAFE_DataRouterContext, Routes, Route } from 'react-router';
|
|
13
|
+
export { AbortedDeferredError, Await, MemoryRouter, Navigate, NavigationType, Outlet, Route, Router, RouterProvider, Routes, UNSAFE_DataRouterContext, UNSAFE_DataRouterStateContext, UNSAFE_LocationContext, UNSAFE_NavigationContext, UNSAFE_RouteContext, createMemoryRouter, createPath, createRoutesFromChildren, createRoutesFromElements, defer, generatePath, isRouteErrorResponse, json, matchPath, matchRoutes, parsePath, redirect, renderMatches, resolvePath, unstable_useBlocker, useActionData, useAsyncError, useAsyncValue, useHref, useInRouterContext, useLoaderData, useLocation, useMatch, useMatches, useNavigate, useNavigation, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRevalidator, useRouteError, useRouteLoaderData, useRoutes } from 'react-router';
|
|
14
|
+
import { createRouter, createBrowserHistory, createHashHistory, ErrorResponse, stripBasename, UNSAFE_warning, UNSAFE_invariant, joinPaths } from '@remix-run/router';
|
|
15
15
|
import { parsePath, Action, createPath as createPath$1 } from 'history';
|
|
16
16
|
import { Route as Route$1, useHistory } from 'react-router-dom';
|
|
17
17
|
|
|
@@ -192,6 +192,69 @@ function getFormSubmissionInfo(target, defaultAction, options) {
|
|
|
192
192
|
const _excluded = ["onClick", "relative", "reloadDocument", "replace", "state", "target", "to", "preventScrollReset"],
|
|
193
193
|
_excluded2 = ["aria-current", "caseSensitive", "className", "end", "style", "to", "children"],
|
|
194
194
|
_excluded3 = ["reloadDocument", "replace", "method", "action", "onSubmit", "fetcherKey", "routeId", "relative", "preventScrollReset"];
|
|
195
|
+
//#region Routers
|
|
196
|
+
////////////////////////////////////////////////////////////////////////////////
|
|
197
|
+
|
|
198
|
+
function createBrowserRouter(routes, opts) {
|
|
199
|
+
return createRouter({
|
|
200
|
+
basename: opts == null ? void 0 : opts.basename,
|
|
201
|
+
history: createBrowserHistory({
|
|
202
|
+
window: opts == null ? void 0 : opts.window
|
|
203
|
+
}),
|
|
204
|
+
hydrationData: (opts == null ? void 0 : opts.hydrationData) || parseHydrationData(),
|
|
205
|
+
routes,
|
|
206
|
+
detectErrorBoundary: UNSAFE_detectErrorBoundary
|
|
207
|
+
}).initialize();
|
|
208
|
+
}
|
|
209
|
+
function createHashRouter(routes, opts) {
|
|
210
|
+
return createRouter({
|
|
211
|
+
basename: opts == null ? void 0 : opts.basename,
|
|
212
|
+
history: createHashHistory({
|
|
213
|
+
window: opts == null ? void 0 : opts.window
|
|
214
|
+
}),
|
|
215
|
+
hydrationData: (opts == null ? void 0 : opts.hydrationData) || parseHydrationData(),
|
|
216
|
+
routes,
|
|
217
|
+
detectErrorBoundary: UNSAFE_detectErrorBoundary
|
|
218
|
+
}).initialize();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function parseHydrationData() {
|
|
222
|
+
var _window;
|
|
223
|
+
|
|
224
|
+
let state = (_window = window) == null ? void 0 : _window.__staticRouterHydrationData;
|
|
225
|
+
|
|
226
|
+
if (state && state.errors) {
|
|
227
|
+
state = _extends({}, state, {
|
|
228
|
+
errors: deserializeErrors(state.errors)
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return state;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function deserializeErrors(errors) {
|
|
236
|
+
if (!errors) return null;
|
|
237
|
+
let entries = Object.entries(errors);
|
|
238
|
+
let serialized = {};
|
|
239
|
+
|
|
240
|
+
for (let [key, val] of entries) {
|
|
241
|
+
// Hey you! If you change this, please change the corresponding logic in
|
|
242
|
+
// serializeErrors in react-router-dom/server.tsx :)
|
|
243
|
+
if (val && val.__type === "RouteErrorResponse") {
|
|
244
|
+
serialized[key] = new ErrorResponse(val.status, val.statusText, val.data, val.internal === true);
|
|
245
|
+
} else if (val && val.__type === "Error") {
|
|
246
|
+
let error = new Error(val.message); // Wipe away the client-side stack trace. Nothing to fill it in with
|
|
247
|
+
// because we don't serialize SSR stack traces for security reasons
|
|
248
|
+
|
|
249
|
+
error.stack = "";
|
|
250
|
+
serialized[key] = error;
|
|
251
|
+
} else {
|
|
252
|
+
serialized[key] = val;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return serialized;
|
|
257
|
+
}
|
|
195
258
|
/**
|
|
196
259
|
* A `<Router>` for use in web browsers. Provides the cleanest URLs.
|
|
197
260
|
*/
|
|
@@ -505,8 +568,27 @@ const FormImpl = /*#__PURE__*/React.forwardRef((_ref6, forwardedRef) => {
|
|
|
505
568
|
if (process.env.NODE_ENV !== "production") {
|
|
506
569
|
FormImpl.displayName = "FormImpl";
|
|
507
570
|
}
|
|
571
|
+
/**
|
|
572
|
+
* This component will emulate the browser's scroll restoration on location
|
|
573
|
+
* changes.
|
|
574
|
+
*/
|
|
508
575
|
|
|
509
|
-
|
|
576
|
+
|
|
577
|
+
function ScrollRestoration(_ref7) {
|
|
578
|
+
let {
|
|
579
|
+
getKey,
|
|
580
|
+
storageKey
|
|
581
|
+
} = _ref7;
|
|
582
|
+
useScrollRestoration({
|
|
583
|
+
getKey,
|
|
584
|
+
storageKey
|
|
585
|
+
});
|
|
586
|
+
return null;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (process.env.NODE_ENV !== "production") {
|
|
590
|
+
ScrollRestoration.displayName = "ScrollRestoration";
|
|
591
|
+
} //#endregion
|
|
510
592
|
////////////////////////////////////////////////////////////////////////////////
|
|
511
593
|
//#region Hooks
|
|
512
594
|
////////////////////////////////////////////////////////////////////////////////
|
|
@@ -536,6 +618,12 @@ function useDataRouterContext(hookName) {
|
|
|
536
618
|
!ctx ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
|
537
619
|
return ctx;
|
|
538
620
|
}
|
|
621
|
+
|
|
622
|
+
function useDataRouterState(hookName) {
|
|
623
|
+
let state = React.useContext(UNSAFE_DataRouterStateContext);
|
|
624
|
+
!state ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
|
625
|
+
return state;
|
|
626
|
+
}
|
|
539
627
|
/**
|
|
540
628
|
* Handles the click behavior for router `<Link>` components. This is useful if
|
|
541
629
|
* you need to create custom `<Link>` components with the same click behavior we
|
|
@@ -577,7 +665,7 @@ function useLinkClickHandler(to, _temp) {
|
|
|
577
665
|
*/
|
|
578
666
|
|
|
579
667
|
function useSearchParams(defaultInit) {
|
|
580
|
-
process.env.NODE_ENV !== "production" ?
|
|
668
|
+
process.env.NODE_ENV !== "production" ? UNSAFE_warning(typeof URLSearchParams !== "undefined", "You cannot use the `useSearchParams` hook in a browser that does not " + "support the URLSearchParams API. If you need to support Internet " + "Explorer 11, we recommend you load a polyfill such as " + "https://github.com/ungap/url-search-params\n\n" + "If you're unsure how to load polyfills, we recommend you check out " + "https://polyfill.io/v3/ which provides some recommendations about how " + "to load polyfills only for users that need them, instead of for every " + "user.") : void 0;
|
|
581
669
|
let defaultSearchParamsRef = React.useRef(createSearchParams(defaultInit));
|
|
582
670
|
let hasSetSearchParamsRef = React.useRef(false);
|
|
583
671
|
let location = useLocation();
|
|
@@ -593,6 +681,14 @@ function useSearchParams(defaultInit) {
|
|
|
593
681
|
}, [navigate, searchParams]);
|
|
594
682
|
return [searchParams, setSearchParams];
|
|
595
683
|
}
|
|
684
|
+
/**
|
|
685
|
+
* Returns a function that may be used to programmatically submit a form (or
|
|
686
|
+
* some arbitrary data) to the server.
|
|
687
|
+
*/
|
|
688
|
+
|
|
689
|
+
function useSubmit() {
|
|
690
|
+
return useSubmitImpl();
|
|
691
|
+
}
|
|
596
692
|
|
|
597
693
|
function useSubmitImpl(fetcherKey, routeId) {
|
|
598
694
|
let {
|
|
@@ -685,25 +781,251 @@ function useFormAction(action, _temp2) {
|
|
|
685
781
|
|
|
686
782
|
return createPath(path);
|
|
687
783
|
}
|
|
688
|
-
////////////////////////////////////////////////////////////////////////////////
|
|
689
|
-
//#region Utils
|
|
690
|
-
////////////////////////////////////////////////////////////////////////////////
|
|
691
784
|
|
|
692
|
-
function
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
throw new Error(message); // eslint-disable-next-line no-empty
|
|
704
|
-
} catch (e) {}
|
|
785
|
+
function createFetcherForm(fetcherKey, routeId) {
|
|
786
|
+
let FetcherForm = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
787
|
+
return /*#__PURE__*/React.createElement(FormImpl, _extends({}, props, {
|
|
788
|
+
ref: ref,
|
|
789
|
+
fetcherKey: fetcherKey,
|
|
790
|
+
routeId: routeId
|
|
791
|
+
}));
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
if (process.env.NODE_ENV !== "production") {
|
|
795
|
+
FetcherForm.displayName = "fetcher.Form";
|
|
705
796
|
}
|
|
706
|
-
|
|
797
|
+
|
|
798
|
+
return FetcherForm;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
let fetcherId = 0;
|
|
802
|
+
/**
|
|
803
|
+
* Interacts with route loaders and actions without causing a navigation. Great
|
|
804
|
+
* for any interaction that stays on the same page.
|
|
805
|
+
*/
|
|
806
|
+
|
|
807
|
+
function useFetcher() {
|
|
808
|
+
var _route$matches;
|
|
809
|
+
|
|
810
|
+
let {
|
|
811
|
+
router
|
|
812
|
+
} = useDataRouterContext(DataRouterHook.UseFetcher);
|
|
813
|
+
let route = React.useContext(UNSAFE_RouteContext);
|
|
814
|
+
!route ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "useFetcher must be used inside a RouteContext") : UNSAFE_invariant(false) : void 0;
|
|
815
|
+
let routeId = (_route$matches = route.matches[route.matches.length - 1]) == null ? void 0 : _route$matches.route.id;
|
|
816
|
+
!(routeId != null) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "useFetcher can only be used on routes that contain a unique \"id\"") : UNSAFE_invariant(false) : void 0;
|
|
817
|
+
let [fetcherKey] = React.useState(() => String(++fetcherId));
|
|
818
|
+
let [Form] = React.useState(() => {
|
|
819
|
+
!routeId ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "No routeId available for fetcher.Form()") : UNSAFE_invariant(false) : void 0;
|
|
820
|
+
return createFetcherForm(fetcherKey, routeId);
|
|
821
|
+
});
|
|
822
|
+
let [load] = React.useState(() => href => {
|
|
823
|
+
!router ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "No router available for fetcher.load()") : UNSAFE_invariant(false) : void 0;
|
|
824
|
+
!routeId ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "No routeId available for fetcher.load()") : UNSAFE_invariant(false) : void 0;
|
|
825
|
+
router.fetch(fetcherKey, routeId, href);
|
|
826
|
+
});
|
|
827
|
+
let submit = useSubmitImpl(fetcherKey, routeId);
|
|
828
|
+
let fetcher = router.getFetcher(fetcherKey);
|
|
829
|
+
let fetcherWithComponents = React.useMemo(() => _extends({
|
|
830
|
+
Form,
|
|
831
|
+
submit,
|
|
832
|
+
load
|
|
833
|
+
}, fetcher), [fetcher, Form, submit, load]);
|
|
834
|
+
React.useEffect(() => {
|
|
835
|
+
// Is this busted when the React team gets real weird and calls effects
|
|
836
|
+
// twice on mount? We really just need to garbage collect here when this
|
|
837
|
+
// fetcher is no longer around.
|
|
838
|
+
return () => {
|
|
839
|
+
if (!router) {
|
|
840
|
+
console.warn("No fetcher available to clean up from useFetcher()");
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
router.deleteFetcher(fetcherKey);
|
|
845
|
+
};
|
|
846
|
+
}, [router, fetcherKey]);
|
|
847
|
+
return fetcherWithComponents;
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Provides all fetchers currently on the page. Useful for layouts and parent
|
|
851
|
+
* routes that need to provide pending/optimistic UI regarding the fetch.
|
|
852
|
+
*/
|
|
853
|
+
|
|
854
|
+
function useFetchers() {
|
|
855
|
+
let state = useDataRouterState(DataRouterStateHook.UseFetchers);
|
|
856
|
+
return [...state.fetchers.values()];
|
|
857
|
+
}
|
|
858
|
+
const SCROLL_RESTORATION_STORAGE_KEY = "react-router-scroll-positions";
|
|
859
|
+
let savedScrollPositions = {};
|
|
860
|
+
/**
|
|
861
|
+
* When rendered inside a RouterProvider, will restore scroll positions on navigations
|
|
862
|
+
*/
|
|
863
|
+
|
|
864
|
+
function useScrollRestoration(_temp3) {
|
|
865
|
+
let {
|
|
866
|
+
getKey,
|
|
867
|
+
storageKey
|
|
868
|
+
} = _temp3 === void 0 ? {} : _temp3;
|
|
869
|
+
let {
|
|
870
|
+
router
|
|
871
|
+
} = useDataRouterContext(DataRouterHook.UseScrollRestoration);
|
|
872
|
+
let {
|
|
873
|
+
restoreScrollPosition,
|
|
874
|
+
preventScrollReset
|
|
875
|
+
} = useDataRouterState(DataRouterStateHook.UseScrollRestoration);
|
|
876
|
+
let location = useLocation();
|
|
877
|
+
let matches = useMatches();
|
|
878
|
+
let navigation = useNavigation(); // Trigger manual scroll restoration while we're active
|
|
879
|
+
|
|
880
|
+
React.useEffect(() => {
|
|
881
|
+
window.history.scrollRestoration = "manual";
|
|
882
|
+
return () => {
|
|
883
|
+
window.history.scrollRestoration = "auto";
|
|
884
|
+
};
|
|
885
|
+
}, []); // Save positions on pagehide
|
|
886
|
+
|
|
887
|
+
usePageHide(React.useCallback(() => {
|
|
888
|
+
if (navigation.state === "idle") {
|
|
889
|
+
let key = (getKey ? getKey(location, matches) : null) || location.key;
|
|
890
|
+
savedScrollPositions[key] = window.scrollY;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
sessionStorage.setItem(storageKey || SCROLL_RESTORATION_STORAGE_KEY, JSON.stringify(savedScrollPositions));
|
|
894
|
+
window.history.scrollRestoration = "auto";
|
|
895
|
+
}, [storageKey, getKey, navigation.state, location, matches])); // Read in any saved scroll locations
|
|
896
|
+
|
|
897
|
+
if (typeof document !== "undefined") {
|
|
898
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
899
|
+
React.useLayoutEffect(() => {
|
|
900
|
+
try {
|
|
901
|
+
let sessionPositions = sessionStorage.getItem(storageKey || SCROLL_RESTORATION_STORAGE_KEY);
|
|
902
|
+
|
|
903
|
+
if (sessionPositions) {
|
|
904
|
+
savedScrollPositions = JSON.parse(sessionPositions);
|
|
905
|
+
}
|
|
906
|
+
} catch (e) {// no-op, use default empty object
|
|
907
|
+
}
|
|
908
|
+
}, [storageKey]); // Enable scroll restoration in the router
|
|
909
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
910
|
+
|
|
911
|
+
React.useLayoutEffect(() => {
|
|
912
|
+
let disableScrollRestoration = router == null ? void 0 : router.enableScrollRestoration(savedScrollPositions, () => window.scrollY, getKey);
|
|
913
|
+
return () => disableScrollRestoration && disableScrollRestoration();
|
|
914
|
+
}, [router, getKey]); // Restore scrolling when state.restoreScrollPosition changes
|
|
915
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
916
|
+
|
|
917
|
+
React.useLayoutEffect(() => {
|
|
918
|
+
// Explicit false means don't do anything (used for submissions)
|
|
919
|
+
if (restoreScrollPosition === false) {
|
|
920
|
+
return;
|
|
921
|
+
} // been here before, scroll to it
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
if (typeof restoreScrollPosition === "number") {
|
|
925
|
+
window.scrollTo(0, restoreScrollPosition);
|
|
926
|
+
return;
|
|
927
|
+
} // try to scroll to the hash
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
if (location.hash) {
|
|
931
|
+
let el = document.getElementById(location.hash.slice(1));
|
|
932
|
+
|
|
933
|
+
if (el) {
|
|
934
|
+
el.scrollIntoView();
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
} // Don't reset if this navigation opted out
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
if (preventScrollReset === true) {
|
|
941
|
+
return;
|
|
942
|
+
} // otherwise go to the top on new locations
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
window.scrollTo(0, 0);
|
|
946
|
+
}, [location, restoreScrollPosition, preventScrollReset]);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Setup a callback to be fired on the window's `beforeunload` event. This is
|
|
951
|
+
* useful for saving some data to `window.localStorage` just before the page
|
|
952
|
+
* refreshes.
|
|
953
|
+
*
|
|
954
|
+
* Note: The `callback` argument should be a function created with
|
|
955
|
+
* `React.useCallback()`.
|
|
956
|
+
*/
|
|
957
|
+
|
|
958
|
+
function useBeforeUnload(callback, options) {
|
|
959
|
+
let {
|
|
960
|
+
capture
|
|
961
|
+
} = options || {};
|
|
962
|
+
React.useEffect(() => {
|
|
963
|
+
let opts = capture != null ? {
|
|
964
|
+
capture
|
|
965
|
+
} : undefined;
|
|
966
|
+
window.addEventListener("beforeunload", callback, opts);
|
|
967
|
+
return () => {
|
|
968
|
+
window.removeEventListener("beforeunload", callback, opts);
|
|
969
|
+
};
|
|
970
|
+
}, [callback, capture]);
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Setup a callback to be fired on the window's `pagehide` event. This is
|
|
974
|
+
* useful for saving some data to `window.localStorage` just before the page
|
|
975
|
+
* refreshes. This event is better supported than beforeunload across browsers.
|
|
976
|
+
*
|
|
977
|
+
* Note: The `callback` argument should be a function created with
|
|
978
|
+
* `React.useCallback()`.
|
|
979
|
+
*/
|
|
980
|
+
|
|
981
|
+
function usePageHide(callback, options) {
|
|
982
|
+
let {
|
|
983
|
+
capture
|
|
984
|
+
} = options || {};
|
|
985
|
+
React.useEffect(() => {
|
|
986
|
+
let opts = capture != null ? {
|
|
987
|
+
capture
|
|
988
|
+
} : undefined;
|
|
989
|
+
window.addEventListener("pagehide", callback, opts);
|
|
990
|
+
return () => {
|
|
991
|
+
window.removeEventListener("pagehide", callback, opts);
|
|
992
|
+
};
|
|
993
|
+
}, [callback, capture]);
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Wrapper around useBlocker to show a window.confirm prompt to users instead
|
|
997
|
+
* of building a custom UI with useBlocker.
|
|
998
|
+
*
|
|
999
|
+
* Warning: This has *a lot of rough edges* and behaves very differently (and
|
|
1000
|
+
* very incorrectly in some cases) across browsers if user click addition
|
|
1001
|
+
* back/forward navigations while the confirm is open. Use at your own risk.
|
|
1002
|
+
*/
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
function usePrompt(_ref8) {
|
|
1006
|
+
let {
|
|
1007
|
+
when,
|
|
1008
|
+
message
|
|
1009
|
+
} = _ref8;
|
|
1010
|
+
let blocker = unstable_useBlocker(when);
|
|
1011
|
+
React.useEffect(() => {
|
|
1012
|
+
if (blocker.state === "blocked" && !when) {
|
|
1013
|
+
blocker.reset();
|
|
1014
|
+
}
|
|
1015
|
+
}, [blocker, when]);
|
|
1016
|
+
React.useEffect(() => {
|
|
1017
|
+
if (blocker.state === "blocked") {
|
|
1018
|
+
let proceed = window.confirm(message);
|
|
1019
|
+
|
|
1020
|
+
if (proceed) {
|
|
1021
|
+
setTimeout(blocker.proceed, 0);
|
|
1022
|
+
} else {
|
|
1023
|
+
blocker.reset();
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}, [blocker, message]);
|
|
1027
|
+
}
|
|
1028
|
+
//#endregion
|
|
707
1029
|
|
|
708
1030
|
// but not worried about that for now.
|
|
709
1031
|
|
|
@@ -816,5 +1138,5 @@ function StaticRouter(_ref2) {
|
|
|
816
1138
|
});
|
|
817
1139
|
}
|
|
818
1140
|
|
|
819
|
-
export { BrowserRouter, CompatRoute, CompatRouter, HashRouter, Link, NavLink, StaticRouter, createSearchParams, HistoryRouter as unstable_HistoryRouter, useLinkClickHandler, useSearchParams };
|
|
1141
|
+
export { BrowserRouter, CompatRoute, CompatRouter, Form, HashRouter, Link, NavLink, ScrollRestoration, StaticRouter, useScrollRestoration as UNSAFE_useScrollRestoration, createBrowserRouter, createHashRouter, createSearchParams, HistoryRouter as unstable_HistoryRouter, usePrompt as unstable_usePrompt, useBeforeUnload, useFetcher, useFetchers, useFormAction, useLinkClickHandler, useSearchParams, useSubmit };
|
|
820
1142
|
//# sourceMappingURL=index.js.map
|