@wrnrlr/prelude 0.2.29 → 0.2.30
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/example/router.html +8 -2
- package/package.json +1 -1
- package/src/mod.ts +1 -1
- package/src/router.js +19 -5
package/example/router.html
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
<script type="module">
|
2
2
|
|
3
|
-
import {h,signal,effect,batch,wrap,render,List,Router} from '../src/mod.ts'
|
3
|
+
import {h,signal,effect,batch,wrap,render,List,Router,useRouter} from '../src/mod.ts'
|
4
4
|
|
5
5
|
function Home() {
|
6
|
-
|
6
|
+
console.log(useRouter)
|
7
|
+
const {navigate} = useRouter()
|
8
|
+
console.log('navigate', navigate)
|
9
|
+
return [
|
10
|
+
h('h1', 'Home'),
|
11
|
+
h('button', {onClick:e=>navigate('/router.html/a')}, 'navigate a')
|
12
|
+
]
|
7
13
|
}
|
8
14
|
|
9
15
|
function A() {
|
package/package.json
CHANGED
package/src/mod.ts
CHANGED
@@ -7,7 +7,7 @@ export {r, type Runtime} from './runtime.ts'
|
|
7
7
|
export type * from './hyperscript.ts'
|
8
8
|
export {hyperscript} from './hyperscript.ts'
|
9
9
|
export {h} from './web.ts'
|
10
|
-
export {useNavigate, useParams, useSearch, Router} from './router.js'
|
10
|
+
export {useNavigate, useParams, useSearch, useRouter, Router} from './router.js'
|
11
11
|
export {Switch, Match} from './switch.ts'
|
12
12
|
export {resource,makeAbortable,abortable} from './resource.js'
|
13
13
|
import {r} from './runtime.ts'
|
package/src/router.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
// @ts-nocheck
|
2
2
|
import { signal, effect, batch, untrack, memo, wrap, context, useContext, onMount } from './reactive.ts'
|
3
|
-
|
3
|
+
import { h } from './web.ts'
|
4
4
|
|
5
|
-
const
|
6
|
-
const useRouter = () => useContext(
|
5
|
+
const RouterCtx = context()
|
6
|
+
export const useRouter = () => useContext(RouterCtx)
|
7
7
|
export const useNavigate = () => wrap(useRouter(),'navigate')
|
8
8
|
export const useParams = () => wrap(useRouter(),'params')
|
9
9
|
export const useSearch = () => wrap(useRouter(),'search')
|
@@ -43,13 +43,22 @@ export function Router(props) {
|
|
43
43
|
})
|
44
44
|
const pathname = stripBase(window.location.pathname + window.location.search + window.location.hash)
|
45
45
|
navigate({pathname})
|
46
|
+
// Listen to browser history navigation (back/forward)
|
47
|
+
const popHandler = () => {
|
48
|
+
const path = stripBase(window.location.pathname + window.location.search + window.location.hash)
|
49
|
+
navigate({ pathname: path })
|
50
|
+
}
|
51
|
+
window.addEventListener('popstate', popHandler)
|
46
52
|
})
|
53
|
+
// TODO cleanup click and popstate handlers...
|
47
54
|
const children = () => {
|
48
55
|
const location = navigate()
|
56
|
+
// console.log('loc', location)
|
49
57
|
const route = matchRoute(routes, location.pathname)
|
50
58
|
return route?.component({params: route.params}) || NotFound
|
51
59
|
}
|
52
|
-
return
|
60
|
+
return h(RouterCtx, {value:{navigate:navigateTo,params,search}}, children)
|
61
|
+
// return RouterCtx({navigate,params,search,children})
|
53
62
|
}
|
54
63
|
|
55
64
|
function bindEvent(target, type, handler) {
|
@@ -68,10 +77,15 @@ function scrollToHash(hash, fallbackTop) {
|
|
68
77
|
|
69
78
|
function matchRoute(routes, pathname) {
|
70
79
|
// Normalize and split incoming path
|
71
|
-
const trim = str =>
|
80
|
+
const trim = str => {
|
81
|
+
// console.log('str', str)
|
82
|
+
return str.replace(/(^\/|\/$)/g, '')
|
83
|
+
}
|
84
|
+
// console.log('pathname', pathname)
|
72
85
|
const requestSegments = trim(pathname).split('/').filter(Boolean);
|
73
86
|
|
74
87
|
for (const route of routes) {
|
88
|
+
// console.log(route, route.path)
|
75
89
|
const routeSegments = trim(route.path).split('/').filter(Boolean);
|
76
90
|
if (routeSegments.length !== requestSegments.length) continue;
|
77
91
|
|