focalboard/webapp/src/components/modal.tsx

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-01-13 19:12:22 +01:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react'
import {injectIntl, IntlShape} from 'react-intl'
import IconButton from '../widgets/buttons/iconButton'
import CloseIcon from '../widgets/icons/close'
import './modal.scss'
type Props = {
onClose: () => void
intl: IntlShape
2021-01-21 19:52:44 +01:00
position?: 'top'|'bottom'|'bottom-right'
2021-01-13 19:12:22 +01:00
}
class Modal extends React.PureComponent<Props> {
private node: React.RefObject<HTMLDivElement>
public constructor(props: Props) {
super(props)
this.node = React.createRef()
}
public componentDidMount(): void {
document.addEventListener('click', this.closeOnBlur, true)
}
public componentWillUnmount(): void {
document.removeEventListener('click', this.closeOnBlur, true)
}
private closeOnBlur = (e: Event) => {
if (this.node && this.node.current && e.target && this.node.current.contains(e.target as Node)) {
return
}
this.props.onClose()
}
render(): JSX.Element {
2021-01-14 01:56:01 +01:00
const {position} = this.props
2021-01-13 19:12:22 +01:00
return (
<div
2021-01-14 01:56:01 +01:00
className={'Modal ' + (position || 'bottom')}
2021-01-13 19:12:22 +01:00
ref={this.node}
>
<div className='toolbar hideOnWidescreen'>
<IconButton
onClick={this.closeClicked}
icon={<CloseIcon/>}
title={'Close'}
/>
</div>
{this.props.children}
</div>
)
}
private closeClicked = () => {
this.props.onClose()
}
}
export default injectIntl(Modal)