@multiplatform.one/router 5.0.16 → 5.0.17
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.cjs +5 -126
- package/dist/cjs/index.js +4 -106
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/index.native.js +6 -130
- package/dist/cjs/index.native.js.map +2 -2
- package/dist/cjs/router/index.cjs +74 -0
- package/dist/cjs/router/index.electron.cjs +74 -0
- package/dist/cjs/router/index.electron.js +48 -0
- package/dist/cjs/router/index.electron.js.map +6 -0
- package/dist/cjs/router/index.electron.native.js +96 -0
- package/dist/cjs/router/index.electron.native.js.map +6 -0
- package/dist/cjs/router/index.js +48 -0
- package/dist/cjs/router/index.js.map +6 -0
- package/dist/cjs/router/index.native.js +96 -0
- package/dist/cjs/router/index.native.js.map +6 -0
- package/dist/cjs/router/index.storybook-expo.cjs +55 -0
- package/dist/cjs/router/index.storybook-expo.js +47 -0
- package/dist/cjs/router/index.storybook-expo.js.map +6 -0
- package/dist/cjs/router/index.storybook-expo.native.js +77 -0
- package/dist/cjs/router/index.storybook-expo.native.js.map +6 -0
- package/dist/cjs/router/index.webext.cjs +74 -0
- package/dist/cjs/router/index.webext.js +48 -0
- package/dist/cjs/router/index.webext.js.map +6 -0
- package/dist/cjs/router/index.webext.native.js +96 -0
- package/dist/cjs/router/index.webext.native.js.map +6 -0
- package/dist/cjs/types.cjs +16 -0
- package/dist/cjs/types.js +14 -0
- package/dist/cjs/types.js.map +6 -0
- package/dist/cjs/types.native.js +15 -0
- package/dist/cjs/types.native.js.map +6 -0
- package/dist/esm/index.js +2 -112
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +2 -112
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +2 -122
- package/dist/esm/index.native.js.map +2 -2
- package/dist/esm/router/index.electron.js +38 -0
- package/dist/esm/router/index.electron.js.map +6 -0
- package/dist/esm/router/index.electron.mjs +48 -0
- package/dist/esm/router/index.electron.mjs.map +1 -0
- package/dist/esm/router/index.electron.native.js +74 -0
- package/dist/esm/router/index.electron.native.js.map +6 -0
- package/dist/esm/router/index.js +38 -0
- package/dist/esm/router/index.js.map +6 -0
- package/dist/esm/router/index.mjs +48 -0
- package/dist/esm/router/index.mjs.map +1 -0
- package/dist/esm/router/index.native.js +74 -0
- package/dist/esm/router/index.native.js.map +6 -0
- package/dist/esm/router/index.storybook-expo.js +31 -0
- package/dist/esm/router/index.storybook-expo.js.map +6 -0
- package/dist/esm/router/index.storybook-expo.mjs +29 -0
- package/dist/esm/router/index.storybook-expo.mjs.map +1 -0
- package/dist/esm/router/index.storybook-expo.native.js +54 -0
- package/dist/esm/router/index.storybook-expo.native.js.map +6 -0
- package/dist/esm/router/index.webext.js +38 -0
- package/dist/esm/router/index.webext.js.map +6 -0
- package/dist/esm/router/index.webext.mjs +48 -0
- package/dist/esm/router/index.webext.mjs.map +1 -0
- package/dist/esm/router/index.webext.native.js +74 -0
- package/dist/esm/router/index.webext.native.js.map +6 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +6 -0
- package/dist/esm/types.mjs +2 -0
- package/dist/esm/types.mjs.map +1 -0
- package/dist/esm/types.native.js +1 -0
- package/dist/esm/types.native.js.map +6 -0
- package/dist/jsx/index.js +2 -112
- package/dist/jsx/index.js.map +1 -1
- package/dist/jsx/index.mjs +2 -112
- package/dist/jsx/index.mjs.map +1 -1
- package/dist/jsx/index.native.js +2 -122
- package/dist/jsx/index.native.js.map +2 -2
- package/dist/jsx/router/index.electron.js +38 -0
- package/dist/jsx/router/index.electron.js.map +6 -0
- package/dist/jsx/router/index.electron.mjs +48 -0
- package/dist/jsx/router/index.electron.mjs.map +1 -0
- package/dist/jsx/router/index.electron.native.js +74 -0
- package/dist/jsx/router/index.electron.native.js.map +6 -0
- package/dist/jsx/router/index.js +38 -0
- package/dist/jsx/router/index.js.map +6 -0
- package/dist/jsx/router/index.mjs +48 -0
- package/dist/jsx/router/index.mjs.map +1 -0
- package/dist/jsx/router/index.native.js +74 -0
- package/dist/jsx/router/index.native.js.map +6 -0
- package/dist/jsx/router/index.storybook-expo.js +31 -0
- package/dist/jsx/router/index.storybook-expo.js.map +6 -0
- package/dist/jsx/router/index.storybook-expo.mjs +29 -0
- package/dist/jsx/router/index.storybook-expo.mjs.map +1 -0
- package/dist/jsx/router/index.storybook-expo.native.js +54 -0
- package/dist/jsx/router/index.storybook-expo.native.js.map +6 -0
- package/dist/jsx/router/index.webext.js +38 -0
- package/dist/jsx/router/index.webext.js.map +6 -0
- package/dist/jsx/router/index.webext.mjs +48 -0
- package/dist/jsx/router/index.webext.mjs.map +1 -0
- package/dist/jsx/router/index.webext.native.js +74 -0
- package/dist/jsx/router/index.webext.native.js.map +6 -0
- package/dist/jsx/types.js +1 -0
- package/dist/jsx/types.js.map +6 -0
- package/dist/jsx/types.mjs +2 -0
- package/dist/jsx/types.mjs.map +1 -0
- package/dist/jsx/types.native.js +1 -0
- package/dist/jsx/types.native.js.map +6 -0
- package/package.json +4 -1
- package/src/index.ts +6 -165
- package/src/router/index.electron.tsx +81 -0
- package/src/router/index.storybook-expo.tsx +56 -0
- package/src/router/index.tsx +76 -0
- package/src/router/index.webext.tsx +81 -0
- package/src/types.tsx +65 -0
- package/types/index.d.ts +2 -35
- package/types/index.d.ts.map +1 -1
- package/types/router/index.d.ts +27 -0
- package/types/router/index.d.ts.map +1 -0
- package/types/router/index.electron.d.ts +27 -0
- package/types/router/index.electron.d.ts.map +1 -0
- package/types/router/index.storybook-expo.d.ts +26 -0
- package/types/router/index.storybook-expo.d.ts.map +1 -0
- package/types/router/index.webext.d.ts +27 -0
- package/types/router/index.webext.d.ts.map +1 -0
- package/types/types.d.ts +58 -0
- package/types/types.d.ts.map +1 -0
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
2
|
* File: /src/index.ts
|
|
3
|
-
* Project: @multiplatform.one/
|
|
4
|
-
* File Created:
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 10-01-2025 15:02:36
|
|
5
5
|
* Author: Clay Risser
|
|
6
6
|
* -----
|
|
7
|
-
* BitSpur (c) Copyright 2021 -
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
8
|
*
|
|
9
9
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
10
|
* you may not use this file except in compliance with the License.
|
|
@@ -19,164 +19,5 @@
|
|
|
19
19
|
* limitations under the License.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
type OneRouter,
|
|
25
|
-
useLinkTo as useOneLinkTo,
|
|
26
|
-
useNavigation as useOneNavigation,
|
|
27
|
-
useParams as useOneParams,
|
|
28
|
-
usePathname as useOnePathname,
|
|
29
|
-
useRouter as useOneRouter,
|
|
30
|
-
} from "one";
|
|
31
|
-
import type { GestureResponderEvent } from "react-native";
|
|
32
|
-
import {
|
|
33
|
-
useLocation,
|
|
34
|
-
useNavigate,
|
|
35
|
-
useParams as useReactParams,
|
|
36
|
-
} from "react-router-dom";
|
|
37
|
-
|
|
38
|
-
const ROUTER_ERROR = "must use one or react-router-dom for router";
|
|
39
|
-
|
|
40
|
-
export function useRouter(): OneRouter.Router {
|
|
41
|
-
if (
|
|
42
|
-
typeof useLocation === "undefined" ||
|
|
43
|
-
typeof useNavigate === "undefined"
|
|
44
|
-
) {
|
|
45
|
-
if (typeof useOneRouter === "undefined") {
|
|
46
|
-
throw new Error(ROUTER_ERROR);
|
|
47
|
-
}
|
|
48
|
-
return useOneRouter();
|
|
49
|
-
}
|
|
50
|
-
const location = useLocation();
|
|
51
|
-
const navigate = useNavigate();
|
|
52
|
-
const oneRouter = useOneRouter();
|
|
53
|
-
return {
|
|
54
|
-
onLoadState: oneRouter.onLoadState,
|
|
55
|
-
subscribe: oneRouter.subscribe,
|
|
56
|
-
back() {
|
|
57
|
-
navigate(-1);
|
|
58
|
-
},
|
|
59
|
-
canGoBack() {
|
|
60
|
-
return window.history.length > 1;
|
|
61
|
-
},
|
|
62
|
-
push(href: OneRouter.Href, options?: OneRouter.LinkToOptions) {
|
|
63
|
-
navigate(href.toString(), { replace: false, ...options });
|
|
64
|
-
},
|
|
65
|
-
navigate(href: OneRouter.Href, options?: OneRouter.LinkToOptions) {
|
|
66
|
-
navigate(href.toString(), { replace: false, ...options });
|
|
67
|
-
},
|
|
68
|
-
replace(href: OneRouter.Href, options?: OneRouter.LinkToOptions) {
|
|
69
|
-
navigate(href.toString(), { replace: true, ...options });
|
|
70
|
-
},
|
|
71
|
-
dismiss(count = 1) {
|
|
72
|
-
navigate(-count);
|
|
73
|
-
},
|
|
74
|
-
dismissAll() {
|
|
75
|
-
navigate("/");
|
|
76
|
-
},
|
|
77
|
-
canDismiss() {
|
|
78
|
-
return location.pathname !== "/";
|
|
79
|
-
},
|
|
80
|
-
setParams<T = "">(
|
|
81
|
-
params?: T extends ""
|
|
82
|
-
? Record<string, string | undefined | null>
|
|
83
|
-
: OneRouter.InputRouteParams<T>,
|
|
84
|
-
) {
|
|
85
|
-
const searchParams = new URLSearchParams(location.search);
|
|
86
|
-
if (params) {
|
|
87
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
88
|
-
if (value === undefined || value === null) {
|
|
89
|
-
searchParams.delete(key);
|
|
90
|
-
} else {
|
|
91
|
-
searchParams.set(key, String(value));
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
navigate(`${location.pathname}?${searchParams.toString()}`);
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function usePathname(): string {
|
|
101
|
-
if (
|
|
102
|
-
typeof useLocation === "undefined" ||
|
|
103
|
-
typeof useNavigate === "undefined"
|
|
104
|
-
) {
|
|
105
|
-
if (typeof useOnePathname === "undefined") {
|
|
106
|
-
throw new Error(ROUTER_ERROR);
|
|
107
|
-
}
|
|
108
|
-
return useOnePathname();
|
|
109
|
-
}
|
|
110
|
-
return useLocation().pathname;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function useParams<T extends string | undefined = string>(): Record<
|
|
114
|
-
string,
|
|
115
|
-
T
|
|
116
|
-
> {
|
|
117
|
-
if (
|
|
118
|
-
typeof useLocation === "undefined" ||
|
|
119
|
-
typeof useNavigate === "undefined"
|
|
120
|
-
) {
|
|
121
|
-
if (typeof useOneParams === "undefined") {
|
|
122
|
-
throw new Error(ROUTER_ERROR);
|
|
123
|
-
}
|
|
124
|
-
// TODO: fix this
|
|
125
|
-
return useOneParams() as any;
|
|
126
|
-
}
|
|
127
|
-
return useReactParams() as Record<string, T>;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function useNavigation<
|
|
131
|
-
T = NavigationProp<ReactNavigation.RootParamList>,
|
|
132
|
-
>(parent?: string): T {
|
|
133
|
-
if (
|
|
134
|
-
typeof useLocation === "undefined" ||
|
|
135
|
-
typeof useNavigate === "undefined"
|
|
136
|
-
) {
|
|
137
|
-
if (typeof useOneNavigation === "undefined") {
|
|
138
|
-
throw new Error(ROUTER_ERROR);
|
|
139
|
-
}
|
|
140
|
-
return useOneNavigation(parent);
|
|
141
|
-
}
|
|
142
|
-
const navigate = useNavigate();
|
|
143
|
-
const location = useLocation();
|
|
144
|
-
return {
|
|
145
|
-
navigate(name: string, params?: Record<string, any>) {
|
|
146
|
-
navigate(name, { state: params });
|
|
147
|
-
},
|
|
148
|
-
goBack() {
|
|
149
|
-
navigate(-1);
|
|
150
|
-
},
|
|
151
|
-
setParams(params: Record<string, any>) {
|
|
152
|
-
navigate(location.pathname, { state: { ...location.state, ...params } });
|
|
153
|
-
},
|
|
154
|
-
} as T;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export function useLinkTo(props: { href: string; replace?: boolean }) {
|
|
158
|
-
if (
|
|
159
|
-
typeof useLocation === "undefined" ||
|
|
160
|
-
typeof useNavigate === "undefined"
|
|
161
|
-
) {
|
|
162
|
-
if (typeof useOneLinkTo === "undefined") {
|
|
163
|
-
throw new Error(ROUTER_ERROR);
|
|
164
|
-
}
|
|
165
|
-
return useOneLinkTo(props);
|
|
166
|
-
}
|
|
167
|
-
const navigate = useNavigate();
|
|
168
|
-
return {
|
|
169
|
-
href: props.href.toString(),
|
|
170
|
-
role: "link",
|
|
171
|
-
onPress: (
|
|
172
|
-
e?:
|
|
173
|
-
| React.MouseEvent<HTMLAnchorElement, MouseEvent>
|
|
174
|
-
| GestureResponderEvent,
|
|
175
|
-
) => {
|
|
176
|
-
if (e?.preventDefault) {
|
|
177
|
-
e.preventDefault();
|
|
178
|
-
}
|
|
179
|
-
navigate(props.href.toString(), { replace: props.replace });
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
}
|
|
22
|
+
export * from "./router/index";
|
|
23
|
+
export * from "./types";
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.electron.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:11:43
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { OneRouter } from "one";
|
|
23
|
+
import {
|
|
24
|
+
Link as ReactRouterLink,
|
|
25
|
+
useLocation,
|
|
26
|
+
useNavigate,
|
|
27
|
+
useParams as useReactParams,
|
|
28
|
+
} from "react-router-dom";
|
|
29
|
+
import type { Href, LinkComponent, Router } from "../types";
|
|
30
|
+
|
|
31
|
+
export const useRouter = (): Router => {
|
|
32
|
+
const location = useLocation();
|
|
33
|
+
const navigate = useNavigate();
|
|
34
|
+
return {
|
|
35
|
+
back: () => navigate(-1),
|
|
36
|
+
canGoBack: () => true,
|
|
37
|
+
push: (href: Href) => navigate(href as string, { replace: false }),
|
|
38
|
+
navigate: (href: Href) => navigate(href as string, { replace: false }),
|
|
39
|
+
replace: (href: Href) => navigate(href as string, { replace: true }),
|
|
40
|
+
dismiss: (count?: number) => navigate(-1 * (count || 1)),
|
|
41
|
+
dismissAll: () => navigate("/"),
|
|
42
|
+
canDismiss: () => true,
|
|
43
|
+
setParams: <T extends string = "">(
|
|
44
|
+
params?: T extends ""
|
|
45
|
+
? OneRouter.InputRouteParamsBlank
|
|
46
|
+
: OneRouter.InputRouteParams<T>,
|
|
47
|
+
) => {
|
|
48
|
+
const searchParams = new URLSearchParams(location.search);
|
|
49
|
+
if (params) {
|
|
50
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
51
|
+
if (value === undefined || value === null) {
|
|
52
|
+
searchParams.delete(key);
|
|
53
|
+
} else {
|
|
54
|
+
searchParams.set(key, String(value));
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
navigate({ search: searchParams.toString() });
|
|
59
|
+
},
|
|
60
|
+
subscribe: (listener) => {
|
|
61
|
+
listener("success");
|
|
62
|
+
return () => {};
|
|
63
|
+
},
|
|
64
|
+
onLoadState: (listener) => {
|
|
65
|
+
listener("loaded");
|
|
66
|
+
return () => {};
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const usePathname = (): string => useLocation().pathname;
|
|
72
|
+
|
|
73
|
+
export const useParams = useReactParams;
|
|
74
|
+
|
|
75
|
+
export const Link: LinkComponent = ({ href, replace, children, ...props }) => (
|
|
76
|
+
<ReactRouterLink to={href as string} replace={replace} {...props}>
|
|
77
|
+
{children}
|
|
78
|
+
</ReactRouterLink>
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
Link.resolveHref = (href: Href) => href.toString();
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.storybook-expo.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:12:32
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { Href, LinkComponent, Router } from "../types";
|
|
23
|
+
|
|
24
|
+
export const useRouter = (): Router => {
|
|
25
|
+
return {
|
|
26
|
+
back: () => {},
|
|
27
|
+
canGoBack: () => false,
|
|
28
|
+
push: () => {},
|
|
29
|
+
navigate: () => {},
|
|
30
|
+
replace: () => {},
|
|
31
|
+
dismiss: () => {},
|
|
32
|
+
dismissAll: () => {},
|
|
33
|
+
canDismiss: () => false,
|
|
34
|
+
setParams: () => {},
|
|
35
|
+
subscribe: (listener) => {
|
|
36
|
+
listener("success");
|
|
37
|
+
return () => {};
|
|
38
|
+
},
|
|
39
|
+
onLoadState: (listener) => {
|
|
40
|
+
listener("loaded");
|
|
41
|
+
return () => {};
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const usePathname = (): string => "/";
|
|
47
|
+
|
|
48
|
+
export const useParams = () => ({});
|
|
49
|
+
|
|
50
|
+
export const Link: LinkComponent = ({ href, replace, children, ...props }) => (
|
|
51
|
+
<a href={href as string} {...props}>
|
|
52
|
+
{children}
|
|
53
|
+
</a>
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
Link.resolveHref = (href: Href) => href.toString();
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:17:57
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import {
|
|
23
|
+
Link as ReactRouterLink,
|
|
24
|
+
useLocation,
|
|
25
|
+
useNavigate,
|
|
26
|
+
useParams as useReactParams,
|
|
27
|
+
} from "react-router-dom";
|
|
28
|
+
import type { Href, LinkComponent, Router } from "../types";
|
|
29
|
+
|
|
30
|
+
export const useRouter = (): Router => {
|
|
31
|
+
const location = useLocation();
|
|
32
|
+
const navigate = useNavigate();
|
|
33
|
+
return {
|
|
34
|
+
back: () => navigate(-1),
|
|
35
|
+
canGoBack: () => true,
|
|
36
|
+
push: (href: Href) => navigate(href.toString(), { replace: false }),
|
|
37
|
+
navigate: (href: Href) => navigate(href.toString(), { replace: false }),
|
|
38
|
+
replace: (href: Href) => navigate(href.toString(), { replace: true }),
|
|
39
|
+
dismiss: (count?: number) => navigate(-1 * (count || 1)),
|
|
40
|
+
dismissAll: () => navigate("/"),
|
|
41
|
+
canDismiss: () => true,
|
|
42
|
+
setParams: (params) => {
|
|
43
|
+
const searchParams = new URLSearchParams(location.search);
|
|
44
|
+
if (params) {
|
|
45
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
46
|
+
if (value === undefined || value === null) {
|
|
47
|
+
searchParams.delete(key);
|
|
48
|
+
} else {
|
|
49
|
+
searchParams.set(key, String(value));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
navigate({ search: searchParams.toString() });
|
|
54
|
+
},
|
|
55
|
+
subscribe: (listener) => {
|
|
56
|
+
listener("success");
|
|
57
|
+
return () => {};
|
|
58
|
+
},
|
|
59
|
+
onLoadState: (listener) => {
|
|
60
|
+
listener("loaded");
|
|
61
|
+
return () => {};
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const usePathname = (): string => useLocation().pathname;
|
|
67
|
+
|
|
68
|
+
export const useParams = useReactParams;
|
|
69
|
+
|
|
70
|
+
export const Link: LinkComponent = ({ href, replace, children, ...props }) => (
|
|
71
|
+
<ReactRouterLink to={href.toString()} replace={replace} {...props}>
|
|
72
|
+
{children}
|
|
73
|
+
</ReactRouterLink>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
Link.resolveHref = (href: Href) => href.toString();
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.webext.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 10-01-2025 15:02:36
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { OneRouter } from "one";
|
|
23
|
+
import {
|
|
24
|
+
Link as ReactRouterLink,
|
|
25
|
+
useLocation,
|
|
26
|
+
useNavigate,
|
|
27
|
+
useParams as useReactParams,
|
|
28
|
+
} from "react-router-dom";
|
|
29
|
+
import type { Href, LinkComponent, Router } from "../types";
|
|
30
|
+
|
|
31
|
+
export const useRouter = (): Router => {
|
|
32
|
+
const location = useLocation();
|
|
33
|
+
const navigate = useNavigate();
|
|
34
|
+
return {
|
|
35
|
+
back: () => navigate(-1),
|
|
36
|
+
canGoBack: () => true,
|
|
37
|
+
push: (href: Href) => navigate(href as string, { replace: false }),
|
|
38
|
+
navigate: (href: Href) => navigate(href as string, { replace: false }),
|
|
39
|
+
replace: (href: Href) => navigate(href as string, { replace: true }),
|
|
40
|
+
dismiss: (count?: number) => navigate(-1 * (count || 1)),
|
|
41
|
+
dismissAll: () => navigate("/"),
|
|
42
|
+
canDismiss: () => true,
|
|
43
|
+
setParams: <T extends string = "">(
|
|
44
|
+
params?: T extends ""
|
|
45
|
+
? OneRouter.InputRouteParamsBlank
|
|
46
|
+
: OneRouter.InputRouteParams<T>,
|
|
47
|
+
) => {
|
|
48
|
+
const searchParams = new URLSearchParams(location.search);
|
|
49
|
+
if (params) {
|
|
50
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
51
|
+
if (value === undefined || value === null) {
|
|
52
|
+
searchParams.delete(key);
|
|
53
|
+
} else {
|
|
54
|
+
searchParams.set(key, String(value));
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
navigate({ search: searchParams.toString() });
|
|
59
|
+
},
|
|
60
|
+
subscribe: (listener) => {
|
|
61
|
+
listener("success");
|
|
62
|
+
return () => {};
|
|
63
|
+
},
|
|
64
|
+
onLoadState: (listener) => {
|
|
65
|
+
listener("loaded");
|
|
66
|
+
return () => {};
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const usePathname = (): string => useLocation().pathname;
|
|
72
|
+
|
|
73
|
+
export const useParams = useReactParams;
|
|
74
|
+
|
|
75
|
+
export const Link: LinkComponent = ({ href, replace, children, ...props }) => (
|
|
76
|
+
<ReactRouterLink to={href as string} replace={replace} {...props}>
|
|
77
|
+
{children}
|
|
78
|
+
</ReactRouterLink>
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
Link.resolveHref = (href: Href) => href.toString();
|
package/src/types.tsx
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/types.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:28:42
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { OneRouter } from "one";
|
|
23
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
24
|
+
|
|
25
|
+
export type Href = OneRouter.Href<{ __branded__: any }>;
|
|
26
|
+
export type LoadingState = "loading" | "loaded" | "error";
|
|
27
|
+
export type ResultState = "success" | "error";
|
|
28
|
+
export type RootStateListener = (state: ResultState) => void;
|
|
29
|
+
export type LoadingStateListener = (state: LoadingState) => void;
|
|
30
|
+
|
|
31
|
+
export interface Router {
|
|
32
|
+
back: () => void;
|
|
33
|
+
canGoBack: () => boolean;
|
|
34
|
+
push: (href: Href) => void;
|
|
35
|
+
navigate: (href: Href) => void;
|
|
36
|
+
replace: (href: Href) => void;
|
|
37
|
+
dismiss: (count?: number) => void;
|
|
38
|
+
dismissAll: () => void;
|
|
39
|
+
canDismiss: () => boolean;
|
|
40
|
+
setParams: <T extends string = "">(
|
|
41
|
+
params?: T extends ""
|
|
42
|
+
? OneRouter.InputRouteParamsBlank
|
|
43
|
+
: OneRouter.InputRouteParams<T>,
|
|
44
|
+
) => void;
|
|
45
|
+
subscribe: (listener: RootStateListener) => () => void;
|
|
46
|
+
onLoadState: (listener: LoadingStateListener) => () => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type UseRouter = () => Router;
|
|
50
|
+
export type UsePathname = () => string;
|
|
51
|
+
export type UseParams = () => Record<string, string>;
|
|
52
|
+
|
|
53
|
+
export interface LinkProps {
|
|
54
|
+
href: Href;
|
|
55
|
+
replace?: boolean;
|
|
56
|
+
children?: ReactNode;
|
|
57
|
+
style?: CSSProperties;
|
|
58
|
+
className?: string;
|
|
59
|
+
[key: string]: any;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface LinkComponent {
|
|
63
|
+
(props: LinkProps): JSX.Element;
|
|
64
|
+
resolveHref: (href: Href) => string;
|
|
65
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,36 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
* Project: @multiplatform.one/use-store
|
|
4
|
-
* File Created: 20-11-2024 04:57:59
|
|
5
|
-
* Author: Clay Risser
|
|
6
|
-
* -----
|
|
7
|
-
* BitSpur (c) Copyright 2021 - 2024
|
|
8
|
-
*
|
|
9
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
-
* you may not use this file except in compliance with the License.
|
|
11
|
-
* You may obtain a copy of the License at
|
|
12
|
-
*
|
|
13
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
-
* See the License for the specific language governing permissions and
|
|
19
|
-
* limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
import type { NavigationProp } from "@react-navigation/native";
|
|
22
|
-
import { type OneRouter } from "one";
|
|
23
|
-
import type { GestureResponderEvent } from "react-native";
|
|
24
|
-
export declare function useRouter(): OneRouter.Router;
|
|
25
|
-
export declare function usePathname(): string;
|
|
26
|
-
export declare function useParams<T extends string | undefined = string>(): Record<string, T>;
|
|
27
|
-
export declare function useNavigation<T = NavigationProp<ReactNavigation.RootParamList>>(parent?: string): T;
|
|
28
|
-
export declare function useLinkTo(props: {
|
|
29
|
-
href: string;
|
|
30
|
-
replace?: boolean;
|
|
31
|
-
}): {
|
|
32
|
-
href: string;
|
|
33
|
-
role: string;
|
|
34
|
-
onPress: (e?: React.MouseEvent<HTMLAnchorElement, MouseEvent> | GestureResponderEvent) => void;
|
|
35
|
-
};
|
|
1
|
+
export * from "./router/index";
|
|
2
|
+
export * from "./types";
|
|
36
3
|
//# sourceMappingURL=index.d.ts.map
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAqBA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:17:57
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
import { useParams as useReactParams } from "react-router-dom";
|
|
22
|
+
import type { LinkComponent, Router } from "../types";
|
|
23
|
+
export declare const useRouter: () => Router;
|
|
24
|
+
export declare const usePathname: () => string;
|
|
25
|
+
export declare const useParams: typeof useReactParams;
|
|
26
|
+
export declare const Link: LinkComponent;
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAIL,SAAS,IAAI,cAAc,EAC5B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAQ,aAAa,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAE5D,eAAO,MAAM,SAAS,QAAO,MAkC5B,CAAC;AAEF,eAAO,MAAM,WAAW,QAAO,MAAgC,CAAC;AAEhE,eAAO,MAAM,SAAS,uBAAiB,CAAC;AAExC,eAAO,MAAM,IAAI,EAAE,aAIlB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.electron.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:11:43
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
import { useParams as useReactParams } from "react-router-dom";
|
|
22
|
+
import type { LinkComponent, Router } from "../types";
|
|
23
|
+
export declare const useRouter: () => Router;
|
|
24
|
+
export declare const usePathname: () => string;
|
|
25
|
+
export declare const useParams: typeof useReactParams;
|
|
26
|
+
export declare const Link: LinkComponent;
|
|
27
|
+
//# sourceMappingURL=index.electron.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.electron.d.ts","sourceRoot":"","sources":["../../src/router/index.electron.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EAIL,SAAS,IAAI,cAAc,EAC5B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAQ,aAAa,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAE5D,eAAO,MAAM,SAAS,QAAO,MAsC5B,CAAC;AAEF,eAAO,MAAM,WAAW,QAAO,MAAgC,CAAC;AAEhE,eAAO,MAAM,SAAS,uBAAiB,CAAC;AAExC,eAAO,MAAM,IAAI,EAAE,aAIlB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File: /src/router/index.storybook-expo.tsx
|
|
3
|
+
* Project: @multiplatform.one/router
|
|
4
|
+
* File Created: 23-01-2025 15:12:32
|
|
5
|
+
* Author: Clay Risser
|
|
6
|
+
* -----
|
|
7
|
+
* BitSpur (c) Copyright 2021 - 2025
|
|
8
|
+
*
|
|
9
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+
* you may not use this file except in compliance with the License.
|
|
11
|
+
* You may obtain a copy of the License at
|
|
12
|
+
*
|
|
13
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+
* See the License for the specific language governing permissions and
|
|
19
|
+
* limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
import type { LinkComponent, Router } from "../types";
|
|
22
|
+
export declare const useRouter: () => Router;
|
|
23
|
+
export declare const usePathname: () => string;
|
|
24
|
+
export declare const useParams: () => {};
|
|
25
|
+
export declare const Link: LinkComponent;
|
|
26
|
+
//# sourceMappingURL=index.storybook-expo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.storybook-expo.d.ts","sourceRoot":"","sources":["../../src/router/index.storybook-expo.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAQ,aAAa,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAE5D,eAAO,MAAM,SAAS,QAAO,MAoB5B,CAAC;AAEF,eAAO,MAAM,WAAW,QAAO,MAAa,CAAC;AAE7C,eAAO,MAAM,SAAS,UAAa,CAAC;AAEpC,eAAO,MAAM,IAAI,EAAE,aAIlB,CAAC"}
|