"use client"; import * as React from "react"; import { XIcon } from "lucide-react"; import { AlertDialog as AlertDialogPrimitive } from "radix-ui"; import { usePortalContainer } from "../context"; import { cn } from "../lib/cn"; import { buttonVariants } from "./button"; import { useCloseLabel } from "./close-label"; import { CORNER_CLOSE } from "./dialog"; import { revealOpeningFocus } from "./reveal-opening-focus"; /** * What the X in the corner needs from the dialog: a way to close it, and * whether "Annulla" can be pressed. Radix's AlertDialog has no Close of its * own, and a second Cancel would take the opening focus from "Annulla" (Radix * keeps one ref for it), so the X closes through the root's `onOpenChange`: * the same path as Esc, and the focus goes back where "Annulla" sends it. */ const CornerCloseContext = React.createContext<{ close: () => void; cancelDisabled: boolean; setCancelDisabled: (disabled: boolean) => void; } | null>(null); function AlertDialog({ open: openProp, defaultOpen = false, onOpenChange, ...props }: React.ComponentProps) { const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen); const controlled = openProp !== undefined; const open = controlled ? openProp : uncontrolledOpen; const setOpen = React.useCallback( (next: boolean) => { if (!controlled) setUncontrolledOpen(next); onOpenChange?.(next); }, [controlled, onOpenChange], ); const [cancelDisabled, setCancelDisabled] = React.useState(false); const corner = React.useMemo( () => ({ close: () => setOpen(false), cancelDisabled, setCancelDisabled }), [setOpen, cancelDisabled], ); return ( ); } function AlertDialogTrigger({ ...props }: React.ComponentProps) { return ( ); } function AlertDialogPortal({ ...props }: React.ComponentProps) { const container = usePortalContainer(); return ( ); } function AlertDialogOverlay({ className, ...props }: React.ComponentProps) { return ( ); } function AlertDialogContent({ className, children, onOpenAutoFocus, ...props }: React.ComponentProps) { const closeLabel = useCloseLabel(); const corner = React.useContext(CornerCloseContext); return ( [data-corner-close])_[data-slot=alert-dialog-title]]:pr-10", className, )} {...props} onOpenAutoFocus={revealOpeningFocus(onOpenAutoFocus)} > {children} {/* Every dialog has the X since 2026-10-21, a confirmation too. It can be pressed when "Annulla" can: not while the answer is being carried out. */} {corner && ( )} ); } function AlertDialogHeader({ className, ...props }: React.ComponentProps<"div">) { return (
); } function AlertDialogFooter({ className, ...props }: React.ComponentProps<"div">) { return (
:where(a,button)]:h-12 [&>:where(a,button)]:px-5", className, )} {...props} /> ); } function AlertDialogTitle({ className, ...props }: React.ComponentProps) { return ( ); } function AlertDialogDescription({ className, ...props }: React.ComponentProps) { return ( ); } function AlertDialogAction({ className, ...props }: React.ComponentProps) { return ( ); } function AlertDialogCancel({ className, ...props }: React.ComponentProps) { // The X follows "Annulla": held while it is. const setCancelDisabled = React.useContext(CornerCloseContext)?.setCancelDisabled; const disabled = Boolean(props.disabled) || props["aria-disabled"] === true || props["aria-disabled"] === "true"; React.useEffect(() => { if (!setCancelDisabled) return; setCancelDisabled(disabled); return () => setCancelDisabled(false); }, [setCancelDisabled, disabled]); return ( ); } export { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogOverlay, AlertDialogPortal, AlertDialogTitle, AlertDialogTrigger, };