import type { JSX } from "@solidjs/web"; import { type Accessor, createContext, useContext } from "solid-js"; import { createPopupCore } from "../internals/create-popup-core"; import type { MaybeAccessor } from "../internals/maybe-accessor"; import { SNAPSHOT_KEYS, type Snapshottable, snapshotKeys, } from "../internals/observability/snapshot"; /** * Why the alert dialog's open state changed. * * Alert dialogs cannot be dismissed by an outside press or Escape without * an explicit action, so those reasons are excluded from the public surface. * * - `"trigger-press"` — trigger was clicked or keyboard-activated. * - `"close-button"` — `` / an action button was pressed. * - `"imperative"` — `ad.setOpen()` called programmatically. */ export type AlertDialogChangeReason = | "trigger-press" | "close-button" | "imperative"; /** * Options for {@link createAlertDialog}. */ export interface CreateAlertDialogOptions { /** * Open state. Consumer-owned signal. */ open: MaybeAccessor; /** * Whether interaction is suppressed. * @default false */ disabled?: MaybeAccessor; /** * Pre-change callback fired via `runWithOwner(null, ...)`. */ onOpenChange?: ( open: boolean, event: Event, reason: AlertDialogChangeReason, ) => void; } /** * Public API returned by {@link createAlertDialog}. */ export interface AlertDialogApi { /** Reactive accessor for the current open state. */ open: Accessor; /** Whether interaction is suppressed. */ disabled: Accessor; /** * Alert dialogs are always modal and never close on outside press — * this is a fixed behavioral invariant, not a knob. */ readonly modal: true; readonly disablePointerDismissal: true; /** Imperative open setter. */ setOpen: ( next: boolean, event?: Event, reason?: AlertDialogChangeReason, ) => void; /** Flip open (no-op when disabled). */ toggle: (event?: Event, reason?: AlertDialogChangeReason) => void; /** Context provider. */ Provider: (props: { children: JSX.Element }) => JSX.Element; } const AlertDialogContext = createContext(null); /** * Read the nearest {@link AlertDialogApi}. Throws unless `optional` is true. */ export function useAlertDialog(): AlertDialogApi; export function useAlertDialog(optional: true): AlertDialogApi | null; export function useAlertDialog(optional?: boolean): AlertDialogApi | null { const ctx = useContext(AlertDialogContext); if (ctx == null && !optional) { throw new Error( "Base UI: useAlertDialog() must be called inside an .", ); } return ctx; } /** * Headless primitive for an alert-dialog compound. Always modal, always * disables outside-press dismissal — both are fixed invariants of the * ARIA `alertdialog` role. DOM machinery lives in ``. */ export function createAlertDialog( options: CreateAlertDialogOptions, ): AlertDialogApi { const core = createPopupCore({ open: options.open, disabled: options.disabled, onOpenChange: options.onOpenChange, }); const api: AlertDialogApi = { open: core.open, disabled: core.disabled, modal: true, disablePointerDismissal: true, setOpen: core.setOpen, toggle: core.toggle, Provider: (props) => ( {props.children} ), }; (api as AlertDialogApi & Snapshottable)[SNAPSHOT_KEYS] = ALERT_DIALOG_SNAPSHOT_KEYS; return api; } const ALERT_DIALOG_SNAPSHOT_KEYS = snapshotKeys( "open", "disabled", );