Vanilla JavaScript Projects

Project 11, Modern UI

UI Component Library

Six components you can drop into any page, with or without a framework. Each one is a custom HTML tag with its own styles, attributes and events.

<ui-tabs><ui-rate><ui-ring><ui-btn>
Main API
Custom Elements v1
Styling
Shadow DOM, CSS parts
Dependencies
None
Your browser
Checking

Live components

Every block below is a real custom element. Click, type and use the keyboard. Events show underneath.

How it works

  1. Define a classEach component extends HTMLElement and builds its markup inside an open shadow root.
  2. Watch attributesobservedAttributes lists what to watch, and attributeChangedCallback re-renders when one changes.
  3. Talk to the pageComponents fire CustomEvents such as change, so any framework can listen with a normal event listener.
class UiToggle extends HTMLElement {
  static observedAttributes = ["checked", "label"];
  constructor() {
    super();
    this.attachShadow({ mode: "open" });
  }
  attributeChangedCallback() { this.render(); }
  toggle() {
    this.toggleAttribute("checked");
    this.dispatchEvent(new CustomEvent("change", { detail: this.hasAttribute("checked") }));
  }
  render() { /* build markup inside this.shadowRoot */ }
}
customElements.define("ui-toggle", UiToggle);