@biem/ui

Alert Dialog

A modal dialog that requires users to confirm or cancel an action.

An alert dialog interrupts users before an action with consequences and blocks the page until they decide. Users close it with the cancel button or the Escape key, and a press outside the dialog doesn't close it. The action button doesn't close it on its own, so its click handler closes the alert dialog once the action has run.

Usage

alert-dialog-default.tsx
import {
	AlertDialog,
	AlertDialogAction,
	AlertDialogCancel,
	AlertDialogContent,
	AlertDialogDescription,
	AlertDialogFooter,
	AlertDialogHeader,
	AlertDialogTitle,
	AlertDialogTrigger,
} from "@biem/ui/components/ui/alert-dialog";
import { Button } from "@biem/ui/components/ui/button";

export function AlertDialogDefault() {
	return (
		<AlertDialog>
			<AlertDialogTrigger render={<Button variant="outline"/>}>Send transfer</AlertDialogTrigger>
			<AlertDialogContent>
				<AlertDialogHeader>
					<AlertDialogTitle>Send this transfer?</AlertDialogTitle>
					<AlertDialogDescription>
						The warehouse reserves 12 cases for the store and ships them on Monday.
					</AlertDialogDescription>
				</AlertDialogHeader>
				<AlertDialogFooter>
					<AlertDialogCancel>Cancel</AlertDialogCancel>
					<AlertDialogAction>Send transfer</AlertDialogAction>
				</AlertDialogFooter>
			</AlertDialogContent>
		</AlertDialog>
	);
}

Examples

Destructive

Deleting a supplier or a promotion removes its data for good. The action button signals this consequence through variant set to "destructive", so users recognize the risk while the cancel button still offers a way back.

Small

A short question needs little room, such as whether to print new shelf labels now. With size set to "sm", the alert dialog stays compact on larger screens as well, so users take in the question and both answers at a single glance.

Default Size

Most confirmations explain their consequence in a sentence or two. Unless another size is set, the alert dialog takes the size "default" and gives that explanation more room on larger screens, so users read it before they decide.

With Media

Users who confirm the same kinds of decisions every day recognize them faster by a symbol than by their wording. Through AlertDialogMedia, an icon or an image accompanies the question, and the title still names the decision where the symbol is unfamiliar.

Controlled

Many decisions take a moment to carry out, such as booking a stock correction, and the alert dialog should stay open until that work is done. With open and onOpenChange, the application holds the open state, closes the alert dialog once the action has run and can open it from other parts of the interface as well.

Disabled Trigger

An alert dialog belongs to an action that is not always available, such as closing a stock count while figures are still arriving. Marked as disabled, the trigger no longer opens the alert dialog, so users never face a decision they cannot make yet.

Disabled Cancel

While an action is already under way, such as a stock booking that has been sent, stepping back is no longer possible. Here, disabled keeps the cancel button from closing the alert dialog and shows users that the decision stands. The Escape key still closes it.

Disabled Action

Some decisions wait for a condition, for example a price change that a second person still has to approve. As long as the action button carries disabled, it accepts no interaction and exposes its state to assistive technology, while users can still leave through the cancel button.

API Reference

AlertDialog

