clear-react-router 2.2.5 → 2.2.7
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/LICENSE +21 -0
- package/README.md +22 -6
- package/dist/components/Link.d.ts +2 -2
- package/dist/index.js +83 -104
- package/dist/types.d.ts +9 -6
- package/package.json +3 -2
- package/dist/hooks/useSearch.d.ts +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrew Bubnov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
[](https://www.npmjs.com/package/clear-react-router)
|
|
2
|
+
[](https://github.com/AndrewBubnov/clear-react-router/actions/workflows/ci.yml)
|
|
3
|
+
|
|
1
4
|
# Clear Router
|
|
2
5
|
|
|
3
|
-
A lightweight
|
|
6
|
+
A lightweight routing library for client side React applications with nested routes, data loading, navigation blocking, prefetching, and route actions.
|
|
4
7
|
|
|
5
8
|
## Why Clear Router?
|
|
6
9
|
|
|
@@ -53,8 +56,7 @@ It provides first-class support for:
|
|
|
53
56
|
| `defaultRetry` | `number \| { count: number; delay: number }` | `optional` | Default cache revalidation retry policy for all routes |
|
|
54
57
|
| `defaultStaleTime` | `number` | `optional` | Default time in milliseconds before cached loader data is considered stale |
|
|
55
58
|
| `defaultBeforeLoad` | `({ params, context, redirect, setContext, location }) => Promise<unknown> \| undefined \| void` | `undefined` | Runs before every navigation. Useful for authentication, analytics, or updating shared context. |
|
|
56
|
-
| `defaultAfterLoad` | `({ params, context,
|
|
57
|
-
| `defaultPreserveScroll` | `boolean \| undefined` | `true` | Default value for save and restore scroll position when navigating between pages |
|
|
59
|
+
| `defaultAfterLoad` | `({ params, context, searchParams }) => Promise<void>` | `undefined` | Runs after every successful navigation. Useful for analytics, page tracking, or other global side effects. |
|
|
58
60
|
| `defaultPrefetch` | `'hover' \| 'render' \| 'viewport' \| 'none'` | `'hover'` for desktop, `'viewport'` for mobile | Default prefetch strategy for all `<Link>` components |
|
|
59
61
|
| `defaultHoverPrefetchDelay` | `number \| undefined` | `150` | Default delay in milliseconds before prefetching on hover (only for `'hover'` strategy) |
|
|
60
62
|
| `defaultScrollRestorationBehavior` | `'auto' \| 'smooth' \| 'instant'` | `auto` | Default scroll restoration behavior |
|
|
@@ -144,6 +146,20 @@ Component for client-side navigation with prefetch support, active state detecti
|
|
|
144
146
|
| `isActive` | `boolean` | `true` when the link's `to` matches the current URL considering `exact` value |
|
|
145
147
|
| `isPending` | `boolean` | `true` when the target route is currently loading (loader is running) |
|
|
146
148
|
|
|
149
|
+
### Accessibility
|
|
150
|
+
|
|
151
|
+
Active links expose `aria-current="page"` so screen readers announce the current page, and links whose target is loading expose `aria-busy="true"` while the loader runs. Both attributes are omitted when inactive, keeping the DOM clean.
|
|
152
|
+
|
|
153
|
+
Extra `data-*` and `aria-*` props passed to `<Link>` (e.g. `data-testid`, `aria-label`) are forwarded to the rendered element:
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
<Link to="/about" aria-label="About section" data-testid="about-link">
|
|
157
|
+
About
|
|
158
|
+
</Link>
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
When using a custom element via `as`, these attributes arrive in `props` together with everything else — spread them onto the host element as usual.
|
|
162
|
+
|
|
147
163
|
### Prefetch Strategies
|
|
148
164
|
|
|
149
165
|
| Strategy | Behavior |
|
|
@@ -216,7 +232,7 @@ import { Button } from '@mui/material';
|
|
|
216
232
|
```
|
|
217
233
|
```tsx
|
|
218
234
|
// Global prefetch: hover with 100ms delay
|
|
219
|
-
<Router routes={routes}
|
|
235
|
+
<Router routes={routes} defaultPrefetchh="hover" defaultHoverPrefetchDelay={100} />
|
|
220
236
|
|
|
221
237
|
// Override for a specific link
|
|
222
238
|
<Link to="/heavy-page" prefetch="viewport">
|
|
@@ -239,7 +255,7 @@ import { Button } from '@mui/material';
|
|
|
239
255
|
</Link>
|
|
240
256
|
|
|
241
257
|
// With dynamic style and search object
|
|
242
|
-
<Link to=
|
|
258
|
+
<Link to='/profile' search={{ user: 'John Doe' }} style={({ isActive }) => ({ fontWeight: isActive ? 'bold' : 'normal' })}>
|
|
243
259
|
Profile
|
|
244
260
|
</Link>
|
|
245
261
|
|
|
@@ -947,7 +963,7 @@ View Transitions API requires modern browsers:
|
|
|
947
963
|
For older browsers, the router gracefully falls back to regular navigation without animation.
|
|
948
964
|
|
|
949
965
|
## Requirements
|
|
950
|
-
- React 16.
|
|
966
|
+
- React 16.8+ (for React.lazy and Suspense)
|
|
951
967
|
- Use `default` export for your lazy-loaded components
|
|
952
968
|
|
|
953
969
|
## License
|
|
@@ -18,6 +18,6 @@ type LinkProps<T extends HTMLElement = HTMLAnchorElement> = {
|
|
|
18
18
|
beforeNavigate?(): Promise<void>;
|
|
19
19
|
style?: CSSProperties | ((arg: ElementState) => CSSProperties);
|
|
20
20
|
exact?: boolean;
|
|
21
|
-
}
|
|
22
|
-
export declare const Link: <T extends HTMLElement = HTMLAnchorElement>({ children, to, search, state, as, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact, activeClassName, pendingClassName, }: LinkProps<T>) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
21
|
+
} & Record<`data-${string}` | `aria-${string}`, unknown>;
|
|
22
|
+
export declare const Link: <T extends HTMLElement = HTMLAnchorElement>({ children, to, search, state, as, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact, activeClassName, pendingClassName, ...rest }: LinkProps<T>) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
23
23
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Suspense, lazy as lazy$1, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
-
|
|
3
|
-
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
|
|
4
|
-
//#endregion
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
3
|
//#region create.ts
|
|
6
4
|
var create = (initialState) => {
|
|
7
5
|
let state = initialState;
|
|
@@ -40,7 +38,7 @@ var createCommitState = ({ loaderState, routeItemDataState, blockerState }) => (
|
|
|
40
38
|
loaderState.setState(loaderStateValue);
|
|
41
39
|
if (blockerState.getState() === "blocked") blockerState.setState("unblocked");
|
|
42
40
|
if ((location.search ? `${location.pathname}${location.search}` : location.pathname) === window.location.pathname + window.location.search) return;
|
|
43
|
-
history.pushState(null, "", `${location.pathname}${location.search}`);
|
|
41
|
+
history.pushState(location.state ?? null, "", `${location.pathname}${location.search}`);
|
|
44
42
|
};
|
|
45
43
|
//#endregion
|
|
46
44
|
//#region constants.ts
|
|
@@ -140,7 +138,8 @@ var getParams = (location, routeItem) => {
|
|
|
140
138
|
};
|
|
141
139
|
var parseWindowLocation = (location) => ({
|
|
142
140
|
pathname: location.pathname,
|
|
143
|
-
search: location.search
|
|
141
|
+
search: location.search,
|
|
142
|
+
state: history.state ?? void 0
|
|
144
143
|
});
|
|
145
144
|
var comparePaths = (route, pathname) => {
|
|
146
145
|
const pattern = route.pattern.split("/").filter(Boolean);
|
|
@@ -228,6 +227,13 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
228
227
|
let navigationSeq = 0;
|
|
229
228
|
let interval = 0;
|
|
230
229
|
let abortController = null;
|
|
230
|
+
let pollingController = null;
|
|
231
|
+
const stopPolling = () => {
|
|
232
|
+
window.clearInterval(interval);
|
|
233
|
+
interval = 0;
|
|
234
|
+
pollingController?.abort();
|
|
235
|
+
pollingController = null;
|
|
236
|
+
};
|
|
231
237
|
const { loaderState, contextState, loaderMap, routeItemDataState, scrollMapState, blockerState, blockedTargetState } = routerState;
|
|
232
238
|
const commitState = createCommitState(routerState);
|
|
233
239
|
const isCacheItemFresh = createIsCacheItemFresh(loaderMap);
|
|
@@ -312,7 +318,9 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
312
318
|
};
|
|
313
319
|
const polling = (routeItem, nextLocation) => {
|
|
314
320
|
if (!routeItem?.pollingInterval) return;
|
|
315
|
-
|
|
321
|
+
pollingController?.abort();
|
|
322
|
+
pollingController = new AbortController();
|
|
323
|
+
const signal = pollingController.signal;
|
|
316
324
|
interval = window.setInterval(() => revalidateCache({
|
|
317
325
|
routeItem,
|
|
318
326
|
location: nextLocation,
|
|
@@ -325,7 +333,6 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
325
333
|
};
|
|
326
334
|
const loader = async (routeItem, nextLocation, seq) => {
|
|
327
335
|
if (!routeItem?.loader) return;
|
|
328
|
-
window.clearInterval(interval);
|
|
329
336
|
const signal = createSignal();
|
|
330
337
|
const [result] = await Promise.all([revalidateCache({
|
|
331
338
|
routeItem,
|
|
@@ -336,7 +343,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
336
343
|
polling(routeItem, nextLocation);
|
|
337
344
|
return result;
|
|
338
345
|
};
|
|
339
|
-
const afterLoad = async (routeItem, params) => {
|
|
346
|
+
const afterLoad = async (routeItem, location, params) => {
|
|
340
347
|
const { defaultAfterLoad } = routerConfig;
|
|
341
348
|
const searchParams = Object.fromEntries(new URLSearchParams(location.search).entries());
|
|
342
349
|
const args = {
|
|
@@ -372,6 +379,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
372
379
|
search: rawLocation.search ?? ""
|
|
373
380
|
};
|
|
374
381
|
if (checkBlocked(nextLocation)) return;
|
|
382
|
+
stopPolling();
|
|
375
383
|
navigationSeq = navigationSeq + 1;
|
|
376
384
|
const seq = navigationSeq;
|
|
377
385
|
const { nextItem, params } = routeResolve(nextLocation);
|
|
@@ -395,7 +403,7 @@ var createNavigate = (routerState, revalidateCache) => {
|
|
|
395
403
|
loaderStateValue
|
|
396
404
|
}));
|
|
397
405
|
clearCurrentGcTimeout(nextItem, nextLocation);
|
|
398
|
-
afterLoad(nextItem, params);
|
|
406
|
+
afterLoad(nextItem, nextLocation, params);
|
|
399
407
|
};
|
|
400
408
|
return navigate;
|
|
401
409
|
};
|
|
@@ -478,6 +486,7 @@ var getRetry = (routeItem) => {
|
|
|
478
486
|
};
|
|
479
487
|
var createRevalidateCache = (routerState) => {
|
|
480
488
|
const { loaderMap, loadingPromises, contextState } = routerState;
|
|
489
|
+
const flightOwners = /* @__PURE__ */ new Map();
|
|
481
490
|
const evict = () => {
|
|
482
491
|
if (loaderMap.size <= routerConfig.maxCacheSize) return;
|
|
483
492
|
const oldestKey = loaderMap.keys().next().value;
|
|
@@ -497,8 +506,15 @@ var createRevalidateCache = (routerState) => {
|
|
|
497
506
|
const { pathname, search = "" } = location;
|
|
498
507
|
const path = `${pathname}${search}`;
|
|
499
508
|
if (loadingPromises.has(path)) {
|
|
500
|
-
|
|
501
|
-
|
|
509
|
+
const owner = flightOwners.get(path);
|
|
510
|
+
if (signal && owner instanceof AbortController) {
|
|
511
|
+
owner.abort();
|
|
512
|
+
loadingPromises.delete(path);
|
|
513
|
+
flightOwners.delete(path);
|
|
514
|
+
} else {
|
|
515
|
+
moveItemToLastPosition(path);
|
|
516
|
+
return loadingPromises.get(path);
|
|
517
|
+
}
|
|
502
518
|
}
|
|
503
519
|
if (isCacheItemFresh(path)) {
|
|
504
520
|
const item = moveItemToLastPosition(path);
|
|
@@ -513,7 +529,11 @@ var createRevalidateCache = (routerState) => {
|
|
|
513
529
|
}
|
|
514
530
|
const promise = (async () => {
|
|
515
531
|
if (!routeItem?.loader) return;
|
|
516
|
-
const
|
|
532
|
+
const prefetchController = signal ? null : new AbortController();
|
|
533
|
+
const effectiveSignal = signal ?? prefetchController.signal;
|
|
534
|
+
const ownerToken = prefetchController ?? signal;
|
|
535
|
+
flightOwners.set(path, ownerToken);
|
|
536
|
+
const isOwner = () => flightOwners.get(path) === ownerToken;
|
|
517
537
|
try {
|
|
518
538
|
const result = await routeItem?.loader({
|
|
519
539
|
...getPartialLoaderArgs(contextState, location, routeItem),
|
|
@@ -540,7 +560,10 @@ var createRevalidateCache = (routerState) => {
|
|
|
540
560
|
};
|
|
541
561
|
const retry = getRetry(routeItem);
|
|
542
562
|
if (retry && retry.count > retried) {
|
|
543
|
-
|
|
563
|
+
if (isOwner()) {
|
|
564
|
+
loadingPromises.delete(path);
|
|
565
|
+
flightOwners.delete(path);
|
|
566
|
+
}
|
|
544
567
|
if (retry.delay) await sleep(retry.delay);
|
|
545
568
|
return revalidateCache({
|
|
546
569
|
routeItem,
|
|
@@ -552,7 +575,10 @@ var createRevalidateCache = (routerState) => {
|
|
|
552
575
|
error
|
|
553
576
|
};
|
|
554
577
|
} finally {
|
|
555
|
-
|
|
578
|
+
if (isOwner()) {
|
|
579
|
+
loadingPromises.delete(path);
|
|
580
|
+
flightOwners.delete(path);
|
|
581
|
+
}
|
|
556
582
|
}
|
|
557
583
|
})();
|
|
558
584
|
loadingPromises.set(path, promise);
|
|
@@ -640,10 +666,9 @@ var createRouterInstance = () => {
|
|
|
640
666
|
});
|
|
641
667
|
},
|
|
642
668
|
useAction: (action, options = {}) => {
|
|
643
|
-
const currentAction = getCurrentAction(action);
|
|
644
669
|
return async (input) => {
|
|
645
670
|
try {
|
|
646
|
-
const data = await
|
|
671
|
+
const data = await getCurrentAction(action)(input);
|
|
647
672
|
await invalidate();
|
|
648
673
|
options.onSuccess?.(data);
|
|
649
674
|
return {
|
|
@@ -685,7 +710,7 @@ var useNavigation = () => {
|
|
|
685
710
|
pathname: location.pathname,
|
|
686
711
|
search: location.search
|
|
687
712
|
};
|
|
688
|
-
if (blockerState === "charged") history.pushState(null, "", location.pathname);
|
|
713
|
+
if (blockerState === "charged") history.pushState(location.state ?? null, "", `${location.pathname}${location.search}`);
|
|
689
714
|
await navigate({
|
|
690
715
|
...newLocation,
|
|
691
716
|
prevLocation
|
|
@@ -697,6 +722,7 @@ var useNavigation = () => {
|
|
|
697
722
|
blockerState,
|
|
698
723
|
location.pathname,
|
|
699
724
|
location.search,
|
|
725
|
+
location.state,
|
|
700
726
|
navigate
|
|
701
727
|
]);
|
|
702
728
|
useEffect(() => {
|
|
@@ -769,62 +795,30 @@ var useReload = () => {
|
|
|
769
795
|
useEffect(() => {
|
|
770
796
|
if (!routerConfig.revalidateOnFocus) return;
|
|
771
797
|
const handler = () => {
|
|
772
|
-
if (document.visibilityState === "visible") invalidate(
|
|
798
|
+
if (document.visibilityState === "visible") invalidate(void 0, { staleOnly: true });
|
|
773
799
|
};
|
|
774
800
|
document.addEventListener("visibilitychange", handler);
|
|
775
801
|
return () => document.removeEventListener("visibilitychange", handler);
|
|
776
802
|
}, [invalidate]);
|
|
777
803
|
useEffect(() => {
|
|
778
804
|
if (!routerConfig.revalidateOnReconnect) return;
|
|
779
|
-
const handler = () => invalidate(
|
|
805
|
+
const handler = () => invalidate(void 0, { staleOnly: true });
|
|
780
806
|
window.addEventListener("online", handler);
|
|
781
807
|
return () => window.removeEventListener("online", handler);
|
|
782
808
|
}, [invalidate]);
|
|
783
809
|
};
|
|
784
810
|
//#endregion
|
|
785
|
-
//#region ../../node_modules/react/cjs/react-jsx-runtime.production.js
|
|
786
|
-
/**
|
|
787
|
-
* @license React
|
|
788
|
-
* react-jsx-runtime.production.js
|
|
789
|
-
*
|
|
790
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
791
|
-
*
|
|
792
|
-
* This source code is licensed under the MIT license found in the
|
|
793
|
-
* LICENSE file in the root directory of this source tree.
|
|
794
|
-
*/
|
|
795
|
-
var require_react_jsx_runtime_production = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
796
|
-
var REACT_ELEMENT_TYPE = Symbol.for("react.transitional.element");
|
|
797
|
-
function jsxProd(type, config, maybeKey) {
|
|
798
|
-
var key = null;
|
|
799
|
-
void 0 !== maybeKey && (key = "" + maybeKey);
|
|
800
|
-
void 0 !== config.key && (key = "" + config.key);
|
|
801
|
-
if ("key" in config) {
|
|
802
|
-
maybeKey = {};
|
|
803
|
-
for (var propName in config) "key" !== propName && (maybeKey[propName] = config[propName]);
|
|
804
|
-
} else maybeKey = config;
|
|
805
|
-
config = maybeKey.ref;
|
|
806
|
-
return {
|
|
807
|
-
$$typeof: REACT_ELEMENT_TYPE,
|
|
808
|
-
type,
|
|
809
|
-
key,
|
|
810
|
-
ref: void 0 !== config ? config : null,
|
|
811
|
-
props: maybeKey
|
|
812
|
-
};
|
|
813
|
-
}
|
|
814
|
-
exports.jsx = jsxProd;
|
|
815
|
-
exports.jsxs = jsxProd;
|
|
816
|
-
}));
|
|
817
|
-
//#endregion
|
|
818
811
|
//#region components/Spinner.tsx
|
|
819
|
-
var
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
812
|
+
var Spinner = () => /* @__PURE__ */ jsx("span", {
|
|
813
|
+
className: "cr-spinner",
|
|
814
|
+
role: "status",
|
|
815
|
+
"aria-label": "Loading"
|
|
816
|
+
});
|
|
823
817
|
//#endregion
|
|
824
818
|
//#region utils/renderElement.tsx
|
|
825
819
|
var renderElement = (Component) => {
|
|
826
820
|
if (!Component) return null;
|
|
827
|
-
return typeof Component === "function" ? /* @__PURE__ */
|
|
821
|
+
return typeof Component === "function" ? /* @__PURE__ */ jsx(Component, {}) : Component;
|
|
828
822
|
};
|
|
829
823
|
//#endregion
|
|
830
824
|
//#region components/Router.tsx
|
|
@@ -860,9 +854,9 @@ var Router = ({ routes, defaultBeforeLoad, defaultAfterLoad, animationDuration,
|
|
|
860
854
|
if (isLoading && !isError) return renderElement(routeItem?.loaderFallback || defaultLoaderFallback);
|
|
861
855
|
if (!routeItem) return null;
|
|
862
856
|
if (isError) return renderElement(routeItem.errorElement || defaultErrorElement);
|
|
863
|
-
return /* @__PURE__ */
|
|
857
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
864
858
|
style: { viewTransitionName: "page" },
|
|
865
|
-
children: [/* @__PURE__ */
|
|
859
|
+
children: [/* @__PURE__ */ jsx(ErrorBoundary, { children: renderElement(routeItem.element) }, location.pathname), optimisticSpinner && isOptimistic && /* @__PURE__ */ jsx(Spinner, {})]
|
|
866
860
|
});
|
|
867
861
|
};
|
|
868
862
|
//#endregion
|
|
@@ -883,8 +877,8 @@ var useLocation = () => {
|
|
|
883
877
|
};
|
|
884
878
|
//#endregion
|
|
885
879
|
//#region components/Link.tsx
|
|
886
|
-
var defaultAs = (props) => /* @__PURE__ */
|
|
887
|
-
var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact = false, activeClassName = "active-link", pendingClassName = "pending-link" }) => {
|
|
880
|
+
var defaultAs = (props) => /* @__PURE__ */ jsx("a", { ...props });
|
|
881
|
+
var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPrefetch, hoverPrefetchDelay, className, style, beforeNavigate, exact = false, activeClassName = "active-link", pendingClassName = "pending-link", ...rest }) => {
|
|
888
882
|
const isPending = useIsRoutePending(to);
|
|
889
883
|
const { pathname } = useLocation();
|
|
890
884
|
const navigate = useNavigate();
|
|
@@ -968,7 +962,10 @@ var Link = ({ children, to, search = "", state, as = defaultAs, prefetch: linkPr
|
|
|
968
962
|
href: `${to}${searchString}`,
|
|
969
963
|
onMouseEnter,
|
|
970
964
|
onMouseLeave,
|
|
971
|
-
children
|
|
965
|
+
children,
|
|
966
|
+
...rest,
|
|
967
|
+
"aria-current": isActive ? "page" : void 0,
|
|
968
|
+
"aria-busy": isPending || void 0
|
|
972
969
|
}, {
|
|
973
970
|
isActive,
|
|
974
971
|
isPending
|
|
@@ -1037,65 +1034,47 @@ var useRouterContext = () => {
|
|
|
1037
1034
|
};
|
|
1038
1035
|
};
|
|
1039
1036
|
//#endregion
|
|
1040
|
-
//#region hooks/useSearch.ts
|
|
1041
|
-
var isPatched = false;
|
|
1042
|
-
var notify = () => {
|
|
1043
|
-
window.dispatchEvent(new Event("locationChange"));
|
|
1044
|
-
};
|
|
1045
|
-
var patchHistory = () => {
|
|
1046
|
-
if (isPatched) return;
|
|
1047
|
-
isPatched = true;
|
|
1048
|
-
const push = history.pushState;
|
|
1049
|
-
const replace = history.replaceState;
|
|
1050
|
-
history.pushState = function(...args) {
|
|
1051
|
-
const result = push.apply(this, args);
|
|
1052
|
-
notify();
|
|
1053
|
-
return result;
|
|
1054
|
-
};
|
|
1055
|
-
history.replaceState = function(...args) {
|
|
1056
|
-
const result = replace.apply(this, args);
|
|
1057
|
-
notify();
|
|
1058
|
-
return result;
|
|
1059
|
-
};
|
|
1060
|
-
window.addEventListener("popstate", notify);
|
|
1061
|
-
};
|
|
1062
|
-
var subscribe = (onChange) => {
|
|
1063
|
-
window.addEventListener("locationChange", onChange);
|
|
1064
|
-
return () => window.removeEventListener("locationChange", onChange);
|
|
1065
|
-
};
|
|
1066
|
-
var getSnapshot = () => window.location.search;
|
|
1067
|
-
var useSearch = () => {
|
|
1068
|
-
useEffect(patchHistory, []);
|
|
1069
|
-
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
1070
|
-
};
|
|
1071
|
-
//#endregion
|
|
1072
1037
|
//#region hooks/useSearchParams.ts
|
|
1038
|
+
var useLatest = (value) => {
|
|
1039
|
+
const ref = useRef(value);
|
|
1040
|
+
useEffect(() => {
|
|
1041
|
+
ref.current = value;
|
|
1042
|
+
}, [value]);
|
|
1043
|
+
return ref;
|
|
1044
|
+
};
|
|
1073
1045
|
var useSearchParams = () => {
|
|
1074
|
-
const
|
|
1075
|
-
const
|
|
1076
|
-
const
|
|
1046
|
+
const [{ location }, setRouteItemData] = router.hooks.useRouteItemData();
|
|
1047
|
+
const { search = window.location.search, pathname = window.location.pathname } = location;
|
|
1048
|
+
const searchRef = useLatest(search);
|
|
1049
|
+
const searchParams = useMemo(() => new URLSearchParams(search), [search]);
|
|
1077
1050
|
const getSearchParams = useCallback((param) => {
|
|
1078
1051
|
const allValues = searchParams.getAll(param);
|
|
1079
1052
|
return allValues.length > 1 ? allValues : allValues[0] ?? "";
|
|
1080
1053
|
}, [searchParams]);
|
|
1081
1054
|
const navigateWithSearchParams = useCallback((params) => {
|
|
1082
1055
|
const newSearch = params.toString();
|
|
1083
|
-
const { pathname } = window.location;
|
|
1084
1056
|
const search = newSearch ? `?${newSearch}` : "";
|
|
1085
|
-
history.replaceState(
|
|
1086
|
-
|
|
1057
|
+
history.replaceState(history.state, "", pathname + search);
|
|
1058
|
+
setRouteItemData((prevState) => ({
|
|
1059
|
+
...prevState,
|
|
1060
|
+
location: {
|
|
1061
|
+
...prevState.location,
|
|
1062
|
+
search
|
|
1063
|
+
}
|
|
1064
|
+
}));
|
|
1065
|
+
}, [pathname, setRouteItemData]);
|
|
1087
1066
|
return {
|
|
1088
1067
|
searchParams,
|
|
1089
1068
|
getSearchParams,
|
|
1090
1069
|
setSearchParams: useCallback((param, value) => {
|
|
1091
|
-
const currentParams = new URLSearchParams(
|
|
1070
|
+
const currentParams = new URLSearchParams(searchRef.current);
|
|
1092
1071
|
if (typeof param === "string" && value !== void 0) {
|
|
1093
1072
|
currentParams.delete(param);
|
|
1094
1073
|
(Array.isArray(value) ? value : [value]).forEach((v) => currentParams.append(param, v));
|
|
1095
1074
|
navigateWithSearchParams(currentParams);
|
|
1096
1075
|
} else if (typeof param === "function") navigateWithSearchParams(param(currentParams));
|
|
1097
1076
|
else throw new Error("useSearchParams first argument must be either function or string");
|
|
1098
|
-
}, [navigateWithSearchParams,
|
|
1077
|
+
}, [navigateWithSearchParams, searchRef])
|
|
1099
1078
|
};
|
|
1100
1079
|
};
|
|
1101
1080
|
//#endregion
|
|
@@ -1131,9 +1110,9 @@ var useSubmitAction = (action, options) => {
|
|
|
1131
1110
|
var createLazyComponent = (importFn, fallback) => {
|
|
1132
1111
|
const load = () => importFn().then((module) => ({ default: module.default || module }));
|
|
1133
1112
|
const LazyComp = lazy$1(load);
|
|
1134
|
-
const Component = () => /* @__PURE__ */
|
|
1113
|
+
const Component = () => /* @__PURE__ */ jsx(Suspense, {
|
|
1135
1114
|
fallback: typeof fallback === "function" ? fallback() : fallback || null,
|
|
1136
|
-
children: /* @__PURE__ */
|
|
1115
|
+
children: /* @__PURE__ */ jsx(LazyComp, {})
|
|
1137
1116
|
});
|
|
1138
1117
|
return {
|
|
1139
1118
|
Component,
|
package/dist/types.d.ts
CHANGED
|
@@ -162,17 +162,20 @@ export type Options = Partial<{
|
|
|
162
162
|
export type InvalidateResult = {
|
|
163
163
|
path: string;
|
|
164
164
|
data: unknown;
|
|
165
|
+
error: Error | null;
|
|
165
166
|
};
|
|
166
167
|
export type ElementProps<T extends HTMLElement = HTMLElement> = {
|
|
167
|
-
ref: Ref<T>;
|
|
168
|
-
href: string;
|
|
169
|
-
className?: string;
|
|
170
|
-
style?: CSSProperties;
|
|
168
|
+
'ref': Ref<T>;
|
|
169
|
+
'href': string;
|
|
170
|
+
'className'?: string;
|
|
171
|
+
'style'?: CSSProperties;
|
|
171
172
|
onClick(event: MouseEvent): void;
|
|
172
173
|
onMouseEnter(event: MouseEvent): void;
|
|
173
174
|
onMouseLeave(event: MouseEvent): void;
|
|
174
|
-
children?: ReactNode;
|
|
175
|
-
|
|
175
|
+
'children'?: ReactNode;
|
|
176
|
+
'aria-current'?: 'page';
|
|
177
|
+
'aria-busy'?: boolean;
|
|
178
|
+
} & Record<`data-${string}` | `aria-${string}`, unknown>;
|
|
176
179
|
export type SearchObject = Record<string, string | number | boolean | null | undefined>;
|
|
177
180
|
export type NavigationLocation = Omit<Location, 'search'> & {
|
|
178
181
|
search?: string | SearchObject;
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clear-react-router",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.7",
|
|
4
4
|
"description": "A lightweight routing library for React applications",
|
|
5
5
|
"author": "Andrew Bubnov",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"build": "vite build && tsc --emitDeclarationOnly"
|
|
7
|
+
"build": "vite build && tsc --emitDeclarationOnly",
|
|
8
|
+
"prepublishOnly": "npm run build"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [
|
|
10
11
|
"react",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const useSearch: () => string;
|