Git Product home page Git Product logo

remount's Introduction



Remount

version   Bundle size

Use your React components anywhere in your
HTML as web components (custom elements).

DemoDocs
2kb gzip'd  ·  No dependencies  ·  IE support



Installation

Remount is available through the npm package repository. React 18 is required.

# npm
npm install remount react react-dom
# yarn
yarn add remount react react-dom
  • Via yarn: yarn add remount
  • or npm: npm install remount

Usage

Let's start with any React component. Here's one:

const Greeter = ({ name }) => {
  return <div>Hello, {name}!</div>
}

Use define() to define custom elements. Let's define a <x-greeter> element:

import { define } from 'remount'

define({ 'x-greeter': Greeter })

You can now use it anywhere in your HTML! 💥

<x-greeter props-json='{"name":"John"}'></x-greeter>

API documentation →

Use cases

Some ideas on why you might want to consider Remount for your project:




Adding React to non-SPA apps
You can use React components on any page of a "regular" HTML site. Great for adding React to apps built on Rails or Phoenix.



💞 Interop with other frameworks
Remount lets you use your React components just like any other HTML element. This means you can use React with Vue, Angular, or any other DOM library/framework.

More features

  • JSON props (eg, <x-greeter props-json="{...}">) (docs)
  • Named attributes (eg, <x-greeter name="John">) (docs)
  • Uses Custom Elements API (when available)
  • Fallback to compatible API for other browers
  • Shadow DOM mode (when available)

Browser support

Remount supports all browsers that React 18 supports.

Custom Elements API# ("Web Components") will be used if it's available (Chrome/67+), and will fallback to a compatible API otherwise.

Browser support docs →

Documentation

Thanks

remount © 2022, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz


 

remount's People

Contributors

dependabot[bot] avatar morbidick avatar paulovitin avatar rstacruz avatar rugk avatar rybon 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

remount's Issues

Add possibility to mount directly into shadowRoot

Instead of creating a span and mounting the React component in there, we could also just mount directly into the shadowRoot. According to this comment the event system of the latest version of React supports shadowRoot now, no retargeting needed. Retargeting is only required when mounting in a child of shadowRoot, so why not avoid the problem altogether by not using a child at all?

Use `extends` option

There's a use-case for custom elements, that allows extending built-in components https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#Customized_built-in_elements. That allows to solve inherent problems of JSX, as stated in the preact issue developit/htm#81

If remount would take extends: tagName as an option, it would enable component-less JSX:

htm`
<nav is="app-nav"/>
<main is="app-page">
  <header slot="header" />
  <section slot="content">...</section>
  <footer slot="footer" />
</main>
`

Autoparse attribute values / propTypes

There is auto-parse package, that detects typed value from string attribute and parses that correspondingly.
Taking into account component's propTypes it would be handy to have automatic attributes parser to corresponding type.
Does that make sense @rstacruz?

MutationObserver strategy fails when script included in <head>

The MutationObserver strategy fails when the script defining elements is included inside the <head> tag, without using defer or async on the script tag. The error is:

Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.

The site of the error is here:

observer.observe(document.body, {
childList: true,
subtree: true
})

The cause is that, inside <head> where the script is being evaluated, document.body does not yet exist.

Here is a repl.it link demonstrating the issue: https://repl.it/@EvanShaw2/Remount-MutationObserver-strategy-issue (This code forces all browsers to use the MutationObserver strategy, even if they support custom elements.)

It would be possible to get around this by calling Remount.define inside a jQuery $().ready event handler or equivalent native JS, which would ensure that document.body exists.

It would be nice if this limitation were documented. Even better if it were handled by remount itself.

Preact

Do you plan to support preact, which is better suited for web components because of its weight?

How To Pass A Instance Variable To Component From ERB

I tried the code below, and am using named attributes. This does not send anything to the props in the React code. When I change it to send in <%= @Items %> it works but then I get the memory address and that's not what I want. I want the array from Model.where etc.

<hello-component items=<% @items %>></hello-component>

[Issue] Installation: Cannot destructure property 'name' of 'node' as it is null.

If I try to install remount with

npm i remount

or directly the github version: npm i https://github.com/rstacruz/remount
I get this Error:

npm ERR! code ERESOLVE
npm ERR! Cannot destructure property 'name' of 'node' as it is null.

"npm" "i" "remount"
36 verbose node v15.5.0
37 verbose npm  v7.3.0
38 error code ERESOLVE
39 verbose stack TypeError: Cannot destructure property 'name' of 'node' as it is null.
39 verbose stack     at printNode (/usr/local/lib/node_modules/npm/lib/utils/explain-dep.js:27:5)
39 verbose stack     at explainNode (/usr/local/lib/node_modules/npm/lib/utils/explain-dep.js:12:3)
39 verbose stack     at explainEresolve (/usr/local/lib/node_modules/npm/lib/utils/explain-eresolve.js:24:24)
39 verbose stack     at report (/usr/local/lib/node_modules/npm/lib/utils/explain-eresolve.js:50:3)
39 verbose stack     at module.exports (/usr/local/lib/node_modules/npm/lib/utils/error-message.js:22:24)
39 verbose stack     at errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:167:15)
39 verbose stack     at /usr/local/lib/node_modules/npm/lib/npm.js:121:9
...
Error: unable to resolve dependency tree
32 verbose stack     at Arborist.[failPeerConflict] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:1061:25)
32 verbose stack     at Arborist.[placeDep] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:1145:30)
32 verbose stack     at /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:807:46
32 verbose stack     at Array.map (<anonymous>)
32 verbose stack     at Arborist.[buildDepStep] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:807:8)
32 verbose stack     at async Arborist.buildIdealTree (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/build-ideal-tree.js:209:7)
32 verbose stack     at async Promise.all (index 1)
32 verbose stack     at async Arborist.reify (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:123:5)
32 verbose stack     at async install (/usr/local/lib/node_modules/npm/lib/install.js:38:3)

On MacOS 10.15.7
Node: v15.5.0
NPM: npm@next-7 and npm@6 and [email protected]

if this is a npm problem, you can close this issue.

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.