hybrids

? Web Components with a strong declarative and functional approach based on plain objects and pure functions

Github星跟踪图

npm version
bundle size
types
build status
coverage status
npm
gitter
twitter
Conventional Commits
GitHub

?One of the four nominated projects to the "Breakthrough of the year" category of Open Source Award in 2019

Hybrids is a UI library for creating web components 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

Install npm package:

Then, import the required features and define a 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);

? Click and play on ⚡StackBlitz

Finally, use your custom element in HTML:

<simple-counter count="10"></simple-counter>

ES Modules

If you target modern browsers and do not want to use external tooling (like webpack or parcel), you can use ES modules:

<script type="module">
  // We can use "/src" here - browsers, which support modules also support ES2015
  import { html, define } from 'https://unpkg.com/hybrids@[PUT_VERSION_HERE:x.x.x]/src';
  ...
</script>

Please take to account, that it does not provide code minification and loads all required files in separate requests.

Built Version

For older browsers support you can use the built version (with window.hybrids global namespace):

<script src="https://unpkg.com/hybrids@[PUT_VERSION_HERE:x.x.x]/dist/hybrids.js"></script>
<script>
  var define = window.hybrids.define;
  var html = window.hybrids.html;
  ...
</script>

Hot Module Replacement

HMR works out of the box, but your bundler setup may require indication that your entry point supports it. For webpack and parcel add the following code to your entry point:

// Enable HMR for development
if (process.env.NODE_ENV !== 'production') module.hot.accept();

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 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 Concepts section of the project documentation.

Documentation

The hybrids documentation is available at hybrids.js.org or in the docs path of the repository:

Articles

Core Concepts Series

Videos

Live Examples

Browser Support

Build Status

The library requires some of the ES2015 APIs and Shadow DOM, Custom Elements, and Template specifications. You can use hybrids in all evergreen browsers and IE11 including a list of required polyfills and shims. The easiest way is to add a bundle from @webcomponents/webcomponentsjs package on top of your project:

import '@webcomponents/webcomponentsjs/webcomponents-bundle.js';
import { define, ... } from 'hybrids';

...

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 use section of the documentation.

Web components shims have some limitations. Especially, webcomponents/shadycss approximates CSS scoping and CSS custom properties inheritance. Read more on the known issues and custom properties shim limitations pages.

License

hybrids is released under the MIT License.

主要指标

概览
名称与所有者hybridsjs/hybrids
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2016-12-06 20:19:43
推送于2025-05-30 11:26:11
最后一次提交
发布数149
最新版本名称v9.1.17 (发布于 2025-05-30 13:22:33)
第一版名称v0.2.0 (发布于 )
用户参与
星数3.1k
关注者数44
派生数86
提交数855
已启用问题?
问题数199
打开的问题数5
拉请求数65
打开的拉请求数2
关闭的拉请求数17
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?