Yet another JSX pragma for Snabbdom
npm install @herp-inc/snabbdom-jsxYet another JSX pragma for Snabbdom
- Straightforward and intuitive syntax
- rather than
- Typechecked attributes on intrinsic elements
- Only for HTML elements for now
- Typechecked children
- className and id will be the part of the sel
- Type-safe custom modules via module augmentation
- Support for React 17 style automatic runtime
``tsx`
const vnode = (
);
Note that the following packages are peer dependencies of this library, which need to be installed separately.
| Package | Version |
| ---------------------------------------------------- | ------- |
| csstype | 3 |snabbdom
| | 3 |
`sh`
$ npm install @herp-inc/snabbdom-jsx
`sh`
$ yarn add @herp-inc/snabbdom-jsx
Note that fragments are still experimental. Make sure you are using Snabbdom v3.2+ and opt it in to enable the feature.
`ts`
const patch = init(modules, undefined, {
experimental: {
fragments: true,
},
});
Add the following options to your tsconfig.json:
`json`
{
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "@herp-inc/snabbdom-jsx"
}
}
Add @babel/plugin-transform-react-jsx to your devDependencies.
Add the following options to your Babel configuration:
`json`
{
"plugins": [
[
"@babel/plugin-transform-react-jsx",
{
"importSource": "@herp-inc/snabbdom-jsx",
"runtime": "automatic"
}
]
]
}
By default, an attribute will be passed to the props module.
`tsx`
// { props: { type: 'text' } }
However, certain attributes will be treated differently.
className and id attributes will be concatenated to the sel with . and # respectively, and won't be passed to any modules. For example, the expression
will yield a virtual node with { sel: 'div#foo.bar.baz' }$3
An attribute starting with
aria- will be passed to the attributes module.`tsx
// { attrs: { 'aria-label': 'Send' } }
`$3
An attribute starting with
data- will be passed to the dataset module. Note that the data- prefix will be removed and dashes will be converted to camel case.`tsx
// { dataset: { fooBar: 'baz' } }
`$3
The
is attribute can be used when you want to instantiate your customized built-in elements.`tsx
// { is: 'custom-element' }
`$3
An attribute starting with
on will passed to the event listeners module.`tsx
onclick={(e) => {
console.log(e);
}}
/>
// { on: { click: f } }
`$3
The
list, the role, and the popoverTarget attributes will be passed to the attributes module.
Note that the attribute names will be lowercased.`tsx
// { attrs: { role: 'button' } }
// { attrs: { list: 'options' } }
// { attrs: { popovertarget: 'popover' } }
`$3
The
$hook attribute is treated as hooks.`tsx
$hook={{
insert(vnode) {
console.log(vnode);
},
}}
/>
// { hook: { insert: f } }
`For the sake of backward compatibility,
hook (without the dollar sign) also behaves the same. However it is deprecated and will be removed in the future.$3
The
$key attribute is treated as a key.`tsx
// { key: 'foo' }
`For the sake of backward compatibility,
key (without the dollar sign) also behaves the same. However it is deprecated and will be removed in the future.$3
Attributes of