npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rich-component

v1.8.0

Published

library providing means to work conveniently with HTML based Web Components

Readme

GitHub npm Travis Codecov Codacy

Summary

rich-component provides a set of means to easily manage WebComponents with HTML template.

Terms web component and custom element are used interchangeably here.

It helps order and cleanness freaks like myself to separate easily the HTML/CSS from the JavaScript.

rich-component ensures correctness of the provided template, fetches/caches the template when needed and injects it into the shadow/light of each newly created component instance.

Few points to stress:

  • rich-component is very small in itself and has very narrow defined functionality, it's more like a utility or a micro-framework, than a full blown beast
  • simplicity is one of the primary things the author concerned with
  • APIs designed to second the native component definition APis, so migration to & from rich-component requires minimal effort - it's not binding you to anything
  • when template needs to be fetched over the network, the custom element definition postponed to that, thus making the component available to the application only when ready to use (dynamic URL case being an exception)
  • template's content defaultly added to an open shadow DOM; one may opt adding it to a light DOM.

Customized built-in elements are not yet supported, will extend the library if/when that spec will become widely supported by browsers.

Last versions (full changelog is here)

  • 1.8.0

    • implemented issue #8
      • added dynamic html URL provisioning
    • updated dependencies versions
  • 1.7.0

    • implemented issue #8 - added test and documentation of already available functionality
    • updated dependencies versions
  • 1.6.0

    • updated dependencies versions
    • adjusted the repo and the dist to the better practice (dist is not committed but published only)

Usage

The workflow is ultimately simple:

  • import rich-component's API
  • define your component's class extending library's ComponentBase class, which extends HTMLElement
  • part of your class implementation should provide a template for your custom element (see API documentation for the details)

Basic example

Lets assume, that the template is found in my-element.html file.

We can now define the my-element component in the my-element.js (assuming it located beside my-element.html):

import { initComponent, ComponentBase } from './dist/rich-component.min.js';
...

initComponent('my-element', class extends ComponentBase {

  ...

  static get htmlUrl() {
		return import.meta.url.replace(/js$/, 'html');
	}
});

Of course, URL of HTML may be given any other way, even hardcoded, but I've found myself typically using the above pattern, making components look uniformly.

Having the code as above, rich-component will do the following steps:

  • first ensure a properly defined template source, failing fast if otherwise
  • then fetch the template and cache it
  • define the custom element in registry
  • inject the template into the shadowRoot each time my-element is constructed

Dynamic resolution example

For the best readability and predictability of the flow, it is most advised to staticly provide the template URL (or template content) upfront.

Sometimes it is required to switch the template dynamically, based on instance attributes, for example.

For those use cases template / htmlUrl may return a function.

That function will be called during element's construction and expected to return a template or template URL (correspondingly to getter).

Consider the following example of dynamic template:

initComponent('my-element', class extends ComponentBase {

  static get template() {
		return self => {
			return self.hasAttribute('readonly') ? x : y;
		};
	}
});

In the example above, getter template provides a function, which will be called each instance construction returning relevant template (in our example x/y) based on instance attribute.

Using the rich-component with 'inline' templates may still be convenient and provide some kind of a future proofness, making it an easy task to externalize HTML/CSS part when overgrown. Yet, IMHO it would also be perfectly right to not use rich-component at all in such a cases, but fall-back to the native API.

Detailed API is here.