mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
577a749e12
no issue - followed the migration guide to switch to from lifecycle hooks to single `modify()` method - https://togithub.com/ember-modifier/ember-modifier/blob/master/MIGRATIONS.md - forced resolution of `ember-in-viewport@4.0.2` to avoid older version of `ember-modifier` being pulled in through `ember-infinity`
38 lines
1003 B
JavaScript
38 lines
1003 B
JavaScript
import Modifier from 'ember-modifier';
|
|
import React from 'react';
|
|
import ReactDOM from 'react-dom';
|
|
import {createRoot} from 'react-dom/client';
|
|
import {registerDestructor} from '@ember/destroyable';
|
|
|
|
// make globals available for any pulled in UMD components
|
|
// - avoids external components needing to bundle React and running into multiple version errors
|
|
window.React = React;
|
|
window.ReactDOM = ReactDOM;
|
|
|
|
export default class ReactRenderModifier extends Modifier {
|
|
constructor(owner, args) {
|
|
super(owner, args);
|
|
registerDestructor(this, this.cleanup);
|
|
}
|
|
|
|
modify(element, [reactComponent], {props}) {
|
|
if (!this.didSetup) {
|
|
if (!this.root) {
|
|
this.root = createRoot(element);
|
|
}
|
|
|
|
this.root.render(React.createElement(reactComponent, {...props}));
|
|
|
|
this.didSetup = true;
|
|
}
|
|
}
|
|
|
|
cleanup = () => {
|
|
if (!this.root) {
|
|
return;
|
|
}
|
|
|
|
this.root.unmount();
|
|
};
|
|
}
|