adding bookmarklet
This commit is contained in:
40
src/outputs/embeddable-widget.js
Normal file
40
src/outputs/embeddable-widget.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import Widget from '../components/widget';
|
||||
|
||||
export default class EmbeddableWidget {
|
||||
static el;
|
||||
|
||||
static mount() {
|
||||
const component = <Widget />;
|
||||
|
||||
function doRender() {
|
||||
if (EmbeddableWidget.el) {
|
||||
throw new Error('EmbeddableWidget is already mounted, unmount first');
|
||||
}
|
||||
const el = document.createElement('div');
|
||||
document.body.appendChild(el);
|
||||
ReactDOM.render(
|
||||
component,
|
||||
el,
|
||||
);
|
||||
EmbeddableWidget.el = el;
|
||||
}
|
||||
if (document.readyState === 'complete') {
|
||||
doRender();
|
||||
} else {
|
||||
window.addEventListener('load', () => {
|
||||
doRender();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static unmount() {
|
||||
if (!EmbeddableWidget.el) {
|
||||
throw new Error('EmbeddableWidget is not mounted, mount first');
|
||||
}
|
||||
ReactDOM.unmountComponentAtNode(EmbeddableWidget.el);
|
||||
EmbeddableWidget.el.parentNode.removeChild(EmbeddableWidget.el);
|
||||
EmbeddableWidget.el = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user