---
title: "Dialog"
description: "Dialog with accessible presentation and local state."
---

# Dialog

Dialog with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Dialog, DialogContent from "@rightui/ui"
```

## Usage

Use Dialog for a confirmation footer or DialogContent for custom actions. Set initialFocus, pass the tabbable control IDs in order, and restore focus to the trigger on dismissal.

## Example

```contract
use Button, DialogContent, Input, InputStyle, uiFontFamily from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
  state active = ""
  state name = "Charlie Cheever"
  state username = "@ccheever"
  state draftName = ""
  state draftUsername = ""
  derive labels = ["Open Dialog", "Share", "No Close Button", "Sticky Footer", "Scrollable Content", "فتح الحوار"]
  derive variants = ["profile", "share", "plain", "sticky", "scroll", "rtl"]
  derive rtl = active == "rtl"
  derive profile = active == "profile" or rtl
  derive longBody = active == "sticky" or active == "scroll"
  derive title = rtl ? "تعديل الملف الشخصي" : profile ? "Edit profile" : active == "share" ? "Share link" : active == "plain" ? "No Close Button" : active == "sticky" ? "Sticky Footer" : "Scrollable Content"
  derive description = rtl ? "حدّث بياناتك واحفظ التغييرات." : profile ? "Update your name and username, then save your changes." : active == "share" ? "Copy this link to share the installation guide." : active == "plain" ? "Press Escape or click outside to dismiss this dialog." : "Review the workspace guidelines below."
  derive targets = profile ? ["dialog-name", "dialog-username", "dialog-cancel", "dialog-save"] : active == "share" ? ["dialog-link", "dialog-done"] : active == "sticky" ? ["dialog-body", "dialog-done"] : active == "scroll" ? ["dialog-body"] : []
  action show(kind: string)
    draftName = name
    draftUsername = username
    active = kind
  action dismissExample
    let trigger = "dialog-trigger-" + active
    active = ""
    focus(trigger)
  action editName(value: string)
    draftName = value
  action editUsername(value: string)
    draftUsername = value
  action save
    name = draftName
    username = draftUsername
    dismissExample()
  view
    column gap="ui.space-16" min-width=0
      row gap="ui.space-8" flex-wrap="wrap"
        for kind, i in variants key=kind
          Button(id="dialog-trigger-" + kind, label=(match labels.at(i) { case some(label) => label, case none => kind }), variant="outline", disabled=false, press=show(kind))
      DialogContent(id="demo-dialog", open=(active != ""), reduced=reduced, title=title, description=description, showCloseButton=(active != "plain" and active != "share"), rtl=rtl, initialFocus=(profile ? "dialog-name" : active == "share" ? "dialog-link" : active == "plain" ? "demo-dialog" : "demo-dialog-close"), focusIds=targets, dismiss=dismissExample)
        if profile
          column gap="ui.space-16" min-height=0 overflow="auto" padding="ui.space-4"
            Input(id="dialog-name", label=(rtl ? "الاسم" : "Name"), value=draftName, placeholder="", error="", disabled=false, edit=editName)
            Input(id="dialog-username", label=(rtl ? "اسم المستخدم" : "Username"), value=draftUsername, placeholder="", error="", disabled=false, edit=editUsername)
          row gap="ui.space-8" justify-content="flex-end" flex-wrap="wrap" flex-shrink=0
            Button(id="dialog-cancel", label=(rtl ? "إلغاء" : "Cancel"), variant="outline", disabled=false, press=dismissExample)
            Button(id="dialog-save", label=(rtl ? "حفظ التغييرات" : "Save changes"), variant="primary", disabled=false, press=save)
        if active == "share"
          input id="dialog-link" testId="dialog-link" class=InputStyle aria-label="Share link" readonly=true value="https://rightui.dev/docs/installation"
        if longBody
          column id="dialog-body" testId="dialog-body" tabindex=0 aria-label="Workspace guidelines" min-height=0 max-height="50vh" overflow="auto" gap="ui.space-16"
            for paragraph in ["Choose a display name that teammates can recognize in shared projects.", "Use your workspace profile to keep contact information up to date.", "Review invitations before adding people to a private project.", "Assign editing access only to teammates who maintain the project.", "Keep project descriptions current so new members can find their way.", "Archive completed work to keep active projects easy to browse.", "Check notification preferences when joining a new team.", "Review shared links periodically and remove access when a project closes.", "Document decisions where everyone working on the project can find them.", "Contact the workspace owner when you need additional permissions."] key=paragraph
              text paragraph font-family=uiFontFamily(uiFont) data-rightui-font="body" line-height="ui.leading-normal"
        if active == "sticky" or active == "share"
          row justify-content="flex-end" flex-shrink=0
            Button(id="dialog-done", label="Close", variant="outline", disabled=false, press=dismissExample)

