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

@actualwave/react-custom-element

v0.0.2

Published

An approach to microfrontends via custom elements and module isolation

Readme

@actualwave/react-custom-element

  • TODO lookup for possibility to add renderer with portal for easier develoment and testing, probably using slot to insert components but need to deal with context propagation

Adds support for independent modules wrapped in HTML Custom Elements. It provides a set of react hooks to establish communication through custom element container, so it is possible to pass data IN via custom element attributes and IN/OUT via events.

Web Components Using custom elements 3 Approaches to Integrate React with Custom Elements Web Components in React

Note: This does not isolate environments of running applications, all sub-applications running on one page will have access to same globals and it is possible for one application to interfere with others.

Installation

This package can be installed via its name @actualwave/react-custom-element. Using NPM

npm install @actualwave/react-custom-element

Or Yarn

yarn add @actualwave/react-custom-element

Integration

We have a react component which we want to use as an independent module, it is a normal react component.

const MyComponent = () => {
  return (
    <div>
      <h1>Hello World!</h1>
    </div>
  );
}

Instead of bootstrapping it via ReactDOM.render(), we register it as a custom element.

import { createCustomElement } from "@actualwave/react-custom-element";

createCustomElement({
  name: "my-component",
  render: () => <MyComponent />,
});

/**
 * For TypeScript you might need to add declaration fo custom element,
 * so it knows which attributes can be added to it.
 */

declare global {
  namespace JSX {
    interface IntrinsicElements {
      ["my-component"]: DetailedHTMLProps<
        HTMLAttributes<HTMLElement> & {
          onSomething: (event: Event) => void;
        },
        HTMLElement
      >;
    }
  }
}

createCustomElement({
  name: "my-component",
  render: () => <MyComponent />,
});

Then you have to import(or load in any other way) this component and use <my-component> HTML element.

import { useEffect, useRef } from "react";
import "./App.css";
import "./MyComponent";

function App() {
  return (
    <div className="app">
      <header className="App-header">
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
      <my-component></my-component>
    </div>
  );
}

export default App;

When custom HTML element is constructed, it will render MyComponent component as an independent React application.

Communication

Since modules integrated with custom elements are independent sub-applications, there are no way to communicate with them traditional React ways -- props or context. But you can communicate with sub-application by passing attributes, exposing methods or dispatching events.

Attributes

Attributes for custom element can be set or changed at any time and these changes could be captured by sub-application. Setting attribute

<my-component data-value="Something here"></my-component>

Reading attribute

import {
  createCustomElement,
  useContainerAttribute,
} from "@actualwave/react-custom-element";

const MyComponent = () => {
  const [readValue] = useContainerAttribute("data-value");

  return (
    <div>
      <h1>Hello World!</h1>
      <span>{readValue()}</span>
    </div>
  );
};

We can read any custom element attribute but listen for changes only for registered attributes. To register an attribute we have to provide it when registering custom element.

createCustomElement({
  name: "my-component",
  render: () => <MyComponent />,
  attributes: ["data-value", "id"],
});

Then we can use a change listener and it will be called whenever value of attribute is changed.

const MyComponent = () => {
  const [value, setValue] = useState("");

  const [readValue] = useContainerAttribute(
    "data-value",
    (name, oldValue, newValue) => setValue(newValue),
    []
  );

  useEffect(() => {
    setValue(readValue());
  }, [readValue]);

  return (
    <div>
      <h1>Hello World!</h1>
      <span>{value}</span>
    </div>
  );
};

And whenever attribute is changed, sub-application will be aware of that change.

import { useEffect, useRef } from "react";
import "./App.css";
import "./MyComponent";

function App() {
  const [value, setValue] = useState("");

  return (
    <div className="app">
      <my-component data-value={value}></my-component>
      <button onClick={() => updateValue("New attribute value")}>
        Change Value
      </button>
    </div>
  );
}

export default App;

This package also provides a way to read/update multiple attributes at once on a container HTML element

export const MyComponent = () => {
  const [values] = useContainerAttributes([
    "data-attr1",
    "data-attr2",
    "data-attr3",
  ]);

  return <div>{JSON.stringify(values)}</div>;
};

Methods

There are might be a case when you would want to expose custom methods on container element so they will be callable from outside. This can be done with useSetContainerCustomMethod() hook.

export const MyComponent = () => {
  const [value, setValue] = useState(0);

  useSetContainerCustomMethod("increment", () => setValue(value + 1));
  // useSetContainerCustomMethod("increment", () => setValue((val) => val++));

  return <div>Value: {value}</div>;
};

createCustomElement({
  name: "my-component",
  render: () => <MyComponent />,
});

When added to HTML tree, my-component tag will expose method increment() and you could call it like this

document.querySelector('my-component').increment();
// or
document.getElementsByTagName('my-component')[0].increment();
// or using any other way when you can get a reference of HTML element

There are no need to register attribute for such method via createCustomElement().

Events

Events allow to communicate both ways and pass non-serialized data by reference. React/JSX currently does not register custom events on custom elements. So, something like this may not work.

<my-component
  data-value="My value"
  onSomething={(event) => console.log("on something:", event)}
/>

One of the ways to make it work is to use CustomElementShim which manually assigns event listeners.

<CustomElementShim
  name="my-component"
  data-value="My value"
  onSomething={(event) => console.log("on something:", event)}
/>

Note: CustomElementShim is not aware of event phases and when using, for example, onClickCapture will not register click event for capture phase but will register clickCapture event for bubbling phase.

These events can be captured from within sub-application components using useContainerListener hook

useContainerListener(
  "something",
  (event) => console.log("Event:", event),
  false,
  []
);

To dispatch an event from sub-application on custom element use useContainerDispatch hook.

import { createCustomElement, useContainerDispatch } from "./custom-element";

const MyComponent = () => {
  const doSomething = useContainerDispatch("something");

  return (
    <div>
      <h1>Hello World!</h1>
      <button
        onClick={() => doSomething("There's something interesting here.")}
      >
        Do Something
      </button>
    </div>
  );
};