signal-desktop/ts/components/ModalHost.tsx

154 lines
4.6 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, { useContext, 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';
import { noop } from 'lodash';
2021-10-14 16:52:42 +00:00
import type { ModalConfigType } from '../hooks/useAnimated';
import type { Theme } from '../util/theme';
2022-09-27 20:24:21 +00:00
import { assertDev } from '../util/assert';
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';
2022-09-27 20:24:21 +00:00
import { usePrevious } from '../hooks/usePrevious';
import { handleOutsideClick } from '../util/handleOutsideClick';
2022-09-27 20:24:21 +00:00
import * as log from '../logging/log';
2020-11-20 17:30:45 +00:00
export const ModalContainerContext = React.createContext<HTMLElement | null>(
null
);
export type PropsType = Readonly<{
children: React.ReactElement;
2022-09-27 20:24:21 +00:00
modalName: string;
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
2022-11-18 00:45:19 +00:00
export const ModalHost = React.memo(function ModalHostInner({
children,
modalName,
moduleClassName,
noMouseClose,
onClose,
onEscape,
onTopOfEverything,
overlayStyles,
theme,
useFocusTrap = true,
}: PropsType) {
const [root, setRoot] = React.useState<HTMLElement | null>(null);
const containerRef = React.useRef<HTMLDivElement | null>(null);
const previousModalName = usePrevious(modalName, modalName);
const modalContainer = useContext(ModalContainerContext) ?? document.body;
2022-09-27 20:24:21 +00:00
2022-11-18 00:45:19 +00:00
if (previousModalName !== modalName) {
log.error(
`ModalHost detected conflict between ${previousModalName} ` +
`and ${modalName}. Consider using "key" attributes on both modals.`
);
assertDev(false, 'Modal conflict');
}
2020-11-20 17:30:45 +00:00
2022-11-18 00:45:19 +00:00
useEffect(() => {
const div = document.createElement('div');
modalContainer.appendChild(div);
setRoot(div);
2020-11-20 17:30:45 +00:00
2022-11-18 00:45:19 +00:00
return () => {
modalContainer.removeChild(div);
setRoot(null);
};
}, [modalContainer]);
2020-11-20 17:30:45 +00:00
2022-11-18 00:45:19 +00:00
useEscapeHandling(onEscape || onClose);
useEffect(() => {
if (noMouseClose) {
return noop;
}
return handleOutsideClick(
node => {
// ignore clicks that originate in the calling/pip
// when we're not handling a component in the calling/pip
if (
modalContainer === document.body &&
node instanceof Element &&
node.closest('.module-calling__modal-container')
) {
return false;
}
onClose();
return true;
},
{ containerElements: [containerRef], name: modalName }
);
}, [noMouseClose, onClose, containerRef, modalName, modalContainer]);
2020-11-20 17:30:45 +00:00
2022-11-18 00:45:19 +00:00
const className = classNames([
theme ? themeClassName(theme) : undefined,
onTopOfEverything ? 'module-modal-host--on-top-of-everything' : undefined,
]);
const getClassName = getClassNamesFor('module-modal-host', moduleClassName);
2022-03-04 21:14:52 +00:00
2022-11-18 00:45:19 +00:00
const modalContent = (
<div className={className}>
<animated.div
role="presentation"
className={getClassName('__overlay')}
style={overlayStyles}
/>
<div className={getClassName('__overlay-container')}>
<div ref={containerRef} className={getClassName('__width-container')}>
{children}
2022-04-15 00:08:46 +00:00
</div>
2022-03-04 21:14:52 +00:00
</div>
2022-11-18 00:45:19 +00:00
</div>
);
2022-11-18 00:45:19 +00:00
return root
? createPortal(
useFocusTrap ? (
<FocusTrap
focusTrapOptions={{
allowOutsideClick: ({ target }) => {
if (!target || !(target instanceof HTMLElement)) {
return false;
2022-11-18 00:45:19 +00:00
}
// Exemptions:
// - TitleBar should always receive clicks.
// - Quill suggestions since they are placed in the document.body
// - Calling module (and pip) are always above everything else
const exemptParent = target.closest(
'.TitleBarContainer__title, ' +
'.module-composition-input__suggestions, ' +
'.module-calling__modal-container'
);
if (exemptParent) {
return true;
}
return false;
},
}}
>
{modalContent}
</FocusTrap>
) : (
modalContent
),
root
)
: null;
});