Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hyperhtml

WebReflection14.9kISC2.34.2TypeScript support: included

A Fast & Light Virtual DOM Alternative

dom, diff, performance, template, literals, lightweight, fast, react, virtual, lit-html, alternative

readme

hyper(HTML)

📣 Community Announcement

Please ask questions in the dedicated discussions repository, to help the community around this project grow ♥


hyperHTML logo

A Fast & Light Virtual DOM Alternative.


donate Backers on Open Collective Sponsors on Open Collective WebReflection status

Coverage Status Build Status License: ISC Greenkeeper badge Blazing Fast


Following an overview of projects related, or inspired by, hyperHTML. For a deep comparison of current libraries, feel free to check this gist out.

µhtml

The latest, smallest, iteration of all best concept from this library since 2017, have been packaged in ~2.5K. If it's extreme minimalism and great DX that you are after, check uhtml out.

hypersimple

If you've just started with template literals based projects and you like components, or you'd like to understand what's hyperHTML capable of, give hypersimple a try 🎉

lighterhtml 💡

This little brother is "showing off" these days, claiming better performance and unprecedented ease of use.

GitHub Repository

Neverland 🌈🦄

If you like React hooks concept, don't miss this little wrap that adds 0.something overhead to the already lightweight hyperHTML, bringing in very similar concepts.

Blog Post

GitHub Repository

Haunted 🦇 🎃

If you also like React hooks mechanism and you'd like to combine these via hyperHTML or HyperHTMLElement, try haunted out!

Bundlers

You can require or import hyperHTML with any bundler and in different ways.

If requiring or importing from "hyperhtml" doesn't work, try requiring from "hyperhtml/cjs" for CommonJS friendly bundlers (WebPack), or "hyperhtml/esm" for ESM compatible bundlers (Rollup).

See HELPERS.md for a list of additional tools which can be helpful for building hyperHTML based web applications.


Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Backers

Thank you to all our backers! 🙏 [Become a backer]

Contributors

This project exists thanks to all the people who contribute. [Contribute].


2.34 Highlights

  • the new ?boolean=${value} syntax from µhtml has landed in hyperHTML too. Feel free to rea this long discussion to better understand why this syntax is necessary.

V2.5 Highlights

  • <self-closing /> tags for both custom elements and any other as well 🎉

V2 Highlights

Following most important changes in version 2:

  • fully rewritten, and consumable, as ES2015 Module
  • usable via CDN as bundled global hyperHTML variable
  • restructured in modules, utilities, helpers, and commented all over for simplified contribution
  • removed .escape and .adopt, either useless or unstable. hyperHTML.adopt will be implemented as module a part
  • added support for objects as style attribute, fully compatible with Preact implementation
  • improved performance in numerous ways
  • custom elements V0 and V1 are now fully, and properly, supported through document.importNode and/or regular cloneNode tested against common polyfills
  • back to 4.6K thanks to rollup and its ability to merge all the things together like it was already in V1

Documentation

A proper documentation full of examples can be found in viperhtml.js.org.

Basic Example

The easiest way to describe hyperHTML is through an example.

// this is hyperHTML
function tick(render) {
  render`
    <div>
      <h1>Hello, world!</h1>
      <h2>It is ${new Date().toLocaleTimeString()}.</h2>
    </div>
  `;
}
setInterval(tick, 1000,
  hyperHTML(document.getElementById('root'))
);

Features

  • Zero dependencies, no polyfills needed, and it fits in about 4.6KB (minified + brotli)
  • Uses directly native DOM, no virtual DOM involved
  • Designed for template literals, a templating feature built in to JS
  • Compatible with plain DOM elements and plain JS data structures
  • Also compatible with Babel transpiled output, hence suitable for every browser you can think of

Compatibility

IE9+ , iOS8+ , Android 4+ and every modern Mobile or Desktop Browser. You can verify directly through the following links:

Weakmap error on ie < 11

'@ungap/weakmap': object is not extensible

Babel freezes the template literals by spec but that causes problems with the weakmap polyfill. To fix this error add the fix explained on ungap/weakmap

HTML Syntax Highlight

If you are using Visual Studio Code you can install literally-html to highlight all literals handled by hyperHTML and others.

literally-html example

Prettier Templates

If you'd like to make your templates prettier than usual, don't miss this plugin: https://github.com/sgtpep/prettier-plugin-html-template-literals

Questions ?

Please ask anything you'd like to know in StackOverflow using the tag hyperhtml so that others can benefit from answers and examples.

hyper or lit ?

You can read more on this hyperHTML vs lit-html comparison.

installation?

npm install hyperhtml

If your bundler does not work with the following:

// ES6
import hyperHTML from 'hyperhtml';

// CJS
const hyperHTML = require('hyperhtml');

You can try any of these other options.

import hyperHTML from 'hyperhtml/esm';
// or
import {hyper, wire, bind, Component} from 'hyperhtml/esm';
// or
import hyperHTML from 'https://unpkg.com/hyperhtml?module';


const hyperHTML = require('hyperhtml/cjs').default;
// or
const {hyper, wire, bind, Component} = require('hyperhtml/cjs');

