ConceptPortal-public/rsconcept/frontend/src/components/ui/Modal.tsx

120 lines
3.0 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
import { motion } from 'framer-motion';
2023-07-25 20:27:29 +03:00
import { useRef } from 'react';
import useEscapeKey from '@/hooks/useEscapeKey';
import { animateModal } from '@/styling/animations';
2024-03-09 16:40:10 +03:00
import { prepareTooltip } from '@/utils/labels';
import { IconClose } from '../Icons';
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';
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-28 14:04:44 +03:00
readonly?: boolean;
canSubmit?: boolean;
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,
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
};
const handleSubmit = () => {
2023-07-25 22:29:33 +03:00
hideWindow();
2023-08-08 23:04:21 +03:00
if (onSubmit) onSubmit();
};
2023-12-04 14:19:54 +03:00
return (
2023-12-28 14:04:44 +03:00
<>
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}
className={clsx(
2023-12-28 14:04:44 +03:00
'z-modal',
'fixed 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
>
<Overlay position='right-2 top-2'>
2024-03-09 16:40:10 +03:00
<MiniButton
noPadding
2024-03-09 16:40:10 +03:00
titleHtml={prepareTooltip('Закрыть диалоговое окно', 'ESC')}
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>
</>
);
2023-07-22 12:24:14 +03:00
}
2023-12-28 14:04:44 +03:00
export default Modal;