// a HTMX approach with JSX?import{Fragment,JSX}from"preact";import{nanoid}from'nanoid'interfaceOptions{path:string}constroutes=[]classHTMX_IN_JSX{id:stringconstructor(publicComponent:(props:any)=>any,publicopt?:Options){this.id=nanoid()routes.push(this)}getpath (){if (this.opt?.path)returnthis.opt.paththrownewError('Href for component not available')}gethref (){returnthis.path}gettarget (){return{"hx-target":`#${this.id}`}}getput (){return{"hx-put":this.path}}getget (){return{"hx-get":this.path}}WrapId (){return (props:any)=>(<spanid={this.id}><this.Component{...props}/></span>)}Anchor (props:any):JSX.Element{return<ahref={this.href}>{props.children}</a>}AnchorBoost (props:any):JSX.Element{return<ahref={this.href}hx-boost="true">{props.children}</a>}}constcomponent=(x:(props:any)=>any,opt?:Options):((props:any)=>any)&HTMX_IN_JSX=>{constinstance=newHTMX_IN_JSX(x,opt)constex=instance.WrapId()returnObject.assign(ex,instance)}constGreeting=component(()=>{return (<div><h1>hello</h1></div>)},{path:'/greeting',})constPlaceholder=component(()=>{})constPage=component(()=>{return (<div><Placeholder/><Greeting.Anchor>Navigates to anchor</Greeting.Anchor><Greeting.AnchorBoost>Fetches anchor page and swaps dom</Greeting.AnchorBoost><ahref={Greeting.href}>Simple link to greeting page</a><div{...{...Greeting.put,...Placeholder.target}}>put to greeting</div></div>)},{path:'/',})serve(routes)
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)