what-router 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# what-router
|
|
2
|
+
|
|
3
|
+
Client-side router for [What Framework](https://whatfw.com). Supports dynamic routes, nested layouts, route groups, middleware, View Transitions API, scroll restoration, and prefetching.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install what-router what-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or use via the main package:
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Router, Link, navigate } from 'what-framework/router';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
```jsx
|
|
20
|
+
import { mount } from 'what-framework';
|
|
21
|
+
import { Router, Link, navigate } from 'what-router';
|
|
22
|
+
|
|
23
|
+
function Home() {
|
|
24
|
+
return <h1>Home</h1>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function User({ params }) {
|
|
28
|
+
return <h1>User {params.id}</h1>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function App() {
|
|
32
|
+
return (
|
|
33
|
+
<div>
|
|
34
|
+
<nav>
|
|
35
|
+
<Link href="/">Home</Link>
|
|
36
|
+
<Link href="/users/1">User 1</Link>
|
|
37
|
+
</nav>
|
|
38
|
+
<Router
|
|
39
|
+
routes={[
|
|
40
|
+
{ path: '/', component: Home },
|
|
41
|
+
{ path: '/users/:id', component: User },
|
|
42
|
+
]}
|
|
43
|
+
/>
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
mount(<App />, '#app');
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Route Patterns
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
{ path: '/users/:id', component: User } // Dynamic param
|
|
55
|
+
{ path: '/docs/*', component: DocsLayout } // Catch-all
|
|
56
|
+
{ path: '/blog/[slug]', component: Post } // File-based syntax
|
|
57
|
+
{ path: '/[...rest]', component: CatchAll } // Named catch-all
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Navigation
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
import { navigate, route } from 'what-router';
|
|
64
|
+
|
|
65
|
+
// Programmatic navigation
|
|
66
|
+
navigate('/dashboard');
|
|
67
|
+
navigate('/login', { replace: true });
|
|
68
|
+
navigate('/page', { transition: false }); // skip View Transition
|
|
69
|
+
|
|
70
|
+
// Reactive route state
|
|
71
|
+
route.path; // current path
|
|
72
|
+
route.params; // { id: '123' }
|
|
73
|
+
route.query; // { page: '1' }
|
|
74
|
+
route.hash; // '#section'
|
|
75
|
+
route.isNavigating;
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Link Component
|
|
79
|
+
|
|
80
|
+
```jsx
|
|
81
|
+
<Link href="/about">About</Link>
|
|
82
|
+
<Link href="/about" activeClass="active" exactActiveClass="exact-active">About</Link>
|
|
83
|
+
<Link href="/about" replace prefetch={false}>About</Link>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Links automatically get `active` and `exact-active` CSS classes based on the current route. Hover prefetching is enabled by default.
|
|
87
|
+
|
|
88
|
+
## Nested Layouts
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import { defineRoutes, nestedRoutes, Outlet } from 'what-router';
|
|
92
|
+
|
|
93
|
+
function DashboardLayout({ children }) {
|
|
94
|
+
return (
|
|
95
|
+
<div>
|
|
96
|
+
<Sidebar />
|
|
97
|
+
<main>{children}</main>
|
|
98
|
+
</div>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const routes = [
|
|
103
|
+
...nestedRoutes('/dashboard', [
|
|
104
|
+
{ path: '/', component: DashboardHome },
|
|
105
|
+
{ path: '/settings', component: Settings },
|
|
106
|
+
], { layout: DashboardLayout }),
|
|
107
|
+
];
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Route Guards & Middleware
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
import { guard, asyncGuard } from 'what-router';
|
|
114
|
+
|
|
115
|
+
// Sync guard
|
|
116
|
+
const requireAuth = guard(
|
|
117
|
+
() => isLoggedIn(),
|
|
118
|
+
'/login' // redirect on failure
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const ProtectedPage = requireAuth(Dashboard);
|
|
122
|
+
|
|
123
|
+
// Async guard
|
|
124
|
+
const requireRole = asyncGuard(
|
|
125
|
+
async () => await checkPermission('admin'),
|
|
126
|
+
{ fallback: '/unauthorized', loading: Spinner }
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
// Route-level middleware
|
|
130
|
+
{
|
|
131
|
+
path: '/admin',
|
|
132
|
+
component: AdminPanel,
|
|
133
|
+
middleware: [authMiddleware, roleMiddleware],
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## View Transitions
|
|
138
|
+
|
|
139
|
+
Navigation uses the View Transitions API by default when available. Use helpers to customize:
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
import { viewTransitionName, setViewTransition } from 'what-router';
|
|
143
|
+
|
|
144
|
+
// Name elements for transitions
|
|
145
|
+
<img {...viewTransitionName('hero-image')} src={url} />
|
|
146
|
+
|
|
147
|
+
// Set transition type
|
|
148
|
+
setViewTransition('slide');
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Scroll Restoration
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
import { enableScrollRestoration } from 'what-router';
|
|
155
|
+
|
|
156
|
+
enableScrollRestoration(); // call once at app entry
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## API
|
|
160
|
+
|
|
161
|
+
| Export | Description |
|
|
162
|
+
|---|---|
|
|
163
|
+
| `Router` | Route matching component |
|
|
164
|
+
| `Link` / `NavLink` | Navigation link with active states |
|
|
165
|
+
| `navigate(to, opts?)` | Programmatic navigation |
|
|
166
|
+
| `route` | Reactive route state object |
|
|
167
|
+
| `useRoute()` | Hook returning computed route properties |
|
|
168
|
+
| `defineRoutes(config)` | Create routes from flat object |
|
|
169
|
+
| `nestedRoutes(base, children, opts?)` | Nested route helper |
|
|
170
|
+
| `routeGroup(name, routes, opts?)` | Group routes without affecting URL |
|
|
171
|
+
| `guard(check, fallback)` | Sync route guard |
|
|
172
|
+
| `asyncGuard(check, opts?)` | Async route guard |
|
|
173
|
+
| `Redirect` | Redirect component |
|
|
174
|
+
| `Outlet` | Nested route outlet |
|
|
175
|
+
| `FileRouter` | File-based router component |
|
|
176
|
+
| `prefetch(href)` | Prefetch a route's assets |
|
|
177
|
+
| `enableScrollRestoration()` | Enable scroll position restoration |
|
|
178
|
+
| `viewTransitionName(name)` | View Transition name helper |
|
|
179
|
+
| `setViewTransition(type)` | Set View Transition type |
|
|
180
|
+
|
|
181
|
+
## Links
|
|
182
|
+
|
|
183
|
+
- [Documentation](https://whatfw.com)
|
|
184
|
+
- [GitHub](https://github.com/zvndev/what-fw)
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "what-router",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "What Framework - File-based & programmatic router with View Transitions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"bugs": {
|
|
36
36
|
"url": "https://github.com/zvndev/what-fw/issues"
|
|
37
37
|
},
|
|
38
|
-
"homepage": "https://
|
|
38
|
+
"homepage": "https://whatfw.com"
|
|
39
39
|
}
|