diff options
| author | navewindre <boneyaard@gmail.com> | 2026-02-11 06:12:26 +0100 |
|---|---|---|
| committer | navewindre <boneyaard@gmail.com> | 2026-02-11 06:12:26 +0100 |
| commit | e66257a6f66bf905d82c8df6e4e4d1a1509e0810 (patch) | |
| tree | 5f035f8878a611997a6953fed97bf7dc3cc82c14 /web/src/jsx.tsx | |
| parent | b8fce7dbca4383a46f74329af4666a09feb2853e (diff) | |
click callback for auto navigation, rendering fixes
Diffstat (limited to 'web/src/jsx.tsx')
| -rw-r--r-- | web/src/jsx.tsx | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/web/src/jsx.tsx b/web/src/jsx.tsx index 749e93f..1243398 100644 --- a/web/src/jsx.tsx +++ b/web/src/jsx.tsx @@ -78,6 +78,7 @@ export function onPostNavigate( callback: Function ) { onpostnavigate = callback; } + /** * replaces the root element with the route component **/ @@ -127,6 +128,17 @@ export function navigateParams( route: string, params: any ) { } /** + * convenience function to pass to href elements + **/ +export function href( e: Event ) { + const el = $( e.target ); + if( el.is( 'a' ) ) { + e.preventDefault(); + navigate( el.attr( 'href' ) ); + } +} + +/** * wrapper for history.pushState **/ export function pushParams( params: any ) { @@ -302,3 +314,23 @@ export function createFragment( props: any ) { function setTitle() { document.title = defaultTitle; } + +document.addEventListener( "click", e => { + let a = ( e.target ) as HTMLAnchorElement; + if( !a ) + return; + if( a.origin !== location.origin ) + return; + + e.preventDefault(); + if( !a.onclick ) { + if( routeForPath( a.pathname ) ) + return navigate( a.pathname ); + + const hasExt = /\.[a-zA-Z0-9]{1,8}$/.test( a.href ); + if( hasExt ) + location.href = a.href; + else + navigate( err404page ); + } +} ); |
