Giter Club home page Giter Club logo

react-webcomponentify's Introduction

react-webcomponentify

npm version

logo

Build and export React Components as Web Components without any extra effort.

Size = ~1.5kB after gzip

* works nicely with preact aswell: See demo

Show me live demo?

Table of Contents

Use cases

  • Export existing react components as web components so you can use them with Vue or Angular.
  • Use react's rich api to build web components with state management, etc. Instruction on how to do exactly that and Live Demo here: https://github.com/a7ul/webcomponents-with-react-webcomponentify
  • Lets say you are writing a component library with web components but you already have a huge collection of component in react.You can use this library to generate a component library with the existing components. And then safely continue to rewrite each one of them behind the scene. This makes sure other teams are not waiting for you to finish.
  • For more crazy people - You can even export your entire react app as a web component and embed it into another app made with Angular or Vue. So you can keep writing newer parts of code in react while keeping your legacy code working on the side.
  • Maybe (not tried) you can embed another old react app (wrapped with this module) inside ur current react app.

Install

npm install react-webcomponentify

or

yarn add react-webcomponentify

Usage

Basic

Simple use case

import React from "react";
import { registerAsWebComponent } from "react-webcomponentify";

export const ExampleReactComponent = () => {
  return <div> Hello </div>;
};

registerAsWebComponent(ExampleReactComponent, "example-component");

In HTML:

<!DOCTYPE html>
<html>
  ....
  <body>
    <example-component />
  </body>
  ....
</html>

Advanced

Sending non string props to react

You can send serializable string props via the html attributes itself. But for props like callback functions or complex objects you can use the setProps method on the element as shown below.

import React from "react";
import { registerAsWebComponent } from "react-webcomponentify";

export const ButtonComponent = props => {
  return (
    <div>
      Hello <button onClick={props.onClick}>{props.text}</button>
    </div>
  );
};

registerAsWebComponent(ButtonComponent, "button-web");

In HTML:

<!DOCTYPE html>
<html>
  ....
  <body>
    <button-web text="click me" id="mybutton" />
  </body>
  ....
  <script>
    const myBtn = document.getElementById("mybutton");
    myBtn.setProps({
      onClick: () => console.log("btn was clicked")
    });
  </script>
</html>

Every custom component built using react-webcomponentify will have an instance method setProps

element.setProps({
  ....
  /* set the props here that you want to send to react */
  ....
})

What about child elements?

Thats possible too 😎

import React from "react";
import { registerAsWebComponent } from "react-webcomponentify";

// You access the children just like you would in react (using this.props.children)
export const ComponentWithChild = props => {
  return (
    <div>
      Hello World
      {this.props.text}
      <div>{this.props.children}</div>
    </div>
  );
};

registerAsWebComponent(ComponentWithChild, "component-with-child");

In HTML:

<!DOCTYPE html>
<html>
  ....
  <body>
    <component-with-child text="I love children">
      <p>Some child</p>
    </component-with-child>
  </body>
  ....
</html>

This will send <p>Some Child</p> via this.props.children to the React component ComponentWithChild. Note that <p>Some Child</p> is a dom node and not a react component. So it will be wrapped with a simple react component found here: https://github.com/a7ul/react-webcomponentify/blob/master/src/react-dom-child.js But for implementation purposed use it like a regular child component.

TypeScript support

This library is written in TypeScript. All declarations are included.

Maintainers


Atul R

Ihor

react-webcomponentify's People

Contributors

a7ul avatar hennessyevan avatar imgbotapp avatar kopach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-webcomponentify's Issues

Incompatibility warning when upgrading to React 18

I have upgraded our app with React 18 and got the below warning since I am using react-webcomponentify.

image

Since React 18 is not supporting ReactDOM.render, we are getting this. Instead of that we should use createRoot API and its mentioned in their upgrade guide.
I suggest to upgrade the library to support React 18 with proper backward compatibility with older versions.

I would love to contribute on that.
Let me know if you have any nicer suggestion or any plans already in mind @kopach @a7ul .

Ability to specify open shadow DOM

Hi, thanks for writing this package. I'd love to use it, but it seems it uses closed shadow DOM by default, whereas in my case I would prefer open. (So that I can style it and so that it doesn't interfere with screen readers for accessibility reasons.)

It would be great to have an option to support open shadow DOM! 😃

CSS not injected

Hi, I have a custom css file imported for my components. It seems that they get ignored when I register the component as a web component. How can I inject it into the web component?

Hooks

Hello,

Thanks for this great library.
I would like to functional compoanent with hooks.
Have you an example ?

Regards

Salvatore

Web Component Attributes Observer

Hi, great work!

Does it watch all the attributes added on a html tag / web component? Even if you don't specify what attributes to watch?

<my-component attr1="value1" attr2="value2"></my-component>

Best regards

using react context with web components

@a7ul, do you have any idea how to update the library in order for it to support react context?

So, I could do like this:

<custom-component-provider>
 <custom-component-consumer />
</custom-component-provider>

Currently, this doesn't work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.