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

@papit/web-component

v0.0.11

Published

Base classes, decorators and an html template tag for building web components from scratch. Every papit component is built on it: reactive properties, attribute reflection, fine-grained DOM updates and form association, with no dependencies.

Readme

@papit/web-component

Base classes, decorators and an html template tag for building web components from scratch. Every papit component is built on it: reactive properties, attribute reflection, fine-grained DOM updates and form association, with no dependencies.

Logo


Type Tests NPM version


Installation

npm install @papit/web-component

The decorators are TypeScript legacy decorators, so set "experimentalDecorators": true in your tsconfig.json.


Usage

import { CustomElement, html, property, query, bind, debounce } from "@papit/web-component";
import sheet from "./style.css" with { type: "css" };

class MyCounter extends CustomElement {
    static sheet = sheet;

    @property({ type: Number, rerender: true }) count = 0;
    @query("#increment") incrementButton!: HTMLButtonElement;

    @bind
    @debounce(300)
    handleIncrement() {
        this.count++;
    }

    @bind
    handleReset() {
        this.count = 0;
    }

    render() {
        return html`
            <h2>Count: ${this.count}</h2>
            <button id="increment" @click=${this.handleIncrement}>+1</button>
            <button @click=${this.handleReset}>Reset</button>
        `;
    }
}

customElements.define("my-counter", MyCounter);
<my-counter count="5"></my-counter>

API

Full per-feature docs live in docs/.

CustomElement

class CustomElement extends HTMLElement {
    constructor(settings?: Partial<ShadowRootInit> & Partial<ElementSetting>)
}

Base class. Attaches an open shadow root, renders render() into it on connect, and patches only the changed parts on each update.

| Setting | Type | Default | Description | | ------- | ---- | ------- | ----------- | | mode, delegatesFocus, … | ShadowRootInit | mode: "open" | Passed to attachShadow | | lightDOM | boolean | false | Render into the element itself instead of a shadow root | | requestUpdateTimeout | number | 50 | Debounce for requestUpdate(), in ms | | throttleUpdateTimeout | number | 50 | Throttle for throttleUpdate(), in ms |

| Member | Description | | ------ | ----------- | | render() | Override. Return an html template, a string or a Node, or null / undefined / "" to render nothing | | firstRender() | Runs after the first render with content. Overrides must call super.firstRender(), which adopts static sheet / static sheets | | update() | Render now | | requestUpdate() | Debounced update() | | throttleUpdate() | Throttled update() | | shadowQuery(selector) / shadowClosest(selector) | querySelector / closest that walk up through shadow roots | | root | The shadow root, or the element itself in light DOM mode | | static sheet, static sheets | CSSStyleSheets adopted into the shadow root |

Fires first-render after the first render with content. disconnectedCallback is empty: if you add listeners or observers in connectedCallback, remove them there.

render() may change what it returns between updates:

render() {
    if (this.items.length === 0) return null;                  // nothing rendered (yet)
    if (this.compact) return html`<span>${this.items.length}</span>`;
    return html`<ul>${this.items.map(i => html`<li key=${i}>${i}</li>`)}</ul>`;
}

The same template is patched in place. A different one (another html call site, or a different string) replaces what is rendered, and so does nothing: the rendered nodes are removed, @query references into them are reset, and the next content mounts fresh. In light DOM mode only the rendered nodes are removed, never the element's own children.

CustomElementInternals

class CustomElementInternals extends CustomElement

CustomElement plus form association (static formAssociated = true). Adds this._internals (ElementInternals), a disabled property mirrored to aria-disabled and kept in sync with a disabled <fieldset>, and protected checkValidity(), reportValidity() and setValidity(flags, message, anchor). Style custom states with this._internals.states and :state(name).

html

html`<template>`: Node
html(markup: string): Node
unsafeHTML(markup: string): UnsafeHTML

Tagged template. The first render builds the DOM once; later renders update only the bound parts.

| Binding | Example | | ------- | ------- | | Text or node | <p>${text}</p> | | Attribute | <div class="card ${cls}"> | | Standalone attribute (a falsy value or "false" removes it) | <option ${selected && "selected"}> | | Event listener (@ or on prefix) | <button @click=${this.handleClick}> | | List | ${items.map(i => html${i.name})} | | Raw HTML | ${unsafeHTML("<b>trusted only</b>")} |

Listeners are attached as passed, so bind methods with @bind. getValues(node) returns the values bound into a template node; the element uses it internally.

Decorators

