diff --git a/webapp/src/components/dialog.tsx b/webapp/src/components/dialog.tsx index c90508dae..357bb1805 100644 --- a/webapp/src/components/dialog.tsx +++ b/webapp/src/components/dialog.tsx @@ -2,6 +2,7 @@ // See LICENSE.txt for license information. import React from 'react' import {injectIntl, IntlShape} from 'react-intl' +import {useHotkeys} from 'react-hotkeys-hook' import IconButton from '../widgets/buttons/iconButton' import CloseIcon from '../widgets/icons/close' @@ -16,73 +17,50 @@ type Props = { intl: IntlShape } -class Dialog extends React.PureComponent { - public componentDidMount(): void { - document.addEventListener('keydown', this.keydownHandler) - } +const Dialog = React.memo((props: Props) => { + const {toolsMenu, intl} = props - public componentWillUnmount(): void { - document.removeEventListener('keydown', this.keydownHandler) - } + const closeDialogText = intl.formatMessage({ + id: 'Dialog.closeDialog', + defaultMessage: 'Close dialog', + }) - private keydownHandler = (e: KeyboardEvent): void => { - if (e.target !== document.body) { - return - } + useHotkeys('esc', () => props.onClose()) - if (e.keyCode === 27) { - this.closeClicked() - e.stopPropagation() - } - } - - public render(): JSX.Element { - const {toolsMenu, intl} = this.props - - const closeDialogText = intl.formatMessage({ - id: 'Dialog.closeDialog', - defaultMessage: 'Close dialog', - }) - - return ( -
{ - if (e.target === e.currentTarget) { - this.closeClicked() - } - }} - > -
-
- {toolsMenu && - <> + return ( +
{ + if (e.target === e.currentTarget) { + props.onClose() + } + }} + > +
+
+ {toolsMenu && + <> + } + title={closeDialogText} + className='IconButton--large' + /> +
+ } - title={closeDialogText} className='IconButton--large' + icon={} /> -
- - } - /> - {toolsMenu} - - - } -
- {this.props.children} + {toolsMenu} +
+ + }
+ {props.children}
- ) - } - - private closeClicked = () => { - this.props.onClose() - } -} +
+ ) +}) export default injectIntl(Dialog)