import { forwardRef } from "react"; import type { HTMLAttributes, ReactNode } from "react"; import { createPortal } from "react-dom"; import { XMarkIcon } from "@navikt/aksel-icons"; import { cn } from "../util/cn"; export interface ModalProps extends HTMLAttributes { /** Whether the modal is rendered. */ open: boolean; /** Called when the user closes the modal via the close button or backdrop. */ onClose?: () => void; /** Accessible label for the dialog. */ label?: string; /** * Close when clicking the backdrop. * @default true */ closeOnBackdropClick?: boolean; children: ReactNode; } export const Modal = forwardRef(function Modal( { open, onClose, label, closeOnBackdropClick = true, className, children, ...rest }, ref, ) { if (!open) { return null; } return createPortal(
{ if (closeOnBackdropClick && event.target === event.currentTarget) { onClose?.(); } }} >
{children}
, document.body, ); });