@aminnairi/react-router 3.0.1 → 3.0.2
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/index.d.ts +63 -0
- package/dist/index.js +689 -0
- package/package.json +10 -4
- package/index.tsx +0 -527
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Dispatch, type FunctionComponent, type ReactNode, type SetStateAction } from "react";
|
|
2
|
+
export interface IssueProps {
|
|
3
|
+
error: Error | null;
|
|
4
|
+
resetError: () => void;
|
|
5
|
+
}
|
|
6
|
+
export interface Router<Path extends string, Locale> {
|
|
7
|
+
prefix?: string;
|
|
8
|
+
locales?: Locale[];
|
|
9
|
+
transition?: Transition;
|
|
10
|
+
fallback: FunctionComponent;
|
|
11
|
+
issue: FunctionComponent<IssueProps>;
|
|
12
|
+
pages: Array<Page<Path>>;
|
|
13
|
+
}
|
|
14
|
+
export interface RouterProviderProps {
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
}
|
|
17
|
+
export interface RouterContextInterface<Locale> {
|
|
18
|
+
locale: Locale | null;
|
|
19
|
+
prefix: string | null;
|
|
20
|
+
path: string;
|
|
21
|
+
setLocale: Dispatch<SetStateAction<Locale | null>>;
|
|
22
|
+
}
|
|
23
|
+
export type NavigationDirection = "forward" | "backward";
|
|
24
|
+
export declare class Uri<Locale> {
|
|
25
|
+
readonly path: string;
|
|
26
|
+
readonly prefix: string | null;
|
|
27
|
+
readonly locale: Locale | null;
|
|
28
|
+
private constructor();
|
|
29
|
+
static from<Locale>(uri: string, expectedPrefix?: string, expectedLocales?: Locale[]): Uri<Locale>;
|
|
30
|
+
}
|
|
31
|
+
export type ExtractParams<Path extends string> = Path extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractParams<Rest> : Path extends `${string}:${infer Param}` ? Param : never;
|
|
32
|
+
export type Params<Path extends string> = {
|
|
33
|
+
[Key in ExtractParams<Path>]: string;
|
|
34
|
+
};
|
|
35
|
+
export interface PageParams<Path extends string> {
|
|
36
|
+
parameters: Params<Path>;
|
|
37
|
+
}
|
|
38
|
+
export interface Page<Path extends string> {
|
|
39
|
+
path: Path;
|
|
40
|
+
element: FunctionComponent<PageParams<Path>>;
|
|
41
|
+
}
|
|
42
|
+
export declare function createPage<Path extends string>(page: Page<Path>): Page<Path>;
|
|
43
|
+
export type Transition = (direction: "forward" | "backward", next: () => void) => void;
|
|
44
|
+
export declare const scaleFadeTransition: Transition;
|
|
45
|
+
export declare const crossFadeTransition: Transition;
|
|
46
|
+
export declare const slideHorizontalTransition: Transition;
|
|
47
|
+
export declare const slideVerticalTransition: Transition;
|
|
48
|
+
export declare function createRouter<Locale extends string = never, Path extends string = never>({ prefix: expectedPrefix, locales, pages, fallback: Fallback, issue: Issue, transition }: Router<Path, Locale>): {
|
|
49
|
+
RouterProvider: ({ children }: RouterProviderProps) => import("react/jsx-runtime").JSX.Element;
|
|
50
|
+
RouterView: () => import("react/jsx-runtime").JSX.Element;
|
|
51
|
+
useLocale: () => {
|
|
52
|
+
locale: Locale | null;
|
|
53
|
+
setLocale: (locale: Locale) => void;
|
|
54
|
+
};
|
|
55
|
+
usePrefix: () => {
|
|
56
|
+
prefix: string | null;
|
|
57
|
+
};
|
|
58
|
+
usePath: () => {
|
|
59
|
+
path: string;
|
|
60
|
+
};
|
|
61
|
+
useNavigateToPage: <P extends Path>(page: Page<P>) => (...[params]: ExtractParams<P> extends never ? [] : [Params<P>]) => void;
|
|
62
|
+
useIsActivePage: <P extends Path>(page: Page<P>) => boolean;
|
|
63
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,689 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region ../../node_modules/react/cjs/react.production.min.js
|
|
5
|
+
/**
|
|
6
|
+
* @license React
|
|
7
|
+
* react.production.min.js
|
|
8
|
+
*
|
|
9
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
10
|
+
*
|
|
11
|
+
* This source code is licensed under the MIT license found in the
|
|
12
|
+
* LICENSE file in the root directory of this source tree.
|
|
13
|
+
*/
|
|
14
|
+
var require_react_production_min = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
15
|
+
var l = Symbol.for("react.element"), n = Symbol.for("react.portal"), p = Symbol.for("react.fragment"), q = Symbol.for("react.strict_mode"), r = Symbol.for("react.profiler"), t = Symbol.for("react.provider"), u = Symbol.for("react.context"), v = Symbol.for("react.forward_ref"), w = Symbol.for("react.suspense"), x = Symbol.for("react.memo"), y = Symbol.for("react.lazy"), z = Symbol.iterator;
|
|
16
|
+
function A(a) {
|
|
17
|
+
if (null === a || "object" !== typeof a) return null;
|
|
18
|
+
a = z && a[z] || a["@@iterator"];
|
|
19
|
+
return "function" === typeof a ? a : null;
|
|
20
|
+
}
|
|
21
|
+
var B = {
|
|
22
|
+
isMounted: function() {
|
|
23
|
+
return !1;
|
|
24
|
+
},
|
|
25
|
+
enqueueForceUpdate: function() {},
|
|
26
|
+
enqueueReplaceState: function() {},
|
|
27
|
+
enqueueSetState: function() {}
|
|
28
|
+
}, C = Object.assign, D = {};
|
|
29
|
+
function E(a, b, e) {
|
|
30
|
+
this.props = a;
|
|
31
|
+
this.context = b;
|
|
32
|
+
this.refs = D;
|
|
33
|
+
this.updater = e || B;
|
|
34
|
+
}
|
|
35
|
+
E.prototype.isReactComponent = {};
|
|
36
|
+
E.prototype.setState = function(a, b) {
|
|
37
|
+
if ("object" !== typeof a && "function" !== typeof a && null != a) throw Error("setState(...): takes an object of state variables to update or a function which returns an object of state variables.");
|
|
38
|
+
this.updater.enqueueSetState(this, a, b, "setState");
|
|
39
|
+
};
|
|
40
|
+
E.prototype.forceUpdate = function(a) {
|
|
41
|
+
this.updater.enqueueForceUpdate(this, a, "forceUpdate");
|
|
42
|
+
};
|
|
43
|
+
function F() {}
|
|
44
|
+
F.prototype = E.prototype;
|
|
45
|
+
function G(a, b, e) {
|
|
46
|
+
this.props = a;
|
|
47
|
+
this.context = b;
|
|
48
|
+
this.refs = D;
|
|
49
|
+
this.updater = e || B;
|
|
50
|
+
}
|
|
51
|
+
var H = G.prototype = new F();
|
|
52
|
+
H.constructor = G;
|
|
53
|
+
C(H, E.prototype);
|
|
54
|
+
H.isPureReactComponent = !0;
|
|
55
|
+
var I = Array.isArray, J = Object.prototype.hasOwnProperty, K = { current: null }, L = {
|
|
56
|
+
key: !0,
|
|
57
|
+
ref: !0,
|
|
58
|
+
__self: !0,
|
|
59
|
+
__source: !0
|
|
60
|
+
};
|
|
61
|
+
function M(a, b, e) {
|
|
62
|
+
var d, c = {}, k = null, h = null;
|
|
63
|
+
if (null != b) for (d in void 0 !== b.ref && (h = b.ref), void 0 !== b.key && (k = "" + b.key), b) J.call(b, d) && !L.hasOwnProperty(d) && (c[d] = b[d]);
|
|
64
|
+
var g = arguments.length - 2;
|
|
65
|
+
if (1 === g) c.children = e;
|
|
66
|
+
else if (1 < g) {
|
|
67
|
+
for (var f = Array(g), m = 0; m < g; m++) f[m] = arguments[m + 2];
|
|
68
|
+
c.children = f;
|
|
69
|
+
}
|
|
70
|
+
if (a && a.defaultProps) for (d in g = a.defaultProps, g) void 0 === c[d] && (c[d] = g[d]);
|
|
71
|
+
return {
|
|
72
|
+
$$typeof: l,
|
|
73
|
+
type: a,
|
|
74
|
+
key: k,
|
|
75
|
+
ref: h,
|
|
76
|
+
props: c,
|
|
77
|
+
_owner: K.current
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function N(a, b) {
|
|
81
|
+
return {
|
|
82
|
+
$$typeof: l,
|
|
83
|
+
type: a.type,
|
|
84
|
+
key: b,
|
|
85
|
+
ref: a.ref,
|
|
86
|
+
props: a.props,
|
|
87
|
+
_owner: a._owner
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function O(a) {
|
|
91
|
+
return "object" === typeof a && null !== a && a.$$typeof === l;
|
|
92
|
+
}
|
|
93
|
+
function escape(a) {
|
|
94
|
+
var b = {
|
|
95
|
+
"=": "=0",
|
|
96
|
+
":": "=2"
|
|
97
|
+
};
|
|
98
|
+
return "$" + a.replace(/[=:]/g, function(a) {
|
|
99
|
+
return b[a];
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
var P = /\/+/g;
|
|
103
|
+
function Q(a, b) {
|
|
104
|
+
return "object" === typeof a && null !== a && null != a.key ? escape("" + a.key) : b.toString(36);
|
|
105
|
+
}
|
|
106
|
+
function R(a, b, e, d, c) {
|
|
107
|
+
var k = typeof a;
|
|
108
|
+
if ("undefined" === k || "boolean" === k) a = null;
|
|
109
|
+
var h = !1;
|
|
110
|
+
if (null === a) h = !0;
|
|
111
|
+
else switch (k) {
|
|
112
|
+
case "string":
|
|
113
|
+
case "number":
|
|
114
|
+
h = !0;
|
|
115
|
+
break;
|
|
116
|
+
case "object": switch (a.$$typeof) {
|
|
117
|
+
case l:
|
|
118
|
+
case n: h = !0;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (h) return h = a, c = c(h), a = "" === d ? "." + Q(h, 0) : d, I(c) ? (e = "", null != a && (e = a.replace(P, "$&/") + "/"), R(c, b, e, "", function(a) {
|
|
122
|
+
return a;
|
|
123
|
+
})) : null != c && (O(c) && (c = N(c, e + (!c.key || h && h.key === c.key ? "" : ("" + c.key).replace(P, "$&/") + "/") + a)), b.push(c)), 1;
|
|
124
|
+
h = 0;
|
|
125
|
+
d = "" === d ? "." : d + ":";
|
|
126
|
+
if (I(a)) for (var g = 0; g < a.length; g++) {
|
|
127
|
+
k = a[g];
|
|
128
|
+
var f = d + Q(k, g);
|
|
129
|
+
h += R(k, b, e, f, c);
|
|
130
|
+
}
|
|
131
|
+
else if (f = A(a), "function" === typeof f) for (a = f.call(a), g = 0; !(k = a.next()).done;) k = k.value, f = d + Q(k, g++), h += R(k, b, e, f, c);
|
|
132
|
+
else if ("object" === k) throw b = String(a), Error("Objects are not valid as a React child (found: " + ("[object Object]" === b ? "object with keys {" + Object.keys(a).join(", ") + "}" : b) + "). If you meant to render a collection of children, use an array instead.");
|
|
133
|
+
return h;
|
|
134
|
+
}
|
|
135
|
+
function S(a, b, e) {
|
|
136
|
+
if (null == a) return a;
|
|
137
|
+
var d = [], c = 0;
|
|
138
|
+
R(a, d, "", "", function(a) {
|
|
139
|
+
return b.call(e, a, c++);
|
|
140
|
+
});
|
|
141
|
+
return d;
|
|
142
|
+
}
|
|
143
|
+
function T(a) {
|
|
144
|
+
if (-1 === a._status) {
|
|
145
|
+
var b = a._result;
|
|
146
|
+
b = b();
|
|
147
|
+
b.then(function(b) {
|
|
148
|
+
if (0 === a._status || -1 === a._status) a._status = 1, a._result = b;
|
|
149
|
+
}, function(b) {
|
|
150
|
+
if (0 === a._status || -1 === a._status) a._status = 2, a._result = b;
|
|
151
|
+
});
|
|
152
|
+
-1 === a._status && (a._status = 0, a._result = b);
|
|
153
|
+
}
|
|
154
|
+
if (1 === a._status) return a._result.default;
|
|
155
|
+
throw a._result;
|
|
156
|
+
}
|
|
157
|
+
var U = { current: null }, V = { transition: null }, W = {
|
|
158
|
+
ReactCurrentDispatcher: U,
|
|
159
|
+
ReactCurrentBatchConfig: V,
|
|
160
|
+
ReactCurrentOwner: K
|
|
161
|
+
};
|
|
162
|
+
function X() {
|
|
163
|
+
throw Error("act(...) is not supported in production builds of React.");
|
|
164
|
+
}
|
|
165
|
+
exports.Children = {
|
|
166
|
+
map: S,
|
|
167
|
+
forEach: function(a, b, e) {
|
|
168
|
+
S(a, function() {
|
|
169
|
+
b.apply(this, arguments);
|
|
170
|
+
}, e);
|
|
171
|
+
},
|
|
172
|
+
count: function(a) {
|
|
173
|
+
var b = 0;
|
|
174
|
+
S(a, function() {
|
|
175
|
+
b++;
|
|
176
|
+
});
|
|
177
|
+
return b;
|
|
178
|
+
},
|
|
179
|
+
toArray: function(a) {
|
|
180
|
+
return S(a, function(a) {
|
|
181
|
+
return a;
|
|
182
|
+
}) || [];
|
|
183
|
+
},
|
|
184
|
+
only: function(a) {
|
|
185
|
+
if (!O(a)) throw Error("React.Children.only expected to receive a single React element child.");
|
|
186
|
+
return a;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
exports.Component = E;
|
|
190
|
+
exports.Fragment = p;
|
|
191
|
+
exports.Profiler = r;
|
|
192
|
+
exports.PureComponent = G;
|
|
193
|
+
exports.StrictMode = q;
|
|
194
|
+
exports.Suspense = w;
|
|
195
|
+
exports.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = W;
|
|
196
|
+
exports.act = X;
|
|
197
|
+
exports.cloneElement = function(a, b, e) {
|
|
198
|
+
if (null === a || void 0 === a) throw Error("React.cloneElement(...): The argument must be a React element, but you passed " + a + ".");
|
|
199
|
+
var d = C({}, a.props), c = a.key, k = a.ref, h = a._owner;
|
|
200
|
+
if (null != b) {
|
|
201
|
+
void 0 !== b.ref && (k = b.ref, h = K.current);
|
|
202
|
+
void 0 !== b.key && (c = "" + b.key);
|
|
203
|
+
if (a.type && a.type.defaultProps) var g = a.type.defaultProps;
|
|
204
|
+
for (f in b) J.call(b, f) && !L.hasOwnProperty(f) && (d[f] = void 0 === b[f] && void 0 !== g ? g[f] : b[f]);
|
|
205
|
+
}
|
|
206
|
+
var f = arguments.length - 2;
|
|
207
|
+
if (1 === f) d.children = e;
|
|
208
|
+
else if (1 < f) {
|
|
209
|
+
g = Array(f);
|
|
210
|
+
for (var m = 0; m < f; m++) g[m] = arguments[m + 2];
|
|
211
|
+
d.children = g;
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
$$typeof: l,
|
|
215
|
+
type: a.type,
|
|
216
|
+
key: c,
|
|
217
|
+
ref: k,
|
|
218
|
+
props: d,
|
|
219
|
+
_owner: h
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
exports.createContext = function(a) {
|
|
223
|
+
a = {
|
|
224
|
+
$$typeof: u,
|
|
225
|
+
_currentValue: a,
|
|
226
|
+
_currentValue2: a,
|
|
227
|
+
_threadCount: 0,
|
|
228
|
+
Provider: null,
|
|
229
|
+
Consumer: null,
|
|
230
|
+
_defaultValue: null,
|
|
231
|
+
_globalName: null
|
|
232
|
+
};
|
|
233
|
+
a.Provider = {
|
|
234
|
+
$$typeof: t,
|
|
235
|
+
_context: a
|
|
236
|
+
};
|
|
237
|
+
return a.Consumer = a;
|
|
238
|
+
};
|
|
239
|
+
exports.createElement = M;
|
|
240
|
+
exports.createFactory = function(a) {
|
|
241
|
+
var b = M.bind(null, a);
|
|
242
|
+
b.type = a;
|
|
243
|
+
return b;
|
|
244
|
+
};
|
|
245
|
+
exports.createRef = function() {
|
|
246
|
+
return { current: null };
|
|
247
|
+
};
|
|
248
|
+
exports.forwardRef = function(a) {
|
|
249
|
+
return {
|
|
250
|
+
$$typeof: v,
|
|
251
|
+
render: a
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
exports.isValidElement = O;
|
|
255
|
+
exports.lazy = function(a) {
|
|
256
|
+
return {
|
|
257
|
+
$$typeof: y,
|
|
258
|
+
_payload: {
|
|
259
|
+
_status: -1,
|
|
260
|
+
_result: a
|
|
261
|
+
},
|
|
262
|
+
_init: T
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
exports.memo = function(a, b) {
|
|
266
|
+
return {
|
|
267
|
+
$$typeof: x,
|
|
268
|
+
type: a,
|
|
269
|
+
compare: void 0 === b ? null : b
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
exports.startTransition = function(a) {
|
|
273
|
+
var b = V.transition;
|
|
274
|
+
V.transition = {};
|
|
275
|
+
try {
|
|
276
|
+
a();
|
|
277
|
+
} finally {
|
|
278
|
+
V.transition = b;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
exports.unstable_act = X;
|
|
282
|
+
exports.useCallback = function(a, b) {
|
|
283
|
+
return U.current.useCallback(a, b);
|
|
284
|
+
};
|
|
285
|
+
exports.useContext = function(a) {
|
|
286
|
+
return U.current.useContext(a);
|
|
287
|
+
};
|
|
288
|
+
exports.useDebugValue = function() {};
|
|
289
|
+
exports.useDeferredValue = function(a) {
|
|
290
|
+
return U.current.useDeferredValue(a);
|
|
291
|
+
};
|
|
292
|
+
exports.useEffect = function(a, b) {
|
|
293
|
+
return U.current.useEffect(a, b);
|
|
294
|
+
};
|
|
295
|
+
exports.useId = function() {
|
|
296
|
+
return U.current.useId();
|
|
297
|
+
};
|
|
298
|
+
exports.useImperativeHandle = function(a, b, e) {
|
|
299
|
+
return U.current.useImperativeHandle(a, b, e);
|
|
300
|
+
};
|
|
301
|
+
exports.useInsertionEffect = function(a, b) {
|
|
302
|
+
return U.current.useInsertionEffect(a, b);
|
|
303
|
+
};
|
|
304
|
+
exports.useLayoutEffect = function(a, b) {
|
|
305
|
+
return U.current.useLayoutEffect(a, b);
|
|
306
|
+
};
|
|
307
|
+
exports.useMemo = function(a, b) {
|
|
308
|
+
return U.current.useMemo(a, b);
|
|
309
|
+
};
|
|
310
|
+
exports.useReducer = function(a, b, e) {
|
|
311
|
+
return U.current.useReducer(a, b, e);
|
|
312
|
+
};
|
|
313
|
+
exports.useRef = function(a) {
|
|
314
|
+
return U.current.useRef(a);
|
|
315
|
+
};
|
|
316
|
+
exports.useState = function(a) {
|
|
317
|
+
return U.current.useState(a);
|
|
318
|
+
};
|
|
319
|
+
exports.useSyncExternalStore = function(a, b, e) {
|
|
320
|
+
return U.current.useSyncExternalStore(a, b, e);
|
|
321
|
+
};
|
|
322
|
+
exports.useTransition = function() {
|
|
323
|
+
return U.current.useTransition();
|
|
324
|
+
};
|
|
325
|
+
exports.version = "18.3.1";
|
|
326
|
+
}));
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region ../../node_modules/react/index.js
|
|
329
|
+
var require_react = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
330
|
+
module.exports = require_react_production_min();
|
|
331
|
+
}));
|
|
332
|
+
//#endregion
|
|
333
|
+
//#region ../../node_modules/react/cjs/react-jsx-runtime.production.min.js
|
|
334
|
+
/**
|
|
335
|
+
* @license React
|
|
336
|
+
* react-jsx-runtime.production.min.js
|
|
337
|
+
*
|
|
338
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
339
|
+
*
|
|
340
|
+
* This source code is licensed under the MIT license found in the
|
|
341
|
+
* LICENSE file in the root directory of this source tree.
|
|
342
|
+
*/
|
|
343
|
+
var require_react_jsx_runtime_production_min = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
344
|
+
var f = require_react(), k = Symbol.for("react.element"), m = Object.prototype.hasOwnProperty, n = f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, p = {
|
|
345
|
+
key: !0,
|
|
346
|
+
ref: !0,
|
|
347
|
+
__self: !0,
|
|
348
|
+
__source: !0
|
|
349
|
+
};
|
|
350
|
+
function q(c, a, g) {
|
|
351
|
+
var b, d = {}, e = null, h = null;
|
|
352
|
+
void 0 !== g && (e = "" + g);
|
|
353
|
+
void 0 !== a.key && (e = "" + a.key);
|
|
354
|
+
void 0 !== a.ref && (h = a.ref);
|
|
355
|
+
for (b in a) m.call(a, b) && !p.hasOwnProperty(b) && (d[b] = a[b]);
|
|
356
|
+
if (c && c.defaultProps) for (b in a = c.defaultProps, a) void 0 === d[b] && (d[b] = a[b]);
|
|
357
|
+
return {
|
|
358
|
+
$$typeof: k,
|
|
359
|
+
type: c,
|
|
360
|
+
key: e,
|
|
361
|
+
ref: h,
|
|
362
|
+
props: d,
|
|
363
|
+
_owner: n.current
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
exports.jsx = q;
|
|
367
|
+
}));
|
|
368
|
+
//#endregion
|
|
369
|
+
//#region ../../node_modules/react/jsx-runtime.js
|
|
370
|
+
var require_jsx_runtime = /* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
371
|
+
module.exports = require_react_jsx_runtime_production_min();
|
|
372
|
+
}));
|
|
373
|
+
//#endregion
|
|
374
|
+
//#region index.tsx
|
|
375
|
+
var import_react = require_react();
|
|
376
|
+
var import_jsx_runtime = require_jsx_runtime();
|
|
377
|
+
function normalize(uri) {
|
|
378
|
+
return uri.trim().toLowerCase().replace(/\/+/g, "/").replace(/^\/+|\/$/g, "");
|
|
379
|
+
}
|
|
380
|
+
function matchPath(path, pathname) {
|
|
381
|
+
const pathParts = normalize(path).split("/").filter(Boolean);
|
|
382
|
+
const pathnameParts = normalize(pathname).split("/").filter(Boolean);
|
|
383
|
+
return pathParts.length === pathnameParts.length && pathParts.every((pathPart, index) => {
|
|
384
|
+
return pathPart.startsWith(":") || pathPart === pathnameParts.at(index);
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
function matchParameters(path, pathname) {
|
|
388
|
+
const pathParts = normalize(path).split("/").filter(Boolean);
|
|
389
|
+
const pathnameParts = normalize(pathname).split("/").filter(Boolean);
|
|
390
|
+
if (pathParts.length !== pathnameParts.length) return {};
|
|
391
|
+
return pathParts.reduce((parameters, pathPart, index) => {
|
|
392
|
+
if (!pathPart.startsWith(":")) return parameters;
|
|
393
|
+
return {
|
|
394
|
+
...parameters,
|
|
395
|
+
[pathPart.slice(1)]: pathnameParts.at(index) ?? ""
|
|
396
|
+
};
|
|
397
|
+
}, {});
|
|
398
|
+
}
|
|
399
|
+
var Uri = class Uri {
|
|
400
|
+
constructor(path, prefix, locale) {
|
|
401
|
+
this.path = path;
|
|
402
|
+
this.prefix = prefix;
|
|
403
|
+
this.locale = locale;
|
|
404
|
+
}
|
|
405
|
+
static from(uri, expectedPrefix, expectedLocales) {
|
|
406
|
+
const [prefixOrLocale, localeOrNothing, ...parts] = normalize(uri).split("/");
|
|
407
|
+
const locales = expectedLocales ?? [];
|
|
408
|
+
if (expectedPrefix && prefixOrLocale && prefixOrLocale === normalize(expectedPrefix)) {
|
|
409
|
+
const locale = locales.find((expectedLocale) => expectedLocale === localeOrNothing);
|
|
410
|
+
if (locale) return new Uri(parts.join("/"), prefixOrLocale, locale);
|
|
411
|
+
return new Uri([localeOrNothing, ...parts].join("/"), prefixOrLocale, null);
|
|
412
|
+
}
|
|
413
|
+
const locale = locales.find((expectedLocale) => expectedLocale === prefixOrLocale);
|
|
414
|
+
if (locale) return new Uri([localeOrNothing, ...parts].join("/"), null, locale);
|
|
415
|
+
return new Uri([
|
|
416
|
+
prefixOrLocale,
|
|
417
|
+
localeOrNothing,
|
|
418
|
+
...parts
|
|
419
|
+
].join("/"), null, null);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
var ErrorBoundary = class extends import_react.Component {
|
|
423
|
+
constructor(props) {
|
|
424
|
+
super(props);
|
|
425
|
+
this.state = {
|
|
426
|
+
error: null,
|
|
427
|
+
resetError: this.resetError.bind(this)
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
resetError() {
|
|
431
|
+
this.setState({ error: null });
|
|
432
|
+
}
|
|
433
|
+
static getDerivedStateFromError(error) {
|
|
434
|
+
return { error };
|
|
435
|
+
}
|
|
436
|
+
componentDidCatch(error) {
|
|
437
|
+
this.setState({ error: error instanceof Error ? error : new Error(String(error)) });
|
|
438
|
+
}
|
|
439
|
+
render() {
|
|
440
|
+
if (this.state.error) {
|
|
441
|
+
const Issue = this.props.issue;
|
|
442
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Issue, {
|
|
443
|
+
error: this.state.error,
|
|
444
|
+
resetError: this.state.resetError
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
return this.props.children;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
function createPage(page) {
|
|
451
|
+
return {
|
|
452
|
+
...page,
|
|
453
|
+
path: normalize(page.path)
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
const scaleFadeTransition = async (direction, next) => {
|
|
457
|
+
try {
|
|
458
|
+
await document.startViewTransition(() => {
|
|
459
|
+
next();
|
|
460
|
+
}).ready;
|
|
461
|
+
document.documentElement.animate([{
|
|
462
|
+
transform: "scale(1)",
|
|
463
|
+
opacity: 1
|
|
464
|
+
}, {
|
|
465
|
+
transform: direction === "forward" ? "scale(1.04)" : "scale(0.96)",
|
|
466
|
+
opacity: 0
|
|
467
|
+
}], {
|
|
468
|
+
duration: 200,
|
|
469
|
+
easing: "ease-in-out",
|
|
470
|
+
fill: "both",
|
|
471
|
+
pseudoElement: `::view-transition-old(root)`
|
|
472
|
+
});
|
|
473
|
+
document.documentElement.animate([{
|
|
474
|
+
transform: direction === "forward" ? "scale(0.96)" : "scale(1.04)",
|
|
475
|
+
opacity: 0
|
|
476
|
+
}, {
|
|
477
|
+
transform: "scale(1)",
|
|
478
|
+
opacity: 1
|
|
479
|
+
}], {
|
|
480
|
+
duration: 200,
|
|
481
|
+
easing: "ease-in-out",
|
|
482
|
+
fill: "both",
|
|
483
|
+
pseudoElement: `::view-transition-new(root)`
|
|
484
|
+
});
|
|
485
|
+
} catch (error) {
|
|
486
|
+
console.error(error);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
const crossFadeTransition = async (direction, next) => {
|
|
490
|
+
try {
|
|
491
|
+
await document.startViewTransition(() => {
|
|
492
|
+
next();
|
|
493
|
+
}).ready;
|
|
494
|
+
document.documentElement.animate([{ opacity: 1 }, { opacity: 0 }], {
|
|
495
|
+
duration: 200,
|
|
496
|
+
easing: "ease-in-out",
|
|
497
|
+
fill: "both",
|
|
498
|
+
pseudoElement: `::view-transition-old(root)`
|
|
499
|
+
});
|
|
500
|
+
document.documentElement.animate([{ opacity: 0 }, { opacity: 1 }], {
|
|
501
|
+
duration: 200,
|
|
502
|
+
easing: "ease-in-out",
|
|
503
|
+
fill: "both",
|
|
504
|
+
pseudoElement: `::view-transition-new(root)`
|
|
505
|
+
});
|
|
506
|
+
} catch (error) {
|
|
507
|
+
console.error(error);
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
const slideHorizontalTransition = async (direction, next) => {
|
|
511
|
+
try {
|
|
512
|
+
await document.startViewTransition(() => {
|
|
513
|
+
next();
|
|
514
|
+
}).ready;
|
|
515
|
+
document.documentElement.animate([{ transform: "translateX(0)" }, { transform: direction === "forward" ? "translateX(-100%)" : "translateX(100%)" }], {
|
|
516
|
+
duration: 200,
|
|
517
|
+
easing: "ease-in-out",
|
|
518
|
+
fill: "both",
|
|
519
|
+
pseudoElement: `::view-transition-old(root)`
|
|
520
|
+
});
|
|
521
|
+
document.documentElement.animate([{ transform: direction === "forward" ? "translateX(100%)" : "translateX(-100%)" }, { transform: "translateX(0)" }], {
|
|
522
|
+
duration: 200,
|
|
523
|
+
easing: "ease-in-out",
|
|
524
|
+
fill: "both",
|
|
525
|
+
pseudoElement: `::view-transition-new(root)`
|
|
526
|
+
});
|
|
527
|
+
} catch (error) {
|
|
528
|
+
console.error(error);
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
const slideVerticalTransition = async (direction, next) => {
|
|
532
|
+
try {
|
|
533
|
+
await document.startViewTransition(() => {
|
|
534
|
+
next();
|
|
535
|
+
}).ready;
|
|
536
|
+
document.documentElement.animate([{ transform: "translateY(0)" }, { transform: direction === "forward" ? "translateY(-100%)" : "translateY(100%)" }], {
|
|
537
|
+
duration: 200,
|
|
538
|
+
easing: "ease-in-out",
|
|
539
|
+
fill: "both",
|
|
540
|
+
pseudoElement: `::view-transition-old(root)`
|
|
541
|
+
});
|
|
542
|
+
document.documentElement.animate([{ transform: direction === "forward" ? "translateY(100%)" : "translateY(-100%)" }, { transform: "translateY(0)" }], {
|
|
543
|
+
duration: 200,
|
|
544
|
+
easing: "ease-in-out",
|
|
545
|
+
fill: "both",
|
|
546
|
+
pseudoElement: `::view-transition-new(root)`
|
|
547
|
+
});
|
|
548
|
+
} catch (error) {
|
|
549
|
+
console.error(error);
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
function createRouter({ prefix: expectedPrefix, locales, pages, fallback: Fallback, issue: Issue, transition }) {
|
|
553
|
+
const RouterContext = (0, import_react.createContext)({
|
|
554
|
+
locale: null,
|
|
555
|
+
prefix: null,
|
|
556
|
+
path: "/",
|
|
557
|
+
setLocale: () => {}
|
|
558
|
+
});
|
|
559
|
+
function useIsActivePage(page) {
|
|
560
|
+
const { path } = usePath();
|
|
561
|
+
return matchPath(page.path, path);
|
|
562
|
+
}
|
|
563
|
+
function useLocale() {
|
|
564
|
+
const context = (0, import_react.useContext)(RouterContext);
|
|
565
|
+
if (!context) throw new Error("component using the useLocale hook has not been wrapped inside RouterProvider.");
|
|
566
|
+
return {
|
|
567
|
+
locale: context.locale,
|
|
568
|
+
setLocale: (locale) => {
|
|
569
|
+
if (!locales?.includes(locale)) return;
|
|
570
|
+
window.history.pushState(null, "", `/${normalize(`${context.prefix ?? ""}/${locale}/${context.path}`)}`);
|
|
571
|
+
window.dispatchEvent(new Event("pushstate"));
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
function usePrefix() {
|
|
576
|
+
const context = (0, import_react.useContext)(RouterContext);
|
|
577
|
+
if (!context) throw new Error("Component using the usePrefix hook has not been wrapped inside RouterProvider");
|
|
578
|
+
return { prefix: context.prefix };
|
|
579
|
+
}
|
|
580
|
+
function usePath() {
|
|
581
|
+
const context = (0, import_react.useContext)(RouterContext);
|
|
582
|
+
if (!context) throw new Error("Component using the usePath hook has not been wrapped inside RouterProvider");
|
|
583
|
+
return { path: context.path };
|
|
584
|
+
}
|
|
585
|
+
function useNavigateToPage(page) {
|
|
586
|
+
const { locale } = useLocale();
|
|
587
|
+
const { prefix } = usePrefix();
|
|
588
|
+
return (0, import_react.useCallback)((...[params]) => {
|
|
589
|
+
const path = Object.entries(params ?? {}).reduce((oldParams, [name, value]) => {
|
|
590
|
+
return oldParams.replace(`:${name}`, String(value));
|
|
591
|
+
}, page.path);
|
|
592
|
+
const pathname = `/${normalize(`${prefix ?? ""}/${locale ?? ""}/${path}`)}`;
|
|
593
|
+
console.log({ pathname });
|
|
594
|
+
window.history.pushState(null, "", pathname);
|
|
595
|
+
window.dispatchEvent(new Event("pushstate"));
|
|
596
|
+
}, [
|
|
597
|
+
page,
|
|
598
|
+
locale,
|
|
599
|
+
prefix
|
|
600
|
+
]);
|
|
601
|
+
}
|
|
602
|
+
function RouterProvider({ children }) {
|
|
603
|
+
const uri = (0, import_react.useMemo)(() => Uri.from(window.location.pathname, expectedPrefix, locales), [expectedPrefix, locales]);
|
|
604
|
+
const [locale, setLocale] = (0, import_react.useState)(uri.locale);
|
|
605
|
+
const [path, setPath] = (0, import_react.useState)(uri.path);
|
|
606
|
+
const [prefix, setPrefix] = (0, import_react.useState)(uri.prefix);
|
|
607
|
+
function setHash(newHash) {
|
|
608
|
+
window.location.hash = newHash;
|
|
609
|
+
}
|
|
610
|
+
const value = (0, import_react.useMemo)(() => {
|
|
611
|
+
return {
|
|
612
|
+
locale,
|
|
613
|
+
path,
|
|
614
|
+
prefix: prefix ?? null,
|
|
615
|
+
setLocale,
|
|
616
|
+
setHash
|
|
617
|
+
};
|
|
618
|
+
}, [
|
|
619
|
+
locale,
|
|
620
|
+
prefix,
|
|
621
|
+
path
|
|
622
|
+
]);
|
|
623
|
+
const onNavigation = (0, import_react.useEffectEvent)((direction) => {
|
|
624
|
+
const pathname = normalize(window.location.pathname);
|
|
625
|
+
const uri = Uri.from(pathname, expectedPrefix, locales);
|
|
626
|
+
const defaultTransition = (_, next) => {
|
|
627
|
+
next();
|
|
628
|
+
};
|
|
629
|
+
(transition ?? defaultTransition)(direction, () => {
|
|
630
|
+
setLocale(uri.locale);
|
|
631
|
+
setPath(uri.path);
|
|
632
|
+
setPrefix(uri.prefix);
|
|
633
|
+
});
|
|
634
|
+
});
|
|
635
|
+
const onMount = (0, import_react.useEffectEvent)(() => {
|
|
636
|
+
const pathname = `/${normalize(window.location.pathname)}`;
|
|
637
|
+
const uri = Uri.from(pathname, expectedPrefix, [locale, ...locales ?? []]);
|
|
638
|
+
const newPathname = `/${normalize(`${uri.prefix ?? expectedPrefix ?? ""}/${uri.locale ?? locales?.at(0) ?? ""}/${uri.path}`)}`;
|
|
639
|
+
if (newPathname !== pathname) {
|
|
640
|
+
window.history.pushState(null, "", newPathname);
|
|
641
|
+
window.dispatchEvent(new Event("pushstate"));
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
(0, import_react.useEffect)(() => {
|
|
645
|
+
const abortController = new AbortController();
|
|
646
|
+
window.addEventListener("pushstate", () => {
|
|
647
|
+
onNavigation("forward");
|
|
648
|
+
}, abortController);
|
|
649
|
+
window.addEventListener("popstate", () => {
|
|
650
|
+
onNavigation("backward");
|
|
651
|
+
}, abortController);
|
|
652
|
+
onMount();
|
|
653
|
+
return () => {
|
|
654
|
+
abortController.abort();
|
|
655
|
+
};
|
|
656
|
+
}, []);
|
|
657
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(RouterContext.Provider, {
|
|
658
|
+
value,
|
|
659
|
+
children
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
function RouterView() {
|
|
663
|
+
const { path } = usePath();
|
|
664
|
+
const foundPage = (0, import_react.useMemo)(() => {
|
|
665
|
+
return pages.find((page) => {
|
|
666
|
+
return matchPath(page.path, path);
|
|
667
|
+
});
|
|
668
|
+
}, [path, pages]);
|
|
669
|
+
if (foundPage) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, {
|
|
670
|
+
issue: Issue,
|
|
671
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(foundPage.element, { parameters: matchParameters(foundPage.path, path) })
|
|
672
|
+
});
|
|
673
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ErrorBoundary, {
|
|
674
|
+
issue: Issue,
|
|
675
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Fallback, {})
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
return {
|
|
679
|
+
RouterProvider,
|
|
680
|
+
RouterView,
|
|
681
|
+
useLocale,
|
|
682
|
+
usePrefix,
|
|
683
|
+
usePath,
|
|
684
|
+
useNavigateToPage,
|
|
685
|
+
useIsActivePage
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
//#endregion
|
|
689
|
+
export { Uri, createPage, createRouter, crossFadeTransition, scaleFadeTransition, slideHorizontalTransition, slideVerticalTransition };
|
package/package.json
CHANGED
|
@@ -2,11 +2,15 @@
|
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@aminnairi/react-router",
|
|
4
4
|
"description": "Type-safe router for the React library",
|
|
5
|
-
"version": "3.0.
|
|
6
|
-
"homepage": "https://github.com/aminnairi/react-router
|
|
5
|
+
"version": "3.0.2",
|
|
6
|
+
"homepage": "https://github.com/aminnairi/react-router",
|
|
7
7
|
"license": "MIT",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"readme": "https://github.com/aminnairi/react-router#readme",
|
|
8
11
|
"files": [
|
|
9
|
-
"index.
|
|
12
|
+
"dist/index.js",
|
|
13
|
+
"dist/index.d.ts"
|
|
10
14
|
],
|
|
11
15
|
"bugs": {
|
|
12
16
|
"url": "https://github.com/aminnairi/react-router/issues"
|
|
@@ -31,13 +35,15 @@
|
|
|
31
35
|
"react": ">=18.0.0"
|
|
32
36
|
},
|
|
33
37
|
"scripts": {
|
|
34
|
-
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0"
|
|
38
|
+
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
|
39
|
+
"build": "tsc && rolldown --config rolldown.config.ts"
|
|
35
40
|
},
|
|
36
41
|
"devDependencies": {
|
|
37
42
|
"@typescript-eslint/eslint-plugin": "^7.2.0",
|
|
38
43
|
"@typescript-eslint/parser": "^7.2.0",
|
|
39
44
|
"eslint": "^8.57.0",
|
|
40
45
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
46
|
+
"rolldown": "^1.0.0-rc.9",
|
|
41
47
|
"typescript": "^5.9.3"
|
|
42
48
|
}
|
|
43
49
|
}
|
package/index.tsx
DELETED
|
@@ -1,527 +0,0 @@
|
|
|
1
|
-
import { Component, createContext, type Dispatch, type FunctionComponent, type ReactNode, type SetStateAction, useCallback, useContext, useEffect, useEffectEvent, useMemo, useState } from "react";
|
|
2
|
-
|
|
3
|
-
export interface IssueProps {
|
|
4
|
-
error: Error | null,
|
|
5
|
-
resetError: () => void
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface Router<Path extends string, Locale> {
|
|
9
|
-
prefix?: string
|
|
10
|
-
locales?: Locale[]
|
|
11
|
-
transition?: Transition
|
|
12
|
-
fallback: FunctionComponent
|
|
13
|
-
issue: FunctionComponent<IssueProps>
|
|
14
|
-
pages: Array<Page<Path>>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface RouterProviderProps {
|
|
18
|
-
children: ReactNode
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface RouterContextInterface<Locale> {
|
|
22
|
-
locale: Locale | null
|
|
23
|
-
prefix: string | null
|
|
24
|
-
path: string
|
|
25
|
-
setLocale: Dispatch<SetStateAction<Locale | null>>
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type NavigationDirection = "forward" | "backward";
|
|
29
|
-
|
|
30
|
-
function normalize(uri: string) {
|
|
31
|
-
return uri
|
|
32
|
-
.trim()
|
|
33
|
-
.toLowerCase()
|
|
34
|
-
.replace(/\/+/g, "/")
|
|
35
|
-
.replace(/^\/+|\/$/g, "");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function matchPath(path: string, pathname: string) {
|
|
39
|
-
const pathParts = normalize(path).split("/").filter(Boolean);
|
|
40
|
-
const pathnameParts = normalize(pathname).split("/").filter(Boolean);
|
|
41
|
-
|
|
42
|
-
return pathParts.length === pathnameParts.length && pathParts.every((pathPart, index) => {
|
|
43
|
-
return pathPart.startsWith(":") || pathPart === pathnameParts.at(index);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function matchParameters(path: string, pathname: string): Record<string, string> {
|
|
48
|
-
const pathParts = normalize(path).split("/").filter(Boolean);
|
|
49
|
-
const pathnameParts = normalize(pathname).split("/").filter(Boolean);
|
|
50
|
-
|
|
51
|
-
if (pathParts.length !== pathnameParts.length) {
|
|
52
|
-
return {};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return pathParts.reduce((parameters, pathPart, index) => {
|
|
56
|
-
if (!pathPart.startsWith(":")) {
|
|
57
|
-
return parameters;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return {
|
|
61
|
-
...parameters,
|
|
62
|
-
[pathPart.slice(1)]: pathnameParts.at(index) ?? ""
|
|
63
|
-
}
|
|
64
|
-
}, {});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export class Uri<Locale> {
|
|
68
|
-
private constructor(public readonly path: string, public readonly prefix: string | null, public readonly locale: Locale | null) { }
|
|
69
|
-
|
|
70
|
-
public static from<Locale>(uri: string, expectedPrefix?: string, expectedLocales?: Locale[]): Uri<Locale> {
|
|
71
|
-
const [prefixOrLocale, localeOrNothing, ...parts] = normalize(uri).split("/");
|
|
72
|
-
const locales = expectedLocales ?? [];
|
|
73
|
-
|
|
74
|
-
if (expectedPrefix && prefixOrLocale && prefixOrLocale === normalize(expectedPrefix)) {
|
|
75
|
-
const locale = locales.find(expectedLocale => expectedLocale === localeOrNothing);
|
|
76
|
-
|
|
77
|
-
if (locale) {
|
|
78
|
-
return new Uri<Locale>(
|
|
79
|
-
parts.join("/"),
|
|
80
|
-
prefixOrLocale,
|
|
81
|
-
locale,
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return new Uri<Locale>(
|
|
86
|
-
[localeOrNothing, ...parts].join("/"),
|
|
87
|
-
prefixOrLocale,
|
|
88
|
-
null
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const locale = locales.find(expectedLocale => expectedLocale === prefixOrLocale);
|
|
93
|
-
|
|
94
|
-
if (locale) {
|
|
95
|
-
return new Uri<Locale>(
|
|
96
|
-
[localeOrNothing, ...parts].join("/"),
|
|
97
|
-
null,
|
|
98
|
-
locale
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return new Uri<Locale>(
|
|
103
|
-
[prefixOrLocale, localeOrNothing, ...parts].join("/"),
|
|
104
|
-
null,
|
|
105
|
-
null
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export type ExtractParams<Path extends string> =
|
|
111
|
-
Path extends `${string}:${infer Param}/${infer Rest}`
|
|
112
|
-
? Param | ExtractParams<Rest>
|
|
113
|
-
: Path extends `${string}:${infer Param}`
|
|
114
|
-
? Param
|
|
115
|
-
: never
|
|
116
|
-
|
|
117
|
-
export type Params<Path extends string> = {
|
|
118
|
-
[Key in ExtractParams<Path>]: string
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export interface PageParams<Path extends string> {
|
|
122
|
-
parameters: Params<Path>
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export interface Page<Path extends string> {
|
|
126
|
-
path: Path
|
|
127
|
-
element: FunctionComponent<PageParams<Path>>
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
interface ErrorBoundaryProps {
|
|
131
|
-
children: ReactNode;
|
|
132
|
-
issue: FunctionComponent<IssueProps>;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
class ErrorBoundary extends Component<ErrorBoundaryProps, IssueProps> {
|
|
136
|
-
public constructor(props: ErrorBoundaryProps) {
|
|
137
|
-
super(props);
|
|
138
|
-
|
|
139
|
-
this.state = {
|
|
140
|
-
error: null,
|
|
141
|
-
resetError: this.resetError.bind(this)
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private resetError() {
|
|
146
|
-
this.setState({ error: null });
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
public static getDerivedStateFromError(error: Error) {
|
|
150
|
-
return {
|
|
151
|
-
error
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
public override componentDidCatch(error: unknown) {
|
|
156
|
-
this.setState({
|
|
157
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public override render() {
|
|
162
|
-
if (this.state.error) {
|
|
163
|
-
const Issue = this.props.issue;
|
|
164
|
-
|
|
165
|
-
return <Issue error={this.state.error} resetError={this.state.resetError} />;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return this.props.children;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
export function createPage<Path extends string>(page: Page<Path>): Page<Path> {
|
|
173
|
-
return {
|
|
174
|
-
...page,
|
|
175
|
-
path: normalize(page.path) as Path
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export type Transition = (direction: "forward" | "backward", next: () => void) => void;
|
|
180
|
-
|
|
181
|
-
export const scaleFadeTransition: Transition = async (direction: NavigationDirection, next) => {
|
|
182
|
-
try {
|
|
183
|
-
const transition = document.startViewTransition(() => {
|
|
184
|
-
next();
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
await transition.ready;
|
|
188
|
-
|
|
189
|
-
document.documentElement.animate(
|
|
190
|
-
[
|
|
191
|
-
{
|
|
192
|
-
transform: 'scale(1)',
|
|
193
|
-
opacity: 1
|
|
194
|
-
},
|
|
195
|
-
{
|
|
196
|
-
transform: direction === "forward" ? "scale(1.04)" : "scale(0.96)",
|
|
197
|
-
opacity: 0
|
|
198
|
-
}
|
|
199
|
-
],
|
|
200
|
-
{
|
|
201
|
-
duration: 200,
|
|
202
|
-
easing: "ease-in-out",
|
|
203
|
-
fill: "both",
|
|
204
|
-
pseudoElement: `::view-transition-old(root)`,
|
|
205
|
-
}
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
document.documentElement.animate(
|
|
209
|
-
[
|
|
210
|
-
{
|
|
211
|
-
transform: direction === "forward" ? "scale(0.96)" : "scale(1.04)",
|
|
212
|
-
opacity: 0
|
|
213
|
-
},
|
|
214
|
-
{
|
|
215
|
-
transform: 'scale(1)',
|
|
216
|
-
opacity: 1
|
|
217
|
-
}
|
|
218
|
-
],
|
|
219
|
-
{
|
|
220
|
-
duration: 200,
|
|
221
|
-
easing: "ease-in-out",
|
|
222
|
-
fill: "both",
|
|
223
|
-
pseudoElement: `::view-transition-new(root)`,
|
|
224
|
-
}
|
|
225
|
-
);
|
|
226
|
-
} catch (error) {
|
|
227
|
-
console.error(error);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export const crossFadeTransition: Transition = async (direction: NavigationDirection, next) => {
|
|
232
|
-
try {
|
|
233
|
-
const transition = document.startViewTransition(() => {
|
|
234
|
-
next();
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
await transition.ready;
|
|
238
|
-
|
|
239
|
-
document.documentElement.animate(
|
|
240
|
-
[
|
|
241
|
-
{ opacity: 1 },
|
|
242
|
-
{ opacity: 0 }
|
|
243
|
-
],
|
|
244
|
-
{
|
|
245
|
-
duration: 200,
|
|
246
|
-
easing: "ease-in-out",
|
|
247
|
-
fill: "both",
|
|
248
|
-
pseudoElement: `::view-transition-old(root)`,
|
|
249
|
-
}
|
|
250
|
-
);
|
|
251
|
-
|
|
252
|
-
document.documentElement.animate(
|
|
253
|
-
[
|
|
254
|
-
{ opacity: 0 },
|
|
255
|
-
{ opacity: 1 }
|
|
256
|
-
],
|
|
257
|
-
{
|
|
258
|
-
duration: 200,
|
|
259
|
-
easing: "ease-in-out",
|
|
260
|
-
fill: "both",
|
|
261
|
-
pseudoElement: `::view-transition-new(root)`,
|
|
262
|
-
}
|
|
263
|
-
);
|
|
264
|
-
} catch (error) {
|
|
265
|
-
console.error(error);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export const slideHorizontalTransition: Transition = async (direction: NavigationDirection, next) => {
|
|
270
|
-
try {
|
|
271
|
-
const transition = document.startViewTransition(() => {
|
|
272
|
-
next();
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
await transition.ready;
|
|
276
|
-
|
|
277
|
-
document.documentElement.animate(
|
|
278
|
-
[
|
|
279
|
-
{ transform: 'translateX(0)' },
|
|
280
|
-
{ transform: direction === "forward" ? 'translateX(-100%)' : 'translateX(100%)' }
|
|
281
|
-
],
|
|
282
|
-
{
|
|
283
|
-
duration: 200,
|
|
284
|
-
easing: "ease-in-out",
|
|
285
|
-
fill: "both",
|
|
286
|
-
pseudoElement: `::view-transition-old(root)`,
|
|
287
|
-
}
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
document.documentElement.animate(
|
|
291
|
-
[
|
|
292
|
-
{ transform: direction === "forward" ? 'translateX(100%)' : 'translateX(-100%)' },
|
|
293
|
-
{ transform: 'translateX(0)' }
|
|
294
|
-
],
|
|
295
|
-
{
|
|
296
|
-
duration: 200,
|
|
297
|
-
easing: "ease-in-out",
|
|
298
|
-
fill: "both",
|
|
299
|
-
pseudoElement: `::view-transition-new(root)`,
|
|
300
|
-
}
|
|
301
|
-
);
|
|
302
|
-
} catch (error) {
|
|
303
|
-
console.error(error);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export const slideVerticalTransition: Transition = async (direction: NavigationDirection, next) => {
|
|
308
|
-
try {
|
|
309
|
-
const transition = document.startViewTransition(() => {
|
|
310
|
-
next();
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
await transition.ready;
|
|
314
|
-
|
|
315
|
-
document.documentElement.animate(
|
|
316
|
-
[
|
|
317
|
-
{ transform: 'translateY(0)' },
|
|
318
|
-
{ transform: direction === "forward" ? 'translateY(-100%)' : 'translateY(100%)' }
|
|
319
|
-
],
|
|
320
|
-
{
|
|
321
|
-
duration: 200,
|
|
322
|
-
easing: "ease-in-out",
|
|
323
|
-
fill: "both",
|
|
324
|
-
pseudoElement: `::view-transition-old(root)`,
|
|
325
|
-
}
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
document.documentElement.animate(
|
|
329
|
-
[
|
|
330
|
-
{ transform: direction === "forward" ? 'translateY(100%)' : 'translateY(-100%)' },
|
|
331
|
-
{ transform: 'translateY(0)' }
|
|
332
|
-
],
|
|
333
|
-
{
|
|
334
|
-
duration: 200,
|
|
335
|
-
easing: "ease-in-out",
|
|
336
|
-
fill: "both",
|
|
337
|
-
pseudoElement: `::view-transition-new(root)`,
|
|
338
|
-
}
|
|
339
|
-
);
|
|
340
|
-
} catch (error) {
|
|
341
|
-
console.error(error);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
export function createRouter<Locale extends string = never, Path extends string = never>({ prefix: expectedPrefix, locales, pages, fallback: Fallback, issue: Issue, transition }: Router<Path, Locale>) {
|
|
346
|
-
const RouterContext = createContext<RouterContextInterface<Locale>>({
|
|
347
|
-
locale: null,
|
|
348
|
-
prefix: null,
|
|
349
|
-
path: "/",
|
|
350
|
-
setLocale: () => { },
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
function useIsActivePage<P extends Path>(page: Page<P>): boolean {
|
|
355
|
-
const { path } = usePath();
|
|
356
|
-
return matchPath(page.path, path);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
function useLocale() {
|
|
360
|
-
const context = useContext(RouterContext);
|
|
361
|
-
|
|
362
|
-
if (!context) {
|
|
363
|
-
throw new Error("component using the useLocale hook has not been wrapped inside RouterProvider.");
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
return {
|
|
367
|
-
locale: context.locale,
|
|
368
|
-
setLocale: (locale: Locale) => {
|
|
369
|
-
if (!locales?.includes(locale)) {
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
window.history.pushState(null, "", `/${normalize(`${context.prefix ?? ""}/${locale}/${context.path}`)}`);
|
|
374
|
-
window.dispatchEvent(new Event("pushstate"));
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function usePrefix() {
|
|
380
|
-
const context = useContext(RouterContext);
|
|
381
|
-
|
|
382
|
-
if (!context) {
|
|
383
|
-
throw new Error("Component using the usePrefix hook has not been wrapped inside RouterProvider");
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
return {
|
|
387
|
-
prefix: context.prefix
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
function usePath() {
|
|
392
|
-
const context = useContext(RouterContext);
|
|
393
|
-
|
|
394
|
-
if (!context) {
|
|
395
|
-
throw new Error("Component using the usePath hook has not been wrapped inside RouterProvider");
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
return {
|
|
399
|
-
path: context.path,
|
|
400
|
-
};
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
function useNavigateToPage<P extends Path>(page: Page<P>) {
|
|
404
|
-
const { locale } = useLocale();
|
|
405
|
-
const { prefix } = usePrefix();
|
|
406
|
-
|
|
407
|
-
return useCallback((...[params]: ExtractParams<P> extends never ? [] : [Params<P>]) => {
|
|
408
|
-
const path = Object.entries(params ?? {}).reduce<string>((oldParams, [name, value]) => {
|
|
409
|
-
return oldParams.replace(`:${name}`, String(value));
|
|
410
|
-
}, page.path);
|
|
411
|
-
|
|
412
|
-
const pathname = `/${normalize(`${prefix ?? ""}/${locale ?? ""}/${path}`)}`
|
|
413
|
-
|
|
414
|
-
console.log({ pathname });
|
|
415
|
-
|
|
416
|
-
window.history.pushState(null, "", pathname);
|
|
417
|
-
window.dispatchEvent(new Event("pushstate"));
|
|
418
|
-
}, [page, locale, prefix]);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
function RouterProvider({ children }: RouterProviderProps) {
|
|
422
|
-
const uri = useMemo(() => Uri.from(window.location.pathname, expectedPrefix, locales), [expectedPrefix, locales]);
|
|
423
|
-
const [locale, setLocale] = useState(uri.locale);
|
|
424
|
-
const [path, setPath] = useState(uri.path);
|
|
425
|
-
const [prefix, setPrefix] = useState(uri.prefix);
|
|
426
|
-
|
|
427
|
-
function setHash(newHash: string) {
|
|
428
|
-
window.location.hash = newHash;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
const value = useMemo(() => {
|
|
432
|
-
return {
|
|
433
|
-
locale,
|
|
434
|
-
path,
|
|
435
|
-
prefix: prefix ?? null,
|
|
436
|
-
setLocale,
|
|
437
|
-
setHash,
|
|
438
|
-
};
|
|
439
|
-
}, [locale, prefix, path]);
|
|
440
|
-
|
|
441
|
-
const onNavigation = useEffectEvent((direction: NavigationDirection) => {
|
|
442
|
-
const pathname = normalize(window.location.pathname);
|
|
443
|
-
const uri = Uri.from(pathname, expectedPrefix, locales);
|
|
444
|
-
|
|
445
|
-
const defaultTransition: Transition = (_, next) => {
|
|
446
|
-
next();
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
const currentTransition: Transition = transition ?? defaultTransition;
|
|
450
|
-
|
|
451
|
-
currentTransition(direction, () => {
|
|
452
|
-
setLocale(uri.locale);
|
|
453
|
-
setPath(uri.path);
|
|
454
|
-
setPrefix(uri.prefix);
|
|
455
|
-
});
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
const onMount = useEffectEvent(() => {
|
|
459
|
-
const pathname = `/${normalize(window.location.pathname)}`;
|
|
460
|
-
const uri = Uri.from(pathname, expectedPrefix, [locale, ...locales ?? []]);
|
|
461
|
-
const newPathname = `/${normalize(`${uri.prefix ?? expectedPrefix ?? ""}/${uri.locale ?? locales?.at(0) ?? ""}/${uri.path}`)}`;
|
|
462
|
-
|
|
463
|
-
if (newPathname !== pathname) {
|
|
464
|
-
window.history.pushState(null, "", newPathname);
|
|
465
|
-
window.dispatchEvent(new Event("pushstate"));
|
|
466
|
-
}
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
useEffect(() => {
|
|
470
|
-
const abortController = new AbortController();
|
|
471
|
-
|
|
472
|
-
window.addEventListener("pushstate", () => {
|
|
473
|
-
onNavigation("forward");
|
|
474
|
-
}, abortController);
|
|
475
|
-
|
|
476
|
-
window.addEventListener("popstate", () => {
|
|
477
|
-
onNavigation("backward");
|
|
478
|
-
}, abortController);
|
|
479
|
-
|
|
480
|
-
onMount();
|
|
481
|
-
|
|
482
|
-
return () => {
|
|
483
|
-
abortController.abort();
|
|
484
|
-
};
|
|
485
|
-
}, []);
|
|
486
|
-
|
|
487
|
-
return (
|
|
488
|
-
<RouterContext.Provider value={value}>
|
|
489
|
-
{children}
|
|
490
|
-
</RouterContext.Provider>
|
|
491
|
-
);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
function RouterView() {
|
|
495
|
-
const { path } = usePath();
|
|
496
|
-
|
|
497
|
-
const foundPage = useMemo(() => {
|
|
498
|
-
return pages.find(page => {
|
|
499
|
-
return matchPath(page.path, path);
|
|
500
|
-
});
|
|
501
|
-
}, [path, pages]);
|
|
502
|
-
|
|
503
|
-
if (foundPage) {
|
|
504
|
-
return (
|
|
505
|
-
<ErrorBoundary issue={Issue}>
|
|
506
|
-
<foundPage.element parameters={matchParameters(foundPage.path, path) as Params<Path>} />
|
|
507
|
-
</ErrorBoundary>
|
|
508
|
-
);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
return (
|
|
512
|
-
<ErrorBoundary issue={Issue}>
|
|
513
|
-
<Fallback />
|
|
514
|
-
</ErrorBoundary>
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
return {
|
|
519
|
-
RouterProvider,
|
|
520
|
-
RouterView,
|
|
521
|
-
useLocale,
|
|
522
|
-
usePrefix,
|
|
523
|
-
usePath,
|
|
524
|
-
useNavigateToPage,
|
|
525
|
-
useIsActivePage,
|
|
526
|
-
};
|
|
527
|
-
}
|