```

## API Reference

| Property | Type |
| --- | --- |
| DialogContent.id | string |
| DialogContent.open | bool |
| DialogContent.reduced | bool |
| DialogContent.title | string |
| DialogContent.description | string |
| DialogContent.focusIds | string[] |
| DialogContent.initialFocus | string |
| DialogContent.dismiss | action |
| DialogContent.showCloseButton | bool |
| DialogContent.rtl | bool |
| Dialog.id | string |
| Dialog.open | bool |
| Dialog.reduced | bool |
| Dialog.title | string |
| Dialog.description | string |
| Dialog.confirmLabel | string |
| Dialog.focusIds | string[] |
| Dialog.dismiss | action |
| Dialog.confirm | action |

## Web overlay setup

Load the packaged assets/sheet.css in the web head for viewport positioning and modal scroll locking. Native hosts use the same RightUI surfaces directly. Keep open state in the parent and restore focus in dismiss; list interactive children in focusIds.

```json
{
  "data": ["rightui-font", "rightui-sheet"],
  "host": {
    "web": {
      "head": [{ "link": { "rel": "stylesheet", "href": "/assets/sheet.css" } }]
    }
  }
}
```

## Source

File: packages/ui/dialog.contract

```contract
use FontWeights from "./typography-context.contract"
use OverlayLayer from "./overlay-layer.contract"
use uiFontFamily from "./typography-context.contract"
use Button from "./button.contract"
use Control from "./styles.contract"
use uiModalFadeIn, uiModalFadeOut, uiModalZoomIn, uiModalZoomOut from "./overlays.contract"
component DialogFrame
  props
    id: string
    open: bool
    reduced: bool
    title: string
    description: string
    focusIds: string[]
    dismiss: action
  slot
  view
    DialogFrameWithFocus(id=id, open=open, reduced=reduced, title=title, description=description, focusIds=focusIds, initialFocus=id + "-close", dismiss=dismiss)
      children

component DialogFrameWithFocus
  props
    id: string
    open: bool
    reduced: bool
    title: string
    description: string
    focusIds: string[]
    initialFocus: string
    dismiss: action
  slot
  view
    DialogContent(id=id, open=open, reduced=reduced, title=title, description=description, focusIds=focusIds, initialFocus=initialFocus, showCloseButton=true, rtl=false, dismiss=dismiss)
      children

