2022-10-17 11:27:48 +00:00
|
|
|
import React, { useCallback, useContext, useEffect } from 'react';
|
2022-10-21 07:42:00 +00:00
|
|
|
import FocusLock from 'react-focus-lock';
|
2022-09-29 10:26:34 +00:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-10-04 09:44:48 +00:00
|
|
|
import { useDispatch } from 'react-redux';
|
2022-09-29 10:26:34 +00:00
|
|
|
import { keyframes } from 'tss-react';
|
|
|
|
import { makeStyles } from 'tss-react/mui';
|
|
|
|
|
2022-10-04 09:44:48 +00:00
|
|
|
import { hideDialog } from '../../../dialog/actions';
|
2022-11-08 10:24:32 +00:00
|
|
|
import { IconCloseLarge } from '../../../icons/svg';
|
2022-09-29 10:26:34 +00:00
|
|
|
import { withPixelLineHeight } from '../../../styles/functions.web';
|
|
|
|
|
|
|
|
import Button from './Button';
|
|
|
|
import ClickableIcon from './ClickableIcon';
|
|
|
|
import { DialogTransitionContext } from './DialogTransition';
|
|
|
|
|
|
|
|
|
2022-11-15 07:50:22 +00:00
|
|
|
const useStyles = makeStyles()(theme => {
|
2022-09-29 10:26:34 +00:00
|
|
|
return {
|
|
|
|
container: {
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
position: 'fixed',
|
2022-10-27 08:36:50 +00:00
|
|
|
color: theme.palette.text01,
|
|
|
|
...withPixelLineHeight(theme.typography.bodyLongRegular),
|
2022-09-29 10:26:34 +00:00
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
animation: `${keyframes`
|
|
|
|
0% {
|
|
|
|
opacity: 0.4;
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
`} 0.2s forwards ease-out`,
|
|
|
|
|
|
|
|
'&.unmount': {
|
|
|
|
animation: `${keyframes`
|
|
|
|
0% {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
opacity: 0.5;
|
|
|
|
}
|
|
|
|
`} 0.15s forwards ease-in`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
backdrop: {
|
|
|
|
position: 'absolute',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
backgroundColor: theme.palette.ui02,
|
|
|
|
opacity: 0.75
|
|
|
|
},
|
|
|
|
|
|
|
|
modal: {
|
|
|
|
backgroundColor: theme.palette.ui01,
|
|
|
|
border: `1px solid ${theme.palette.ui03}`,
|
|
|
|
boxShadow: '0px 4px 25px 4px rgba(20, 20, 20, 0.6)',
|
|
|
|
borderRadius: `${theme.shape.borderRadius}px`,
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
height: 'auto',
|
|
|
|
minHeight: '200px',
|
2022-11-21 09:09:03 +00:00
|
|
|
maxHeight: '760px',
|
2022-09-29 10:26:34 +00:00
|
|
|
marginTop: '64px',
|
|
|
|
animation: `${keyframes`
|
|
|
|
0% {
|
|
|
|
margin-top: 85px
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
margin-top: 64px
|
|
|
|
}
|
|
|
|
`} 0.2s forwards ease-out`,
|
|
|
|
|
|
|
|
'&.medium': {
|
|
|
|
width: '400px'
|
|
|
|
},
|
|
|
|
|
|
|
|
'&.large': {
|
|
|
|
width: '664px'
|
|
|
|
},
|
|
|
|
|
|
|
|
'&.unmount': {
|
|
|
|
animation: `${keyframes`
|
|
|
|
0% {
|
|
|
|
margin-top: 64px
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
margin-top: 40px
|
|
|
|
}
|
|
|
|
`} 0.15s forwards ease-in`
|
|
|
|
},
|
|
|
|
|
|
|
|
'@media (max-width: 448px)': {
|
|
|
|
width: '100% !important',
|
|
|
|
maxHeight: 'initial',
|
|
|
|
height: '100%',
|
|
|
|
margin: 0,
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
bottom: 0,
|
|
|
|
animation: `${keyframes`
|
|
|
|
0% {
|
|
|
|
margin-top: 15px
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
margin-top: 0
|
|
|
|
}
|
|
|
|
`} 0.2s forwards ease-out`,
|
|
|
|
|
|
|
|
'&.unmount': {
|
|
|
|
animation: `${keyframes`
|
|
|
|
0% {
|
|
|
|
margin-top: 0
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
margin-top: 15px
|
|
|
|
}
|
|
|
|
`} 0.15s forwards ease-in`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
header: {
|
|
|
|
width: '100%',
|
|
|
|
padding: '24px',
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
justifyContent: 'space-between'
|
|
|
|
},
|
|
|
|
|
2022-11-21 09:09:03 +00:00
|
|
|
closeIcon: {
|
|
|
|
'&:focus': {
|
|
|
|
boxShadow: 'none'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
title: {
|
|
|
|
color: theme.palette.text01,
|
|
|
|
...withPixelLineHeight(theme.typography.heading5),
|
|
|
|
margin: 0,
|
|
|
|
padding: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
content: {
|
|
|
|
height: 'auto',
|
|
|
|
overflowY: 'auto',
|
|
|
|
width: '100%',
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
padding: '0 24px',
|
|
|
|
overflowX: 'hidden',
|
2022-10-11 08:24:11 +00:00
|
|
|
minHeight: '40px',
|
2022-09-29 10:26:34 +00:00
|
|
|
|
|
|
|
'@media (max-width: 448px)': {
|
|
|
|
height: '100%'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
footer: {
|
|
|
|
width: '100%',
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'flex-end',
|
|
|
|
padding: '24px',
|
|
|
|
|
|
|
|
'& button:last-child': {
|
|
|
|
marginLeft: '16px'
|
|
|
|
}
|
2022-10-21 07:42:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
focusLock: {
|
|
|
|
zIndex: 1
|
2022-09-29 10:26:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
interface IDialogProps {
|
2022-10-18 10:56:08 +00:00
|
|
|
back?: {
|
|
|
|
hidden?: boolean;
|
|
|
|
onClick?: () => void;
|
|
|
|
translationKey?: string;
|
|
|
|
};
|
2022-10-04 09:44:48 +00:00
|
|
|
cancel?: {
|
|
|
|
hidden?: boolean;
|
|
|
|
translationKey?: string;
|
|
|
|
};
|
2022-10-17 11:27:48 +00:00
|
|
|
children?: React.ReactNode;
|
2022-10-07 09:16:41 +00:00
|
|
|
className?: string;
|
2022-09-29 10:26:34 +00:00
|
|
|
description?: string;
|
2022-11-17 22:12:40 +00:00
|
|
|
disableAutoHideOnSubmit?: boolean;
|
2022-10-17 11:27:48 +00:00
|
|
|
disableBackdropClose?: boolean;
|
2022-10-18 10:56:08 +00:00
|
|
|
disableEnter?: boolean;
|
2022-10-17 11:27:48 +00:00
|
|
|
hideCloseButton?: boolean;
|
2022-09-29 10:26:34 +00:00
|
|
|
ok?: {
|
|
|
|
disabled?: boolean;
|
2022-10-04 09:44:48 +00:00
|
|
|
hidden?: boolean;
|
|
|
|
translationKey?: string;
|
2022-09-29 10:26:34 +00:00
|
|
|
};
|
2022-10-04 09:44:48 +00:00
|
|
|
onCancel?: () => void;
|
2022-09-29 10:26:34 +00:00
|
|
|
onSubmit?: () => void;
|
|
|
|
size?: 'large' | 'medium';
|
|
|
|
title?: string;
|
|
|
|
titleKey?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Dialog = ({
|
2022-10-18 10:56:08 +00:00
|
|
|
back = { hidden: true },
|
2022-10-04 09:44:48 +00:00
|
|
|
cancel = { translationKey: 'dialog.Cancel' },
|
|
|
|
children,
|
2022-10-07 09:16:41 +00:00
|
|
|
className,
|
2022-09-29 10:26:34 +00:00
|
|
|
description,
|
2022-11-17 22:12:40 +00:00
|
|
|
disableAutoHideOnSubmit = false,
|
2022-10-17 11:27:48 +00:00
|
|
|
disableBackdropClose,
|
|
|
|
hideCloseButton,
|
2022-10-18 10:56:08 +00:00
|
|
|
disableEnter,
|
2022-10-04 09:44:48 +00:00
|
|
|
ok = { translationKey: 'dialog.Ok' },
|
2022-09-29 10:26:34 +00:00
|
|
|
onCancel,
|
2022-10-04 09:44:48 +00:00
|
|
|
onSubmit,
|
|
|
|
size = 'medium',
|
|
|
|
title,
|
|
|
|
titleKey
|
2022-10-20 09:11:27 +00:00
|
|
|
}: IDialogProps) => {
|
2022-09-29 10:26:34 +00:00
|
|
|
const { classes, cx } = useStyles();
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const { isUnmounting } = useContext(DialogTransitionContext);
|
2022-10-04 09:44:48 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const onClose = useCallback(() => {
|
|
|
|
dispatch(hideDialog());
|
2022-10-27 15:13:11 +00:00
|
|
|
onCancel?.();
|
2022-10-04 09:44:48 +00:00
|
|
|
}, [ onCancel ]);
|
2022-09-29 10:26:34 +00:00
|
|
|
|
2022-10-17 11:27:48 +00:00
|
|
|
const submit = useCallback(() => {
|
2022-11-17 22:12:40 +00:00
|
|
|
!disableAutoHideOnSubmit && dispatch(hideDialog());
|
2022-10-27 15:13:11 +00:00
|
|
|
onSubmit?.();
|
2022-10-17 11:27:48 +00:00
|
|
|
}, [ onSubmit ]);
|
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
const handleKeyDown = useCallback((e: KeyboardEvent) => {
|
|
|
|
if (e.key === 'Escape') {
|
2022-10-04 09:44:48 +00:00
|
|
|
onClose();
|
2022-09-29 10:26:34 +00:00
|
|
|
}
|
2022-10-18 10:56:08 +00:00
|
|
|
if (e.key === 'Enter' && !disableEnter) {
|
2022-10-17 11:27:48 +00:00
|
|
|
submit();
|
|
|
|
}
|
2022-09-29 10:26:34 +00:00
|
|
|
}, []);
|
|
|
|
|
2022-10-17 11:27:48 +00:00
|
|
|
const onBackdropClick = useCallback(() => {
|
|
|
|
!disableBackdropClose && onClose();
|
|
|
|
}, [ disableBackdropClose, onClose ]);
|
2022-10-04 09:44:48 +00:00
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
|
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className = { cx(classes.container, isUnmounting && 'unmount') }>
|
|
|
|
<div
|
|
|
|
className = { classes.backdrop }
|
2022-10-17 11:27:48 +00:00
|
|
|
onClick = { onBackdropClick } />
|
2022-10-21 07:42:00 +00:00
|
|
|
<FocusLock className = { classes.focusLock }>
|
|
|
|
<div
|
|
|
|
aria-describedby = { description }
|
|
|
|
aria-labelledby = { title ?? t(titleKey ?? '') }
|
|
|
|
aria-modal = { true }
|
|
|
|
className = { cx(classes.modal, isUnmounting && 'unmount', size, className) }
|
|
|
|
role = 'dialog'>
|
|
|
|
<div className = { classes.header }>
|
|
|
|
<p
|
|
|
|
className = { classes.title }
|
|
|
|
id = 'dialog-title'>
|
|
|
|
{title ?? t(titleKey ?? '')}
|
|
|
|
</p>
|
|
|
|
{!hideCloseButton && (
|
|
|
|
<ClickableIcon
|
|
|
|
accessibilityLabel = { t('dialog.close') }
|
2022-11-21 09:09:03 +00:00
|
|
|
className = { classes.closeIcon }
|
2022-11-08 10:24:32 +00:00
|
|
|
icon = { IconCloseLarge }
|
2022-10-21 07:42:00 +00:00
|
|
|
id = 'modal-header-close-button'
|
|
|
|
onClick = { onClose } />
|
|
|
|
)}
|
|
|
|
</div>
|
2022-11-09 10:07:37 +00:00
|
|
|
<div
|
|
|
|
className = { classes.content }
|
|
|
|
data-autofocus-inside = 'true'>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className = { classes.footer }
|
|
|
|
data-autofocus-inside = 'true'>
|
2022-10-21 07:42:00 +00:00
|
|
|
{!back.hidden && <Button
|
|
|
|
accessibilityLabel = { t(back.translationKey ?? '') }
|
|
|
|
labelKey = { back.translationKey }
|
|
|
|
// eslint-disable-next-line react/jsx-handler-names
|
|
|
|
onClick = { back.onClick }
|
|
|
|
type = 'secondary' />}
|
|
|
|
{!cancel.hidden && <Button
|
|
|
|
accessibilityLabel = { t(cancel.translationKey ?? '') }
|
|
|
|
labelKey = { cancel.translationKey }
|
|
|
|
onClick = { onClose }
|
|
|
|
type = 'tertiary' />}
|
|
|
|
{!ok.hidden && <Button
|
|
|
|
accessibilityLabel = { t(ok.translationKey ?? '') }
|
|
|
|
disabled = { ok.disabled }
|
|
|
|
id = 'modal-dialog-ok-button'
|
|
|
|
labelKey = { ok.translationKey }
|
|
|
|
onClick = { submit } />}
|
|
|
|
</div>
|
2022-09-29 10:26:34 +00:00
|
|
|
</div>
|
2022-10-21 07:42:00 +00:00
|
|
|
</FocusLock>
|
2022-09-29 10:26:34 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Dialog;
|