2020-11-20 17:30:45 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import classNames from 'classnames';
|
2020-11-20 17:30:45 +00:00
|
|
|
import { createPortal } from 'react-dom';
|
2021-04-27 19:29:59 +00:00
|
|
|
import { Theme, themeClassName } from '../util/theme';
|
2020-11-20 17:30:45 +00:00
|
|
|
|
|
|
|
export type PropsType = {
|
2021-05-28 16:15:17 +00:00
|
|
|
readonly noMouseClose?: boolean;
|
2021-04-28 20:44:48 +00:00
|
|
|
readonly onEscape?: () => unknown;
|
2020-11-20 17:30:45 +00:00
|
|
|
readonly onClose: () => unknown;
|
|
|
|
readonly children: React.ReactElement;
|
2021-04-27 19:29:59 +00:00
|
|
|
readonly theme?: Theme;
|
2020-11-20 17:30:45 +00:00
|
|
|
};
|
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
export const ModalHost = React.memo(
|
2021-05-28 16:15:17 +00:00
|
|
|
({ onEscape, onClose, children, noMouseClose, theme }: PropsType) => {
|
2021-04-27 19:29:59 +00:00
|
|
|
const [root, setRoot] = React.useState<HTMLElement | null>(null);
|
2021-07-12 20:02:57 +00:00
|
|
|
const [isMouseDown, setIsMouseDown] = React.useState(false);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
document.body.appendChild(div);
|
|
|
|
setRoot(div);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
return () => {
|
|
|
|
document.body.removeChild(div);
|
|
|
|
setRoot(null);
|
|
|
|
};
|
|
|
|
}, []);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const handler = (event: KeyboardEvent) => {
|
|
|
|
if (event.key === 'Escape') {
|
2021-04-28 20:44:48 +00:00
|
|
|
if (onEscape) {
|
|
|
|
onEscape();
|
|
|
|
} else {
|
|
|
|
onClose();
|
|
|
|
}
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document.addEventListener('keydown', handler);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', handler);
|
|
|
|
};
|
2021-04-28 20:44:48 +00:00
|
|
|
}, [onEscape, onClose]);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
// This makes it easier to write dialogs to be hosted here; they won't have to worry
|
|
|
|
// as much about preventing propagation of mouse events.
|
2021-07-12 20:02:57 +00:00
|
|
|
const handleMouseDown = React.useCallback(
|
2021-04-27 19:29:59 +00:00
|
|
|
(e: React.MouseEvent) => {
|
|
|
|
if (e.target === e.currentTarget) {
|
2021-07-12 20:02:57 +00:00
|
|
|
setIsMouseDown(true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setIsMouseDown]
|
|
|
|
);
|
|
|
|
const handleMouseUp = React.useCallback(
|
|
|
|
(e: React.MouseEvent) => {
|
|
|
|
setIsMouseDown(false);
|
|
|
|
|
|
|
|
if (e.target === e.currentTarget && isMouseDown) {
|
2021-04-27 19:29:59 +00:00
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
},
|
2021-07-12 20:02:57 +00:00
|
|
|
[onClose, isMouseDown, setIsMouseDown]
|
2021-04-27 19:29:59 +00:00
|
|
|
);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
return root
|
|
|
|
? createPortal(
|
|
|
|
<div
|
|
|
|
role="presentation"
|
|
|
|
className={classNames(
|
|
|
|
'module-modal-host__overlay',
|
|
|
|
theme ? themeClassName(theme) : undefined
|
|
|
|
)}
|
2021-07-12 20:02:57 +00:00
|
|
|
onMouseDown={noMouseClose ? undefined : handleMouseDown}
|
|
|
|
onMouseUp={noMouseClose ? undefined : handleMouseUp}
|
2021-04-27 19:29:59 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>,
|
|
|
|
root
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
);
|