@webtaku/h
v0.1.1
Published
A small, type-safe utility for creating **HTML strings** with a concise API in TypeScript.
Downloads
135
Readme
@webtaku/h
A small, type-safe utility for creating HTML strings with a concise API in TypeScript.
Features
- Type-safe
Selectorsyntax (div#id.class) - Create HTML strings
- Supports attributes and inline styles
- Simple and composable API
Installation
yarn add @webtaku/hor
npm install @webtaku/hAPI
h<S extends Selector>(selector?: S, ...args): string
Creates an HTML string.
Parameters
selector(optional): A string selector such asdiv,span#my-id,p.my-class,section#id.class. Defaults todiv....args:HTMLElement— converted toouterHTMLstring— added as HTML textElementProps<S>— sets attributes and styles
Returns
An HTML string.
Example
import { h } from '@webtaku/h';
const markup = h('a.link#home',
'Home',
{ href: '/', style: { textDecoration: 'none' } }
);
console.log(markup);
// <a id="home" class="link" href="/" style="textDecoration: none">Home</a>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 attributes and styles for the element.
{
href?: string;
style?: Partial<CSSStyleDeclaration>;
[prop: string]: any;
}Example
console.log(
h('div#container.box',
h('h1', 'Hello World'),
h('p', 'This is a paragraph.', { style: { color: 'blue' } })
)
);
// <div id="container" class="box"><h1>Hello World</h1><p style="color: blue">This is a paragraph.</p></div>License
MIT OR Apache-2.0
Contributing
Contributions are welcome. Feel free to open issues or submit pull requests to improve the library.
