import { h , text } from "hyperapp" ; import PreventDefault from "../actions/PreventDefault" ; import navigate from "../effects/navigate" ; import { PathInfo , State , ViewContext } from '../types' ; const textTypes = [ 'string' , 'number' , 'bigint' ] ; const childNode = ( child ) => textTypes . includes ( typeof child ) ? text ( child ) : child ; interface LinkProps { href : string ; [ x ] : any ; } /**
 * Link component to import in user code.
 *
 * Handles navigation, preloading and
 * offers info about targeted paths.
 *
 */ const Link = ( { href , ... rest } , children ) => ( { state , options , getLocation , PreloadPage , } ) => { const location = getLocation ( href ) ; const { route , path } = location ; const status = state . paths [ path ] ?? "iddle" ; const active = state . location . path === path ; const navigateEventName = options . fastClicks ? "onmousedown" : "onclick" ; const renderChildren = ( child ) => { if ( typeof child === "function" ) { const info = { ... location , status , active , } ; return childNode ( child ( info ) ) ; } return childNode ( child ) ; } ; if ( ! href . startsWith ( '/' ) ) { return h ( "a" , { href , ... rest , } , [ ... children ] . map ( renderChildren ) ) ; } if ( ! route ) { if ( process . env . NODE_ENV === "development" ) { console . warn ( `Invalid link pointing to ${ href } will 404.` ) ; } const DumbNavigate = ( state , ev ) => { ev . preventDefault ( ) ; return [ state , navigate ( { to : href , delay : options . navigationDelay } ) ] ; } ; return h ( "a" , { href , onclick : PreventDefault , [ navigateEventName ] : DumbNavigate , "aria-current" : active , ... rest , } , [ ... children ] . map ( renderChildren ) ) ; } if ( window . registerPath ) { window . registerPath ( path ) ; } const RequestNavigation = ( state , ev ) => { ev . preventDefault ( ) ; const action = PreloadPage ( state , href ) ; if ( Array . isArray ( action ) ) { action . push ( navigate ( { to : href , delay : options . navigationDelay } ) ) ; return action ; } return [ action , navigate ( { to : href , delay : options . navigationDelay } ) ] ; } ; const PreloadPageHandler = ( state , _ev ) => { return PreloadPage ( state , href ) ; } ; return h ( "a" , { href , onclick : PreventDefault , [ navigateEventName ] : RequestNavigation , onmouseover : PreloadPageHandler , onfocus : PreloadPageHandler , "data-path" : path , "data-status" : status , "aria-current" : active , ... rest , } , [ ... children ] . map ( renderChildren ) ) ; } ; export default Link ;