wc-jsx-runtime
v1.0.1
Published
A JSX transform for Web Components
Downloads
5
Readme
wc-jsx-runtime
A JSX transform for Web Components
Usage
This library transforms JSX in web components into DOM operations. React's new JSX transform, so all you need to do is configure your build tool to use https://esm.sh/wc-jsx-runtime
for jsxImportSource
.
This transform:
- Turns all JSX into DOM operations (ie,
<div/>
becomesdocument.createElement('div')
), - Automatically calculates the tag name of each web component by hyphen-casing its constructor name (ie,
AppBody
becomesapp-body
), - Determines whether the web component is autonomous (ie,
<hello-world>
) or custom (ie,<div is="hello-world">
) based on whether it inherits fromHTMLElement
. - Automatically runs
customElements.define
the first time the web component is instantiated.
Example
Here's an example that uses both custom (such as <html is="app-html">
) and autonomous (such as <hello-world></hello-world>
) web components.
class AppHtml extends HTMLHtmlElement {
connectedCallback() {
this.append(
<AppHead />,
<AppBody />
)
}
}
class AppHead extends HTMLHeadElement {
connectedCallback() {
this.append(
<title>Testing ws-jsx-runtime...</title>
)
}
}
class AppBody extends HTMLBodyElement {
connectedCallback() {
this.append(<HelloWorld name='JSX'/>)
}
}
class HelloWorld extends HTMLElement {
connectedCallback() {
this.append(`Hello, ${this.getAttribute('name')}!`)
}
}
document.documentElement.replaceWith(<AppHtml />)
Once run, the DOM will look like this:
<html is="app-html">
<head is="app-head">
<title>Testing ws-jsx-runtime...</title>
</head>
<body is="app-body">
<hello-world>Hello, JSX!</hello-world>
</body>
</html>
Todo
- Add special cases for elements with names that don't conform to
HTML${tagName}Element
pattern.