import type { DialogSize } from "@yaakapp-internal/plugins"; import { Heading, HStack, Overlay } from "@yaakapp-internal/ui"; import classNames from "classnames"; import * as m from "motion/react-m"; import type { ReactNode } from "react"; import { createContext, useContext, useMemo, useState } from "react"; import { createPortal } from "react-dom"; import { Button, type ButtonProps } from "./Button"; import { IconButton } from "./IconButton"; export interface DialogProps { children: ReactNode; open: boolean; onClose?: () => void; /** Block dismissal from the backdrop, Escape key, and built-in close button. */ disableClose?: boolean; title?: ReactNode; description?: ReactNode; className?: string; size?: DialogSize; /** Hide the built-in close button without changing backdrop or Escape behavior. */ hideX?: boolean; noPadding?: boolean; noScroll?: boolean; vAlign?: "top" | "center"; } export function Dialog({ children, className, size = "full", open, onClose, disableClose, title, description, hideX, noPadding, noScroll, vAlign = "center", }: DialogProps) { const titleId = useMemo(() => Math.random().toString(36).slice(2), []); const [footerEl, setFooterEl] = useState(null); const descriptionId = useMemo( () => (description ? Math.random().toString(36).slice(2) : undefined), [description], ); return (
{ // NOTE: We handle Escape on the element itself so that it doesn't close multiple // dialogs and can be intercepted by children if needed. if (e.key === "Escape") { if (!disableClose) onClose?.(); e.stopPropagation(); e.preventDefault(); } }} > {title ? ( {title} ) : ( )} {description ? (
{description}
) : ( )}
{children}
{/*Put close at the end so that it's the last thing to be tabbed to*/} {!disableClose && !hideX && (
)}
); } const DialogFooterSlot = createContext(null); export interface DialogFooterAction { label: string; onClick?: () => void; /** Submit the form with this id, which can be anywhere in the dialog body */ form?: string; color?: ButtonProps["color"]; variant?: ButtonProps["variant"]; disabled?: boolean; isLoading?: boolean; autoFocus?: boolean; leftSlot?: ReactNode; rightSlot?: ReactNode; } /** * The action row at the bottom of a dialog. Render it anywhere in the dialog's content and it is * placed below the scrolling body, outside its padding. `inline` drops the divider for small * dialogs whose body never scrolls. */ export function DialogFooter({ actions, leftSlot, inline, }: { actions: DialogFooterAction[]; leftSlot?: ReactNode; inline?: boolean; }) { const slot = useContext(DialogFooterSlot); const footer = ( ); return slot == null ? footer : createPortal(footer, slot); }