Propopen
Typeboolean
Default–
DescriptionSets whether the alert dialog is open and makes it controlled. Update it in onOpenChange.
PropdefaultOpen
Typeboolean
Defaultfalse
DescriptionOpens the alert dialog at first render. The alert dialog then tracks its open state itself.
ProponOpenChange
Type(open: boolean, eventDetails: AlertDialogRootChangeEventDetails) => void
Default–
DescriptionRuns when users open or close the alert dialog. It receives the open state that follows.
ProponOpenChangeComplete
Type(open: boolean) => void
Default–
DescriptionRuns after the alert dialog has opened or closed and its animations have finished.
ProptriggerId
Typestring | null
Default–
DescriptionSets the ID of the trigger that a controlled alert dialog is associated with. An alert dialog without open doesn't need it.
PropdefaultTriggerId
Typestring | null
Default–
DescriptionSets the ID of the trigger that the alert dialog is associated with when defaultOpen opens it at first render.
Prophandle
TypeAlertDialogHandle<Payload>
Default–
DescriptionConnects the alert dialog with triggers that are rendered outside of it. Create the handle with AlertDialog.createHandle() from @base-ui/react/alert-dialog.
PropactionsRef
TypeRefObject<AlertDialogRootActions | null>
Default–
DescriptionReceives the functions close and unmount. close closes the alert dialog from code, and unmount removes it from the DOM after a closing animation that the code controls itself.
Propchildren
TypeReactNode | ((arg: { payload: Payload | undefined }) => ReactNode)
Default–
DescriptionHolds the parts of the alert dialog. A function receives the payload of the trigger that opened the alert dialog.

AlertDialogTrigger

Prophandle
TypeAlertDialogHandle<Payload>
Default–
DescriptionConnects the trigger with an alert dialog that it is rendered outside of. Pass the same handle to AlertDialog.
Proppayload
TypePayload
Default–
DescriptionPasses a value to the alert dialog when this trigger opens it. A function as children of AlertDialog receives it.
Propid
Typestring
Default–
DescriptionSets the ID of the trigger element. triggerId and defaultTriggerId of AlertDialog refer to it.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the trigger with another component.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents users from opening the alert dialog with this trigger.

AlertDialogPortal

PropkeepMounted
Typeboolean
Defaultfalse
DescriptionKeeps the portal in the DOM while the alert dialog is closed.
Propcontainer
TypeHTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null
Default–
DescriptionSets the element that the portal is rendered into. Without it, the portal is appended to the body.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the portal with another component.

AlertDialogOverlay

PropforceRender
Typeboolean
Defaultfalse
DescriptionRenders the overlay even when the alert dialog is nested in another dialog.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the overlay with another component.

AlertDialogContent

Propsize
Type"default" | "sm"
Default"default"
DescriptionControls the width of the alert dialog and the layout of its header and footer. With "sm", the alert dialog stays narrow, the header is centered and the two footer buttons share one row.
PropinitialFocus
Typeboolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | HTMLElement | null | void)
Default–
DescriptionDetermines the element that receives focus when the alert dialog opens. Without it, focus moves to the first focusable element in the alert dialog.
PropfinalFocus
Typeboolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void)
Default–
DescriptionDetermines the element that receives focus when the alert dialog closes. Without it, focus returns to the trigger or to the element that had focus before.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the content with another component.

AlertDialogHeader

AlertDialogHeader groups the media, the title and the description. It renders a div and has no props of its own.

AlertDialogMedia

AlertDialogMedia frames an icon or an image in the header. It renders a div and has no props of its own.

AlertDialogTitle

Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the title with another component.

AlertDialogDescription

Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the description with another component.

AlertDialogFooter

AlertDialogFooter holds the cancel button and the action button. It renders a div and has no props of its own.

AlertDialogCancel

Propvariant
Type"default" | "outline" | "secondary" | "ghost" | "destructive" | "link"
Default"outline"
DescriptionControls the visual emphasis and semantic role of the cancel button.
Propsize
Type"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg"
Default"default"
DescriptionControls the dimensions and spacing of the cancel button.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the button that the cancel part renders. variant and size then have no effect.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents users from closing the alert dialog with this button.

AlertDialogAction

Propvariant
Type"default" | "outline" | "secondary" | "ghost" | "destructive" | "link"
Default"default"
DescriptionControls the visual emphasis and semantic role of the action button.
Propsize
Type"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg"
Default"default"
DescriptionControls the dimensions and spacing of the action button.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the action button with another component.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents interaction and exposes the disabled state to assistive technology.
PropfocusableWhenDisabled
Typeboolean
Defaultfalse
DescriptionKeeps the action button focusable while it is disabled.