| Decorator | Purpose | | --------- | ------- | | @property / @property(settings) | Reactive property synced with an attribute | | @query / @query(selector) / @query(settings) | Reference to an element in the shadow root (or, with outside, around the element), resolved after each render until found. Selector defaults to the property name | | @bind | Binds a method to the instance on first access | | @debounce / @debounce(delay \| name \| { delay, name }) | Debounces a method. Default delay 300 ms. With name, adds the debounced version under that name and keeps the original | | @throttle / @throttle(delay \| name \| { delay, name }) | Same, throttled | | @context / @context(settings) | Reads a value from the nearest ancestor that has the property or attribute (or the element its query names), and follows its changes |

@property settings

| Setting | Type | Default | Description | | ------- | ---- | ------- | ----------- | | type | Function | — | Coerces attribute strings: Number, Boolean, Object, Array, …. Boolean also parses strings assigned to the property (el.x = "false" → false) | | attribute | boolean \| string | true | Attribute name, true for the property name, false for none | | reflect | boolean | true | Write property changes back to the attribute | | removeAttribute | boolean | true | Remove the attribute when the value is false, null or undefined | | rerender | boolean | false | Call requestUpdate() when the value changes | | aria | string | — | Mirror the value to this ARIA attribute | | readonly | boolean | false | Throw a TypeError on any assignment after the initial value | | context | boolean | false | Fire context-<name> on change, for @context consumers | | before(value, old, initial, attributeUpdate) | function | — | Runs before the value is stored | | after(value, old, initial, attributeUpdate) | function | — | Runs after the value is stored (on connect, for values set before the first connect) | | set(value) / get(value) | function | — | Transform on write / on read. set may return a promise | | hasChanged(value, old) | function | — | Custom change check | | maxReqursiveSteps | number | 20 | Depth limit when comparing nested objects |

Attribute reflection and after hooks are deferred until the element is connected, so document.createElement works.

@query settings

| Setting | Type | Default | Description | | ------- | ---- | ------- | ----------- | | selector | string \| (this) => string | property name | CSS selector; a function is called with the element as this | | outside | boolean | false | Look outside the shadow root with findTarget: a single match in the element's root, else the nearest matching ancestor. Falls back to the shadow root | | load(element) | function | — | Runs once the element is found | | error() | function | — | Runs after each render that still finds nothing |

// the carousel named by aria-controls, else the pap-carousel this button sits in
@query<Carousel>({
    outside: true,
    selector(this: Next) {
        const id = this.getAttribute("aria-controls");
        return id ? `#${CSS.escape(id)}` : "pap-carousel";
    },
}) carousel: Carousel | null = null;

@context settings

| Setting | Type | Default | Description | | ------- | ---- | ------- | ----------- | | query | string \| (this) => string | — | Selector for the provider when it is not an ancestor (e.g. #id from aria-controls). An empty result walks up as usual | | name | string | property name | Property to read on the provider | | attribute | string | property name | Attribute to read when the provider has no such property | | applyattribute | boolean | false | Also set the value as an attribute on the consumer | | rerender | boolean | true | Call requestUpdate() when the value changes | | update(value) | function | — | Runs on every change |

Functions

| Function | Description | | -------- | ----------- | | debounceFn(fn, delay = 300) | Debounced wrapper | | throttleFn(fn, delay = 300) | Throttled wrapper | | FormatNumber(value, locale = "en-US", options?) | Intl.NumberFormat with a cached formatter. STANDARD_OPTIONS gives compact notation | | ExtractSlotValue(slot) | Non-empty text of a slot's assigned nodes, as string[] | | lerp(a, b, t) / lerpValue(value, min, max, newmin, newmax) | Interpolate, or remap between ranges | | generateUUID() | Random v4 UUID | | CumulativeOffset(element) | { top, left } relative to the document | | nextParent(element) | Parent element, or the shadow host at a shadow root | | findTarget(element, query?, finder?) | Finds a related element. A query with exactly one match in the element's root returns it; otherwise it walks up from the parent (through shadow hosts), returning the first element finder accepts, or that matches the query. null if nothing does | | resolve(value) | Awaits a value, a promise or a function returning either |

STANDARD_DELAY (300) is exported too.


Known issues

  • Keyed lists can reuse the wrong DOM nodes when items are reordered (#99).
  • Some references are kept after disconnect (#83).

License

Licensed under the @Papit License 1.0 Copyright (c) 2024 Henry Pap (@onkelhoy)

Key points

  • ✅ Free to use in commercial projects
  • ✅ Free to modify and distribute
  • ✅ Attribution required
  • ❌ Cannot resell the component itself as a standalone product

See the LICENSE file for full details.


Acknowledgements

💌 Special thanks to my loving wife Phuong — your support and patience make all the difference. 💛


Related Components