@webtaku/el
v0.1.1
Published
A small, type-safe utility for creating **DOM elements** with a concise API in TypeScript.
Readme
@webtaku/el
A small, type-safe utility for creating DOM elements with a concise API in TypeScript.
Features
- Type-safe
Selectorsyntax (div#id.class) - Create HTMLElement instances
- Supports DOM properties, attributes, and inline styles
- Simple and composable API
Installation
yarn add @webtaku/elor
npm install @webtaku/elAPI
el<S extends Selector>(selector?: S, ...args): ElementBySelector<S>
Creates a DOM element.
Parameters
selector(optional): A string selector such asdiv,span#my-id,p.my-class,section#id.class. Defaults todiv....args:HTMLElement— appended as a child nodestring— added as a text nodeElementProps<S>— sets properties, attributes, or styles
Returns
A type-safe HTMLElement instance.
Example
import { el } from '@webtaku/el';
const button = el('button#myBtn.primary',
'Click me',
{ onclick: () => alert('Clicked!'), style: { color: 'red' } }
);
document.body.appendChild(button);Selector Syntax
| Selector String | Output |
| --------------------- | ---------------------------------- |
| '' | <div> |
| 'span' | <span> |
| 'div#app' | <div id="app"> |
| 'p.text' | <p class="text"> |
| 'section#main.hero' | <section id="main" class="hero"> |
ElementProps
An object that specifies properties, attributes, or styles for the element.
{
href?: string;
style?: Partial<CSSStyleDeclaration>;
[prop: string]: any;
}Example
document.body.appendChild(
el('div#container.box',
el('h1', 'Hello World'),
el('p', 'This is a paragraph.', { style: { color: 'blue' } })
)
);License
MIT OR Apache-2.0
Contributing
Contributions are welcome. Feel free to open issues or submit pull requests to improve the library.
