@wrnrlr/prelude 0.2.19 → 0.2.21
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/deno.jsonc +1 -1
- package/example/index.html +1 -0
- package/example/router.html +42 -0
- package/example/widget.html +6 -1
- package/package.json +1 -1
- package/src/form.js +3 -1
- package/src/mod.ts +1 -1
- package/src/router.js +73 -57
package/deno.jsonc
CHANGED
package/example/index.html
CHANGED
@@ -13,6 +13,7 @@
|
|
13
13
|
<li><a href="table.html">Table</a></li>
|
14
14
|
<li><a href="context.html">Context (Dependency Injection)</a></li>
|
15
15
|
<li><a href="h.html">h</a></li>
|
16
|
+
<li><a href="router.html">Router</a></li>
|
16
17
|
</ul>
|
17
18
|
<!-- <a href="router.html">Router</a> -->
|
18
19
|
<!-- <a href="context.html">Context</a> -->
|
@@ -0,0 +1,42 @@
|
|
1
|
+
<script type="module">
|
2
|
+
|
3
|
+
import {h,signal,effect,batch,wrap,render,List,Router} from '../src/mod.ts'
|
4
|
+
|
5
|
+
function Home() {
|
6
|
+
return h('h1', 'Home')
|
7
|
+
}
|
8
|
+
|
9
|
+
function A() {
|
10
|
+
return h('h1', 'A')
|
11
|
+
}
|
12
|
+
|
13
|
+
function B() {
|
14
|
+
return h('h1', 'B')
|
15
|
+
}
|
16
|
+
|
17
|
+
function C(props) {
|
18
|
+
return h('h1', ['C: ', props.params.id])
|
19
|
+
}
|
20
|
+
|
21
|
+
function App() {
|
22
|
+
return [
|
23
|
+
h('nav', {style: 'display: flex; gap: 1rem;'}, [
|
24
|
+
h('a', {href:'/router.html'}, '/'),
|
25
|
+
h('a', {href:'/router.html/a'}, '/a'),
|
26
|
+
h('a', {href:'/router.html/b'}, '/b'),
|
27
|
+
h('a', {href:'/router.html/c'}, '/c'),
|
28
|
+
h('a', {href:'/router.html/c/1'}, '/c/1'),
|
29
|
+
]),
|
30
|
+
h(Router, {base:'/router.html'}, [
|
31
|
+
{path:'/', component:Home},
|
32
|
+
{path:'/a', component:A},
|
33
|
+
{path:'/b', component:B},
|
34
|
+
{path:'/c', component:C},
|
35
|
+
{path:'/c/:id', component:C},
|
36
|
+
])
|
37
|
+
]
|
38
|
+
}
|
39
|
+
|
40
|
+
render(h(App), document.body)
|
41
|
+
|
42
|
+
</script>
|
package/example/widget.html
CHANGED
package/package.json
CHANGED
package/src/form.js
CHANGED
@@ -54,10 +54,12 @@ export function Select(props) {
|
|
54
54
|
})
|
55
55
|
const fallback = props.placeholder ? h('span', props.placeholder) : 'nbsp'
|
56
56
|
return h('.select', [
|
57
|
-
h('button', {onClick:e=>show(s=>!s)},
|
57
|
+
h('button', {onClick:e=>show(s=>!s)},
|
58
|
+
h(Show, {when:()=>props.value, fallback}, ()=>props.value() || nbsp)),
|
58
59
|
h(Show, {when: show}, h('.options', {style:'position:absolute'}, h(List, {each:()=>selected().options},
|
59
60
|
(option) => h('.option', {
|
60
61
|
onClick: (e) => { e.preventDefault(); props.value(option()); show(false) },
|
62
|
+
class: () => props.value()===option() ? 'selected' : '',
|
61
63
|
style: 'cursor: pointer'
|
62
64
|
}, option)
|
63
65
|
)))
|
package/src/mod.ts
CHANGED
@@ -6,7 +6,7 @@ export {Show} from './show.ts'
|
|
6
6
|
export {r, type Runtime} from './runtime.ts'
|
7
7
|
export type * from './hyperscript.ts'
|
8
8
|
export {hyperscript, h} from './hyperscript.ts'
|
9
|
-
export {
|
9
|
+
export {Router} from './router.js'
|
10
10
|
export {resource,makeAbortable,abortable} from './resource.js'
|
11
11
|
import {r} from './runtime.ts'
|
12
12
|
export const render = r.render
|
package/src/router.js
CHANGED
@@ -1,29 +1,6 @@
|
|
1
|
-
/*
|
2
|
-
MIT License
|
3
|
-
|
4
|
-
Copyright (c) 2016-2025 Ryan Carniato
|
5
|
-
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
8
|
-
in the Software without restriction, including without limitation the rights
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
11
|
-
furnished to do so, subject to the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be included in all
|
14
|
-
copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
-
SOFTWARE.
|
23
|
-
*/
|
24
1
|
// @ts-nocheck
|
25
|
-
import {signal,effect,batch,untrack,memo,wrap,context,useContext,onMount} from './reactive.ts'
|
26
|
-
// import {h} from './hyperscript.ts'
|
2
|
+
import { signal, effect, batch, untrack, memo, wrap, context, useContext, onMount } from './reactive.ts'
|
3
|
+
// import { h } from './hyperscript.ts'
|
27
4
|
|
28
5
|
const Ctx = context()
|
29
6
|
const useRouter = () => useContext(Ctx)
|
@@ -31,54 +8,93 @@ export const useNavigate = () => wrap(useRouter(),'navigate')
|
|
31
8
|
export const useParams = () => wrap(useRouter(),'params')
|
32
9
|
export const useSearch = () => wrap(useRouter(),'search')
|
33
10
|
|
34
|
-
export function
|
11
|
+
export function Router(props) {
|
12
|
+
const BASE = props.base || ''
|
35
13
|
const routes = props.children
|
36
|
-
const NotFound = ()=>'Not found'
|
37
|
-
const render = () => {console.log('render')}
|
38
|
-
// const loaded = signal(false)
|
14
|
+
const NotFound = () => 'Not found'
|
39
15
|
const navigate = signal(null)
|
40
16
|
const params = signal(null)
|
41
17
|
const search = signal(null)
|
42
|
-
|
43
|
-
|
44
|
-
|
18
|
+
function stripBase(fullPath) {
|
19
|
+
if (fullPath.startsWith(BASE)) {
|
20
|
+
return fullPath.slice(BASE.length) || '/'
|
21
|
+
}
|
22
|
+
return fullPath
|
23
|
+
}
|
24
|
+
function withBase(relPath) {
|
25
|
+
if (!relPath.startsWith('/')) relPath = '/' + relPath;
|
26
|
+
return BASE + relPath;
|
27
|
+
}
|
28
|
+
function navigateTo(fullPath) {
|
29
|
+
const stripedPath = stripBase(fullPath)
|
30
|
+
history.pushState({}, '', withBase(stripedPath));
|
31
|
+
navigate({pathname:stripedPath});
|
32
|
+
}
|
33
|
+
onMount(() => {
|
34
|
+
document.addEventListener('click', (e) => {
|
35
|
+
const target = e.target.closest('a')
|
36
|
+
if (!target) return
|
37
|
+
const url = new URL(target.href);
|
38
|
+
if (url.origin === window.location.origin && url.pathname.startsWith(BASE)) {
|
39
|
+
e.preventDefault()
|
40
|
+
const pathname = url.pathname + url.search + url.hash
|
41
|
+
navigateTo(pathname)
|
42
|
+
}
|
45
43
|
})
|
46
|
-
|
47
|
-
|
48
|
-
const children = memo(() => {
|
49
|
-
let location = navigate()
|
50
|
-
const route = routes.find(r=>r.path===location.pathname)
|
51
|
-
return route?.component() || NotFound
|
44
|
+
const pathname = stripBase(window.location.pathname + window.location.search + window.location.hash)
|
45
|
+
navigate({pathname})
|
52
46
|
})
|
47
|
+
const children = () => {
|
48
|
+
const location = navigate()
|
49
|
+
const route = matchRoute(routes, location.pathname)
|
50
|
+
return route?.component({params: route.params}) || NotFound
|
51
|
+
}
|
53
52
|
return Ctx({navigate,params,search,children:()=>children})
|
54
53
|
}
|
55
54
|
|
56
|
-
function
|
57
|
-
|
58
|
-
|
59
|
-
s = s.substr(1)
|
60
|
-
|
61
|
-
let i = s.indexOf('?')
|
62
|
-
if (i===-1) i = s.length
|
63
|
-
res.pathname += s.substr(0,i)
|
64
|
-
if (res.pathname==='') res.pathname = '/'
|
65
|
-
return res
|
55
|
+
function bindEvent(target, type, handler) {
|
56
|
+
target.addEventListener(type, handler)
|
57
|
+
return () => target.removeEventListener(type, handler)
|
66
58
|
}
|
67
59
|
|
68
|
-
function
|
69
|
-
|
70
|
-
if (
|
71
|
-
|
60
|
+
function scrollToHash(hash, fallbackTop) {
|
61
|
+
const el = hash && document.getElementById(hash);
|
62
|
+
if (el) {
|
63
|
+
el.scrollIntoView();
|
64
|
+
} else if (fallbackTop) {
|
65
|
+
window.scrollTo(0, 0);
|
66
|
+
}
|
72
67
|
}
|
73
68
|
|
74
|
-
function
|
69
|
+
function matchRoute(routes, pathname) {
|
70
|
+
// Normalize and split incoming path
|
71
|
+
const trim = str => str.replace(/(^\/|\/$)/g, '');
|
72
|
+
const requestSegments = trim(pathname).split('/').filter(Boolean);
|
75
73
|
|
76
|
-
|
74
|
+
for (const route of routes) {
|
75
|
+
const routeSegments = trim(route.path).split('/').filter(Boolean);
|
76
|
+
if (routeSegments.length !== requestSegments.length) continue;
|
77
77
|
|
78
|
-
|
78
|
+
const params = {};
|
79
|
+
let matched = true;
|
79
80
|
|
80
|
-
|
81
|
+
for (let i = 0; i < routeSegments.length; i++) {
|
82
|
+
const rSeg = routeSegments[i];
|
83
|
+
const pSeg = requestSegments[i];
|
81
84
|
|
82
|
-
|
85
|
+
if (rSeg.startsWith(':')) {
|
86
|
+
// dynamic parameter
|
87
|
+
const paramName = rSeg.slice(1);
|
88
|
+
params[paramName] = decodeURIComponent(pSeg);
|
89
|
+
} else if (rSeg === pSeg) {
|
90
|
+
// exact segment match
|
91
|
+
} else {
|
92
|
+
matched = false;
|
93
|
+
break;
|
94
|
+
}
|
95
|
+
}
|
83
96
|
|
97
|
+
if (matched) return { component: route.component, params }
|
98
|
+
}
|
99
|
+
return null;
|
84
100
|
}
|