2021-04-13 14:20:02 +00:00
|
|
|
// Copyright 2019-2021 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';
|
|
|
|
import { Modal, ModalWindow } 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';
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
export type ActionSpec = {
|
|
|
|
text: string;
|
|
|
|
action: () => unknown;
|
|
|
|
style?: 'affirmative' | 'negative';
|
|
|
|
};
|
|
|
|
|
2021-12-03 23:04:34 +00:00
|
|
|
export type OwnProps = Readonly<{
|
|
|
|
actions?: Array<ActionSpec>;
|
2022-06-13 18:42:19 +00:00
|
|
|
cancelButtonVariant?: ButtonVariant;
|
2021-12-03 23:04:34 +00:00
|
|
|
cancelText?: string;
|
|
|
|
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;
|
2021-12-03 23:04:34 +00:00
|
|
|
onCancel?: () => unknown;
|
|
|
|
onClose: () => unknown;
|
|
|
|
onTopOfEverything?: boolean;
|
2022-06-13 18:42:19 +00:00
|
|
|
theme?: Theme;
|
|
|
|
title?: string | 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ConfirmationDialog = React.memo(
|
|
|
|
({
|
2021-06-01 20:45:43 +00:00
|
|
|
moduleClassName,
|
2021-04-27 19:29:59 +00:00
|
|
|
actions = [],
|
|
|
|
cancelText,
|
|
|
|
children,
|
|
|
|
i18n,
|
|
|
|
onCancel,
|
|
|
|
onClose,
|
|
|
|
theme,
|
|
|
|
title,
|
2021-06-01 20:45:43 +00:00
|
|
|
hasXButton,
|
2021-11-01 19:13:35 +00:00
|
|
|
cancelButtonVariant,
|
2021-12-03 23:04:34 +00:00
|
|
|
onTopOfEverything,
|
2021-04-27 19:29:59 +00:00
|
|
|
}: Props) => {
|
2021-10-14 16:52:42 +00:00
|
|
|
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
|
|
|
|
|
|
|
const cancelAndClose = useCallback(() => {
|
2021-04-27 19:29:59 +00:00
|
|
|
if (onCancel) {
|
|
|
|
onCancel();
|
|
|
|
}
|
2021-09-29 20:59:37 +00:00
|
|
|
close();
|
|
|
|
}, [close, onCancel]);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2021-09-29 20:59:37 +00:00
|
|
|
const handleCancel = useCallback(
|
|
|
|
(e: MouseEvent) => {
|
2019-05-16 22:32:11 +00:00
|
|
|
if (e.target === e.currentTarget) {
|
2021-04-27 19:29:59 +00:00
|
|
|
cancelAndClose();
|
2019-05-16 22:32:11 +00:00
|
|
|
}
|
|
|
|
},
|
2021-04-27 19:29:59 +00:00
|
|
|
[cancelAndClose]
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
|
2021-04-27 19:29:59 +00:00
|
|
|
const hasActions = Boolean(actions.length);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
return (
|
2021-12-03 23:04:34 +00:00
|
|
|
<ModalHost
|
|
|
|
onTopOfEverything={onTopOfEverything}
|
|
|
|
onClose={close}
|
|
|
|
theme={theme}
|
|
|
|
overlayStyles={overlayStyles}
|
|
|
|
>
|
2021-10-14 16:52:42 +00:00
|
|
|
<animated.div style={modalStyles}>
|
2021-09-29 20:59:37 +00:00
|
|
|
<ModalWindow
|
|
|
|
hasXButton={hasXButton}
|
|
|
|
i18n={i18n}
|
|
|
|
moduleClassName={moduleClassName}
|
|
|
|
onClose={cancelAndClose}
|
|
|
|
title={title}
|
2021-04-27 19:29:59 +00:00
|
|
|
>
|
2021-09-29 20:59:37 +00:00
|
|
|
{children}
|
|
|
|
<Modal.ButtonFooter>
|
|
|
|
<Button
|
|
|
|
onClick={handleCancel}
|
|
|
|
ref={focusRef}
|
|
|
|
variant={
|
2021-11-01 19:13:35 +00:00
|
|
|
cancelButtonVariant ||
|
|
|
|
(hasActions ? ButtonVariant.Secondary : ButtonVariant.Primary)
|
2021-09-29 20:59:37 +00:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{cancelText || i18n('confirmation-dialog--Cancel')}
|
|
|
|
</Button>
|
|
|
|
{actions.map((action, i) => (
|
|
|
|
<Button
|
|
|
|
key={action.text}
|
|
|
|
onClick={() => {
|
|
|
|
action.action();
|
|
|
|
close();
|
|
|
|
}}
|
|
|
|
data-action={i}
|
|
|
|
variant={getButtonVariant(action.style)}
|
|
|
|
>
|
|
|
|
{action.text}
|
|
|
|
</Button>
|
|
|
|
))}
|
|
|
|
</Modal.ButtonFooter>
|
|
|
|
</ModalWindow>
|
2021-10-14 16:52:42 +00:00
|
|
|
</animated.div>
|
2021-09-29 20:59:37 +00:00
|
|
|
</ModalHost>
|
2019-05-16 22:32:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|