GithubHelp home page GithubHelp logo

sil0rak / convert-react-to-web-component Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 2.0 969 KB

Convert React component to web component without breaking a sweat.

Home Page: https://www.npmjs.com/package/convert-react-to-web-component

JavaScript 15.62% TypeScript 84.38%
legacy react webcomponent microfrontend twig tpl javascript

convert-react-to-web-component's Introduction

npm Snyk Vulnerabilities for GitHub Repo

React to Web Component

This library is meant to make maintaining legacy code less painful. Micro frontend will reduce your legacy core complexity enabling you to integrate React (application/widgets) in it.

Warning This library is not tested with react 18.

Install

npm install convert-react-to-web-component

or

yarn add convert-react-to-web-component

and don't forget to install peer dependencies:

  • react
  • react-dom

Usage

import React from 'react';
import reactToWebComponent from 'convert-react-to-web-component';

const ReactApp = () => <div>Hello, world</div>;

reactToWebComponent(ReactApp);

this snippet will create <react-app> tag that can be used in plain html, angular template or twig.

Attributes

To pass tag attributes to React component you have to specify them.

import React from 'react';
import reactToWebComponent from 'convert-react-to-web-component';

const ReactApp = ({ name, userLastName }) => (
  <div>Hello, {name} {userLastName}</div>
);

reactToWebComponent(ReactApp, { attributes: ['name', 'userLastName'] });

Attributes should be passed as kebab-case to your element.

  <div>
    <react-app name="john" user-last-name="Cena">
  </div>

React component will rerender on attribute change.

Tag name

By default created tag name will be given React component name converted to kebab-case. You can change name of tab by specifying your desired name.

import React from 'react';
import reactToWebComponent from 'convert-react-to-web-component';

const ReactApp = ({ name }) => <div>Hello, {name}</div>;

reactToWebComponent(ReactApp, { attributes: ['name']; name: 'app' });

now you can call you component with <app> tag.

Attribute modifier (middleware)

In some cases, you may want to use middleware to modify value before it is passed to React component. This can be useful when implementing functionality in twig or angular apps.

import React from 'react';
import reactToWebComponent from 'convert-react-to-web-component';

const ReactApp = ({ name }) => <div>Hello, {name}</div>;

const middleware = (value) => (
    value.includes('{') ? undefined : value
);

reactToWebComponent(ReactApp, { attributes: ['name'], middleware });

Shadow DOM

To prevent style interference between parent application and your react app you can pass shadowDom property which will create shadow root and mount react app to it.

import React from 'react';
import reactToWebComponent from 'convert-react-to-web-component';

const ReactApp = () => <div>Hello, World</div>;

reactToWebComponent(ReactApp, { shadowDom: "open" });

Additional stuff

If you came this far and read all that stuff above you may be planning to use this lib. If this is the case you may want to see a demo. But let's be honest I know you are pro, and you will definitely check out package.json to see what is available.

convert-react-to-web-component's People

Contributors

dependabot[bot] avatar sil0rak avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

convert-react-to-web-component's Issues

Adding more than 1 attribute tag

Is it possible to pass more than 1 attribute tag? I have tried passing more than 1 into the array however seems to return undefined.

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.