Dialog
A modal dialog with focus management, keyboard dismissal and a labelled title.
Give your AI assistant this page as context.
Connect your library with MCPui/dialogUsage
Get this component from a saved library export or the connected CLI. The imports below refer to local files installed in your app.
Import styles/components.css, then styles/theme.css once. Wrap your app in ThemeScope from components/ui/utils so typography, focus and overlays share the theme. The installation guide covers runtime dependencies and React/Next.js setup.
import { Dialog } from './components/ui/dialog';
<Dialog trigger="Open dialog" title="Workspace settings" description="Manage the details of your workspace."><Input aria-label="Workspace name" placeholder="Acme"/></Dialog>Design system
This component consumes your semantic colors, spacing, typography and shape tokens. Import the compiled styles and your theme once as described in Export.
Dependencies: Input.
Behavior and accessibility
- Focus trap
- Escape dismisses
- Focus returns to trigger
Status: experimental during early access. Sample previews do not send emails, process payments or upload files to a service.
Source and props
This is the exported source, using the same implementation as the editor.
"use client";
import { Dialog as Base } from "@base-ui/react/dialog";
import type { ComponentProps, ReactNode } from "react";
import { cx, usePortalContainer } from "./utils";
export function Dialog({
trigger,
triggerRender,
title,
description,
children,
open,
onOpenChange,
initialFocus,
finalFocus,
className,
footer,
closeLabel = "Close",
size = "default",
...rootProps
}: Omit<ComponentProps<typeof Base.Root>, "children"> & {
size?: "default" | "wide";
trigger?: ReactNode;
/** Replace the trigger element without nesting interactive controls. */
triggerRender?: ComponentProps<typeof Base.Trigger>["render"];
className?: string;
title: string;
description?: string;
children: ReactNode;
initialFocus?: ComponentProps<typeof Base.Popup>["initialFocus"];
finalFocus?: ComponentProps<typeof Base.Popup>["finalFocus"];
footer?: ReactNode;
closeLabel?: string;
}) {
const container = usePortalContainer();
return (
<Base.Root {...rootProps} open={open} onOpenChange={onOpenChange}>
{(trigger != null || triggerRender != null) && (
<Base.Trigger
render={triggerRender}
className="cr-button"
data-variant="outline"
>
{trigger}
</Base.Trigger>
)}
<Base.Portal container={container}>
<Base.Backdrop className="cr-backdrop" />
<Base.Viewport className="cr-modal-viewport" data-kind="dialog">
<Base.Popup
className={cx("cr-modal cr-dialog", className)}
data-size={size}
initialFocus={initialFocus}
finalFocus={finalFocus}
>
<header className="cr-modal-header">
<Base.Title className="cr-modal-title">{title}</Base.Title>
{description && (
<Base.Description className="cr-description">
{description}
</Base.Description>
)}
<Base.Close
className="cr-modal-close"
aria-label={
closeLabel === "Close" ? "Close dialog" : closeLabel
}
>
<svg
width="18"
height="18"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.75"
strokeLinecap="round"
aria-hidden="true"
>
<path d="m6 6 12 12M18 6 6 18" />
</svg>
</Base.Close>
</header>
<div className="cr-modal-content">{children}</div>
{footer !== null && (
<footer className="cr-modal-footer">
{footer ?? (
<Base.Close className="cr-button" data-variant="secondary">
{closeLabel}
</Base.Close>
)}
</footer>
)}
</Base.Popup>
</Base.Viewport>
</Base.Portal>
</Base.Root>
);
}