what-router 0.3.0 → 0.4.1
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 +2 -2
- package/src/index.js +46 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "what-router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "What Framework - File-based & programmatic router with View Transitions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"author": "",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"what-core": "^0.
|
|
29
|
+
"what-core": "^0.4.0"
|
|
30
30
|
},
|
|
31
31
|
"repository": {
|
|
32
32
|
"type": "git",
|
package/src/index.js
CHANGED
|
@@ -108,8 +108,11 @@ function compilePath(path) {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
function matchRoute(path, routes) {
|
|
111
|
+
// Filter out routes without a path (layout-only routes, etc.)
|
|
112
|
+
const routable = routes.filter(r => r.path);
|
|
113
|
+
|
|
111
114
|
// Sort routes by specificity (more specific first)
|
|
112
|
-
const sorted =
|
|
115
|
+
const sorted = routable.sort((a, b) => {
|
|
113
116
|
const aSpecific = (a.path.match(/:/g) || []).length + (a.path.includes('*') ? 100 : 0);
|
|
114
117
|
const bSpecific = (b.path.match(/:/g) || []).length + (b.path.includes('*') ? 100 : 0);
|
|
115
118
|
return aSpecific - bSpecific;
|
|
@@ -145,6 +148,7 @@ function parseQuery(search) {
|
|
|
145
148
|
// Build the layout chain for a route
|
|
146
149
|
function buildLayoutChain(route, routes) {
|
|
147
150
|
const layouts = [];
|
|
151
|
+
if (!route.path) return layouts;
|
|
148
152
|
|
|
149
153
|
// Check for nested layouts based on path segments
|
|
150
154
|
const segments = route.path.split('/').filter(Boolean);
|
|
@@ -261,7 +265,10 @@ export function Link({
|
|
|
261
265
|
...rest
|
|
262
266
|
}) {
|
|
263
267
|
const currentPath = route.path;
|
|
264
|
-
|
|
268
|
+
// Segment-boundary matching: '/blog' matches '/blog/123' but not '/blog-archive'
|
|
269
|
+
const isActive = href === '/'
|
|
270
|
+
? currentPath === '/'
|
|
271
|
+
: currentPath === href || currentPath.startsWith(href + '/');
|
|
265
272
|
const isExactActive = currentPath === href;
|
|
266
273
|
|
|
267
274
|
const classes = [
|
|
@@ -273,13 +280,13 @@ export function Link({
|
|
|
273
280
|
return h('a', {
|
|
274
281
|
href,
|
|
275
282
|
class: classes,
|
|
276
|
-
|
|
283
|
+
onclick: (e) => {
|
|
277
284
|
// Only intercept left-clicks without modifiers
|
|
278
285
|
if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.button !== 0) return;
|
|
279
286
|
e.preventDefault();
|
|
280
287
|
navigate(href, { replace: rep, transition });
|
|
281
288
|
},
|
|
282
|
-
|
|
289
|
+
onmouseenter: shouldPrefetch ? () => prefetch(href) : undefined,
|
|
283
290
|
...rest,
|
|
284
291
|
}, ...(Array.isArray(children) ? children : [children]));
|
|
285
292
|
}
|
|
@@ -480,3 +487,38 @@ export function Outlet({ children }) {
|
|
|
480
487
|
// Children passed from parent layout
|
|
481
488
|
return children || null;
|
|
482
489
|
}
|
|
490
|
+
|
|
491
|
+
// --- File-Based Router ---
|
|
492
|
+
// Consumes routes generated by what-compiler's file router (virtual:what-routes).
|
|
493
|
+
// Usage:
|
|
494
|
+
// import { routes } from 'virtual:what-routes';
|
|
495
|
+
// mount(<FileRouter routes={routes} />, '#app');
|
|
496
|
+
|
|
497
|
+
export function FileRouter({
|
|
498
|
+
routes,
|
|
499
|
+
layout: globalLayout,
|
|
500
|
+
fallback,
|
|
501
|
+
error: globalError,
|
|
502
|
+
}) {
|
|
503
|
+
// Convert file-router route format to Router's expected format
|
|
504
|
+
const routerRoutes = routes.map(r => ({
|
|
505
|
+
path: r.path,
|
|
506
|
+
component: r.component,
|
|
507
|
+
layout: r.layout || undefined,
|
|
508
|
+
// Attach page mode as metadata for build system
|
|
509
|
+
_mode: r.mode || 'client',
|
|
510
|
+
}));
|
|
511
|
+
|
|
512
|
+
return Router({
|
|
513
|
+
routes: routerRoutes,
|
|
514
|
+
globalLayout,
|
|
515
|
+
fallback: fallback || Default404,
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function Default404() {
|
|
520
|
+
return h('div', { style: 'text-align:center;padding:60px 20px' },
|
|
521
|
+
h('h1', { style: 'font-size:48px;margin-bottom:8px' }, '404'),
|
|
522
|
+
h('p', { style: 'color:#64748b' }, 'Page not found'),
|
|
523
|
+
);
|
|
524
|
+
}
|