In alternative, there is a pre-bundled require("hyperhtml/umd") or via unpkg as UMD module.

changelog

hyper(html) Changelog

v.2.23

  • monkey patched rollup generated code to export once the same module shared within sub-modules

v2.22

  • using latest domtagger

v2.21

  • refactored out all dependencies

v2.20

  • re-tested every single supported browser nd fixed few outstanding issues with the 2.19 release

v2.19

  • refactored out most of the code
  • finally managed to have coveralls show coverage stats
  • attributes can have spaces around as per DOM standard - #244
  • fixed SVG (non-critical) errors when interpolations are used for numerically expected values
  • fixed minor issues with Edge attributes
  • changed the unique id so if any of your logic was trusting _hyper: ....; comments you need to update your logic - #300

v2.16.8

  • improved MutationObserver and fallback so that double dis/connected events won't happen again
  • exposed observe utility for 3rd parts so that it is possible to observe any node, not only those defined via template literals. Once observed, a node can have connected and disconnected listeners that will be triggered automatically.

v2.16

  • modified Wire class to better handle "same target" case, making the haunted.html demo work same way as if it was bound to the node, through valueOf() invoke which would result in just exactly the same node if the wired content produced a node instead of a fragment. While regular users won't be affected, this is an implementation detail that changes a lot for libraries integrating hyperHTML.wire in their logic, making wires as fast as bind in most component related use cases.

v2.15

  • added invokable slots to let developers explore patterns through callbacks that will receive a unique live node for weak references while rendered.

v2.14

  • updated domdiff to match petit-dom performance
    • up to 3X performance on huge lists
    • improved reliability over random changes
    • unfortunately there's a +0.6K overall size increase due amount of extra logic involved

v2.13.2

  • added support for custom CSS properties as object keys.

v2.13.1

v2.13

  • added the ability to define custom attributes via hyperHTML.define("hyper-attribute", callback), so that <p hyper-attribute=${anyValue}/> would invoke callback(target, anyValue) where p would be the target.

v2.12

  • added hyper.Component#dispatch(type, detail) method to simplify events dispatching between lightweight components, bubbling a cancelable Custom Event with a .component property that points at the dispatcher, while the event.currentTarget will be the first node found within the component render.

v2.11

v2.10.12

v2.10.10

  • updated domdiff to solve issue #243 (breaking with some sorted list)

v2.10.5

v2.8.0

  • updated domdiff engine to boost performance with segments and lists

v2.7.2

  • fixed #218 which was a variant of #200

v2.7.0

  • the Component.for(obj) is now created first time via new Component(obj) - #216

v2.6.0

  • declarative hyper.Component via Component.for(context, uid?) - #202
  • hyperHTML TypeScript information - #201

v2.5.12

  • fixed #200: textarea/style with initial undefined value

v2.5.11

  • fixed #198: connected/disconnected events for nested components

v2.5.10

  • more rigid / explicit RegExp to avoid glitches with self-closing tags

v2.5.8

  • improved VOID_ELEMENTS regular expression (aligned with the viperHTML one)

v2.5.7

  • fixed no.js patch when wrong count of args is passed

v2.5.6

  • added no.js file for environments without the ability to use modern JS or based on other languages such Dart.

v2.5.5

  • build runs on macOS too
  • added umd.js file

v2.5.2

  • fixed weird SVG case (see #172)

v2.5.1

  • improved self-closing reliability recycling and sharing attributes RegExp

v2.5.0

  • updated domdiff library to the latest version
  • implemented self-closing tags (and after various tests)

v2.4.3

  • ensure attributes values are updated when different from previous one
  • avoid the usage of the word global in the whole code

v2.4.2

  • fix scripts with actual content too.

v2.4.1

  • fix a bug with scripts that don't trigger network requests in both Firefox and Safari (see bug #152)

v2.4.0

  • created a Wire class to handle via domdiff multiple wired nodes.
  • brought back multi nodes per wire, a feature lost since v2.0
  • simplified Component handling too, making it compatible again with multi wired content.
  • fixed some check to make IE9+ tests green again

v2.3.0

  • dropped the engine already. Too complex, no real benefits, refactored the whole internal logic to use domdiff instead. Deprecated hyperhtml-majinbuu and solved diffing "forever".

v2.2.0

  • the whole hyperHTML.engine has been refactored to use dom-splicer as an effort to make engine development easier

v2.1.3

  • the MutationObserver is installed only once and only if there are components that have on(dis)?connect handlers.

v2.1.2

  • using a new folders convention with esm/index.js as main module and cjs/index.js as transformed artifact. This plays very well with bundlers when you import {hyper} from 'hyperhtml/esm' or const {hyper} = require('hyperhtml/cjs');

v2.1.1

  • fast changes where prepending or appending same lists; now dropping upfront or removing at the end are part of the fast path too.

v2.1.0

  • created a simple default merge engine focused on performance
  • remove majinbuu as core dependency, created hyperhtml-majinbuu project to swap it back via hyperHTML.engine = require('hyperhtml-majinbuu') or as ESM
  • reduced final bundle size down to 4.1K via brotli

v2.0.0

Refactoring following ticket #140