| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Social Media Photo by Andrii Ganzevych on Unsplash
micro html is a ~2.5K lighterhtml subset to build declarative and reactive UI via template literals tags.
The recently introduced data helper could conflict with some node such as <object>, hence it has been replaced by the .dataset utility. Since element.dataset = object is an invalid operation, the sugar to simplify data- attributes is now never ambiguous and future-proof: <element .dataset=${...} /> it is.
Install the module via npm i uhtml and consume it as such:
import {render, html, svg} from 'uhtml';
// const {render, html, svg} = require('uhtml');
render(document.body, html`<h1>Hello 👋 µhtml</h1>`);Alternatively you can use a CDN such as unpkg, as shown in this demo.
<script src="https://unpkg.com/uhtml">/* global uhtml */</script>
<!-- or -->
<script type="module">
import {render, html, svg} from 'https://unpkg.com/uhtml?module';
</script>Most information about µhtml are written in the documentation file, but following you can read most essential details.
API SummaryThe module exports the following functionalities:
Any element can have one or more attribute, either interpolated or not.
render(document.body, html`
<div id="main"
class=${`content ${extra}`}
data-fancy=${fancy}>
<p contenteditable=${editable}
onclick=${listener}
class="${['container', 'user'].join(' ')}">
Hello ${user.name}, feel free to edit this content.
</p>
</div>
`);These are the rules to follow for attributes:
Following an example of both aria and data cases:
// the aria special case
html`<div aria=${{labelledBy: 'id', role: 'button'}} />`;
//=> <div aria-labelledby="id" role="button"></div>
// the data special case
html`<div .dataset=${{key: 'value', otherKey: 'otherValue'}} />`;
//=> <div data-key="value" data-other-key="otherValue"></div>It is possible to place interpolations within any kind of node, and together with text or other nodes too.
render(document.body, html`
<table>
${lines.map((text, i) => html`
<tr><td>Row ${i} with text: ${text}</td></tr>
`)}
</table>
`);There are only two exceptional nodes that do not allow sparse content within themselves: the style element, and the textarea one.
// DON'T DO THIS
render(document.body, html`
<style>
body { font-size: ${fontSize}; }
</style>
<textarea>
Write here ${user.name}
</textarea>
`);
// DO THIS INSTEAD
render(document.body, html`
<style>
${`
body { font-size: ${fontSize}; }
`}
</style>
<textarea>
${`
Write here ${user.name}
`}
</textarea>
`);Beside nodes where the content will be inevitably just text, like it is for style or textarea, as example, every other interpolation can contain primitives, as strings, numbers, or even booleans, or the returned value of html or svg, plus regular DOM nodes.
The only special case are Array of either primitives, or returned values from html or svg.
render(document.body, html`
<ul>
<li>This is ${'primitive'}</li>
<li>This is joined as primitives: ${[1, 2, 3]}</li>
${lines.map((text, i) => html`
<li>Row ${i} with content: ${text}</li>
`)}
</ul>
`);The second what argument of the render(where, what) signature can be either a function, which returning value will be used to populate the content, the result of html or svg tags, or a DOM node, so that it is possible to render within a render.
const Button = selector => {
const button = document.querySelector(selector);
return count => render(button, html`Clicks: ${count}`);
};
const Clicker = selector => {
const button = Button(selector);
return function update(count) {
return render(document.body, html`
<div onclick=${() => update(++count)}>
Click again:
${button(count)}
</div>
`);
};
}
const clicker = Clicker('#btn-clicker');
clicker(0);µhtml html and svg tags implement exact same API offered by lighterhtml.
This means that both html.for(reference[, id]) and svg.for(reference[, id]) will weakly relate the node with the reference, and an optional unique id, instead of using its internal auto-referenced algorithm.
render(document.body, html`
<ul>
${items.map(item => html.for(item)`
<li>Keyed row with content: ${item.text}</li>
`)}
</ul>
`);Custom Elements are either brought you, in a simplified manner, via µce (micro custom elements), which is a 3K library based on µhtml, or via vanilla JS, as demoed in WebComponents.dev.
This module works in IE11, Edge, and every other Desktop to Mobile browser, including KaiOS.
You could read an exhaustive summary of features differences, but the first thing to keep in mind, is that lighterhtml is at pair with uhtml features, but not vice-versa, meaning if you need anything more, you can always switch to lighterhtml later on, and without changing a single line of code.
Following a list of other points to consider when choosing µhtml instead of lighterhtml (or vice-versa).
Differently from lighterhtml| Back | FazBrowse Home | New Git URL |