2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-12-25 16:53:27 +03:00
|
|
|
import { motion } from 'framer-motion';
|
2023-07-25 20:27:29 +03:00
|
|
|
import { useRef } from 'react';
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
import useEscapeKey from '@/hooks/useEscapeKey';
|
2024-01-06 03:15:02 +03:00
|
|
|
import { animateModal } from '@/styling/animations';
|
2024-03-09 16:40:10 +03:00
|
|
|
import { prepareTooltip } from '@/utils/labels';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
2024-04-03 21:05:53 +03:00
|
|
|
import { IconClose } from '../Icons';
|
2023-12-18 19:42:27 +03:00
|
|
|
import { CProps } from '../props';
|
2023-07-25 20:27:29 +03:00
|
|
|
import Button from './Button';
|
2023-12-04 14:19:54 +03:00
|
|
|
import MiniButton from './MiniButton';
|
2023-12-05 01:22:44 +03:00
|
|
|
import Overlay from './Overlay';
|
2023-07-22 12:24:14 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export interface ModalProps extends CProps.Styling {
|
|
|
|
header?: string;
|
|
|
|
submitText?: string;
|
|
|
|
submitInvalidTooltip?: string;
|
2023-12-18 19:42:27 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
readonly?: boolean;
|
|
|
|
canSubmit?: boolean;
|
2023-12-18 19:42:27 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
hideWindow: () => void;
|
|
|
|
onSubmit?: () => void;
|
|
|
|
onCancel?: () => void;
|
|
|
|
|
|
|
|
children: React.ReactNode;
|
2023-07-22 12:24:14 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
function Modal({
|
|
|
|
header,
|
|
|
|
hideWindow,
|
|
|
|
onSubmit,
|
|
|
|
readonly,
|
|
|
|
onCancel,
|
|
|
|
canSubmit,
|
|
|
|
submitInvalidTooltip,
|
|
|
|
className,
|
2023-08-23 01:36:17 +03:00
|
|
|
children,
|
2023-12-18 19:42:27 +03:00
|
|
|
submitText = 'Продолжить',
|
|
|
|
...restProps
|
2023-08-23 01:36:17 +03:00
|
|
|
}: ModalProps) {
|
2023-07-22 12:24:14 +03:00
|
|
|
const ref = useRef(null);
|
2023-07-25 22:29:33 +03:00
|
|
|
useEscapeKey(hideWindow);
|
2023-07-22 12:24:14 +03:00
|
|
|
|
|
|
|
const handleCancel = () => {
|
2023-07-25 22:29:33 +03:00
|
|
|
hideWindow();
|
2023-07-25 20:27:29 +03:00
|
|
|
if (onCancel) onCancel();
|
2023-07-22 12:24:14 +03:00
|
|
|
};
|
|
|
|
|
2023-07-23 15:23:01 +03:00
|
|
|
const handleSubmit = () => {
|
2023-07-25 22:29:33 +03:00
|
|
|
hideWindow();
|
2023-08-08 23:04:21 +03:00
|
|
|
if (onSubmit) onSubmit();
|
2023-07-23 15:23:01 +03:00
|
|
|
};
|
|
|
|
|
2023-12-04 14:19:54 +03:00
|
|
|
return (
|
2024-05-10 15:28:33 +03:00
|
|
|
<div className='fixed top-0 left-0 w-full h-full z-modal'>
|
2024-04-03 21:53:11 +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')} />
|
2023-12-28 14:04:44 +03:00
|
|
|
<motion.div
|
|
|
|
ref={ref}
|
2024-05-10 15:28:33 +03:00
|
|
|
className={clsx(
|
|
|
|
'z-modal',
|
|
|
|
'absolute bottom-1/2 left-1/2 -translate-x-1/2 translate-y-1/2',
|
|
|
|
'border shadow-md',
|
|
|
|
'clr-app'
|
|
|
|
)}
|
2023-12-28 14:04:44 +03:00
|
|
|
initial={{ ...animateModal.initial }}
|
|
|
|
animate={{ ...animateModal.animate }}
|
|
|
|
exit={{ ...animateModal.exit }}
|
|
|
|
{...restProps}
|
2023-12-07 01:21:27 +03:00
|
|
|
>
|
2024-03-27 22:54:24 +03:00
|
|
|
<Overlay position='right-2 top-2'>
|
2024-03-09 16:40:10 +03:00
|
|
|
<MiniButton
|
2024-03-27 22:54:24 +03:00
|
|
|
noPadding
|
2024-03-09 16:40:10 +03:00
|
|
|
titleHtml={prepareTooltip('Закрыть диалоговое окно', 'ESC')}
|
2024-04-03 21:05:53 +03:00
|
|
|
icon={<IconClose size='1.25rem' />}
|
2024-03-09 16:40:10 +03:00
|
|
|
onClick={handleCancel}
|
|
|
|
/>
|
2023-12-28 14:04:44 +03:00
|
|
|
</Overlay>
|
|
|
|
|
|
|
|
{header ? <h1 className='px-12 py-2 select-none'>{header}</h1> : null}
|
|
|
|
|
|
|
|
<div
|
2024-05-02 21:34:47 +03:00
|
|
|
className={clsx('overflow-auto overscroll-contain', className)}
|
2023-12-28 14:04:44 +03:00
|
|
|
style={{
|
|
|
|
maxHeight: 'calc(100vh - 8rem)',
|
|
|
|
maxWidth: 'calc(100vw - 2rem)'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
2023-12-04 14:19:54 +03:00
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
<div className={clsx('z-modal-controls', 'px-6 py-3 flex gap-12 justify-center')}>
|
|
|
|
{!readonly ? (
|
|
|
|
<Button
|
|
|
|
autoFocus
|
|
|
|
text={submitText}
|
|
|
|
title={!canSubmit ? submitInvalidTooltip : ''}
|
|
|
|
className='min-w-[8rem] min-h-[2.6rem]'
|
|
|
|
colors='clr-btn-primary'
|
|
|
|
disabled={!canSubmit}
|
|
|
|
onClick={handleSubmit}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
<Button
|
|
|
|
text={readonly ? 'Закрыть' : 'Отмена'}
|
|
|
|
className='min-w-[8rem] min-h-[2.6rem]'
|
|
|
|
onClick={handleCancel}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</motion.div>
|
2024-05-10 15:28:33 +03:00
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-07-22 12:24:14 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default Modal;
|