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 🙏

© 2025 – Pkg Stats / Ryan Hefner

preact-web-component

v0.0.2

Published

This package allows you to generate and register custom elements (Web Components) from Preact components. It enables seamless integration of Preact-based UI components into any HTML environment—framework-agnostic and standards-compliant.

Readme

preact-web-component

This package allows you to generate and register custom elements (Web Components) from Preact components. It enables seamless integration of Preact-based UI components into any HTML environment—framework-agnostic and standards-compliant.

Use it to encapsulate your Preact components as reusable Web Components that can be dropped into any web app, regardless of the underlying JavaScript framework.

This package is built on top of preactjs/preact-custom-element but adds first-class TypeScript support.

Usage

Import register and call it with your component, a tag name, and a list of attribute names you want to observe:

import { register, WebComponentProps } from 'preact-web-component';
import type { FunctionComponent } from 'preact';

const Greeting: FunctionComponent<WebComponentProps & { name: string }> = ({ name = 'World' }) => (
  <p>Hello, {name}!</p>
);

register(Greeting, 'x-greeting', ['name']);

* Note: as per the Custom Elements specification, the tag name must contain a hyphen.

Use the new tag name in HTML, attribute keys and values will be passed in as props:

<x-greeting name="Billy Jo"></x-greeting>

Output:

<p>Hello, Billy Jo!</p>

Getting custom element base element inside component

The _rootElement variable is passed as a prop to each component and can be used to access the base element. When using TypeScript, you can use the WebComponentProps type to ensure correct typing.

Customized built-in elements

import { Component } from 'preact';
import { register } from 'preact-web-component';

// <x-greeting name="Bo"></x-greeting>
class Greeting extends Component {
  // Register as <x-greeting>:
  static tagName = 'x-greeting';

  // Track these attributes:
  static observedAttributes = ['name'];

  render({ name }) {
    return <p>Hello, {name}!</p>;
  }
}
register(Greeting, 'x-greeting', [name], { extends: "p", ExtendsComponent: HTMLParagraphElement });
<p is="x-greeting" name="Billy Jo"></p>

Output:

<p>Hello, Billy Jo!</p>

For more info see Customized built-in elements

Creating a formAssociated Element

import { useEffect } from 'preact';
import { register, WebComponentProps } from 'preact-web-component';
import type { FunctionComponent } from 'preact';

const Greeting: FunctionComponent<WebComponentProps & { name: string }> = ({ name = 'World' }) => {
  useEffect(() => {
    props._internals?.setFormValue('demo')
  })
  return <p>Hello, {name}!</p>;
}

register(Greeting, 'x-greeting', ['name'], [], { shadow: true, formAssociated: true });

With the ElementInternals API (attachInternals()), you can create custom elements that behave like native form controls. This enables your element to:

  • Have a value that participates in form submission
  • Be validated as part of a form
  • Integrate with form-related properties (e.g., form, name, type)

If enabled, the _internals property will be available on the element, containing the result of attachInternals() and allowing access to form-associated features.

see: attachInternals

Prop Names and Automatic Prop Names

The Custom Elements V1 specification requires explicitly stating the names of any attributes you want to observe. From your Preact component perspective, props could be an object with any keys at runtime, so it's not always clear which props should be accepted as attributes.

If you omit the third parameter to register(), the list of attributes to observe can be specified using a static observedAttributes property on your Component. This also works for the Custom Element's name, which can be specified using a tagName static property:

import { Component } from 'preact';
import { register } from 'preact-web-component';

// <x-greeting name="Bo"></x-greeting>
class Greeting extends Component {
  // Register as <x-greeting>:
  static tagName = 'x-greeting';

  // Track these attributes:
  static observedAttributes = ['name'];

  render({ name }) {
    return <p>Hello, {name}!</p>;
  }
}
register(Greeting);

If no observedAttributes are specified, they will be inferred from the keys of propTypes if present on the Component:

// Other option: use PropTypes:
function FullName(props: { first: string; last: string }) {
  return <span>{props.first} {props.last}</span>
}
FullName.propTypes = {
  first: Object, // you can use PropTypes, or this
  last: Object   // trick to define untyped props.
};
register(FullName, 'full-name');

Related