focalboard/webapp/src/components/rootPortal.tsx

45 lines
871 B
TypeScript
Raw Normal View History

// Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-10-14 21:39:34 +02:00
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
type Props = {
2020-10-14 21:39:34 +02:00
children: React.ReactNode
}
export default class RootPortal extends React.PureComponent<Props> {
2020-10-14 21:39:34 +02:00
el: HTMLDivElement
static propTypes = {
children: PropTypes.node,
}
constructor(props: Props) {
super(props)
this.el = document.createElement('div')
}
componentDidMount() {
const rootPortal = document.getElementById('root-portal')
if (rootPortal) {
rootPortal.appendChild(this.el)
}
}
componentWillUnmount() {
const rootPortal = document.getElementById('root-portal')
if (rootPortal) {
rootPortal.removeChild(this.el)
}
}
render() {
return ReactDOM.createPortal(
this.props.children,
this.el,
)
}
}