|
|
|
@ -6,16 +6,23 @@ import { useLockBodyScroll } from '../../../hooks/useLockBodyScroll';
|
|
|
|
|
import LoadingSpinner from '../LoadingSpinner';
|
|
|
|
|
import useClickOutside from '../../../hooks/useClickOutside';
|
|
|
|
|
|
|
|
|
|
interface ModalProps {
|
|
|
|
|
interface ModalProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
|
|
|
title?: string;
|
|
|
|
|
onCancel?: (e?: MouseEvent<HTMLElement>) => void;
|
|
|
|
|
onOk?: (e: MouseEvent<HTMLButtonElement>) => void;
|
|
|
|
|
onOk?: (e?: MouseEvent<HTMLButtonElement>) => void;
|
|
|
|
|
onSecondary?: (e?: MouseEvent<HTMLButtonElement>) => void;
|
|
|
|
|
onTertiary?: (e?: MouseEvent<HTMLButtonElement>) => void;
|
|
|
|
|
cancelText?: string;
|
|
|
|
|
okText?: string;
|
|
|
|
|
secondaryText?: string;
|
|
|
|
|
tertiaryText?: string;
|
|
|
|
|
okDisabled?: boolean;
|
|
|
|
|
cancelButtonType?: ButtonType;
|
|
|
|
|
okButtonType?: ButtonType;
|
|
|
|
|
visible?: boolean;
|
|
|
|
|
secondaryButtonType?: ButtonType;
|
|
|
|
|
secondaryDisabled?: boolean;
|
|
|
|
|
tertiaryDisabled?: boolean;
|
|
|
|
|
tertiaryButtonType?: ButtonType;
|
|
|
|
|
disableScrollLock?: boolean;
|
|
|
|
|
backgroundClickable?: boolean;
|
|
|
|
|
iconSvg?: ReactNode;
|
|
|
|
@ -29,14 +36,22 @@ const Modal: React.FC<ModalProps> = ({
|
|
|
|
|
cancelText,
|
|
|
|
|
okText,
|
|
|
|
|
okDisabled = false,
|
|
|
|
|
cancelButtonType,
|
|
|
|
|
okButtonType,
|
|
|
|
|
cancelButtonType = 'default',
|
|
|
|
|
okButtonType = 'primary',
|
|
|
|
|
children,
|
|
|
|
|
visible,
|
|
|
|
|
disableScrollLock,
|
|
|
|
|
backgroundClickable = true,
|
|
|
|
|
iconSvg,
|
|
|
|
|
loading = false,
|
|
|
|
|
secondaryButtonType = 'default',
|
|
|
|
|
secondaryDisabled = false,
|
|
|
|
|
onSecondary,
|
|
|
|
|
secondaryText,
|
|
|
|
|
tertiaryButtonType = 'default',
|
|
|
|
|
tertiaryDisabled = false,
|
|
|
|
|
tertiaryText,
|
|
|
|
|
onTertiary,
|
|
|
|
|
...props
|
|
|
|
|
}) => {
|
|
|
|
|
const modalRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
useClickOutside(modalRef, () => {
|
|
|
|
@ -44,39 +59,26 @@ const Modal: React.FC<ModalProps> = ({
|
|
|
|
|
? onCancel()
|
|
|
|
|
: undefined;
|
|
|
|
|
});
|
|
|
|
|
useLockBodyScroll(!!visible, disableScrollLock);
|
|
|
|
|
const transitions = useTransition(visible, null, {
|
|
|
|
|
from: { opacity: 0, backdropFilter: 'blur(0px)' },
|
|
|
|
|
enter: { opacity: 1, backdropFilter: 'blur(3px)' },
|
|
|
|
|
leave: { opacity: 0, backdropFilter: 'blur(0px)' },
|
|
|
|
|
config: { tension: 500, velocity: 40, friction: 60 },
|
|
|
|
|
});
|
|
|
|
|
const containerTransitions = useTransition(visible && !loading, null, {
|
|
|
|
|
useLockBodyScroll(true, disableScrollLock);
|
|
|
|
|
const containerTransitions = useTransition(!loading, null, {
|
|
|
|
|
from: { opacity: 0, transform: 'scale(0.5)' },
|
|
|
|
|
enter: { opacity: 1, transform: 'scale(1)' },
|
|
|
|
|
leave: { opacity: 0, transform: 'scale(0.5)' },
|
|
|
|
|
config: { tension: 500, velocity: 40, friction: 60 },
|
|
|
|
|
});
|
|
|
|
|
const loadingTransitions = useTransition(visible && loading, null, {
|
|
|
|
|
const loadingTransitions = useTransition(loading, null, {
|
|
|
|
|
from: { opacity: 0, transform: 'scale(0.5)' },
|
|
|
|
|
enter: { opacity: 1, transform: 'scale(1)' },
|
|
|
|
|
leave: { opacity: 0, transform: 'scale(0.5)' },
|
|
|
|
|
config: { tension: 500, velocity: 40, friction: 60 },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const cancelType = cancelButtonType ?? 'default';
|
|
|
|
|
const okType = okButtonType ?? 'primary';
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{transitions.map(
|
|
|
|
|
({ props, item, key }) =>
|
|
|
|
|
item &&
|
|
|
|
|
ReactDOM.createPortal(
|
|
|
|
|
{ReactDOM.createPortal(
|
|
|
|
|
<animated.div
|
|
|
|
|
className="fixed top-0 left-0 right-0 bottom-0 bg-cool-gray-800 bg-opacity-50 w-full h-full z-50 flex justify-center items-center"
|
|
|
|
|
style={props}
|
|
|
|
|
key={key}
|
|
|
|
|
style={props.style}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Escape') {
|
|
|
|
|
typeof onCancel === 'function' && backgroundClickable
|
|
|
|
@ -132,11 +134,11 @@ const Modal: React.FC<ModalProps> = ({
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{(onCancel || onOk) && (
|
|
|
|
|
{(onCancel || onOk || onSecondary || onTertiary) && (
|
|
|
|
|
<div className="mt-5 sm:mt-4 flex justify-center sm:justify-start flex-row-reverse">
|
|
|
|
|
{typeof onOk === 'function' && (
|
|
|
|
|
<Button
|
|
|
|
|
buttonType={okType}
|
|
|
|
|
buttonType={okButtonType}
|
|
|
|
|
onClick={onOk}
|
|
|
|
|
className="ml-3"
|
|
|
|
|
disabled={okDisabled}
|
|
|
|
@ -144,9 +146,29 @@ const Modal: React.FC<ModalProps> = ({
|
|
|
|
|
{okText ? okText : 'Ok'}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
{typeof onSecondary === 'function' && secondaryText && (
|
|
|
|
|
<Button
|
|
|
|
|
buttonType={secondaryButtonType}
|
|
|
|
|
onClick={onSecondary}
|
|
|
|
|
className="ml-3"
|
|
|
|
|
disabled={secondaryDisabled}
|
|
|
|
|
>
|
|
|
|
|
{secondaryText}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
{typeof onTertiary === 'function' && tertiaryText && (
|
|
|
|
|
<Button
|
|
|
|
|
buttonType={tertiaryButtonType}
|
|
|
|
|
onClick={onTertiary}
|
|
|
|
|
className="ml-3"
|
|
|
|
|
disabled={tertiaryDisabled}
|
|
|
|
|
>
|
|
|
|
|
{tertiaryText}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
{typeof onCancel === 'function' && (
|
|
|
|
|
<Button
|
|
|
|
|
buttonType={cancelType}
|
|
|
|
|
buttonType={cancelButtonType}
|
|
|
|
|
onClick={onCancel}
|
|
|
|
|
className="ml-3 sm:ml-0 sm:px-4"
|
|
|
|
|
>
|
|
|
|
@ -160,7 +182,6 @@ const Modal: React.FC<ModalProps> = ({
|
|
|
|
|
)}
|
|
|
|
|
</animated.div>,
|
|
|
|
|
document.body
|
|
|
|
|
)
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|