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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-to-can-webcomponent

v1.5.0

Published

Convert react components to native Web Components that work with CanJS.

Downloads

19

Readme

react-to-can-webcomponent

react-to-can-webcomponent converts React components to custom elements! It lets you share react components as native elements that don't require mounted being through React. The custom element acts as a wrapper for the underlying react component. Use these custom elements in any framework (vue, svelte, angular, ember, canjs) the same way you would use standard HTML elements.

react-to-can-webcomponent:

Basic Use

Given a react component like:

class Greeting extends React.Component {
	render() {
		return <h1>Hello, {this.props.name}</h1>;
	}
}

Call reactToCanWebComponent and customElements.define as follows:

import reactToCanWebComponent from "react-to-can-webcomponent";

const WebGreeting = reactToCanWebComponent(Greeting, React, ReactDOM);

customElements.define("web-greeting", WebGreeting);

Now you can use <web-greeting> like any other HTML element!

You can create it programatically:

const webGreeting = document.createElement("web-greeting");
webGreeting.name = "StandardsFan";

document.body.append(webGreeting);

webGreeting.innerHTML //-> "<h1>Hello, StandardsFan</h1>"

Or you can use it declaratively:

document.body.innerHTML = "<web-greeting></web-greeting>";

document.body.firstChild.name = "CoolBeans";

document.body.firstChild.innerHTML //-> "<h1>Hello, CoolBeans</h1>"

Working with Attributes

By default, custom elements created by reactToCanWebComponent only pass properties to the underlying React component. To make attributes work, you must specify your component's properties with PropTypes as follows:

class Greeting extends React.Component {
	render() {
		return <h1>Hello, {this.props.name}</h1>;
	}
}

Greeting.propTypes = {
	name: PropTypes.string.isRequired
};

Now reactToCanWebComponent will know to look for name attributes as follows:

document.body.innerHTML = "<web-greeting name='Amazed'></web-greeting>";

document.body.firstChild.innerHTML //-> "<h1>Hello, Amazed</h1>"

Setup

From NPM

To install from npm:

npm i react-to-can-webcomponent

CodePen

Greeting example in a CodePen

API

reactToCanWebComponent(ReactComponent, React, ReactDOM) takes the following:

  • ReactComponent - A react component that you want to convert to a Web Component.
  • React - A version of React (or preact-compat) the component works with.
  • ReactDOM - A version of ReactDOM (or preact-compat) that the component works with.

A new class inheriting from HTMLElement is returned. This class can be directly passed to customElements.define as follows:

customElements.define("web-greeting",
	reactToCanWebComponent(Greeting, React, ReactDOM) );

Or the class can be defined and used later:

const WebGreeting = reactToCanWebComponent(Greeting, React, ReactDOM);

customElements.define("web-greeting", WebGreeting);

var myGreeting = new WebGreeting();
document.body.appendChild(myGreeting);

Or the class can be extended:

class WebGreeting extends reactToCanWebComponent(Greeting, React, ReactDOM)
{
	disconnectedCallback(){
		super.disconnectedCallback();
		// special stuff
	}
}
customElements.define("web-greeting", WebGreeting);

How it works

reactToCanWebComponent creates a constructor function whose prototype is a Proxy. This acts as a trap for any property set on instances of the custom element. When a property is set, the proxy:

  • re-renders the React component inside the custom element.
  • creates an enumerable getter / setter on the instance to save the set value and avoid hitting the proxy in the future.
  • sets up an observer to listen for observable properties that are read by the component. (Note: this is the only difference between this library and Bitovi's react-to-webcomponent library )

Also:

  • Enumerable properties and values on the custom element are used as the props passed to the React component.
  • The React component is not rendered until the custom element is inserted into the page.