@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.
@@ -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
- return h('h1', 'Home')
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wrnrlr/prelude",
3
3
  "type": "module",
4
- "version": "0.2.29",
4
+ "version": "0.2.30",
5
5
  "author": "Werner Laurensse",
6
6
  "description": "A signal based frontend library with fine-grained reactivity",
7
7
  "main": "./src/mod.ts",
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
- // import { h } from './hyperscript.ts'
3
+ import { h } from './web.ts'
4
4
 
5
- const Ctx = context()
6
- const useRouter = () => useContext(Ctx)
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 Ctx({navigate,params,search,children:()=>children})
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 => str.replace(/(^\/|\/$)/g, '');
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