Skip to content

jsx-dom

jsx-dom uses JSX to create DOM elements, without a framework or a virtual DOM.

TypeScript Configuration

Add the component types to your TypeScript configuration:

tsconfig.json
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "jsx-dom",
"types": [
// Required: types the component tags in jsx-dom JSX
// For one component: "@staticview/ui/types/jsxDom/[component-name]"
"@staticview/ui/types/jsxDom",
// Optional: types what `querySelector` and `createElement` return for each tag
// For one component: "@staticview/ui/types/dom/[component-name]"
"@staticview/ui/types/dom",
// Optional: declares each component type globally, with no import
// For one component: "@staticview/ui/types/global/[component-name]"
"@staticview/ui/types/global",
],
},
}

Importing Web Components

Since components register globally, import each one once, in your app's entry point:

main.tsx
import "@staticview/ui/[component-name]";

Setting Attributes

jsx-dom sets every string, number, and boolean value as an attribute:

<component-name label="Title"></component-name>

Boolean Attributes

A boolean attribute is on whenever it is present, and close-button="false" turns the close button on. Pass a boolean, and jsx-dom adds the attribute for true and leaves it out for false:

<component-name close-button={hasCloseButton}></component-name>

Setting Properties

Since jsx-dom does not set properties from JSX, set them in a ref callback, which receives the typed element:

<component-name ref={element => (element.items = items)}></component-name>

Handling Events

Listen to component events with on[eventName]:

<component-name onopenedchange={handleOpenedChange}></component-name>

Using Slots

Put the content between the tags, with a slot attribute for a named slot:

<component-name>
<span slot="title">Title</span>
<span>Content</span>
</component-name>