focalboard/webapp/src/components/rootPortal.tsx

45 lines
1,009 B
TypeScript
Raw Normal View History

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