@real-router/solid 0.0.1
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/README.md +313 -0
- package/dist/cjs/index.d.ts +103 -0
- package/dist/cjs/index.js +351 -0
- package/dist/esm/index.d.mts +103 -0
- package/dist/esm/index.mjs +334 -0
- package/dist/types/RouterProvider.d.ts +8 -0
- package/dist/types/RouterProvider.d.ts.map +1 -0
- package/dist/types/components/Link.d.ts +5 -0
- package/dist/types/components/Link.d.ts.map +1 -0
- package/dist/types/components/RouteView/RouteView.d.ts +13 -0
- package/dist/types/components/RouteView/RouteView.d.ts.map +1 -0
- package/dist/types/components/RouteView/components.d.ts +25 -0
- package/dist/types/components/RouteView/components.d.ts.map +1 -0
- package/dist/types/components/RouteView/helpers.d.ts +8 -0
- package/dist/types/components/RouteView/helpers.d.ts.map +1 -0
- package/dist/types/components/RouteView/index.d.ts +3 -0
- package/dist/types/components/RouteView/index.d.ts.map +1 -0
- package/dist/types/components/RouteView/types.d.ts +15 -0
- package/dist/types/components/RouteView/types.d.ts.map +1 -0
- package/dist/types/constants.d.ts +9 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/context.d.ts +11 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/createSignalFromSource.d.ts +4 -0
- package/dist/types/createSignalFromSource.d.ts.map +1 -0
- package/dist/types/createStoreFromSource.d.ts +3 -0
- package/dist/types/createStoreFromSource.d.ts.map +1 -0
- package/dist/types/directives/link.d.ts +11 -0
- package/dist/types/directives/link.d.ts.map +1 -0
- package/dist/types/hooks/useNavigator.d.ts +3 -0
- package/dist/types/hooks/useNavigator.d.ts.map +1 -0
- package/dist/types/hooks/useRoute.d.ts +4 -0
- package/dist/types/hooks/useRoute.d.ts.map +1 -0
- package/dist/types/hooks/useRouteNode.d.ts +4 -0
- package/dist/types/hooks/useRouteNode.d.ts.map +1 -0
- package/dist/types/hooks/useRouteNodeStore.d.ts +3 -0
- package/dist/types/hooks/useRouteNodeStore.d.ts.map +1 -0
- package/dist/types/hooks/useRouteStore.d.ts +3 -0
- package/dist/types/hooks/useRouteStore.d.ts.map +1 -0
- package/dist/types/hooks/useRouteUtils.d.ts +3 -0
- package/dist/types/hooks/useRouteUtils.d.ts.map +1 -0
- package/dist/types/hooks/useRouter.d.ts +3 -0
- package/dist/types/hooks/useRouter.d.ts.map +1 -0
- package/dist/types/hooks/useRouterTransition.d.ts +4 -0
- package/dist/types/hooks/useRouterTransition.d.ts.map +1 -0
- package/dist/types/index.d.ts +21 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/types.d.ts +17 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +89 -0
- package/src/RouterProvider.tsx +60 -0
- package/src/components/Link.tsx +91 -0
- package/src/components/RouteView/RouteView.tsx +53 -0
- package/src/components/RouteView/components.tsx +50 -0
- package/src/components/RouteView/helpers.tsx +108 -0
- package/src/components/RouteView/index.ts +7 -0
- package/src/components/RouteView/types.ts +17 -0
- package/src/constants.ts +9 -0
- package/src/context.ts +15 -0
- package/src/createSignalFromSource.ts +20 -0
- package/src/createStoreFromSource.ts +20 -0
- package/src/directives/link.tsx +79 -0
- package/src/directives.d.ts +10 -0
- package/src/hooks/useNavigator.tsx +15 -0
- package/src/hooks/useRoute.tsx +16 -0
- package/src/hooks/useRouteNode.tsx +14 -0
- package/src/hooks/useRouteNodeStore.tsx +12 -0
- package/src/hooks/useRouteStore.tsx +20 -0
- package/src/hooks/useRouteUtils.tsx +12 -0
- package/src/hooks/useRouter.tsx +15 -0
- package/src/hooks/useRouterTransition.tsx +14 -0
- package/src/index.tsx +43 -0
- package/src/types.ts +24 -0
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@real-router/solid",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"description": "Solid.js integration for Real-Router",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.mjs",
|
|
8
|
+
"types": "./dist/cjs/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"development": "./src/index.tsx",
|
|
12
|
+
"types": {
|
|
13
|
+
"import": "./dist/esm/index.d.mts",
|
|
14
|
+
"require": "./dist/cjs/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"import": "./dist/esm/index.mjs",
|
|
17
|
+
"require": "./dist/cjs/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"homepage": "https://github.com/greydragon888/real-router",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/greydragon888/real-router.git"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/greydragon888/real-router/issues"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "rimraf dist && tsc -p tsconfig.build.json && rollup -c rollup.config.mjs",
|
|
34
|
+
"test": "vitest",
|
|
35
|
+
"test:stress": "vitest run --config vitest.config.stress.mts",
|
|
36
|
+
"type-check": "tsc --noEmit",
|
|
37
|
+
"lint": "eslint --cache --ext .ts,.tsx src/ tests/ --fix --max-warnings 0",
|
|
38
|
+
"lint:package": "publint",
|
|
39
|
+
"lint:types": "attw --pack ."
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"router",
|
|
43
|
+
"html5",
|
|
44
|
+
"history",
|
|
45
|
+
"tree",
|
|
46
|
+
"solid",
|
|
47
|
+
"solid-js",
|
|
48
|
+
"signals",
|
|
49
|
+
"reactive",
|
|
50
|
+
"hooks",
|
|
51
|
+
"components",
|
|
52
|
+
"functional",
|
|
53
|
+
"real-router"
|
|
54
|
+
],
|
|
55
|
+
"author": {
|
|
56
|
+
"name": "Oleg Ivanov",
|
|
57
|
+
"email": "greydragon888@gmail.com",
|
|
58
|
+
"url": "https://github.com/greydragon888"
|
|
59
|
+
},
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"sideEffects": false,
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@real-router/core": "workspace:^",
|
|
64
|
+
"@real-router/route-utils": "workspace:^",
|
|
65
|
+
"@real-router/sources": "workspace:^",
|
|
66
|
+
"dom-utils": "workspace:^"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@babel/core": "7.26.0",
|
|
70
|
+
"@babel/preset-typescript": "7.26.0",
|
|
71
|
+
"@real-router/browser-plugin": "workspace:^",
|
|
72
|
+
"@rollup/plugin-babel": "6.0.4",
|
|
73
|
+
"@rollup/plugin-node-resolve": "15.3.0",
|
|
74
|
+
"@solidjs/testing-library": "0.8.10",
|
|
75
|
+
"@testing-library/dom": "10.4.1",
|
|
76
|
+
"@testing-library/jest-dom": "6.9.1",
|
|
77
|
+
"@testing-library/user-event": "14.6.1",
|
|
78
|
+
"babel-preset-solid": "1.9.3",
|
|
79
|
+
"rimraf": "6.0.1",
|
|
80
|
+
"rollup": "4.27.4",
|
|
81
|
+
"rollup-plugin-dts": "6.1.1",
|
|
82
|
+
"solid-js": "1.9.5",
|
|
83
|
+
"vite-plugin-solid": "2.11.11",
|
|
84
|
+
"vitest": "4.1.0"
|
|
85
|
+
},
|
|
86
|
+
"peerDependencies": {
|
|
87
|
+
"solid-js": ">=1.7.0"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { getNavigator } from "@real-router/core";
|
|
2
|
+
import { createRouteSource } from "@real-router/sources";
|
|
3
|
+
import { createRouteAnnouncer } from "dom-utils";
|
|
4
|
+
import { createSelector, onCleanup, onMount } from "solid-js";
|
|
5
|
+
|
|
6
|
+
import { RouterContext, RouteContext } from "./context";
|
|
7
|
+
import { createSignalFromSource } from "./createSignalFromSource";
|
|
8
|
+
|
|
9
|
+
import type { Router } from "@real-router/core";
|
|
10
|
+
import type { ParentProps, JSX } from "solid-js";
|
|
11
|
+
|
|
12
|
+
export interface RouteProviderProps {
|
|
13
|
+
router: Router;
|
|
14
|
+
announceNavigation?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isRouteActive(
|
|
18
|
+
linkRouteName: string,
|
|
19
|
+
currentRouteName: string,
|
|
20
|
+
): boolean {
|
|
21
|
+
return (
|
|
22
|
+
currentRouteName === linkRouteName ||
|
|
23
|
+
currentRouteName.startsWith(`${linkRouteName}.`)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function RouterProvider(
|
|
28
|
+
props: ParentProps<RouteProviderProps>,
|
|
29
|
+
): JSX.Element {
|
|
30
|
+
onMount(() => {
|
|
31
|
+
if (!props.announceNavigation) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const announcer = createRouteAnnouncer(props.router);
|
|
36
|
+
|
|
37
|
+
onCleanup(() => {
|
|
38
|
+
announcer.destroy();
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const navigator = getNavigator(props.router);
|
|
43
|
+
const routeSource = createRouteSource(props.router);
|
|
44
|
+
const routeSignal = createSignalFromSource(routeSource);
|
|
45
|
+
|
|
46
|
+
const routeSelector = createSelector(
|
|
47
|
+
() => routeSignal().route?.name ?? "",
|
|
48
|
+
isRouteActive,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<RouterContext.Provider
|
|
53
|
+
value={{ router: props.router, navigator, routeSelector }}
|
|
54
|
+
>
|
|
55
|
+
<RouteContext.Provider value={routeSignal}>
|
|
56
|
+
{props.children}
|
|
57
|
+
</RouteContext.Provider>
|
|
58
|
+
</RouterContext.Provider>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createActiveRouteSource } from "@real-router/sources";
|
|
2
|
+
import { shouldNavigate, buildHref, buildActiveClassName } from "dom-utils";
|
|
3
|
+
import { createMemo, mergeProps, splitProps, useContext } from "solid-js";
|
|
4
|
+
|
|
5
|
+
import { EMPTY_PARAMS, EMPTY_OPTIONS } from "../constants";
|
|
6
|
+
import { RouterContext } from "../context";
|
|
7
|
+
import { createSignalFromSource } from "../createSignalFromSource";
|
|
8
|
+
import { useRouter } from "../hooks/useRouter";
|
|
9
|
+
|
|
10
|
+
import type { LinkProps } from "../types";
|
|
11
|
+
import type { Params } from "@real-router/core";
|
|
12
|
+
import type { JSX } from "solid-js";
|
|
13
|
+
|
|
14
|
+
export function Link<P extends Params = Params>(
|
|
15
|
+
props: Readonly<LinkProps<P>>,
|
|
16
|
+
): JSX.Element {
|
|
17
|
+
const merged = mergeProps(
|
|
18
|
+
{
|
|
19
|
+
routeParams: EMPTY_PARAMS as P,
|
|
20
|
+
routeOptions: EMPTY_OPTIONS,
|
|
21
|
+
activeClassName: "active",
|
|
22
|
+
activeStrict: false,
|
|
23
|
+
ignoreQueryParams: true,
|
|
24
|
+
},
|
|
25
|
+
props,
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const [local, rest] = splitProps(merged, [
|
|
29
|
+
"routeName",
|
|
30
|
+
"routeParams",
|
|
31
|
+
"routeOptions",
|
|
32
|
+
"activeClassName",
|
|
33
|
+
"activeStrict",
|
|
34
|
+
"ignoreQueryParams",
|
|
35
|
+
"onClick",
|
|
36
|
+
"target",
|
|
37
|
+
"class",
|
|
38
|
+
"children",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const router = useRouter();
|
|
42
|
+
const ctx = useContext(RouterContext);
|
|
43
|
+
|
|
44
|
+
const useFastPath =
|
|
45
|
+
ctx?.routeSelector &&
|
|
46
|
+
!local.activeStrict &&
|
|
47
|
+
local.ignoreQueryParams &&
|
|
48
|
+
local.routeParams === EMPTY_PARAMS;
|
|
49
|
+
|
|
50
|
+
const isActive = useFastPath
|
|
51
|
+
? () => ctx.routeSelector(local.routeName)
|
|
52
|
+
: createSignalFromSource(
|
|
53
|
+
createActiveRouteSource(router, local.routeName, local.routeParams, {
|
|
54
|
+
strict: local.activeStrict,
|
|
55
|
+
ignoreQueryParams: local.ignoreQueryParams,
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const href = createMemo(() =>
|
|
60
|
+
buildHref(router, local.routeName, local.routeParams),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const handleClick = (evt: MouseEvent) => {
|
|
64
|
+
if (local.onClick) {
|
|
65
|
+
local.onClick(evt);
|
|
66
|
+
|
|
67
|
+
if (evt.defaultPrevented) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!shouldNavigate(evt) || local.target === "_blank") {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
evt.preventDefault();
|
|
77
|
+
router
|
|
78
|
+
.navigate(local.routeName, local.routeParams, local.routeOptions)
|
|
79
|
+
.catch(() => {});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const finalClassName = createMemo(() =>
|
|
83
|
+
buildActiveClassName(isActive(), local.activeClassName, local.class),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<a {...rest} href={href()} class={finalClassName()} onClick={handleClick}>
|
|
88
|
+
{local.children}
|
|
89
|
+
</a>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { children as resolveChildren } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import { Match, NotFound } from "./components";
|
|
4
|
+
import { buildRenderList, collectElements } from "./helpers";
|
|
5
|
+
import { useRouteNode } from "../../hooks/useRouteNode";
|
|
6
|
+
|
|
7
|
+
import type { RouteViewMarker } from "./components";
|
|
8
|
+
import type { RouteViewProps } from "./types";
|
|
9
|
+
import type { JSX } from "solid-js";
|
|
10
|
+
|
|
11
|
+
function RouteViewRoot(props: Readonly<RouteViewProps>): JSX.Element {
|
|
12
|
+
const routeState = useRouteNode(props.nodeName);
|
|
13
|
+
|
|
14
|
+
const resolved = resolveChildren(() => props.children);
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<>
|
|
18
|
+
{(() => {
|
|
19
|
+
const state = routeState();
|
|
20
|
+
|
|
21
|
+
if (!state.route) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const elements: RouteViewMarker[] = [];
|
|
26
|
+
|
|
27
|
+
collectElements(resolved(), elements);
|
|
28
|
+
|
|
29
|
+
const { rendered } = buildRenderList(
|
|
30
|
+
elements,
|
|
31
|
+
state.route.name,
|
|
32
|
+
props.nodeName,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (rendered.length > 0) {
|
|
36
|
+
return rendered;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
})()}
|
|
41
|
+
</>
|
|
42
|
+
) as unknown as JSX.Element;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
RouteViewRoot.displayName = "RouteView";
|
|
46
|
+
|
|
47
|
+
export const RouteView = Object.assign(RouteViewRoot, { Match, NotFound });
|
|
48
|
+
|
|
49
|
+
export type {
|
|
50
|
+
RouteViewProps,
|
|
51
|
+
MatchProps as RouteViewMatchProps,
|
|
52
|
+
NotFoundProps as RouteViewNotFoundProps,
|
|
53
|
+
} from "./types";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { MatchProps, NotFoundProps } from "./types";
|
|
2
|
+
import type { JSX } from "solid-js";
|
|
3
|
+
|
|
4
|
+
export const MATCH_MARKER = Symbol.for("RouteView.Match");
|
|
5
|
+
|
|
6
|
+
export const NOT_FOUND_MARKER = Symbol.for("RouteView.NotFound");
|
|
7
|
+
|
|
8
|
+
export interface MatchMarker {
|
|
9
|
+
$$type: typeof MATCH_MARKER;
|
|
10
|
+
segment: string;
|
|
11
|
+
exact: boolean;
|
|
12
|
+
fallback?: JSX.Element;
|
|
13
|
+
children: JSX.Element;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface NotFoundMarker {
|
|
17
|
+
$$type: typeof NOT_FOUND_MARKER;
|
|
18
|
+
children: JSX.Element;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type RouteViewMarker = MatchMarker | NotFoundMarker;
|
|
22
|
+
|
|
23
|
+
export function Match(props: MatchProps): JSX.Element {
|
|
24
|
+
const result = {
|
|
25
|
+
$$type: MATCH_MARKER,
|
|
26
|
+
segment: props.segment,
|
|
27
|
+
exact: props.exact ?? false,
|
|
28
|
+
fallback: props.fallback,
|
|
29
|
+
get children(): JSX.Element {
|
|
30
|
+
return props.children;
|
|
31
|
+
},
|
|
32
|
+
} as MatchMarker;
|
|
33
|
+
|
|
34
|
+
return result as unknown as JSX.Element;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
Match.displayName = "RouteView.Match";
|
|
38
|
+
|
|
39
|
+
export function NotFound(props: NotFoundProps): JSX.Element {
|
|
40
|
+
const result = {
|
|
41
|
+
$$type: NOT_FOUND_MARKER,
|
|
42
|
+
get children(): JSX.Element {
|
|
43
|
+
return props.children;
|
|
44
|
+
},
|
|
45
|
+
} as NotFoundMarker;
|
|
46
|
+
|
|
47
|
+
return result as unknown as JSX.Element;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
NotFound.displayName = "RouteView.NotFound";
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { UNKNOWN_ROUTE } from "@real-router/core";
|
|
2
|
+
import { startsWithSegment } from "@real-router/route-utils";
|
|
3
|
+
import { Suspense } from "solid-js";
|
|
4
|
+
|
|
5
|
+
import { MATCH_MARKER, NOT_FOUND_MARKER } from "./components";
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
MatchMarker,
|
|
9
|
+
NotFoundMarker,
|
|
10
|
+
RouteViewMarker,
|
|
11
|
+
} from "./components";
|
|
12
|
+
import type { JSX } from "solid-js";
|
|
13
|
+
|
|
14
|
+
function isSegmentMatch(
|
|
15
|
+
routeName: string,
|
|
16
|
+
fullSegmentName: string,
|
|
17
|
+
exact: boolean,
|
|
18
|
+
): boolean {
|
|
19
|
+
if (exact) {
|
|
20
|
+
return routeName === fullSegmentName;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return startsWithSegment(routeName, fullSegmentName);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isMatchMarker(value: unknown): value is MatchMarker {
|
|
27
|
+
return (
|
|
28
|
+
value != null &&
|
|
29
|
+
typeof value === "object" &&
|
|
30
|
+
"$$type" in value &&
|
|
31
|
+
value.$$type === MATCH_MARKER
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isNotFoundMarker(value: unknown): value is NotFoundMarker {
|
|
36
|
+
return (
|
|
37
|
+
value != null &&
|
|
38
|
+
typeof value === "object" &&
|
|
39
|
+
"$$type" in value &&
|
|
40
|
+
value.$$type === NOT_FOUND_MARKER
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function collectElements(
|
|
45
|
+
children: unknown,
|
|
46
|
+
result: RouteViewMarker[],
|
|
47
|
+
): void {
|
|
48
|
+
if (children == null) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (Array.isArray(children)) {
|
|
53
|
+
for (const child of children) {
|
|
54
|
+
collectElements(child, result);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (isMatchMarker(children) || isNotFoundMarker(children)) {
|
|
61
|
+
result.push(children);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function buildRenderList(
|
|
66
|
+
elements: RouteViewMarker[],
|
|
67
|
+
routeName: string,
|
|
68
|
+
nodeName: string,
|
|
69
|
+
): { rendered: JSX.Element[]; activeMatchFound: boolean } {
|
|
70
|
+
let notFoundChildren: JSX.Element | null = null;
|
|
71
|
+
let activeMatchFound = false;
|
|
72
|
+
const rendered: JSX.Element[] = [];
|
|
73
|
+
|
|
74
|
+
for (const child of elements) {
|
|
75
|
+
if (isNotFoundMarker(child)) {
|
|
76
|
+
notFoundChildren = child.children;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const { segment, exact, fallback } = child;
|
|
81
|
+
const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
|
|
82
|
+
const isActive =
|
|
83
|
+
!activeMatchFound && isSegmentMatch(routeName, fullSegmentName, exact);
|
|
84
|
+
|
|
85
|
+
if (isActive) {
|
|
86
|
+
activeMatchFound = true;
|
|
87
|
+
const matchContent = child.children;
|
|
88
|
+
const content =
|
|
89
|
+
fallback === undefined ? (
|
|
90
|
+
matchContent
|
|
91
|
+
) : (
|
|
92
|
+
<Suspense fallback={fallback}>{matchContent}</Suspense>
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
rendered.push(content);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
!activeMatchFound &&
|
|
101
|
+
routeName === UNKNOWN_ROUTE &&
|
|
102
|
+
notFoundChildren !== null
|
|
103
|
+
) {
|
|
104
|
+
rendered.push(notFoundChildren);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { rendered, activeMatchFound };
|
|
108
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { JSX } from "solid-js";
|
|
2
|
+
|
|
3
|
+
export interface RouteViewProps {
|
|
4
|
+
readonly nodeName: string;
|
|
5
|
+
readonly children: JSX.Element;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface MatchProps {
|
|
9
|
+
readonly segment: string;
|
|
10
|
+
readonly exact?: boolean;
|
|
11
|
+
readonly fallback?: JSX.Element;
|
|
12
|
+
readonly children: JSX.Element;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface NotFoundProps {
|
|
16
|
+
readonly children: JSX.Element;
|
|
17
|
+
}
|
package/src/constants.ts
ADDED
package/src/context.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createContext } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import type { RouteState } from "./types";
|
|
4
|
+
import type { Router, Navigator } from "@real-router/core";
|
|
5
|
+
import type { Accessor } from "solid-js";
|
|
6
|
+
|
|
7
|
+
export interface RouterContextValue {
|
|
8
|
+
router: Router;
|
|
9
|
+
navigator: Navigator;
|
|
10
|
+
routeSelector: (routeName: string) => boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const RouterContext = createContext<RouterContextValue | null>(null);
|
|
14
|
+
|
|
15
|
+
export const RouteContext = createContext<Accessor<RouteState> | null>(null);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createSignal, onCleanup } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import type { RouterSource } from "@real-router/sources";
|
|
4
|
+
import type { Accessor } from "solid-js";
|
|
5
|
+
|
|
6
|
+
export function createSignalFromSource<T>(
|
|
7
|
+
source: RouterSource<T>,
|
|
8
|
+
): Accessor<T> {
|
|
9
|
+
const [value, setValue] = createSignal<T>(source.getSnapshot());
|
|
10
|
+
|
|
11
|
+
const unsubscribe = source.subscribe(() => {
|
|
12
|
+
setValue(() => source.getSnapshot());
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
onCleanup(() => {
|
|
16
|
+
unsubscribe();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { onCleanup } from "solid-js";
|
|
2
|
+
import { createStore, reconcile } from "solid-js/store";
|
|
3
|
+
|
|
4
|
+
import type { RouterSource } from "@real-router/sources";
|
|
5
|
+
|
|
6
|
+
export function createStoreFromSource<T extends object>(
|
|
7
|
+
source: RouterSource<T>,
|
|
8
|
+
): T {
|
|
9
|
+
const [state, setState] = createStore<T>(
|
|
10
|
+
structuredClone(source.getSnapshot()),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const unsubscribe = source.subscribe(() => {
|
|
14
|
+
setState(reconcile(source.getSnapshot()));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
onCleanup(unsubscribe);
|
|
18
|
+
|
|
19
|
+
return state;
|
|
20
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createActiveRouteSource } from "@real-router/sources";
|
|
2
|
+
import { shouldNavigate, applyLinkA11y } from "dom-utils";
|
|
3
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
4
|
+
|
|
5
|
+
import { EMPTY_PARAMS, EMPTY_OPTIONS } from "../constants";
|
|
6
|
+
import { createSignalFromSource } from "../createSignalFromSource";
|
|
7
|
+
import { useRouter } from "../hooks/useRouter";
|
|
8
|
+
|
|
9
|
+
import type { Params } from "@real-router/core";
|
|
10
|
+
|
|
11
|
+
export interface LinkDirectiveOptions<P extends Params = Params> {
|
|
12
|
+
routeName: string;
|
|
13
|
+
routeParams?: P;
|
|
14
|
+
routeOptions?: Record<string, unknown>;
|
|
15
|
+
activeClassName?: string;
|
|
16
|
+
activeStrict?: boolean;
|
|
17
|
+
ignoreQueryParams?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function link<P extends Params = Params>(
|
|
21
|
+
element: HTMLElement,
|
|
22
|
+
accessor: () => LinkDirectiveOptions<P>,
|
|
23
|
+
): void {
|
|
24
|
+
const router = useRouter();
|
|
25
|
+
const options = accessor();
|
|
26
|
+
|
|
27
|
+
// Set href on <a> elements
|
|
28
|
+
if (element instanceof HTMLAnchorElement) {
|
|
29
|
+
element.href = router.buildPath(
|
|
30
|
+
options.routeName,
|
|
31
|
+
options.routeParams ?? (EMPTY_PARAMS as P),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
applyLinkA11y(element);
|
|
36
|
+
|
|
37
|
+
// Active class tracking (reactive)
|
|
38
|
+
if (options.activeClassName) {
|
|
39
|
+
const activeClassName = options.activeClassName;
|
|
40
|
+
const activeSource = createActiveRouteSource(
|
|
41
|
+
router,
|
|
42
|
+
options.routeName,
|
|
43
|
+
options.routeParams ?? (EMPTY_PARAMS as P),
|
|
44
|
+
{
|
|
45
|
+
strict: options.activeStrict ?? false,
|
|
46
|
+
ignoreQueryParams: options.ignoreQueryParams ?? true,
|
|
47
|
+
},
|
|
48
|
+
);
|
|
49
|
+
const isActive = createSignalFromSource(activeSource);
|
|
50
|
+
|
|
51
|
+
createEffect(() => {
|
|
52
|
+
element.classList.toggle(activeClassName, isActive());
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Click handler
|
|
57
|
+
function handleClick(evt: MouseEvent) {
|
|
58
|
+
if (!shouldNavigate(evt)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (element instanceof HTMLAnchorElement) {
|
|
62
|
+
evt.preventDefault();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
router
|
|
66
|
+
.navigate(
|
|
67
|
+
options.routeName,
|
|
68
|
+
options.routeParams ?? (EMPTY_PARAMS as P),
|
|
69
|
+
options.routeOptions ?? EMPTY_OPTIONS,
|
|
70
|
+
)
|
|
71
|
+
.catch(() => {});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
element.addEventListener("click", handleClick);
|
|
75
|
+
|
|
76
|
+
onCleanup(() => {
|
|
77
|
+
element.removeEventListener("click", handleClick);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useContext } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import { RouterContext } from "../context";
|
|
4
|
+
|
|
5
|
+
import type { Navigator } from "@real-router/core";
|
|
6
|
+
|
|
7
|
+
export const useNavigator = (): Navigator => {
|
|
8
|
+
const ctx = useContext(RouterContext);
|
|
9
|
+
|
|
10
|
+
if (!ctx) {
|
|
11
|
+
throw new Error("useNavigator must be used within a RouterProvider");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return ctx.navigator;
|
|
15
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useContext } from "solid-js";
|
|
2
|
+
|
|
3
|
+
import { RouteContext } from "../context";
|
|
4
|
+
|
|
5
|
+
import type { RouteState } from "../types";
|
|
6
|
+
import type { Accessor } from "solid-js";
|
|
7
|
+
|
|
8
|
+
export const useRoute = (): Accessor<RouteState> => {
|
|
9
|
+
const routeSignal = useContext(RouteContext);
|
|
10
|
+
|
|
11
|
+
if (!routeSignal) {
|
|
12
|
+
throw new Error("useRoute must be used within a RouterProvider");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return routeSignal;
|
|
16
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createRouteNodeSource } from "@real-router/sources";
|
|
2
|
+
|
|
3
|
+
import { createSignalFromSource } from "../createSignalFromSource";
|
|
4
|
+
import { useRouter } from "./useRouter";
|
|
5
|
+
|
|
6
|
+
import type { RouteState } from "../types";
|
|
7
|
+
import type { Accessor } from "solid-js";
|
|
8
|
+
|
|
9
|
+
export function useRouteNode(nodeName: string): Accessor<RouteState> {
|
|
10
|
+
const router = useRouter();
|
|
11
|
+
const store = createRouteNodeSource(router, nodeName);
|
|
12
|
+
|
|
13
|
+
return createSignalFromSource(store);
|
|
14
|
+
}
|