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'

Basic usage

Some examples of how to use the Dialog component.

Simple dialog

Dialog header

Dialog content
Lorem ipsum, dolor sit amet consectetur adipisicing elit.
Dialog Footer

Controlled by form

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.

Submitted form values:
{
  "keyword": ""
}

Drawer controlled by form

Drawer Form

In this form example, all inputs are uncontrolled input.
Check the "Keep form value" checkbox to persist the form value after submitting and closing the Dialog.

Section 2: Very long content here, scroll down ↓ for more

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis minus velit non quibusdam? Magni labore consequatur accusantium eum exercitationem esse velit, ex saepe nesciunt itaque quibusdam autem? Sequi, aut dolorem.

Section 3: No idea

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis minus velit non quibusdam? Magni labore consequatur accusantium eum exercitationem esse velit, ex saepe nesciunt itaque quibusdam autem? Sequi, aut dolorem.

Section 4: Ok there are no contents

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis minus velit non quibusdam? Magni labore consequatur accusantium eum exercitationem esse velit, ex saepe nesciunt itaque quibusdam autem? Sequi, aut dolorem.

Compound components

  • 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.

Uh… No thanks!

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

Drawer controlled by form

Drawer Form

In this form example, all inputs are uncontrolled input.

Section 2: Very long content here, scroll down ↓ for more

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis minus velit non quibusdam? Magni labore consequatur accusantium eum exercitationem esse velit, ex saepe nesciunt itaque quibusdam autem? Sequi, aut dolorem.

Section 3: No idea

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis minus velit non quibusdam? Magni labore consequatur accusantium eum exercitationem esse velit, ex saepe nesciunt itaque quibusdam autem? Sequi, aut dolorem.

Section 4: Ok there are no contents

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Blanditiis minus velit non quibusdam? Magni labore consequatur accusantium eum exercitationem esse velit, ex saepe nesciunt itaque quibusdam autem? Sequi, aut dolorem.

Use in server components

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>
)
}

Variants

drawer

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

Position

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

Drawer header

Position
<Dialog variant="drawer" position="..." />

mobile WIP

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

Coming soon…

non-modal mode

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.

Non-modal Dialog

Read more about non-modal mode and
related accessibility considerations.

Count: 0

Some page content

You can interact with the content below the non-modal dialog.
Try click the 2 buttons above.
<Dialog dialogMode="non-modal" />

Accessibility

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.

Keyboard interactions

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.

Customization

CSS Variables

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.

Offset

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

You can set the offset prop to adjust the Dialog's position from the edge of the viewport.
Try dragging the range slider below.
<Dialog offset={16} />

Styling Backdrop

We can customize the Dialog’s backdrop by

  • Using the backdropProps prop.
  • Update related CSS variables.
  • Apply styles directly to the ::backdrop pseudo-element.

Drawer header

We can use backdropProps to style the Dialog's backdrop.

Special Types

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.

<Dialog> Props

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 DEPRECATED Partial<Record<DialogStylingSelectors, string>>Applies className to related selector element.
styles DEPRECATED Partial<Record<DialogStylingSelectors, CSSProperties>>styles[selector] applies inline styles to related selector element ONLY if its style property has not been provided.

Variant 'drawer'

NameTypeDefaultDescription
position'right' | 'bottom' | 'left' | 'top''right'Side of the screen where the 'drawer' variant will be opened.