Getting Started

hybrids - the web components

npm versionarrow-up-right bundle sizearrow-up-right typesarrow-up-right build statusarrow-up-right coverage statusarrow-up-right npmarrow-up-right gitterarrow-up-right twitterarrow-up-right Conventional Commitsarrow-up-right code style: prettierarrow-up-right GitHubarrow-up-right

🏅One of the four nominated projects to the "Breakthrough of the year" category of Open Source Awardarrow-up-right in 2019

Hybrids is a UI library for creating web componentsarrow-up-right with strong declarative and functional approach based on plain objects and pure functions.

  • The simplest definition — just plain objects and pure functions - no class and this syntax

  • No global lifecycle — independent properties with own simplified lifecycle methods

  • Composition over inheritance — easy re-use, merge or split property definitions

  • Super fast recalculation — built-in smart cache and change detection mechanisms

  • Templates without external tooling — template engine based on tagged template literals

  • Developer tools included — Hot module replacement support for a fast and pleasant development

Getting Started

Add the hybrids npm packagearrow-up-right to your application, or use unpkg.com/hybridsarrow-up-right CDN for direct usage in the browser.

Then, import required features and define your custom element:

import { html, define } from 'hybrids';

export function increaseCount(host) {
  host.count += 1;
}

export const SimpleCounter = {
  count: 0,
  render: ({ count }) => html`
    <button onclick="${increaseCount}">
      Count: ${count}
    </button>
  `,
};

define('simple-counter', SimpleCounter);

Finally, use your custom element in HTML document:

Click and play with <simple-counter> example:

Edit <simple-counter> web component built with hybrids libraryarrow-up-right

ES Modules

If you target modern browsers you can use source code directly in the script tag:

Be aware, that this mode does not provide code minification and loads all required files in separate requests.

Hot Module Replacement

HMR works out of the box, but your bundler setup may require indication that your entry point supports it. For webpackarrow-up-right and parcelarrow-up-right add the following code to your entry point:

If your entry point imports files that do not support HMR, you can place the above snippet in a module where you define a custom element. (where define method from the library is used).

Overview

There are some common patterns among JavaScript UI libraries like class syntax, a complex lifecycle or stateful architecture. What can we say about them?

Classes can be confusing, especially about how to use this or super() calls. They are also hard to compose. Multiple lifecycle callbacks have to be studied to understand very well. A stateful approach can open doors for difficult to maintain, imperative code. Is there any way out from all of those challenges?

After all, the class syntax in JavaScript is only sugar on top of the constructors and prototypes. Because of that, we can switch the component structure to a map of properties applied to the prototype of the custom element class constructor. Lifecycle callbacks can be minimized with smart change detection and cache mechanism. Moreover, they can be implemented independently in the property scope rather than globally in the component definition.

With all of that, the code may become simple to understand, and the code is written in a declarative way. Not yet sold? You can read more in the Core Conceptsarrow-up-right section of the project documentation.

Documentation

The hybrids documentation is available at hybrids.js.orgarrow-up-right or in the docsarrow-up-right path of the repository:

Articles

Core Concepts Series

Videos

Live Examples

Browser Support

Build Statusarrow-up-right

The library requires ES2015 APIs, Shadow DOMarrow-up-right, Custom Elementsarrow-up-right, and Templatearrow-up-right specifications. You can use hybrids in all evergreen browsers without additional preparation.

Older Browsers

The library test suite runs on IE11, but in near future only evergreen browsers will be supported starting from v5.0.0 release. However, if you still target obsolete or dead browsers (like IE11) you must add a list of required polyfills and shims.

Web Components

At first, add @webcomponents/webcomponentsjsarrow-up-right package on top of your project:

The polyfill package provides two modes in which you can use it (webcomponents-bundle.js and webcomponents-loader.js). Read more in the How to usearrow-up-right section of the documentation.

Web components shims have some limitations. Especially, webcomponents/shadycssarrow-up-right approximates CSS scoping and CSS custom properties inheritance. Read more on the known issuesarrow-up-right and custom properties shim limitationsarrow-up-right pages.

Store

Additionally, the store feature requires a fix for broken implementationarrow-up-right of the WeakMap in IE11:

License

hybrids is released under the MIT Licensearrow-up-right.

Last updated

Was this helpful?