import { Dialog as BaseDialog } from '@base-ui/react/dialog' import { X } from 'lucide-react' import { useCallback, useEffect, useState, type ComponentProps, type ReactElement, type ReactNode, } from 'react' import { cn } from '../lib/cn' import { focusRing } from '../lib/styles' import { IconButton } from './Button' import { DIALOG_SIZES, backdropClass, dialogDescriptionClass, dialogPanelClass, dialogTitleClass, type DialogSize, } from './dialog.tokens' type OpenProps = { /** Controlled open state. Pair with `onOpenChange`. */ open?: boolean /** Initial open state when uncontrolled. */ defaultOpen?: boolean /** Fires with the next open state: trigger press, Escape, outside click or a close button. */ onOpenChange?: (open: boolean) => void } export type DialogRootProps = OpenProps & { /** Close when the backdrop is clicked. Escape always closes. */ closeOnOutsideClick?: boolean /** `false` lets the page behind stay interactive; the default traps focus and locks scroll. */ modal?: boolean children?: ReactNode } /** Holds the open state. Wrap Trigger and Content in it for a custom layout. */ function DialogRoot({ closeOnOutsideClick = true, modal = true, onOpenChange, children, ...props }: DialogRootProps) { return ( onOpenChange(open) : undefined} > {children} ) } /** * Opens the dialog. Pass your button as `render` (`Edit} />`) * so it keeps the Tansy styles. */ function DialogTrigger(props: BaseDialog.Trigger.Props) { return } export type DialogContentProps = Omit & { /** Panel width. `full` fills the screen. */ size?: DialogSize /** Hide the × button in the corner. Escape still closes. */ hideCloseButton?: boolean className?: string } /** Portal, backdrop and the panel. Put Header, Body and Footer inside. */ function DialogContent({ size = 'md', hideCloseButton = false, className, children, ...props }: DialogContentProps) { return ( {children} {!hideCloseButton && ( ) } /** Title and description block at the top. It stays put while the body scrolls. */ export function DialogHeader({ className, ...props }: ComponentProps<'div'>) { return
} /** The dialog's accessible name. Renders an `

`. */ function DialogTitle({ className, ...props }: Omit & { className?: string }) { return } /** Supporting text, announced with the title. */ function DialogDescription({ className, ...props }: Omit & { className?: string }) { return } /** * The scrolling middle of a dialog or drawer. When its content overflows it * becomes keyboard-focusable (so it can be scrolled without a mouse) and * draws a rule under the header. */ export function DialogBody({ className, ref, ...props }: ComponentProps<'div'>) { const [node, setNode] = useState(null) const [scrollable, setScrollable] = useState(false) const setRefs = useCallback( (element: HTMLDivElement | null) => { setNode(element) if (typeof ref === 'function') ref(element) else if (ref) ref.current = element }, [ref], ) useEffect(() => { if (!node) return const measure = () => setScrollable(node.scrollHeight > node.clientHeight + 1) measure() const observer = new ResizeObserver(measure) observer.observe(node) for (const child of Array.from(node.children)) observer.observe(child) return () => observer.disconnect() }, [node]) return (
) } /** Actions at the bottom, right-aligned. Stacks full-width on phones, primary action on top. */ export function DialogFooter({ className, ...props }: ComponentProps<'div'>) { return (
) } /** Closes the dialog. Pass a Button as `render`. */ function DialogClose(props: BaseDialog.Close.Props) { return } export type DialogProps = DialogRootProps & { /** Element that opens the dialog, usually a `