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
|
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2020-11-20 17:30:45 +00:00
|
|
|
import { createPortal } from 'react-dom';
|
2021-10-04 17:14:00 +00:00
|
|
|
import FocusTrap from 'focus-trap-react';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { SpringValues } from '@react-spring/web';
|
|
|
|
import { animated } from '@react-spring/web';
|
2021-12-03 23:04:34 +00:00
|
|
|
import classNames from 'classnames';
|
2022-09-15 01:58:35 +00:00
|
|
|
import { noop } from 'lodash';
|
2021-10-04 17:14:00 +00:00
|
|
|
|
2021-10-14 16:52:42 +00:00
|
|
|
import type { ModalConfigType } from '../hooks/useAnimated';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Theme } from '../util/theme';
|
2022-03-04 21:14:52 +00:00
|
|
|
import { getClassNamesFor } from '../util/getClassNamesFor';
|
2021-10-26 19:15:33 +00:00
|
|
|
import { themeClassName } from '../util/theme';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useEscapeHandling } from '../hooks/useEscapeHandling';
|
2022-09-15 01:58:35 +00:00
|
|
|
import { handleOutsideClick } from '../util/handleOutsideClick';
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-12-03 23:04:34 +00:00
|
|
|
export type PropsType = Readonly<{
|
|
|
|
children: React.ReactElement;
|
2022-03-04 21:14:52 +00:00
|
|
|
moduleClassName?: string;
|
2021-12-03 23:04:34 +00:00
|
|
|
noMouseClose?: boolean;
|
|
|
|
onClose: () => unknown;
|
|
|
|
onEscape?: () => unknown;
|
2022-03-04 21:14:52 +00:00
|
|
|
onTopOfEverything?: boolean;
|
2021-12-03 23:04:34 +00:00
|
|
|
overlayStyles?: SpringValues<ModalConfigType>;
|
|
|
|
theme?: Theme;
|
2022-03-04 21:14:52 +00:00
|
|
|
useFocusTrap?: boolean;
|
2021-12-03 23:04:34 +00:00
|
|
|
}>;
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-04-27 19:29:59 +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,
|
2021-12-03 23:04:34 +00:00
|
|
|
onTopOfEverything,
|
2022-03-04 21:14:52 +00:00
|
|
|
overlayStyles,
|
|
|
|
theme,
|
|
|
|
useFocusTrap = true,
|
2021-10-14 16:52:42 +00:00
|
|
|
}: PropsType) => {
|
2021-04-27 19:29:59 +00:00
|
|
|
const [root, setRoot] = React.useState<HTMLElement | null>(null);
|
2022-09-15 01:58:35 +00:00
|
|
|
const containerRef = React.useRef<HTMLDivElement | null>(null);
|
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-09-17 22:24:21 +00:00
|
|
|
useEscapeHandling(onEscape || onClose);
|
2022-09-15 01:58:35 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (noMouseClose) {
|
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
return handleOutsideClick(
|
|
|
|
() => {
|
2021-04-27 19:29:59 +00:00
|
|
|
onClose();
|
2022-09-15 01:58:35 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
{ containerElements: [containerRef] }
|
|
|
|
);
|
|
|
|
}, [noMouseClose, onClose]);
|
2020-11-20 17:30:45 +00:00
|
|
|
|
2021-12-03 23:04:34 +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-09-15 01:58:35 +00:00
|
|
|
<div className={getClassName('__overlay-container')}>
|
|
|
|
<div ref={containerRef} className={getClassName('__width-container')}>
|
|
|
|
{children}
|
|
|
|
</div>
|
2022-04-15 00:08:46 +00:00
|
|
|
</div>
|
2022-03-04 21:14:52 +00:00
|
|
|
</div>
|
|
|
|
);
|
2021-12-03 23:04:34 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
return root
|
|
|
|
? createPortal(
|
2022-03-04 21:14:52 +00:00
|
|
|
useFocusTrap ? (
|
|
|
|
<FocusTrap
|
|
|
|
focusTrapOptions={{
|
2022-06-08 22:00:32 +00:00
|
|
|
allowOutsideClick: ({ target }) => {
|
|
|
|
if (!target || !(target instanceof HTMLElement)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const titleBar = document.querySelector(
|
|
|
|
'.TitleBarContainer__title'
|
|
|
|
);
|
|
|
|
if (titleBar?.contains(target)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2022-03-04 21:14:52 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{modalContent}
|
|
|
|
</FocusTrap>
|
|
|
|
) : (
|
|
|
|
modalContent
|
|
|
|
),
|
2021-04-27 19:29:59 +00:00
|
|
|
root
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
);
|