Web · Components
Dialog
Modal window that opens above the page.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
confirm(): a confirmation question in one line
Custom footer: the footer function receives close
Import
Modal window that opens above the page. It focuses the user on a single task; the page behind becomes inert and does not scroll.
Title, description, icon, body and buttons are given as props; no nested parts are written. The body is a form: onConfirm receives the values of its fields as FormData. If the body needs to close the window, pass children as a function: {({ close }) => …}.
Don't write a component just for an "Are you sure?" question; use confirm.
Built on the native <dialog> element: no z-index wars, focus is kept inside the window, returns to the opening button on close, and Esc closes it. Animations are CSS only.
Mobile: On small screens it automatically becomes a bottom sheet; it can be closed by pulling down and with the phone's back button, moves above the keyboard when it opens, and respects the bottom inset of notched screens.
Examples
confirm
functionAsks the user a confirmation question and waits for the answer. Returns true if confirmed, false if cancelled (Cancel, Esc, backdrop, back button).
The dialog has all the behaviors of Dialog (bottom sheet on mobile, closing with the back button…). If called several times at once, the questions are shown in order.
Requires UIProvider at the root of the app; without it, it falls back to the browser's own window.confirm dialog.
Examples
ConfirmOptions
interfaceOptions of the confirm function.
| Name | Type | Default | Description |
|---|---|---|---|
title | ReactNode | The question; becomes the dialog title. | |
description? | ReactNode | Description under the question (e.g. "This action cannot be undone."). | |
icon? | IconProp | Icon next to the title: a name or an element (e.g. "warning"). | |
confirmLabel? | ReactNode | "Onayla" | Text of the confirm button. |
cancelLabel? | ReactNode | "İptal" | Text of the cancel button. |
tone? | Tone | "primary" | Color of the confirm button. "danger" for irreversible actions such as deleting. |
onConfirm? | () => unknown | Runs on confirm, before the dialog closes. If it returns a Promise, the confirm button shows loading until it settles; if it returns false or throws, the dialog stays open. |
DialogSize
typeMaximum width of the Dialog window.
DialogProps
interfaceProps of the Dialog component.
| Name | Type | Default | Description |
|---|---|---|---|
size? | DialogSize | "md" | Maximum width of the window: sm 384 px, md 512 px, lg 672 px, xl 896 px. Always full width as a mobile bottom sheet. |
mobileSheet? | boolean | true | Turn into a bottom sheet on small screens (below 640 px)? If false, it opens centered on every screen. |
effect? | EffectProp | Effect or effects of the window surface (e.g. "beam", ["border", "glow"]). If omitted, UIProvider appearance.defaults.dialog.effect. See BuiltinEffect. | |
open? | boolean | Whether it is open when controlled. | |
defaultOpen? | boolean | false | Is it initially open when uncontrolled? |
onOpenChange? | (open: boolean) => void | Called with the new state when it opens or closes. | |
trigger? | ReactElement | Element that opens the window, usually a Button. The window opens on click; aria-expanded and aria-controls are added automatically. | |
title | ReactNode | Title. Screen readers announce the window with this name, so it is required. To hide it, use classNames={{ title: "sr-only" }}. | |
description? | ReactNode | Description under the title; screen readers read it when the window opens. | |
icon? | IconProp | Icon next to the title: a name or an element. | |
children? | ModalSlot | Body. If a function is given it receives { close, pending }; for example to close the window when a link is clicked. | |
onConfirm? | (data: FormData) => unknown | Called when the confirm button is pressed. The body works like a form: the values of its fields arrive as FormData, and it is not called until browser validation (required etc.) passes. - If it returns a Promise, the confirm button shows loading until it settles and the window does not close (Esc, backdrop and back button wait too). - If it returns false or throws, the window stays open. - Otherwise the window closes. | |
confirmLabel? | ReactNode | Text of the confirm button. If onConfirm is given but this isn't, the UIProvider text ("Onayla") is used. | |
cancelLabel? | ReactNode | {@link UIProvider} text ("İptal") | Text of the cancel button. With null the button is not shown. |
tone? | Tone | "primary" | Color of the confirm button. "danger" for irreversible actions such as deleting. |
footer? | ModalSlot | Footer shown instead of the ready-made buttons. If a function is given it receives { close, pending }. With null no footer is drawn at all. | |
showCloseButton? | boolean | true | Show a close (×) button in the top right corner? |
closeOnBackdropClick? | boolean | true | Close on backdrop click? Pass false for forms where you don't want the user to lose data by accident. |
closeOnBackButton? | boolean | true | Should the browser or phone back button close the window? |
swipeToClose? | boolean | true | Swipe to close with a finger. |
stickyHeader? | boolean | true | Should the header (title, description, icon) stay fixed? When true, only the body scrolls with long content and the header is always visible. When false, the header scrolls with the body. |
stickyFooter? | boolean | true | Should the footer (buttons) stay fixed? When true, the buttons are always visible with long content and the body scrolls above them. When false, the footer is at the end of the content and scrolls with the body. |
className? | string | Classes added to the window itself. | |
classNames? | ModalClassNames | Classes added to the inner parts. |
ModalRenderContext
interfaceValues received when children and footer are given as functions; so the window can be closed without writing nested components.
| Name | Type | Description |
|---|---|---|
close | () => void | Closes the window. |
pending | boolean | Is the confirmation (onConfirm) in progress? |
ModalSlot
typeContent that can be given as children or footer.
ModalClassNames
interfaceClasses that can be passed to the inner parts of the ready-made window.
| Name | Type | Description |
|---|---|---|
header? | string | Top area with the title, description and icon. |
icon? | string | Icon box. |
title? | string | Title. |
description? | string | Description. |
body? | string | Body wrapping children. |
footer? | string | Footer area with the buttons. |