/* eslint-disable jsx-a11y/no-static-element-interactions */ /* eslint-disable jsx-a11y/click-events-have-key-events */ import noop from "lodash"; import { PropsWithChildren, useEffect, useRef, useState } from "react"; export function useModal() { const [show, setShowModal] = useState(false); return { show, setShowModal, openModal() { setShowModal(true); }, closeModal() { setShowModal(false); } }; } export interface ModalProps extends Record { show?: boolean; closeModal?: () => void; onClose?: () => void; footer?: any; title?: string; } export function Modal({ show, children, closeModal = noop as any, onClose = noop as any, title, footer: ModalFooter, style, className = "", ...props }: PropsWithChildren) { const titleRef: any = useRef(); const footerRef: any = useRef(); const [maxHeight, setMaxHeight] = useState(); const onClickClose = () => { closeModal(); onClose(); }; useEffect(() => { if (titleRef.current || footerRef.current) { const height = (titleRef?.current?.offsetHeight || 0) + (footerRef?.current?.offsetHeight || 0); setMaxHeight(`calc(85vh - ${height}px)`); } else { setMaxHeight("auto"); } }, [show]); if (!show) { return null; } return (
{title ? (
{title}
) : null}
{show && children}
{ModalFooter ? (
) : null}
); }