bertui 0.1.9 → 0.2.0
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/index.js +1 -1
- package/package.json +1 -1
- package/src/router/Router.js +7 -22
package/index.js
CHANGED
package/package.json
CHANGED
package/src/router/Router.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
// src/router/Router.jsx
|
|
2
1
|
import { useState, useEffect, createContext, useContext } from 'react';
|
|
3
2
|
|
|
4
|
-
// Router context
|
|
5
3
|
const RouterContext = createContext(null);
|
|
6
4
|
|
|
7
5
|
export function useRouter() {
|
|
@@ -12,20 +10,13 @@ export function useRouter() {
|
|
|
12
10
|
return context;
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
export function
|
|
16
|
-
const { params } = useRouter();
|
|
17
|
-
return params;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function Router({ routes, children }) {
|
|
13
|
+
export function Router({ routes }) {
|
|
21
14
|
const [currentRoute, setCurrentRoute] = useState(null);
|
|
22
15
|
const [params, setParams] = useState({});
|
|
23
16
|
|
|
24
17
|
useEffect(() => {
|
|
25
|
-
// Match initial route
|
|
26
18
|
matchAndSetRoute(window.location.pathname);
|
|
27
19
|
|
|
28
|
-
// Handle browser navigation
|
|
29
20
|
const handlePopState = () => {
|
|
30
21
|
matchAndSetRoute(window.location.pathname);
|
|
31
22
|
};
|
|
@@ -35,7 +26,6 @@ export function Router({ routes, children }) {
|
|
|
35
26
|
}, []);
|
|
36
27
|
|
|
37
28
|
function matchAndSetRoute(pathname) {
|
|
38
|
-
// Try exact match first (static routes)
|
|
39
29
|
for (const route of routes) {
|
|
40
30
|
if (route.type === 'static' && route.path === pathname) {
|
|
41
31
|
setCurrentRoute(route);
|
|
@@ -44,7 +34,6 @@ export function Router({ routes, children }) {
|
|
|
44
34
|
}
|
|
45
35
|
}
|
|
46
36
|
|
|
47
|
-
// Try dynamic routes
|
|
48
37
|
for (const route of routes) {
|
|
49
38
|
if (route.type === 'dynamic') {
|
|
50
39
|
const pattern = route.path.replace(/\[([^\]]+)\]/g, '([^/]+)');
|
|
@@ -52,7 +41,6 @@ export function Router({ routes, children }) {
|
|
|
52
41
|
const match = pathname.match(regex);
|
|
53
42
|
|
|
54
43
|
if (match) {
|
|
55
|
-
// Extract params
|
|
56
44
|
const paramNames = [...route.path.matchAll(/\[([^\]]+)\]/g)].map(m => m[1]);
|
|
57
45
|
const extractedParams = {};
|
|
58
46
|
paramNames.forEach((name, i) => {
|
|
@@ -66,7 +54,6 @@ export function Router({ routes, children }) {
|
|
|
66
54
|
}
|
|
67
55
|
}
|
|
68
56
|
|
|
69
|
-
// No match found - 404
|
|
70
57
|
setCurrentRoute(null);
|
|
71
58
|
setParams({});
|
|
72
59
|
}
|
|
@@ -83,18 +70,16 @@ export function Router({ routes, children }) {
|
|
|
83
70
|
pathname: window.location.pathname
|
|
84
71
|
};
|
|
85
72
|
|
|
73
|
+
const Component = currentRoute?.component;
|
|
74
|
+
|
|
86
75
|
return (
|
|
87
76
|
<RouterContext.Provider value={routerValue}>
|
|
88
|
-
{
|
|
89
|
-
<currentRoute.component />
|
|
90
|
-
) : (
|
|
91
|
-
children || <NotFound />
|
|
92
|
-
)}
|
|
77
|
+
{Component ? <Component params={params} /> : <NotFound />}
|
|
93
78
|
</RouterContext.Provider>
|
|
94
79
|
);
|
|
95
80
|
}
|
|
96
81
|
|
|
97
|
-
export function Link({ to, children,
|
|
82
|
+
export function Link({ to, children, ...props }) {
|
|
98
83
|
const { navigate } = useRouter();
|
|
99
84
|
|
|
100
85
|
function handleClick(e) {
|
|
@@ -103,7 +88,7 @@ export function Link({ to, children, className, ...props }) {
|
|
|
103
88
|
}
|
|
104
89
|
|
|
105
90
|
return (
|
|
106
|
-
<a href={to} onClick={handleClick}
|
|
91
|
+
<a href={to} onClick={handleClick} {...props}>
|
|
107
92
|
{children}
|
|
108
93
|
</a>
|
|
109
94
|
);
|
|
@@ -117,7 +102,7 @@ function NotFound() {
|
|
|
117
102
|
alignItems: 'center',
|
|
118
103
|
justifyContent: 'center',
|
|
119
104
|
minHeight: '100vh',
|
|
120
|
-
fontFamily: 'system-ui
|
|
105
|
+
fontFamily: 'system-ui'
|
|
121
106
|
}}>
|
|
122
107
|
<h1 style={{ fontSize: '6rem', margin: 0 }}>404</h1>
|
|
123
108
|
<p style={{ fontSize: '1.5rem', color: '#666' }}>Page not found</p>
|