createElement
Creates an HTML or SVG element with properties, attributes, listeners, and children.
Usage
ts
import { createElement } from 'rattail'
const button = createElement(
'button',
{
class: 'submit',
'aria-label': 'Save',
onClick: () => console.log('saved'),
},
'Save',
)Arguments
| Argument | Description |
|---|---|
tag | HTML, custom element, or SVG tag name |
options | Optional flat configuration, such as src, class, aria-label, or onClick |
children | Optional node, string, number, or nested array of children |
Return
Returns the created element. TypeScript infers the type for known tags, such as HTMLButtonElement for button.
Notes
- Use
onClickfor a listener oronClick: [listener, { once: true }]for listener options. - Use
stylefor CSS text or a style object. Usedatasetor keys such as'data-id'for data attributes. - Strings and numbers in
childrenbecome text. Empty values and booleans are skipped. Choose one ofchildren,textContent, orinnerHTMLto set content. - The
svgtag selects SVG automatically. For other SVG tags, passnamespace: 'svg', as increateElement('circle', { namespace: 'svg', r: 8 }).