Home / Modern UI / UI Component Library

UI Component Library in JavaScript, Free with Live Demo

Free UI component library in plain JavaScript. Six Web Components with Shadow DOM: button, toggle, tabs, rating, progress ring and accordion. Works with any framework.

Open live demoDownload HTML fileView code on GitHub
UI Component Library JavaScript project: six reusable Web Components you can drop into any page

Runs on: Custom Elements v1. Every modern browser. Custom Elements and Shadow DOM have been supported everywhere since 2020.

What is the UI Component Library?

Web Components let you make your own HTML tags, like <ui-toggle> or <ui-rating>, that work in any page and any framework. This project builds six of them: a button, a toggle switch, tabs, a star rating, a progress ring and an accordion.

Each component is a JavaScript class that keeps its markup and styles inside a Shadow DOM, so nothing leaks in or out. They react when you change an attribute and fire normal events, which is why the same tag works in React, Vue or plain HTML.

Good for

  • Design systems shared across several apps
  • Widgets you embed on other people's sites
  • Teams that use more than one framework
  • Learning how browsers build components

What this project does

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.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

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);

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Can I use Web Components in React?

Yes. They are real HTML elements, so React can render them. Pass values as attributes and listen to their events with a ref or the onChange style props in React 19.

Do Web Components work in all browsers?

Yes. Custom Elements and Shadow DOM have worked in Chrome, Edge, Firefox and Safari since 2020. No polyfill is needed.

How do I style a component from outside?

Use CSS custom properties, like --ui-accent in this project. They pass through the Shadow DOM, so you can theme every component from one place.

More Modern UI projects