component DialogContent
  props
    id: string
    open: bool
    reduced: bool
    title: string
    description: string
    focusIds: string[]
    initialFocus: string
    dismiss: action
    showCloseButton: bool
    rtl: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  slot
  state focused = ""
  action rememberFocus(e: FocusEvent)
    focused = e.target
  action keys(k: string, e: KeyboardEvent)
    if not e.isComposing
      if k == "Escape"
        preventDefault()
        stopPropagation()
        dismiss()
      else if k == "Tab"
        preventDefault()
        stopPropagation()
        let targets = showCloseButton ? [id + "-close"].concat(focusIds) : focusIds
        let index = targets.indexOf(focused)
        let next = index < 0 ? (e.shiftKey ? targets.length - 1 : 0) : (index + (e.shiftKey ? targets.length - 1 : 1)) % max(1, targets.length)
        match targets.at(next)
          case some(target)
            focus(target)
          case none
            focus(id)
  action blockScroll(e: WheelEvent)
    if not e.ctrlKey
      preventDefault()
  view
    if open
      OverlayLayer(id=id, modal=true, initialFocus=initialFocus, reduced=reduced)
        column position="absolute" inset=0 z-index=110 pointer-events="none" align-items="center" justify-content="center" padding="ui.space-16" box-sizing="border-box"
          if open
            view id=(id + "-backdrop") testId=(id + "-backdrop") position="absolute" inset=0 background-color="ui.overlay" pointer-events="auto" aria-hidden=true press=dismiss wheel=blockScroll touch-action="none"
              animation=(reduced ? "none" : "uiModalFadeIn ui.duration-normal ui.easing both")
              -exact-exit-animation=(reduced ? "none" : "uiModalFadeOut ui.duration-normal ui.easing both")
          if open
            column id=id testId=id tabindex=-1 direction=(rtl ? "rtl" : "ltr") role="dialog" aria-modal=true aria-labelledby=(id + "-title")
              aria-describedby=(id + "-description") pointer-events="auto" position="relative" keycapture=keys focusin=rememberFocus width="100%" max-width="32rem" max-height="100%" box-sizing="border-box"
              background-color="ui.surface" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-panel" padding="ui.space-24" gap="ui.space-16" box-shadow="ui.shadow-panel"
              animation=(reduced ? "none" : "uiModalZoomIn ui.duration-normal ui.easing both")
              -exact-exit-animation=(reduced ? "none" : "uiModalZoomOut ui.duration-normal ui.easing both")
              if showCloseButton
                button id=(id + "-close") testId=(id + "-close") class=Control font-weight=uiWeights.medium autofocus=(initialFocus == id + "-close") press=dismiss aria-label="Close" position="absolute" top="ui.space-12" left=(rtl ? "ui.space-12" : "auto") right=(rtl ? "auto" : "ui.space-12") width="ui.space-32" height="ui.space-32" min-height="ui.space-32" display="flex" align-items="center" justify-content="center" padding="0px" border="none" background-color="transparent" color="ui.foreground" opacity="ui.opacity-soft"
                  svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.stroke-standard" aria-hidden=true
                    path d="m6 6 12 12M6 18 18 6"
              column gap="ui.space-8" padding-right=(showCloseButton and not rtl ? "ui.space-24" : "0px") padding-left=(showCloseButton and rtl ? "ui.space-24" : "0px") flex-shrink=0
                text title hyphens="auto" id=(id + "-title") role="heading" aria-level=2 font-size="ui.text-lg" font-weight=uiWeights.semibold color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
                text description hyphens="auto" id=(id + "-description") font-size="ui.text-sm" line-height="ui.leading-normal" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
              children

component Dialog
  props
    id: string
    open: bool
    reduced: bool
    title: string
    description: string
    confirmLabel: string
    focusIds: string[]
    dismiss: action
    confirm: action
  inject
    uiFont: string
  slot
  view
    DialogFrame(id=id, open=open, reduced=reduced, title=title, description=description, focusIds=focusIds, dismiss=dismiss)
      scroll flex="1 1 auto" min-height="0px"
        column gap="ui.space-16"
          children
      row gap="ui.space-8" flex-shrink=0 justify-content="flex-end" flex-wrap="wrap"
        button id=(id + "-cancel") testId=(id + "-cancel") appearance="none" press=dismiss
          min-height="ui.space-36" max-width="100%" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-control" padding="ui.space-8 ui.space-16" background-color="ui.surface" color="ui.foreground"
          :focus-visible
            box-shadow="ui.shadow-focus"
          text "Cancel" font-size="ui.text-sm" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        Button(id=id + "-confirm", label=confirmLabel, variant="primary", disabled=false, press=confirm)

```
