用于服务器端 html 的 HTMX + JSX 原型语法。
2022/11/7 23:24:13
本文主要是介绍用于服务器端 html 的 HTMX + JSX 原型语法。,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
// a HTMX approach with JSX? import { Fragment, JSX } from "preact"; import { nanoid } from 'nanoid' interface Options { path: string } const routes = [] class HTMX_IN_JSX { id: string constructor( public Component: (props: any) => any, public opt?: Options ) { this.id = nanoid() routes.push(this) } get path () { if (this.opt?.path) return this.opt.path throw new Error('Href for component not available') } get href () { return this.path } get target () { return {"hx-target": `#${this.id}`} } get put () { return {"hx-put": this.path} } get get () { return {"hx-get": this.path} } WrapId () { return (props: any) => ( <span id={this.id}> <this.Component {...props}/> </span> ) } Anchor (props: any): JSX.Element { return <a href={this.href}>{props.children}</a> } AnchorBoost (props: any): JSX.Element { return <a href={this.href} hx-boost="true">{props.children}</a> } } const component = (x: (props: any) => any, opt?: Options): ((props: any) => any) & HTMX_IN_JSX => { const instance = new HTMX_IN_JSX(x, opt) const ex = instance.WrapId() return Object.assign(ex, instance) } const Greeting = component(() => { return ( <div> <h1>hello</h1> </div> ) }, { path: '/greeting', }) const Placeholder = component(() => {}) const Page = component(() => { return ( <div> <Placeholder/> <Greeting.Anchor>Navigates to anchor</Greeting.Anchor> <Greeting.AnchorBoost>Fetches anchor page and swaps dom</Greeting.AnchorBoost> <a href={Greeting.href}>Simple link to greeting page</a> <div {...{...Greeting.put, ...Placeholder.target}}>put to greeting</div> </div> ) }, { path: '/', }) serve(routes)
这篇关于用于服务器端 html 的 HTMX + JSX 原型语法。的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-19vue2 开发移动端h5 使用那个ui框架比较好?-icode9专业技术文章分享
- 2024-11-19ReactJS结合TypeScript、Vite、Redux和TanStack (React Query) 实战教程
- 2024-11-19Vue3资料入门教程:零基础快速上手指南
- 2024-11-19Vue3资料:新手入门教程与实战指南
- 2024-11-19Vue资料:新手入门vue.js教程与实践指南
- 2024-11-19Vue资料:初学者入门指南
- 2024-11-18tcpdf可以等待vue动态页面加载完成后再生成pdf吗?-icode9专业技术文章分享
- 2024-11-16Vue3资料:新手入门必读教程
- 2024-11-16Vue3资料:新手入门全面指南
- 2024-11-16Vue资料:新手入门完全指南