2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
import { motion } from 'framer-motion';
|
|
|
|
import { useRef } from 'react';
|
|
|
|
|
|
|
|
import useEscapeKey from '@/hooks/useEscapeKey';
|
2024-10-29 12:05:23 +03:00
|
|
|
import { HelpTopic } from '@/models/miscellaneous';
|
2024-06-07 20:17:03 +03:00
|
|
|
import { animateModal } from '@/styling/animations';
|
2024-10-29 12:05:23 +03:00
|
|
|
import { PARAMETER } from '@/utils/constants';
|
2024-06-07 20:17:03 +03:00
|
|
|
import { prepareTooltip } from '@/utils/labels';
|
|
|
|
|
|
|
|
import { IconClose } from '../Icons';
|
2024-10-29 12:05:23 +03:00
|
|
|
import BadgeHelp from '../info/BadgeHelp';
|
2024-06-07 20:17:03 +03:00
|
|
|
import { CProps } from '../props';
|
|
|
|
import Button from './Button';
|
|
|
|
import MiniButton from './MiniButton';
|
|
|
|
import Overlay from './Overlay';
|
|
|
|
|
|
|
|
export interface ModalProps extends CProps.Styling {
|
|
|
|
header?: string;
|
|
|
|
submitText?: string;
|
|
|
|
submitInvalidTooltip?: string;
|
|
|
|
|
|
|
|
readonly?: boolean;
|
|
|
|
canSubmit?: boolean;
|
|
|
|
overflowVisible?: boolean;
|
|
|
|
|
|
|
|
hideWindow: () => void;
|
2024-08-30 20:18:04 +03:00
|
|
|
beforeSubmit?: () => boolean;
|
2024-06-07 20:17:03 +03:00
|
|
|
onSubmit?: () => void;
|
|
|
|
onCancel?: () => void;
|
2024-10-29 12:05:23 +03:00
|
|
|
|
|
|
|
helpTopic?: HelpTopic;
|
|
|
|
hideHelpWhen?: () => boolean;
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function Modal({
|
2024-10-29 12:05:23 +03:00
|
|
|
children,
|
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
header,
|
2024-10-29 12:05:23 +03:00
|
|
|
submitText = 'Продолжить',
|
|
|
|
submitInvalidTooltip,
|
|
|
|
|
|
|
|
readonly,
|
|
|
|
canSubmit,
|
|
|
|
overflowVisible,
|
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
hideWindow,
|
2024-08-30 20:18:04 +03:00
|
|
|
beforeSubmit,
|
2024-06-07 20:17:03 +03:00
|
|
|
onSubmit,
|
|
|
|
onCancel,
|
|
|
|
className,
|
2024-10-29 12:05:23 +03:00
|
|
|
|
|
|
|
helpTopic,
|
|
|
|
hideHelpWhen,
|
2024-06-07 20:17:03 +03:00
|
|
|
...restProps
|
2024-09-19 17:48:48 +03:00
|
|
|
}: React.PropsWithChildren<ModalProps>) {
|
2024-06-07 20:17:03 +03:00
|
|
|
const ref = useRef(null);
|
|
|
|
useEscapeKey(hideWindow);
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
hideWindow();
|
|
|
|
if (onCancel) onCancel();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
2024-08-30 20:18:04 +03:00
|
|
|
if (beforeSubmit && !beforeSubmit()) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-07 20:17:03 +03:00
|
|
|
if (onSubmit) onSubmit();
|
2024-08-30 20:18:04 +03:00
|
|
|
hideWindow();
|
2024-06-07 20:17:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2024-10-29 12:05:23 +03:00
|
|
|
<div className='fixed top-0 left-0 w-full h-full z-modal cursor-default'>
|
2024-06-07 20:17:03 +03:00
|
|
|
<div className={clsx('z-navigation', 'fixed top-0 left-0', 'w-full h-full', 'cc-modal-blur')} />
|
|
|
|
<div className={clsx('z-navigation', 'fixed top-0 left-0', 'w-full h-full', 'cc-modal-backdrop')} />
|
|
|
|
<motion.div
|
|
|
|
ref={ref}
|
|
|
|
className={clsx(
|
|
|
|
'z-modal',
|
|
|
|
'absolute bottom-1/2 left-1/2 -translate-x-1/2 translate-y-1/2',
|
2024-10-29 12:44:51 +03:00
|
|
|
'border rounded-xl',
|
2024-06-07 20:17:03 +03:00
|
|
|
'clr-app'
|
|
|
|
)}
|
|
|
|
initial={{ ...animateModal.initial }}
|
|
|
|
animate={{ ...animateModal.animate }}
|
|
|
|
exit={{ ...animateModal.exit }}
|
|
|
|
{...restProps}
|
|
|
|
>
|
|
|
|
<Overlay position='right-2 top-2'>
|
|
|
|
<MiniButton
|
|
|
|
noPadding
|
|
|
|
titleHtml={prepareTooltip('Закрыть диалоговое окно', 'ESC')}
|
|
|
|
icon={<IconClose size='1.25rem' />}
|
|
|
|
onClick={handleCancel}
|
|
|
|
/>
|
|
|
|
</Overlay>
|
2024-10-29 12:05:23 +03:00
|
|
|
{helpTopic && !hideHelpWhen?.() ? (
|
|
|
|
<Overlay position='left-2 top-2'>
|
|
|
|
<BadgeHelp topic={helpTopic} className={clsx(PARAMETER.TOOLTIP_WIDTH, 'sm:max-w-[40rem]')} padding='p-0' />
|
|
|
|
</Overlay>
|
|
|
|
) : null}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
{header ? <h1 className='px-12 py-2 select-none'>{header}</h1> : null}
|
|
|
|
|
|
|
|
<div
|
2024-09-06 13:49:36 +03:00
|
|
|
className={clsx(
|
|
|
|
'overscroll-contain max-h-[calc(100svh-8rem)] max-w-[100svw] xs:max-w-[calc(100svw-2rem)]',
|
|
|
|
{
|
|
|
|
'overflow-auto': !overflowVisible,
|
|
|
|
'overflow-visible': overflowVisible
|
|
|
|
},
|
|
|
|
className
|
|
|
|
)}
|
2024-06-07 20:17:03 +03:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
|
2024-09-10 17:40:30 +03:00
|
|
|
<div className='z-modalControls my-2 flex gap-12 justify-center text-sm'>
|
2024-06-07 20:17:03 +03:00
|
|
|
{!readonly ? (
|
|
|
|
<Button
|
|
|
|
autoFocus
|
|
|
|
text={submitText}
|
|
|
|
title={!canSubmit ? submitInvalidTooltip : ''}
|
2024-09-10 17:40:30 +03:00
|
|
|
className='min-w-[7rem]'
|
2024-06-07 20:17:03 +03:00
|
|
|
colors='clr-btn-primary'
|
|
|
|
disabled={!canSubmit}
|
|
|
|
onClick={handleSubmit}
|
|
|
|
/>
|
|
|
|
) : null}
|
2024-09-10 17:40:30 +03:00
|
|
|
<Button text={readonly ? 'Закрыть' : 'Отмена'} className='min-w-[7rem]' onClick={handleCancel} />
|
2024-06-07 20:17:03 +03:00
|
|
|
</div>
|
|
|
|
</motion.div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Modal;
|