Spaces:
Running
Running
File size: 1,821 Bytes
f2bee8a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import omit from 'lodash.omit';
import PropTypes from 'prop-types';
import React from 'react';
import Style from 'to-style';
import stylePropType from 'react-style-proptype';
/*
* DOMElementRenderer wraps a DOM element, allowing it to be
* rendered by React. It's up to the containing component
* to retain a reference to the element prop, or else it
* will be garbage collected after unmounting.
*
* Props passed to the DOMElementRenderer will be set on the
* DOM element like it's a normal component.
*/
class DOMElementRenderer extends React.Component {
constructor (props) {
super(props);
this.setContainer = this.setContainer.bind(this);
}
componentDidMount () {
this.container.appendChild(this.props.domElement);
}
componentWillUnmount () {
if (this.props.domElement.parentNode !== this.container.childNodes[0]) return console.error('idk how to solve this, all i know is the site just keeps dieing with this exact issue');
this.container.removeChild(this.props.domElement);
}
setContainer (c) {
this.container = c;
}
render () {
// Apply props to the DOM element, so its attributes
// are updated as if it were a normal component.
// Look at me, I'm the React now!
Object.assign(
this.props.domElement,
omit(this.props, ['domElement', 'children', 'style'])
);
// Convert react style prop to dom element styling.
if (this.props.style) {
this.props.domElement.style.cssText = Style.string(this.props.style);
}
return <div ref={this.setContainer} />;
}
}
DOMElementRenderer.propTypes = {
domElement: PropTypes.instanceOf(Element).isRequired,
style: stylePropType
};
export default DOMElementRenderer;
|