@multiplatform.one/router 0.0.8

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/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@multiplatform.one/router",
3
+ "version": "0.0.8",
4
+ "description": "multiplatform.one router with support for react-router-dom and react-navigation",
5
+ "sideEffects": false,
6
+ "keywords": [
7
+ "react",
8
+ "react-native"
9
+ ],
10
+ "homepage": "https://multiplatform.one",
11
+ "bugs": {
12
+ "url": "https://gitlab.com/bitspur/multiplatform.one/multiplatform.one/issues",
13
+ "email": "support@risserlabs.com"
14
+ },
15
+ "license": "Apache-2.0",
16
+ "author": "BitSpur <support@risserlabs.com> (https://risserlabs.com)",
17
+ "main": "dist/cjs",
18
+ "module": "dist/esm",
19
+ "module:jsx": "dist/jsx",
20
+ "react-native": "src/index.ts",
21
+ "source": "src/index.ts",
22
+ "types": "types/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./types/index.d.ts",
26
+ "import": "./dist/esm/index.mjs",
27
+ "require": "./dist/cjs/index.cjs"
28
+ },
29
+ "./package.json": "./package.json"
30
+ },
31
+ "files": [
32
+ "dist",
33
+ "src",
34
+ "types"
35
+ ],
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "https://gitlab.com/bitspur/multiplatform.one/multiplatform.one"
39
+ },
40
+ "engines": {
41
+ "node": ">=16.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@react-navigation/native": "^6.1.17",
45
+ "@tamagui/build": "^1.115.5",
46
+ "one": "^1.1.317",
47
+ "react-native": "0.74.2",
48
+ "react-router-dom": "^6.28.0",
49
+ "typescript": "^5.6.2",
50
+ "vitest": "^1.4.0",
51
+ "vite": "6.0.0-beta.1"
52
+ },
53
+ "peerDependencies": {
54
+ "one": "^1.1.317",
55
+ "react-router-dom": "^6.28.0"
56
+ },
57
+ "scripts": {
58
+ "build": "rm -rf dist types 2>/dev/null && tamagui-build",
59
+ "test": "vitest run"
60
+ }
61
+ }
package/src/index.ts ADDED
@@ -0,0 +1,181 @@
1
+ /**
2
+ * File: /src/index.ts
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
+
22
+ import type { NavigationProp } from "@react-navigation/native";
23
+ import {
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
+ return useOneParams();
125
+ }
126
+ return useReactParams() as Record<string, T>;
127
+ }
128
+
129
+ export function useNavigation<
130
+ T = NavigationProp<ReactNavigation.RootParamList>,
131
+ >(parent?: string): T {
132
+ if (
133
+ typeof useLocation === "undefined" ||
134
+ typeof useNavigate === "undefined"
135
+ ) {
136
+ if (typeof useOneNavigation === "undefined") {
137
+ throw new Error(ROUTER_ERROR);
138
+ }
139
+ return useOneNavigation(parent);
140
+ }
141
+ const navigate = useNavigate();
142
+ const location = useLocation();
143
+ return {
144
+ navigate(name: string, params?: Record<string, any>) {
145
+ navigate(name, { state: params });
146
+ },
147
+ goBack() {
148
+ navigate(-1);
149
+ },
150
+ setParams(params: Record<string, any>) {
151
+ navigate(location.pathname, { state: { ...location.state, ...params } });
152
+ },
153
+ } as T;
154
+ }
155
+
156
+ export function useLinkTo(props: { href: string; replace?: boolean }) {
157
+ if (
158
+ typeof useLocation === "undefined" ||
159
+ typeof useNavigate === "undefined"
160
+ ) {
161
+ if (typeof useOneLinkTo === "undefined") {
162
+ throw new Error(ROUTER_ERROR);
163
+ }
164
+ return useOneLinkTo(props);
165
+ }
166
+ const navigate = useNavigate();
167
+ return {
168
+ href: props.href.toString(),
169
+ role: "link",
170
+ onPress: (
171
+ e?:
172
+ | React.MouseEvent<HTMLAnchorElement, MouseEvent>
173
+ | GestureResponderEvent,
174
+ ) => {
175
+ if (e?.preventDefault) {
176
+ e.preventDefault();
177
+ }
178
+ navigate(props.href.toString(), { replace: props.replace });
179
+ },
180
+ };
181
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * File: /src/index.ts
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
+ };
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EACL,KAAK,SAAS,EAMf,MAAM,KAAK,CAAC;AACb,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAS1D,wBAAgB,SAAS,IAAI,SAAS,CAAC,MAAM,CA0D5C;AAED,wBAAgB,WAAW,IAAI,MAAM,CAWpC;AAED,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,KAAK,MAAM,CACxE,MAAM,EACN,CAAC,CACF,CAWA;AAED,wBAAgB,aAAa,CAC3B,CAAC,GAAG,cAAc,CAAC,eAAe,CAAC,aAAa,CAAC,EACjD,MAAM,CAAC,EAAE,MAAM,GAAG,CAAC,CAuBpB;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE;;;kBAgB1D,KAAK,CAAC,UAAU,CAAC,iBAAiB,EAAE,UAAU,CAAC,GAC/C,qBAAqB;EAQ9B"}