signal-desktop/ts/components/ModalHost.tsx

120 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2019-2022 Signal Messenger, LLC
2020-11-20 17:30:45 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useEffect } from 'react';
2020-11-20 17:30:45 +00:00
import { createPortal } from 'react-dom';
import FocusTrap from 'focus-trap-react';
import type { SpringValues } from '@react-spring/web';
import { animated } from '@react-spring/web';
import classNames from 'classnames';
2021-10-14 16:52:42 +00:00
import type { ModalConfigType } from '../hooks/useAnimated';
import type { Theme } from '../util/theme';
2022-03-04 21:14:52 +00:00
import { getClassNamesFor } from '../util/getClassNamesFor';
import { themeClassName } from '../util/theme';
2021-09-17 22:24:21 +00:00
import { useEscapeHandling } from '../hooks/useEscapeHandling';
2020-11-20 17:30:45 +00:00
export type PropsType = Readonly<{
children: React.ReactElement;
2022-03-04 21:14:52 +00:00
moduleClassName?: string;
noMouseClose?: boolean;
onClose: () => unknown;
onEscape?: () => unknown;
2022-03-04 21:14:52 +00:00
onTopOfEverything?: boolean;
overlayStyles?: SpringValues<ModalConfigType>;
theme?: Theme;
2022-03-04 21:14:52 +00:00
useFocusTrap?: boolean;
}>;
2020-11-20 17:30:45 +00:00
export const ModalHost = React.memo(
2021-10-14 16:52:42 +00:00
({
children,
2022-03-04 21:14:52 +00:00
moduleClassName,
2021-10-14 16:52:42 +00:00
noMouseClose,
onClose,
onEscape,
onTopOfEverything,
2022-03-04 21:14:52 +00:00
overlayStyles,
theme,
useFocusTrap = true,
2021-10-14 16:52:42 +00:00
}: PropsType) => {
const [root, setRoot] = React.useState<HTMLElement | null>(null);
const [isMouseDown, setIsMouseDown] = React.useState(false);
2020-11-20 17:30:45 +00:00
useEffect(() => {
const div = document.createElement('div');
document.body.appendChild(div);
setRoot(div);
2020-11-20 17:30:45 +00:00
return () => {
document.body.removeChild(div);
setRoot(null);
};
}, []);
2020-11-20 17:30:45 +00:00
2021-09-17 22:24:21 +00:00
useEscapeHandling(onEscape || onClose);
2020-11-20 17:30:45 +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.
const handleMouseDown = React.useCallback(
(e: React.MouseEvent) => {
if (e.target === e.currentTarget) {
setIsMouseDown(true);
}
},
[setIsMouseDown]
);
const handleMouseUp = React.useCallback(
(e: React.MouseEvent) => {
setIsMouseDown(false);
if (e.target === e.currentTarget && isMouseDown) {
onClose();
}
},
[onClose, isMouseDown, setIsMouseDown]
);
2020-11-20 17:30:45 +00:00
const className = classNames([
theme ? themeClassName(theme) : undefined,
onTopOfEverything ? 'module-modal-host--on-top-of-everything' : undefined,
]);
2022-03-04 21:14:52 +00:00
const getClassName = getClassNamesFor('module-modal-host', moduleClassName);
const modalContent = (
<div className={className}>
<animated.div
role="presentation"
className={getClassName('__overlay')}
style={overlayStyles}
/>
2022-04-15 00:08:46 +00:00
<div
className={getClassName('__overlay-container')}
onMouseDown={noMouseClose ? undefined : handleMouseDown}
onMouseUp={noMouseClose ? undefined : handleMouseUp}
>
{children}
</div>
2022-03-04 21:14:52 +00:00
</div>
);
return root
? createPortal(
2022-03-04 21:14:52 +00:00
useFocusTrap ? (
<FocusTrap
focusTrapOptions={{
// This is alright because the overlay covers the entire screen
allowOutsideClick: false,
}}
>
{modalContent}
</FocusTrap>
) : (
modalContent
),
root
)
: null;
}
);