Skip to content

Dialog

Display overlay area on top of a page, represents a modal or non-modal dialog box.
Build on top of the native HTML <dialog/> element.

import {Dialog} from '@kaiverse/k/ui'

Some examples of how to use the Dialog component.

Unstyled Dialog component for fully custom UI.

It only provides the structure, control logic, and accessibility features of a dialog.

Crafting demo...
Crafting demo...

When submitting a <form> that is nested within a <Dialog>, you can close that <Dialog> by using form method method="dialog" or set formmethod="dialog" to the <button> used to submit the form.
Also as a result, the onClose event will be fired.

Let use the drawer variant for this example.

Crafting demo...
  • Dialog.Header — a fully customizable header, usually contains a title and the close button.
  • Dialog.Title — render a h2 element.
  • Dialog.CloseButton — a button that closes the Dialog, this component must be used inside the Dialog.
  • Dialog.Content — a wrapper for the main content of the Dialog.
  • Dialog.Footer — bottom section, usually contains actions.

For instance, here is a drawer with similar UI as the previous “Controlled by form” example that doesn’t use any compound components.

Crafting demo...
page.tsx
'use client';
import {Dialog} from '@kaiverse/k/ui'
export default function ClientComponent() {
return (
<Dialog open>
<Dialog.Header>
<Dialog.Title>Dialog header</Dialog.Title>
<Dialog.CloseButton />
</Dialog.Header>
<Dialog.Content>Dialog content</Dialog.Content>
<Dialog.Footer>Dialog Footer</Dialog.Footer>
</Dialog>
)
}

A Dialog that slides in from the side of the screen.

Position of the drawer. It only available when variant is set to 'drawer'.

Crafting demo...
<Dialog variant="drawer" position="..." />

A variant that utilizes modern web technologies to replicate the iOS drawer (sheets) experience on the web.

Coming soon…

A Dialog that has no backdrop and also doesn’t render on the top-layer. It can NOT be closed by pressing the ESC key and the below page content can be interacted.

Crafting demo...
<Dialog dialogMode="non-modal" />

Dialog component build on top of the HTML <dialog/> element.

By default, it respects the default accessibility behavior and settings of a <dialog/> element. You can opt-out some behaviors by using the preventFocus and preventClose props.

KeyDescription
EscapeClose the Dialog. This behavior will be disabled if dialogMode is 'non-modal'.
SpacebarTrigger focusing element.

eg: The close button when a Dialog just opened, and the button that opens the Dialog after closing it. You can continue press Spacebar to open/close a Dialog.

Tab | Shift + Tab
Cycles through all the focusable elements of the Dialog only.
VariableDefaultDescription
--k-dialog-animation-timing-fncubic-bezier(0.32, 0.72, 0, 1)Dialog opening and closing stages animation timing function.
--k-dialog-open-animation-duration500msDialog opening animation duration.
--k-dialog-close-animation-duration200msDialog closing animation duration.
--k-dialog-transition-timing-fncubic-bezier(0.32, 0.72, 0, 1)Dialog opening and closing stages transition timing function.
--k-dialog-open-transition-duration200msOpening transition duration of the Dialog and its backdrop.
It affects opacity, and discrete transitions (display, overlay).
--k-dialog-close-transition-duration200msSame as --k-dialog-open-transition-duration but for the closing stage.
--k-dialog-offset0Dialog’s offset from the edge of current viewport.
--k-dialog-backdrop-bgrgba(0, 0, 0, 0.4)Backdrop background.
--k-dialog-backdrop-blur0Backdrop blur effect. It will be used by backdrop-filter’s blur function.
--k-dialog-backdrop-opacity1Backdrop opacity.

You can set the offset prop to adjust the Dialog’s position from the edge of the viewport.

Crafting demo...
<Dialog offset={16} />

We can customize the Dialog’s backdrop by

  • Using the backdropProps prop.
  • Update related CSS variables.
  • Apply styles directly to the ::backdrop pseudo-element.
Crafting demo...
NameTypeDescription
BackdropProps{background?: string; blur?: string | number; opacity?: number}backdropProps prop’s options.
DialogStylingSelectors DEPRECATED'header' | 'content' | 'footer'Available selectors of special styles APIs: classNames, styles.
NameTypeDefaultDescription
variant'default' | 'drawer''default'
dialogMode'modal' | 'non-modal''modal''modal'(1)
'non-modal'(2)
preventFocusbooleanfalseIf true, disable a default behavior of <dialog> element: Browser won’t autofocus on the first nested focusable element anymore.
preventClosebooleanfalseIf true, the Dialog won’t close when users press Escape key or click/tap on the backdrop.
backdropPropsBackdropPropsbackground: rgba(0,0,0,0.4)
blur: 0
opacity: 1
Styling Dialog’s backdrop.
offsetnumber | string0 (px)Dialog’s offset from the edge of current viewport.
classNames DEPRECATEDPartial<Record<DialogStylingSelectors, string>>Applies className to related selector element.
styles DEPRECATEDPartial<Record<DialogStylingSelectors, CSSProperties>>styles[selector] applies inline styles to related selector element ONLY if its style property has not been provided.
NameTypeDefaultDescription
position'right' | 'bottom' | 'left' | 'top''right'Side of the screen where the 'drawer' variant will be opened.