hyperscript-string

2.1.0 • Public • Published

hyperscript-string

Build status NPM version XO code style

Create HTML strings with JavaScript

Goes hand in hand with hyperscript-helpers.

Installation

Install hyperscript-string using npm:

npm install --save hyperscript-string

Usage

Module usage

const h = require('hyperscript-string');
 
const html = h('ul.list', [
    h('li.item', 'Lorem ipsum'),
    h('li.item', {title: 'Second item'}, 'Dolor sit'),
    h('li.item', 'Amet')
]);
/*
<ul class="list">
    <li class="item">Lorem ipsum</li>
    <li title="Second item" class="item">Dolor sit</li>
    <li class="item">Amet</li>
</ul>
*/

Attributes that are null, undefined or false are excluded in the generated HTML.
Empty strings are left as is, e.g. h('a', {href: ''}) => '<a href=""></a>', and true are generated without value, e.g. h('button', {disabled: true}, 'Click me') => '<button disabled>Click me</button>'.
Void elements' children are excluded in the generated HTML as well.

Related packages

API

h(selector[, attrs, children])

Name Type Description
selector String Contains at least the HTML tag name
attrs Object Attributes for the HTML tag
children String or Array The children for the HTML tag

Returns: String, the generated HTML.

License

MIT © Joakim Carlstein

Package Sidebar

Install

npm i hyperscript-string

Weekly Downloads

9

Version

2.1.0

License

MIT

Last publish

Collaborators

  • joakimbeng