A dialog that opens on top of the entire page. The Dialog component extends Base UI's Dialog. It adds the Dialog.Header, Dialog.Body, and Dialog.Footer subcomponents, and the size and disableInteractions props on Dialog.Popup. For a confirmation that needs an explicit answer, use AlertDialog.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/dialog
// https://base-ui.com/react/components/dialog
import { Dialog as BaseDialog } from "@base-ui/react/dialog";
import clsx from "clsx";
import type React from "react";
import styles from "./Dialog.module.scss";
type DialogSize = "sm" | "md" | "lg" | "xl";
const Root = BaseDialog.Root;
const createHandle = BaseDialog.createHandle;
const Trigger = BaseDialog.Trigger;
const Portal = BaseDialog.Portal;
const Close = BaseDialog.Close;
function Backdrop({ className, ...props }: Dialog.Backdrop.Props) {
return (
<BaseDialog.Backdrop
className={clsx(styles.backdrop, className)}
{...props}
/>
);
}
function blockKeyboard(event: React.KeyboardEvent) {
event.preventDefault();
event.stopPropagation();
}
function Popup({
className,
size = "md",
disableInteractions = false,
...props
}: Dialog.Popup.Props) {
return (
<BaseDialog.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 }: Dialog.Title.Props) {
return (
<BaseDialog.Title className={clsx(styles.title, className)} {...props} />
);
}
function Description({ className, ...props }: Dialog.Description.Props) {
return (
<BaseDialog.Description
className={clsx(styles.description, className)}
{...props}
/>
);
}
function Header({ className, ...props }: Dialog.Header.Props) {
return <div className={clsx(styles.header, className)} {...props} />;
}
function Footer({ className, ...props }: Dialog.Footer.Props) {
return <div className={clsx(styles.footer, className)} {...props} />;
}
function Body({ className, ...props }: Dialog.Body.Props) {
return <div className={clsx(styles.body, className)} {...props} />;
}
const Dialog = {
Root,
createHandle,
Trigger,
Portal,
Close,
Backdrop,
Popup,
Header,
Footer,
Title,
Description,
Body,
};
namespace Dialog {
export type Size = DialogSize;
export namespace Root {
export type Props = BaseDialog.Root.Props;
}
export namespace Trigger {
export type Props = BaseDialog.Trigger.Props;
}
export namespace Portal {
export type Props = BaseDialog.Portal.Props;
}
export namespace Close {
export type Props = BaseDialog.Close.Props;
}
export namespace Backdrop {
export type Props = Omit<BaseDialog.Backdrop.Props, "className"> & {
className?: string;
};
}
export namespace Popup {
export interface Props extends Omit<BaseDialog.Popup.Props, "className"> {
className?: string;
size?: DialogSize;
disableInteractions?: boolean;
}
}
export namespace Title {
export type Props = Omit<BaseDialog.Title.Props, "className"> & {
className?: string;
};
}
export namespace Description {
export type Props = Omit<BaseDialog.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 Dialog;Use Dialog.Trigger to open the dialog and Dialog.Close to dismiss it.
Set size on Dialog.Popup to sm, md, lg, or xl. The default is md.
Use open and onOpenChange to control the dialog without a Dialog.Trigger.
Place a child Dialog.Root inside the parent, give its popup a smaller size, and keep the stack to two levels. Base UI renders no second backdrop for the child.
Set disableInteractions on Dialog.Popup while an action is pending, and ignore close requests in onOpenChange until it completes.