@real-router/react 0.4.3 → 0.4.5
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/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/metafile-cjs.json +1 -1
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/metafile-esm.json +1 -1
- package/package.json +9 -7
- package/src/RouterProvider.tsx +64 -0
- package/src/components/BaseLink.tsx +130 -0
- package/src/components/ConnectedLink.tsx +20 -0
- package/src/components/Link.tsx +16 -0
- package/src/components/interfaces.ts +28 -0
- package/src/constants.ts +11 -0
- package/src/context.ts +12 -0
- package/src/hooks/useIsActiveRoute.tsx +95 -0
- package/src/hooks/useNavigator.tsx +17 -0
- package/src/hooks/useRoute.tsx +17 -0
- package/src/hooks/useRouteNode.tsx +74 -0
- package/src/hooks/useRouter.tsx +17 -0
- package/src/hooks/useRouterSubscription.tsx +81 -0
- package/src/hooks/useStableValue.tsx +29 -0
- package/src/index.ts +27 -0
- package/src/types.ts +36 -0
- package/src/utils.ts +71 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// packages/react/modules/utils.ts
|
|
2
|
+
|
|
3
|
+
import type { Params, Router, State } from "@real-router/core";
|
|
4
|
+
import type { MouseEvent } from "react";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Cache for shouldUpdateNode functions to avoid recreating them
|
|
8
|
+
*/
|
|
9
|
+
export const shouldUpdateCache = new WeakMap<
|
|
10
|
+
Router,
|
|
11
|
+
Map<string, (toState: State, fromState?: State) => boolean>
|
|
12
|
+
>();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Get cached shouldUpdateNode function for a router and nodeName
|
|
16
|
+
*/
|
|
17
|
+
export function getCachedShouldUpdate(
|
|
18
|
+
router: Router,
|
|
19
|
+
nodeName: string,
|
|
20
|
+
): (toState: State, fromState?: State) => boolean {
|
|
21
|
+
let cache = shouldUpdateCache.get(router);
|
|
22
|
+
|
|
23
|
+
if (!cache) {
|
|
24
|
+
cache = new Map();
|
|
25
|
+
shouldUpdateCache.set(router, cache);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let fn = cache.get(nodeName);
|
|
29
|
+
|
|
30
|
+
if (!fn) {
|
|
31
|
+
fn = router.shouldUpdateNode(nodeName);
|
|
32
|
+
|
|
33
|
+
const originalFn = fn;
|
|
34
|
+
|
|
35
|
+
fn = (toState: State, fromState?: State) => originalFn(toState, fromState);
|
|
36
|
+
|
|
37
|
+
cache.set(nodeName, fn);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return fn;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Check if navigation should be handled by router
|
|
45
|
+
*/
|
|
46
|
+
export function shouldNavigate(evt: MouseEvent): boolean {
|
|
47
|
+
return (
|
|
48
|
+
evt.button === 0 && // left click
|
|
49
|
+
!evt.metaKey &&
|
|
50
|
+
!evt.altKey &&
|
|
51
|
+
!evt.ctrlKey &&
|
|
52
|
+
!evt.shiftKey
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create cache key for route active check
|
|
58
|
+
*/
|
|
59
|
+
export function createActiveCheckKey(
|
|
60
|
+
routeName: string,
|
|
61
|
+
routeParams: Params,
|
|
62
|
+
activeStrict: boolean,
|
|
63
|
+
ignoreQueryParams: boolean,
|
|
64
|
+
): string {
|
|
65
|
+
return JSON.stringify({
|
|
66
|
+
routeName,
|
|
67
|
+
routeParams,
|
|
68
|
+
activeStrict,
|
|
69
|
+
ignoreQueryParams,
|
|
70
|
+
});
|
|
71
|
+
}
|