signal-desktop/ts/components/ModalHost.tsx

121 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-01-03 11:55:46 -08:00
// Copyright 2019 Signal Messenger, LLC
2020-11-20 09:30:45 -08:00
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useContext, useEffect } from 'react';
2020-11-20 09:30:45 -08:00
import { createPortal } from 'react-dom';
import type { SpringValues } from '@react-spring/web';
import { animated } from '@react-spring/web';
import classNames from 'classnames';
import { noop } from 'lodash';
import { FocusScope } from 'react-aria';
2021-10-14 12:52:42 -04:00
import type { ModalConfigType } from '../hooks/useAnimated';
import type { Theme } from '../util/theme';
2022-09-27 13:24:21 -07:00
import { assertDev } from '../util/assert';
2022-03-04 16:14:52 -05:00
import { getClassNamesFor } from '../util/getClassNamesFor';
import { themeClassName } from '../util/theme';
2021-09-17 18:24:21 -04:00
import { useEscapeHandling } from '../hooks/useEscapeHandling';
2022-09-27 13:24:21 -07:00
import { usePrevious } from '../hooks/usePrevious';
import { handleOutsideClick } from '../util/handleOutsideClick';
2025-06-16 11:59:31 -07:00
import { createLogger } from '../logging/log';
const log = createLogger('ModalHost');
2020-11-20 09:30:45 -08:00
export const ModalContainerContext = React.createContext<HTMLElement | null>(
null
);
export type PropsType = Readonly<{
children: React.ReactElement;
2022-09-27 13:24:21 -07:00
modalName: string;
2022-03-04 16:14:52 -05:00
moduleClassName?: string;
2024-06-25 11:56:28 -07:00
noEscapeClose?: boolean;
noMouseClose?: boolean;
onClose: () => unknown;
onEscape?: () => unknown;
2022-03-04 16:14:52 -05:00
onTopOfEverything?: boolean;
overlayStyles?: SpringValues<ModalConfigType>;
theme?: Theme;
}>;
2020-11-20 09:30:45 -08:00
2022-11-17 16:45:19 -08:00
export const ModalHost = React.memo(function ModalHostInner({
children,
modalName,
moduleClassName,
2024-06-25 11:56:28 -07:00
noEscapeClose,
2022-11-17 16:45:19 -08:00
noMouseClose,
onClose,
onEscape,
onTopOfEverything,
overlayStyles,
theme,
}: PropsType) {
const containerRef = React.useRef<HTMLDivElement | null>(null);
const previousModalName = usePrevious(modalName, modalName);
const modalContainer = useContext(ModalContainerContext) ?? document.body;
2022-09-27 13:24:21 -07:00
2022-11-17 16:45:19 -08:00
if (previousModalName !== modalName) {
log.error(
2025-06-16 11:59:31 -07:00
`detected conflict between ${previousModalName} ` +
2022-11-17 16:45:19 -08:00
`and ${modalName}. Consider using "key" attributes on both modals.`
);
assertDev(false, 'Modal conflict');
}
2020-11-20 09:30:45 -08:00
2024-06-25 11:56:28 -07:00
useEscapeHandling(noEscapeClose ? noop : onEscape || onClose);
2022-11-17 16:45:19 -08:00
useEffect(() => {
if (noMouseClose) {
return noop;
}
return handleOutsideClick(
node => {
// In strange event propagation situations we can get the actual document.body
// node here. We don't want to handle those events.
if (node === document.body) {
return false;
}
2022-11-17 16:45:19 -08:00
// 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 &&
2025-04-10 14:50:00 -07:00
node.closest('.module-calling__modal-container, [data-fun-overlay]')
2022-11-17 16:45:19 -08:00
) {
return false;
}
onClose();
return true;
},
{ containerElements: [containerRef], name: modalName }
);
}, [noMouseClose, onClose, containerRef, modalName, modalContainer]);
2020-11-20 09:30:45 -08:00
2022-11-17 16:45:19 -08: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 16:14:52 -05:00
2022-11-17 16:45:19 -08: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-14 20:08:46 -04:00
</div>
2022-03-04 16:14:52 -05:00
</div>
2022-11-17 16:45:19 -08:00
</div>
);
return createPortal(
<FocusScope contain autoFocus restoreFocus>
{modalContent}
</FocusScope>,
document.body
);
2022-11-17 16:45:19 -08:00
});