2021-04-13 14:20:02 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, { useState, ReactElement, ReactNode } from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
|
|
|
|
import { LocalizerType } from '../types/Util';
|
|
|
|
import { ModalHost } from './ModalHost';
|
2021-04-27 19:29:59 +00:00
|
|
|
import { Theme } from '../util/theme';
|
2021-04-13 14:20:02 +00:00
|
|
|
|
|
|
|
type PropsType = {
|
|
|
|
children: ReactNode;
|
|
|
|
hasXButton?: boolean;
|
|
|
|
i18n: LocalizerType;
|
2021-04-21 16:31:12 +00:00
|
|
|
moduleClassName?: string;
|
2021-04-13 14:20:02 +00:00
|
|
|
onClose?: () => void;
|
|
|
|
title?: ReactNode;
|
2021-04-27 19:29:59 +00:00
|
|
|
theme?: Theme;
|
2021-04-13 14:20:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function Modal({
|
|
|
|
children,
|
|
|
|
hasXButton,
|
|
|
|
i18n,
|
2021-04-21 16:31:12 +00:00
|
|
|
moduleClassName,
|
2021-04-13 14:20:02 +00:00
|
|
|
onClose = noop,
|
|
|
|
title,
|
2021-04-27 19:29:59 +00:00
|
|
|
theme,
|
2021-04-13 14:20:02 +00:00
|
|
|
}: Readonly<PropsType>): ReactElement {
|
|
|
|
const [scrolled, setScrolled] = useState(false);
|
|
|
|
|
|
|
|
const hasHeader = Boolean(hasXButton || title);
|
|
|
|
|
|
|
|
return (
|
2021-04-27 19:29:59 +00:00
|
|
|
<ModalHost onClose={onClose} theme={theme}>
|
2021-04-13 14:20:02 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-Modal',
|
2021-04-21 16:31:12 +00:00
|
|
|
hasHeader ? 'module-Modal--has-header' : 'module-Modal--no-header',
|
|
|
|
moduleClassName
|
2021-04-13 14:20:02 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{hasHeader && (
|
|
|
|
<div className="module-Modal__header">
|
|
|
|
{hasXButton && (
|
|
|
|
<button
|
|
|
|
aria-label={i18n('close')}
|
|
|
|
type="button"
|
|
|
|
className="module-Modal__close-button"
|
|
|
|
onClick={() => {
|
|
|
|
onClose();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{title && <h1 className="module-Modal__title">{title}</h1>}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
className={classNames('module-Modal__body', {
|
|
|
|
'module-Modal__body--scrolled': scrolled,
|
|
|
|
})}
|
|
|
|
onScroll={event => {
|
|
|
|
setScrolled((event.target as HTMLDivElement).scrollTop > 2);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ModalHost>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.Footer = ({
|
|
|
|
children,
|
|
|
|
}: Readonly<{ children: ReactNode }>): ReactElement => (
|
|
|
|
<div className="module-Modal__footer">{children}</div>
|
|
|
|
);
|