2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MouseEvent } from 'react';
|
|
|
|
import React, { useCallback } from 'react';
|
2021-10-14 16:52:42 +00:00
|
|
|
import { animated } from '@react-spring/web';
|
2021-04-27 19:29:59 +00:00
|
|
|
import { Button, ButtonVariant } from './Button';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-09-29 20:59:37 +00:00
|
|
|
import { ModalHost } from './ModalHost';
|
2022-09-29 22:40:09 +00:00
|
|
|
import { ModalPage } from './Modal';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Theme } from '../util/theme';
|
2021-09-29 20:59:37 +00:00
|
|
|
import { useAnimated } from '../hooks/useAnimated';
|
2023-02-27 22:34:43 +00:00
|
|
|
import { Spinner } from './Spinner';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
export type ActionSpec = {
|
|
|
|
action: () => unknown;
|
|
|
|
style?: 'affirmative' | 'negative';
|
2023-02-27 22:34:43 +00:00
|
|
|
autoClose?: boolean;
|
2023-03-20 20:42:00 +00:00
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
text: string;
|
|
|
|
id?: string;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
text: string | JSX.Element;
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
);
|
2020-05-27 21:37:06 +00:00
|
|
|
|
2021-12-03 23:04:34 +00:00
|
|
|
export type OwnProps = Readonly<{
|
|
|
|
actions?: Array<ActionSpec>;
|
2022-09-27 20:24:21 +00:00
|
|
|
dialogName: string;
|
2022-06-13 18:42:19 +00:00
|
|
|
cancelButtonVariant?: ButtonVariant;
|
2021-12-03 23:04:34 +00:00
|
|
|
cancelText?: string;
|
2023-02-27 22:34:43 +00:00
|
|
|
isSpinning?: boolean;
|
2021-12-03 23:04:34 +00:00
|
|
|
children?: React.ReactNode;
|
2022-06-13 18:42:19 +00:00
|
|
|
hasXButton?: boolean;
|
2021-12-03 23:04:34 +00:00
|
|
|
i18n: LocalizerType;
|
2022-06-13 18:42:19 +00:00
|
|
|
moduleClassName?: string;
|
2022-08-16 23:59:11 +00:00
|
|
|
noMouseClose?: boolean;
|
2022-11-02 00:10:27 +00:00
|
|
|
noDefaultCancelButton?: boolean;
|
2021-12-03 23:04:34 +00:00
|
|
|
onCancel?: () => unknown;
|
|
|
|
onClose: () => unknown;
|
|
|
|
onTopOfEverything?: boolean;
|
2022-06-13 18:42:19 +00:00
|
|
|
theme?: Theme;
|
2022-11-04 13:22:07 +00:00
|
|
|
title?: React.ReactNode;
|
2021-12-03 23:04:34 +00:00
|
|
|
}>;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
export type Props = OwnProps;
|
|
|
|
|
|
|
|
function focusRef(el: HTMLElement | null) {
|
|
|
|
if (el) {
|
|
|
|
el.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
function getButtonVariant(
|
|
|
|
buttonStyle?: 'affirmative' | 'negative'
|
|
|
|
): ButtonVariant {
|
|
|
|
if (buttonStyle === 'affirmative') {
|
|
|
|
return ButtonVariant.Primary;
|
|
|
|
}
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
if (buttonStyle === 'negative') {
|
|
|
|
return ButtonVariant.Destructive;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ButtonVariant.Secondary;
|
|
|
|
}
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export const ConfirmationDialog = React.memo(function ConfirmationDialogInner({
|
|
|
|
actions = [],
|
|
|
|
dialogName,
|
|
|
|
cancelButtonVariant,
|
|
|
|
cancelText,
|
|
|
|
children,
|
|
|
|
hasXButton,
|
|
|
|
i18n,
|
2023-02-27 22:34:43 +00:00
|
|
|
isSpinning,
|
2022-11-18 00:45:19 +00:00
|
|
|
moduleClassName,
|
|
|
|
noMouseClose,
|
|
|
|
noDefaultCancelButton,
|
|
|
|
onCancel,
|
|
|
|
onClose,
|
|
|
|
onTopOfEverything,
|
|
|
|
theme,
|
|
|
|
title,
|
|
|
|
}: Props) {
|
|
|
|
const { close, overlayStyles, modalStyles } = useAnimated(onClose, {
|
|
|
|
getFrom: () => ({ opacity: 0, transform: 'scale(0.25)' }),
|
|
|
|
getTo: isOpen => ({ opacity: isOpen ? 1 : 0, transform: 'scale(1)' }),
|
|
|
|
});
|
2021-09-29 20:59:37 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
const cancelAndClose = useCallback(() => {
|
|
|
|
if (onCancel) {
|
|
|
|
onCancel();
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
}, [close, onCancel]);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
const handleCancel = useCallback(
|
|
|
|
(e: MouseEvent) => {
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
cancelAndClose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[cancelAndClose]
|
|
|
|
);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
const hasActions = Boolean(actions.length);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
const footer = (
|
|
|
|
<>
|
2023-02-27 22:34:43 +00:00
|
|
|
{!isSpinning && !noDefaultCancelButton ? (
|
2022-11-18 00:45:19 +00:00
|
|
|
<Button
|
|
|
|
onClick={handleCancel}
|
|
|
|
ref={focusRef}
|
|
|
|
variant={
|
|
|
|
cancelButtonVariant ||
|
|
|
|
(hasActions ? ButtonVariant.Secondary : ButtonVariant.Primary)
|
|
|
|
}
|
|
|
|
>
|
2023-03-30 00:03:25 +00:00
|
|
|
{cancelText || i18n('icu:confirmation-dialog--Cancel')}
|
2022-11-18 00:45:19 +00:00
|
|
|
</Button>
|
|
|
|
) : null}
|
|
|
|
{actions.map((action, i) => (
|
|
|
|
<Button
|
2023-03-20 20:42:00 +00:00
|
|
|
key={
|
|
|
|
typeof action.text === 'string'
|
|
|
|
? action.id ?? action.text
|
|
|
|
: action.id
|
|
|
|
}
|
2023-02-27 22:34:43 +00:00
|
|
|
disabled={isSpinning}
|
2022-11-18 00:45:19 +00:00
|
|
|
onClick={() => {
|
|
|
|
action.action();
|
2023-02-27 22:34:43 +00:00
|
|
|
if (action.autoClose !== false) {
|
|
|
|
close();
|
|
|
|
}
|
2022-11-18 00:45:19 +00:00
|
|
|
}}
|
|
|
|
data-action={i}
|
|
|
|
variant={getButtonVariant(action.style)}
|
|
|
|
>
|
2023-02-27 22:34:43 +00:00
|
|
|
{isSpinning ? (
|
|
|
|
<Spinner
|
|
|
|
size="22px"
|
|
|
|
svgSize="small"
|
|
|
|
direction="on-primary-button"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
action.text
|
|
|
|
)}
|
2022-11-18 00:45:19 +00:00
|
|
|
</Button>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
2022-09-29 22:40:09 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
const modalName = `ConfirmationDialog.${dialogName}`;
|
2022-09-27 20:24:21 +00:00
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
return (
|
|
|
|
<ModalHost
|
|
|
|
modalName={modalName}
|
|
|
|
noMouseClose={noMouseClose}
|
|
|
|
onClose={close}
|
|
|
|
onEscape={cancelAndClose}
|
|
|
|
onTopOfEverything={onTopOfEverything}
|
|
|
|
overlayStyles={overlayStyles}
|
|
|
|
theme={theme}
|
|
|
|
>
|
|
|
|
<animated.div style={modalStyles}>
|
|
|
|
<ModalPage
|
|
|
|
modalName={modalName}
|
|
|
|
hasXButton={hasXButton}
|
|
|
|
i18n={i18n}
|
|
|
|
moduleClassName={moduleClassName}
|
|
|
|
onClose={cancelAndClose}
|
|
|
|
title={title}
|
|
|
|
modalFooter={footer}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</ModalPage>
|
|
|
|
</animated.div>
|
|
|
|
</ModalHost>
|
|
|
|
);
|
|
|
|
});
|