signal-desktop/ts/components/Modal.tsx

314 lines
8.7 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2021-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactElement, ReactNode } from 'react';
import React, { useRef, useState } from 'react';
import type { ContentRect, MeasuredComponentProps } from 'react-measure';
import Measure from 'react-measure';
import classNames from 'classnames';
import { noop } from 'lodash';
2021-10-14 16:52:42 +00:00
import { animated } from '@react-spring/web';
import type { LocalizerType } from '../types/Util';
import { ModalHost } from './ModalHost';
import type { Theme } from '../util/theme';
2021-05-11 00:50:43 +00:00
import { getClassNamesFor } from '../util/getClassNamesFor';
import { useAnimated } from '../hooks/useAnimated';
2021-09-17 22:24:21 +00:00
import { useHasWrapped } from '../hooks/useHasWrapped';
import { useRefMerger } from '../hooks/useRefMerger';
type PropsType = {
children: ReactNode;
2022-09-27 20:24:21 +00:00
modalName: string;
hasXButton?: boolean;
2022-10-04 23:17:15 +00:00
hasHeaderDivider?: boolean;
hasFooterDivider?: boolean;
i18n: LocalizerType;
2022-08-10 18:37:19 +00:00
modalFooter?: JSX.Element;
2021-04-21 16:31:12 +00:00
moduleClassName?: string;
onBackButtonClick?: () => unknown;
onClose?: () => void;
title?: ReactNode;
2022-03-04 21:14:52 +00:00
useFocusTrap?: boolean;
padded?: boolean;
};
export type ModalPropsType = PropsType & {
noMouseClose?: boolean;
theme?: Theme;
};
2021-05-11 00:50:43 +00:00
const BASE_CLASS_NAME = 'module-Modal';
export function Modal({
children,
2022-09-27 20:24:21 +00:00
modalName,
hasXButton,
i18n,
2022-08-10 18:37:19 +00:00
modalFooter,
2021-04-21 16:31:12 +00:00
moduleClassName,
2021-05-28 16:15:17 +00:00
noMouseClose,
onBackButtonClick,
onClose = noop,
theme,
2022-08-10 18:37:19 +00:00
title,
2022-03-04 21:14:52 +00:00
useFocusTrap,
2022-10-04 23:17:15 +00:00
hasHeaderDivider = false,
hasFooterDivider = false,
padded = true,
}: Readonly<ModalPropsType>): ReactElement {
2021-10-14 16:52:42 +00:00
const { close, modalStyles, overlayStyles } = useAnimated(onClose, {
getFrom: () => ({ opacity: 0, transform: 'translateY(48px)' }),
getTo: isOpen =>
isOpen
? { opacity: 1, transform: 'translateY(0px)' }
: { opacity: 0, transform: 'translateY(48px)' },
});
return (
2021-10-14 16:52:42 +00:00
<ModalHost
2022-09-27 20:24:21 +00:00
modalName={modalName}
2022-03-04 21:14:52 +00:00
moduleClassName={moduleClassName}
2021-10-14 16:52:42 +00:00
noMouseClose={noMouseClose}
onClose={close}
overlayStyles={overlayStyles}
theme={theme}
2022-03-04 21:14:52 +00:00
useFocusTrap={useFocusTrap}
2021-10-14 16:52:42 +00:00
>
<animated.div style={modalStyles}>
<ModalPage
2022-09-27 20:24:21 +00:00
modalName={modalName}
hasXButton={hasXButton}
i18n={i18n}
2022-08-10 18:37:19 +00:00
modalFooter={modalFooter}
moduleClassName={moduleClassName}
onBackButtonClick={onBackButtonClick}
onClose={close}
title={title}
padded={padded}
2022-10-04 23:17:15 +00:00
hasHeaderDivider={hasHeaderDivider}
hasFooterDivider={hasFooterDivider}
>
{children}
</ModalPage>
2021-10-14 16:52:42 +00:00
</animated.div>
</ModalHost>
);
}
type ModalPageProps = Readonly<{
// should be the one provided by PagedModal
onClose: () => void;
}> &
Omit<Readonly<PropsType>, 'onClose'>;
/**
* Represents a single instance (or page) of a modal window.
*
* It should not be used by itself, either wrap it with PagedModal,
* render it in a component that has PagedModal as an ancestor, or
* use Modal instead.
*
* It does not provide open/close animation.
*
* NOTE: When used in conjunction with PagedModal (almost always the case):
* onClose" handler should be the one provided by the parent PagedModal,
* not one that has any logic. If you have some logic to execute when the
* modal closes, pass it to PagedModal.
*/
export function ModalPage({
children,
hasXButton,
i18n,
2022-08-10 18:37:19 +00:00
modalFooter,
moduleClassName,
onBackButtonClick,
onClose,
title,
padded = true,
2022-10-04 23:17:15 +00:00
hasHeaderDivider = false,
hasFooterDivider = false,
}: ModalPageProps): JSX.Element {
2021-08-06 00:17:05 +00:00
const modalRef = useRef<HTMLDivElement | null>(null);
const refMerger = useRefMerger();
2021-09-21 16:25:21 +00:00
const bodyRef = useRef<HTMLDivElement | null>(null);
const [scrolled, setScrolled] = useState(false);
2021-08-06 00:17:05 +00:00
const [hasOverflow, setHasOverflow] = useState(false);
const hasHeader = Boolean(hasXButton || title || onBackButtonClick);
2021-05-11 00:50:43 +00:00
const getClassName = getClassNamesFor(BASE_CLASS_NAME, moduleClassName);
2021-08-06 00:17:05 +00:00
function handleResize({ scroll }: ContentRect) {
const modalNode = modalRef?.current;
if (!modalNode) {
return;
}
if (scroll) {
setHasOverflow(scroll.height > modalNode.clientHeight);
}
}
return (
<>
{/* We don't want the click event to propagate to its container node. */}
{/* eslint-disable-next-line max-len */}
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */}
<div
className={classNames(
2021-05-11 00:50:43 +00:00
getClassName(''),
2021-08-06 00:17:05 +00:00
getClassName(hasHeader ? '--has-header' : '--no-header'),
2022-10-04 23:17:15 +00:00
Boolean(modalFooter) && getClassName('--has-footer'),
padded && getClassName('--padded'),
hasHeaderDivider && getClassName('--header-divider'),
hasFooterDivider && getClassName('--footer-divider')
)}
2021-08-06 00:17:05 +00:00
ref={modalRef}
onClick={event => {
event.stopPropagation();
}}
>
{hasHeader && (
<div
className={classNames(
getClassName('__header'),
onBackButtonClick
? getClassName('__header--with-back-button')
: null
)}
>
{onBackButtonClick && (
<button
aria-label={i18n('back')}
className={getClassName('__back-button')}
onClick={onBackButtonClick}
tabIndex={0}
type="button"
/>
)}
{title && (
<h1
className={classNames(
2021-05-11 00:50:43 +00:00
getClassName('__title'),
hasXButton ? getClassName('__title--with-x-button') : null
)}
>
{title}
</h1>
)}
{hasXButton && !title && (
<div className={getClassName('__title')} />
)}
{hasXButton && (
<button
aria-label={i18n('close')}
className={getClassName('__close-button')}
onClick={onClose}
tabIndex={0}
type="button"
/>
)}
</div>
)}
2021-08-06 00:17:05 +00:00
<Measure scroll onResize={handleResize}>
{({ measureRef }: MeasuredComponentProps) => (
<div
className={classNames(
getClassName('__body'),
scrolled ? getClassName('__body--scrolled') : null,
hasOverflow || scrolled
? getClassName('__body--overflow')
: null
)}
2021-09-21 16:25:21 +00:00
onScroll={() => {
const scrollTop = bodyRef.current?.scrollTop || 0;
setScrolled(scrollTop > 2);
}}
ref={refMerger(measureRef, bodyRef)}
2021-08-06 00:17:05 +00:00
>
{children}
</div>
2021-05-11 00:50:43 +00:00
)}
2021-08-06 00:17:05 +00:00
</Measure>
{modalFooter && <Modal.ButtonFooter>{modalFooter}</Modal.ButtonFooter>}
</div>
</>
);
}
2022-11-18 00:45:19 +00:00
function ButtonFooter({
children,
2021-05-11 00:50:43 +00:00
}: Readonly<{
children: ReactNode;
}>): ReactElement {
const [ref, hasWrapped] = useHasWrapped<HTMLDivElement>();
const className = getClassNamesFor(BASE_CLASS_NAME)('__button-footer');
return (
<div
className={classNames(
className,
hasWrapped ? `${className}--one-button-per-line` : undefined
)}
ref={ref}
>
{children}
</div>
);
2022-11-18 00:45:19 +00:00
}
Modal.ButtonFooter = ButtonFooter;
type PagedModalProps = Readonly<{
modalName: string;
children: RenderModalPage;
moduleClassName?: string;
onClose?: () => void;
useFocusTrap?: boolean;
noMouseClose?: boolean;
theme?: Theme;
}>;
/**
* Provides modal animation and click to close functionality to a
* ModalPage descendant.
*
* Useful when we want to swap between different ModalPages (possibly
* rendered by different components) without triggering an open/close
* transition animation.
*/
export function PagedModal({
modalName,
children,
moduleClassName,
noMouseClose,
onClose = noop,
theme,
useFocusTrap,
}: PagedModalProps): ReactElement {
const { close, modalStyles, overlayStyles } = useAnimated(onClose, {
getFrom: () => ({ opacity: 0, transform: 'translateY(48px)' }),
getTo: isOpen =>
isOpen
? { opacity: 1, transform: 'translateY(0px)' }
: { opacity: 0, transform: 'translateY(48px)' },
});
return (
<ModalHost
modalName={modalName}
moduleClassName={moduleClassName}
noMouseClose={noMouseClose}
onClose={close}
overlayStyles={overlayStyles}
theme={theme}
useFocusTrap={useFocusTrap}
>
<animated.div style={modalStyles}>{children(close)}</animated.div>
</ModalHost>
);
}
export type RenderModalPage = (onClose: () => void) => JSX.Element;