"use client"; import { createContext, useContext, useEffect, useRef, type ReactNode, type CSSProperties, } from "react"; let scrollLocks = 0, previousOverflow = ""; const DialogContext = createContext<(() => void) | null>(null); /** Native modality supplies inert background, focus containment and nested-dialog order. */ export function Dialog({ open, onOpenChange, children, label, className = "", style, }: { open: boolean; onOpenChange: (open: boolean) => void; children: ReactNode; label: string; className?: string; style?: CSSProperties; }) { const ref = useRef(null); const change = useRef(onOpenChange); useEffect(() => { change.current = onOpenChange; }); useEffect(() => { const dialog = ref.current; if (!dialog || !open) return; const restore = document.activeElement as HTMLElement | null; dialog.showModal(); if (!dialog.contains(document.activeElement)) ( dialog.querySelector("[autofocus]") ?? dialog.querySelector( "button:enabled, input:enabled, select:enabled, textarea:enabled, a[href], [tabindex='0']", ) ?? dialog ).focus(); if (scrollLocks++ === 0) { previousOverflow = document.body.style.overflow; document.body.style.overflow = "hidden"; } return () => { dialog.close(); if (--scrollLocks === 0) document.body.style.overflow = previousOverflow; if (restore?.isConnected) restore.focus(); }; }, [open]); return ( { e.preventDefault(); change.current(false); }} onClick={(e) => { if (e.target !== e.currentTarget) return; const r = e.currentTarget.getBoundingClientRect(); if ( e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom ) change.current(false); }} > {open && ( change.current(false)}> {children} )} ); } export function DialogTitle({ children }: { children: ReactNode }) { return

{children}

; } export function DialogBody({ children }: { children: ReactNode }) { return
{children}
; } export function DialogFooter({ children }: { children: ReactNode }) { return
{children}
; } export function DialogClose() { const close = useContext(DialogContext); return ( ); }