/* eslint-disable react-hooks/exhaustive-deps */ "use client"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { X } from "lucide-react"; import Image from "next/image"; import * as React from "react"; import { useEffect } from "react"; // icons import errorIcon from "@/assets/svg/icons/error.svg"; import successIcon from "@/assets/svg/icons/success.svg"; import warningIcon from "@/assets/svg/icons/warning.svg"; // utils import { cn } from "@/libs/utils"; import { useModalStore } from "@/store/modal-store/use-modal-store"; import type { ButtonProps } from "../button/button"; import { Button } from "../button/button"; import { Dialog, DialogClose, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, } from "../dialog/dialog"; import styles from "./modal.module.css"; const ModalRoot = Dialog; const ModalTrigger = DialogTrigger; const ModalPortal = DialogPortal; const ModalClose = DialogClose; const ModalOverlay = DialogOverlay; ModalOverlay.displayName = "ModalOverlay"; const ModalHeader = DialogHeader; ModalHeader.displayName = "ModalHeader"; const ModalTitle = DialogTitle; ModalTitle.displayName = "ModalTitle"; const ModalFooter = DialogFooter; ModalFooter.displayName = "ModalFooter"; const ModalDescription = DialogDescription; ModalDescription.displayName = "ModalDescription"; const ModalContentTypeIcons = { success: successIcon, warning: warningIcon, error: errorIcon, }; interface ModalContentProps extends React.ComponentPropsWithoutRef { title?: string; description?: string; closable?: boolean; size?: | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "5xl" | "6xl" | "7xl" | "full"; onOkText?: string; onCancelText?: string; variant?: undefined | "success" | "warning" | "error"; okVariant?: ButtonProps["variant"]; cancelVariant?: ButtonProps["variant"]; header?: React.ReactNode; footer?: React.ReactNode; onClose?: () => void; onOk?: () => void; onCancel?: () => void; shouldCloseOnOverlayClick?: boolean; } const ModalContent = React.forwardRef< React.ElementRef, ModalContentProps >( ( { className, title, description, children, closable = true, size = "sm", onOkText, onCancelText, variant, okVariant = "primary", cancelVariant = "outline", header, footer, onOk, onCancel, onClose, shouldCloseOnOverlayClick = true, ...props }, ref, ) => ( { if (onClose && shouldCloseOnOverlayClick) { onClose(); } return false; }} /> {/* Modal Head Tool Area - This field includes customized icon and close button */} {variant && (
{`${variant} {/* @todo close button's visibility'll be dynamic */} {closable && ( )}
)} {/* Header area */} {header || ( {title && {title}} {description && {description}} )} {children} {/* Footer area */} {footer || ( {/* @todo Add translation here */} {onCancel && ( )} {/* @todo Add translation here */} )} {/* Close Button - If there is no icon this'll set on here */} {!variant && closable && ( )}
), ); ModalContent.displayName = DialogPrimitive.Content.displayName; export interface ModalProps extends ModalContentProps { open: boolean; urlHash?: string; } function Modal({ open, urlHash, ...props }: ModalProps) { const { openModal } = useModalStore(); useEffect(() => { if (open && urlHash) { const modalSlug = `#${urlHash}-modal`; openModal(modalSlug, props.onClose); } }, [open, urlHash]); return ( ); } export { Modal, ModalClose, ModalContent, ModalDescription, ModalFooter, ModalHeader, ModalOverlay, ModalPortal, ModalTitle, ModalTrigger, };