A dialog that requires a user response to proceed. The AlertDialog component extends Base UI's Alert Dialog. It adds the AlertDialog.Header, AlertDialog.Body, and AlertDialog.Footer subcomponents, and the size and disableInteractions props on AlertDialog.Popup.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/alert-dialog
// https://base-ui.com/react/components/alert-dialog
import { AlertDialog as BaseAlertDialog } from "@base-ui/react/alert-dialog";
import clsx from "clsx";
import type React from "react";
import styles from "./AlertDialog.module.scss";
type DialogSize = "sm" | "md" | "lg" | "xl";
const Root = BaseAlertDialog.Root;
const createHandle = BaseAlertDialog.createHandle;
const Trigger = BaseAlertDialog.Trigger;
const Portal = BaseAlertDialog.Portal;
const Close = BaseAlertDialog.Close;
function Backdrop({ className, ...props }: AlertDialog.Backdrop.Props) {
return (
<BaseAlertDialog.Backdrop
className={clsx(styles.backdrop, className)}
{...props}
/>
);
}
function blockKeyboard(event: React.KeyboardEvent) {
event.preventDefault();
event.stopPropagation();
}
function Popup({
className,
size = "md",
disableInteractions = false,
...props
}: AlertDialog.Popup.Props) {
return (
<BaseAlertDialog.Popup
className={clsx(
styles.popup,
size === "sm" && styles.popupSm,
size === "md" && styles.popupMd,
size === "lg" && styles.popupLg,
size === "xl" && styles.popupXl,
disableInteractions && styles.popupNonInteractive,
className,
)}
aria-busy={disableInteractions || undefined}
{...props}
onKeyDownCapture={
disableInteractions ? blockKeyboard : props.onKeyDownCapture
}
/>
);
}
function Title({ className, ...props }: AlertDialog.Title.Props) {
return (
<BaseAlertDialog.Title
className={clsx(styles.title, className)}
{...props}
/>
);
}
function Description({ className, ...props }: AlertDialog.Description.Props) {
return (
<BaseAlertDialog.Description
className={clsx(styles.description, className)}
{...props}
/>
);
}
function Header({ className, ...props }: AlertDialog.Header.Props) {
return <div className={clsx(styles.header, className)} {...props} />;
}
function Footer({ className, ...props }: AlertDialog.Footer.Props) {
return <div className={clsx(styles.footer, className)} {...props} />;
}
function Body({ className, ...props }: AlertDialog.Body.Props) {
return <div className={clsx(styles.body, className)} {...props} />;
}
const AlertDialog = {
Root,
createHandle,
Trigger,
Portal,
Close,
Backdrop,
Popup,
Header,
Footer,
Title,
Description,
Body,
};
namespace AlertDialog {
export type Size = DialogSize;
export namespace Root {
export type Props = BaseAlertDialog.Root.Props;
}
export namespace Trigger {
export type Props = BaseAlertDialog.Trigger.Props;
}
export namespace Portal {
export type Props = BaseAlertDialog.Portal.Props;
}
export namespace Close {
export type Props = BaseAlertDialog.Close.Props;
}
export namespace Backdrop {
export type Props = Omit<BaseAlertDialog.Backdrop.Props, "className"> & {
className?: string;
};
}
export namespace Popup {
export interface Props extends Omit<
BaseAlertDialog.Popup.Props,
"className"
> {
className?: string;
size?: DialogSize;
disableInteractions?: boolean;
}
}
export namespace Title {
export type Props = Omit<BaseAlertDialog.Title.Props, "className"> & {
className?: string;
};
}
export namespace Description {
export type Props = Omit<BaseAlertDialog.Description.Props, "className"> & {
className?: string;
};
}
export namespace Header {
export type Props = React.ComponentProps<"div">;
}
export namespace Footer {
export type Props = React.ComponentProps<"div">;
}
export namespace Body {
export type Props = React.ComponentProps<"div">;
}
}
export default AlertDialog;Place AlertDialog.Trigger inside AlertDialog.Root to open the dialog.
Set size on AlertDialog.Popup to sm, md, lg, or xl. The default is md.
Create a handle with AlertDialog.createHandle() to place the trigger anywhere in the tree.
Control the dialog with open and onOpenChange on AlertDialog.Root, and ignore close requests while the submission is pending. Set disableInteractions on AlertDialog.Popup until it completes.
alignItemWithTrigger={false} to Select.Positioner. Otherwise the popup reserves empty space below the last item.