Skip to content

Instantly share code, notes, and snippets.

@ezr-ondrej
Last active April 29, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezr-ondrej/01a9f8b7a4f959af9b095fb0febc72be to your computer and use it in GitHub Desktop.
Save ezr-ondrej/01a9f8b7a4f959af9b095fb0febc72be to your computer and use it in GitHub Desktop.
web_components.js
import React from 'react';
import { mountNode } from 'foremanReact/common/MountingService';
/**
* This tag can be used for mounting one react component from ComponentRegistry
* This way, we would decide to rewrite the mounter completely and we would make all components there to the custom tags.
*/
class ComponentInRegistryElement extends HTMLElement {
connectedCallback() {
const mountPoint = document.createElement('span');
this.appendChild(mountPoint);
const props = JSON.parse(this.dataset.props);
mountNode('ComponentInRegistry', mountPoint, props);
}
};
window.customElements.define('one-component-per-tag', ComponentInRegistryElement);
import React from 'react';
import { mountNode } from './common/MountingService';
/**
* This tag can be used for mounting all components in react ComponentRegistry
*/
class ReactComponentElement extends HTMLElement {
connectedCallback() {
const mountPoint = document.createElement('span');
this.appendChild(mountPoint);
const componentNameFromRegistry = this.dataset.name;
const props = JSON.parse(this.dataset.props);
mountNode(componentNameFromRegistry, mountPoint, props);
}
};
window.customElements.define('react-component', ReactComponentElement);
<!-- and the cool part we mount the react component just as easily -->
<one-component-per-tag props="{'json':'props'}"></one-component-per-tag>
<!-- Any component -->
<react-component name="ComponentInRegistry" props="{'json':'props'}"></react-component>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment