2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
import * as React from 'react';
|
|
|
|
import { createPortal } from 'react-dom';
|
2020-05-27 21:37:06 +00:00
|
|
|
import {
|
|
|
|
ConfirmationDialog,
|
|
|
|
Props as ConfirmationDialogProps,
|
|
|
|
} from './ConfirmationDialog';
|
2019-05-16 22:32:11 +00:00
|
|
|
import { LocalizerType } from '../types/Util';
|
|
|
|
|
|
|
|
export type OwnProps = {
|
|
|
|
readonly i18n: LocalizerType;
|
|
|
|
readonly onClose: () => unknown;
|
|
|
|
};
|
|
|
|
|
2020-05-27 21:37:06 +00:00
|
|
|
export type Props = OwnProps & ConfirmationDialogProps;
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
export const ConfirmationModal = React.memo(
|
2020-05-27 21:37:06 +00:00
|
|
|
({ i18n, onClose, children, ...rest }: Props) => {
|
2019-05-16 22:32:11 +00:00
|
|
|
const [root, setRoot] = React.useState<HTMLElement | null>(null);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
document.body.appendChild(div);
|
|
|
|
setRoot(div);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.body.removeChild(div);
|
|
|
|
setRoot(null);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
const handler = (event: KeyboardEvent) => {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
onClose();
|
2019-11-07 21:36:16 +00:00
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
document.addEventListener('keydown', handler);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
2020-01-08 17:44:54 +00:00
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', handler);
|
|
|
|
};
|
|
|
|
}, [onClose]);
|
2019-05-16 22:32:11 +00:00
|
|
|
|
|
|
|
const handleCancel = React.useCallback(
|
|
|
|
(e: React.MouseEvent) => {
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[onClose]
|
|
|
|
);
|
|
|
|
|
2020-09-12 00:46:52 +00:00
|
|
|
const handleKeyCancel = React.useCallback(
|
|
|
|
(e: React.KeyboardEvent) => {
|
|
|
|
if (e.target === e.currentTarget && e.keyCode === 27) {
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[onClose]
|
|
|
|
);
|
|
|
|
|
2019-05-16 22:32:11 +00:00
|
|
|
return root
|
|
|
|
? createPortal(
|
|
|
|
<div
|
2020-09-12 00:46:52 +00:00
|
|
|
role="presentation"
|
2019-05-16 22:32:11 +00:00
|
|
|
className="module-confirmation-dialog__overlay"
|
|
|
|
onClick={handleCancel}
|
2020-09-12 00:46:52 +00:00
|
|
|
onKeyUp={handleKeyCancel}
|
2019-05-16 22:32:11 +00:00
|
|
|
>
|
2020-05-27 21:37:06 +00:00
|
|
|
<ConfirmationDialog i18n={i18n} {...rest} onClose={onClose}>
|
2019-05-16 22:32:11 +00:00
|
|
|
{children}
|
|
|
|
</ConfirmationDialog>
|
|
|
|
</div>,
|
|
|
|
root
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
);
|