Kobalte.v0.12.1

Dialog

A window overlaid on either the primary window or another dialog window. Content behind a modal dialog is inert, meaning that users cannot interact with it.

Import

ts
import { Dialog } from "@kobalte/core";
ts
import { Dialog } from "@kobalte/core";

Features

  • Follow the WAI ARIA Dialog design pattern.
  • Supports modal and non-modal modes.
  • Provides screen reader announcements via rendered title and description.
  • Focus is trapped and scrolling is blocked while it is open.
  • Pressing Esc closes the dialog.
  • Can be controlled or uncontrolled.

Anatomy

The dialog consists of:

  • Dialog.Root: Contains all the parts of a dialog.
  • Dialog.Trigger: The button that opens the dialog.
  • Dialog.Portal: Portals its children into the body when the dialog is open.
  • Dialog.Overlay: The layer that covers the inert portion of the view when the dialog is open.
  • Dialog.Content: Contains the content to be rendered when the dialog is open.
  • Dialog.CloseButton: The button that closes the dialog.
  • Dialog.Title: An accessible title to be announced when the dialog is opened.
  • Dialog.Description: An optional accessible description to be announced when the dialog is opened.
tsx
<Dialog.Root>
<Dialog.Trigger />
<Dialog.Portal>
<Dialog.Overlay />
<Dialog.Content>
<Dialog.CloseButton />
<Dialog.Title />
<Dialog.Description />
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
tsx
<Dialog.Root>
<Dialog.Trigger />
<Dialog.Portal>
<Dialog.Overlay />
<Dialog.Content>
<Dialog.CloseButton />
<Dialog.Title />
<Dialog.Description />
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>

Example

Usage

Default open

An initial, uncontrolled open value can be provided using the defaultOpen prop.

tsx
<Dialog.Root defaultOpen>...</Dialog.Root>
tsx
<Dialog.Root defaultOpen>...</Dialog.Root>

Controlled open

The open prop can be used to make the open state controlled. The onOpenChange event is fired when the user presses the trigger, close button or overlay, and receives the new value.

tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<Dialog.Root open={open()} onOpenChange={setOpen}>
...
</Dialog.Root>
);
}
tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<Dialog.Root open={open()} onOpenChange={setOpen}>
...
</Dialog.Root>
);
}

API Reference

Dialog.Root

PropDescription
openboolean
The controlled open state of the dialog.
defaultOpenboolean
The default open state when initially rendered. Useful when you do not need to control the open state.
onOpenChange(open: boolean) => void
Event handler called when the open state of the dialog changes.
idstring
A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used.
modalboolean
Whether the dialog should be the only visible content for screen readers, when set to true:
- interaction with outside elements will be disabled.
- scroll will be locked.
- focus will be locked inside the dialog content.
- elements outside the dialog content will not be visible for screen readers.
preventScrollboolean
Whether the scroll should be locked even if the dialog is not modal.
forceMountboolean
Used to force mounting the dialog (portal, overlay and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.
translationsDialogIntlTranslations
Localization strings.

Dialog.Trigger

Dialog.Trigger consists of Button.

Data attributeDescription
data-expandedPresent when the dialog is open.
data-closedPresent when the dialog is close.

Dialog.Content and Dialog.Overlay shares the same data-attributes.

Dialog.Content

PropDescription
onOpenAutoFocus(event: Event) => void
Event handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault.
onCloseAutoFocus(event: Event) => void
Event handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault.
onEscapeKeyDown(event: KeyboardEvent) => void
Event handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutside(event: PointerDownOutsideEvent) => void
Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault.
onFocusOutside(event: FocusOutsideEvent) => void
Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside(event: InteractOutsideEvent) => void
Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.

Rendered elements

ComponentDefault rendered element
Dialog.Rootnone
Dialog.Triggerbutton
Dialog.PortalPortal
Dialog.Overlaydiv
Dialog.Contentdiv
Dialog.CloseButtonbutton
Dialog.Titleh2
Dialog.Descriptionp

Accessibility

Keyboard Interactions

KeyDescription
SpaceWhen focus is on the trigger, opens/closes the dialog.
EnterWhen focus is on the trigger, opens/closes the dialog.
TabMoves focus to the next focusable element.
Shift + TabMoves focus to the previous focusable element.
EscCloses the dialog and moves focus to the trigger.