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

@digital-nature-ltd/web-component

v1.0.8

Published

A base web component class containing utility methods and configuration

Downloads

19

Readme

Digital Nature - Web Component

This package contains a single base class for use with Web Components. This class comes with some configuration options and utility methods for use in your extending web component class.

Usage

Simple usage

import DigitalNatureWebComponent from "@digital-nature-ltd/web-component";

class MyNewComponent extends DigitalNatureWebComponent {
    constructor() {
        super({
            options: gohere
        });
    }
}

customElements.define('my-new-component', MyNewComponent);
// can be used <my-new-component></my-new-component>

Template usage

HTML/CSS can be added directly into an HTML file for improved readability, this can be imported and passed into the web component constructor

import DigitalNatureWebComponent from "@digital-nature-ltd/web-component";
// remember the ?raw param when including HTML directly
import myComponentTemplate from "./templates/my-new-component-template.html?raw"

class MyNewComponent extends DigitalNatureWebComponent {
    constructor() {
        super({
            template: myComponentTemplate
        });
    }
}

customElements.define('my-new-component', MyNewComponent);
// can be used <my-new-component></my-new-component> - will contain the contents of your template

Handling events

Your custom component will be able to handle any js events with a handleEventtype functions, e.g. handleClick

import DigitalNatureWebComponent from "@digital-nature-ltd/web-component";

class MyNewComponent extends DigitalNatureWebComponent {
    constructor() {
        super({
            options: gohere
        });

        // add the event listener with any type, pass `this` in as the handler
        this.addEventListener("click", this);
    }

    // function name should be `handle` followed by the event type.
    // note that the event type first letter will be capitalised
    handleClick (event)
    {
        console.log('You clicked the custom element');
    }
}

customElements.define('my-new-component', MyNewComponent);
// can be used <my-new-component></my-new-component> - clicking on the component will run the handleClick function and log to console

Creating a new web component in js

Extending components should have a static tagName property set.

// MyNewComponent.js
class MyNewComponent extends DigitalNatureWebComponent {
    static tagName = 'my-new-component';
}

The base web component class comes with a static helper method create that will create a new instance of the extending component and return it.

// some-other-js-file.js
// create the element
let myComponentInstance = MyNewComponent.create({
    // you can pass in options here, e.g. template, attachShadow, shadowMode
    template: "<div>My component <em>has some content</em></div>"
});
// add it to the page
document.appendChild(myComponentInstance);

Adopted stylesheets

Stylesheets can be adopted in 3 ways, giving flexibility over how you style the component globally whilst retaining the ability to style components individually.

What can be adopted?

  • A css file, by url
  • css rules in a string
  • a CSSStyleSheet

Styles for all instance of your component

Add the getAdoptedStyles function to your component, it should return an array of the items to be adopted

class MyNewComponent extends DigitalNatureWebComponent {
    getAdoptedStyles() {
        return [
            "/path/to/your/css/file.css",
            "div { background: blue; }"
        ];
    }
}

Styles passed as options

Pass in a stylesheets option when using the static create method.

let myComponentInstance = MyNewComponent.create({
    stylesheets: [
        "/path/to/your/css/file.css",
        "div { background: red; }"
    ]
});

Styles passed in direct to instance of component

Components have an adopt method, note that this accepts only one rule at a time

let oneParticularComponent = document.querySelector('my-new-component#the-one');
oneParticularComponent.adopt("div { background: purple; }");

Configuration

When calling the base component constructor you can pass in a configuration object, the available options are listed below

Example

{
    attachShadow: false, // default true
    shadowMode: 'open', // default 'open'
    template: "<div>My component <em>has some content</em></div>",
    stylesheets: [adoptedStylesheet, "div { color: red !important; }"]
}

attachShadow

Decides whether to attach the shadow DOM for this component

shadowMode

If you are attaching the shadow DOM then this controls the mode, options are open or closed

template

The template should be a string of HTML

stylesheets

An array of stylesheets to be adopted by the component, either as a URL string, a CSS string or an instance of CSSStyleSheet