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

# Alert Dialog

Alert 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 AlertDialog from "@rightui/ui"
```

## Usage

Import AlertDialog from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use Button, AlertDialog from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  derive narrow = width < 600
  state desktopOverlay = ""
  derive reduce = reduced
  action closeArchive
    desktopOverlay = ""
    focus("alert-dialog-open")
  action archive
    archived = true
    desktopOverlay = ""
    focus("alert-dialog-open")
  state overlayReturn = "desktop-alert"
  state archived = false
  action openExample
    desktopOverlay = "alert"
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Button(id="alert-dialog-open", label="Open alert dialog", variant="outline", disabled=false, press=openExample)
      AlertDialog(id="desktop-confirm", open=desktopOverlay == "alert", title="Archive this project?", description="This demo project will be archived. You can restore it later.", confirmLabel="Archiver", reduced=reduce, dismiss=closeArchive, confirm=archive)

```

## API Reference

| Property | Type |
| --- | --- |
| AlertDialog.id | string |
| AlertDialog.open | bool |
| AlertDialog.title | string |
| AlertDialog.description | string |
| AlertDialog.confirmLabel | string |
| AlertDialog.reduced | bool |
| AlertDialog.dismiss | action |
| AlertDialog.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/overlays.contract

```contract
use FontWeights from "./typography-context.contract"
use OverlayLayer from "./overlay-layer.contract"
use uiFontFamily from "./typography-context.contract"
use Control from "./styles.contract"
keyframes uiModalFadeIn
  from opacity=0
  to opacity=1
keyframes uiModalFadeOut
  to opacity=0
keyframes uiModalZoomIn
  from opacity=0 scale=0.95
  to opacity=1 scale=1
keyframes uiModalZoomOut
  to opacity=0 scale=0.95
keyframes uiPanelRightIn
  from translate="100% 0px"
  to translate="0px 0px"
keyframes uiPanelRightOut
  to translate="100% 0px"
keyframes uiPanelLeftIn
  from translate="-100% 0px"
  to translate="0px 0px"
keyframes uiPanelLeftOut
  to translate="-100% 0px"
keyframes uiPanelTopIn
  from translate="0px -100%"
  to translate="0px 0px"
keyframes uiPanelTopOut
  to translate="0px -100%"
keyframes uiPanelBottomIn
  from translate="0px 100%"
  to translate="0px 0px"
keyframes uiPanelBottomOut
  to translate="0px 100%"

component ModalFrame
  props
    id: string
    open: bool
    title: string
    description: string
    side: string
    alert: bool
    reduced: bool
    focusIds: string[]
    dismiss: action
  inject
    uiWeights: FontWeights
    uiFont: string
  slot
  state focused = ""
  derive horizontal = side == "left" or side == "right"
  action remember(e: FocusEvent)
    focused = e.target
  action outside
    if not alert
      dismiss()
  action keys(k: string, e: KeyboardEvent)
    if not e.isComposing
      if k == "Escape"
        preventDefault()
        stopPropagation()
        dismiss()
      else if k == "Tab"
        preventDefault()
        let targets = (alert ? focusIds : [id + "-close"].concat(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=(alert ? id + "-cancel" : id + "-close"), reduced=reduced)
        column position="absolute" inset=0 z-index=110 pointer-events="none" padding=(alert ? "ui.space-16" : "0px") box-sizing="border-box"
          align-items=(alert ? "center" : side == "left" ? "flex-start" : side == "right" ? "flex-end" : "stretch") justify-content=(alert ? "center" : side == "bottom" ? "flex-end" : "flex-start")
          if open
            view id=(id + "-backdrop") testId=(id + "-backdrop") position="absolute" inset=0 background-color="ui.overlay" pointer-events="auto" aria-hidden=true press=outside 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 role=(alert ? "alertdialog" : "dialog") aria-modal=true aria-labelledby=(id + "-title") aria-describedby=(id + "-description")
              position="relative" tabindex=-1 keycapture=keys focusin=remember pointer-events="auto"
              width=(alert ? "100%" : horizontal ? "75%" : "100%") max-width=(alert ? "32rem" : horizontal ? "24rem" : "100%")
              height=(alert ? "auto" : horizontal ? "100%" : "auto") max-height="100%" min-height="0px" box-sizing="border-box"
              padding=(alert ? "ui.space-24" : "0px") gap=(alert ? "ui.space-16" : "0px") background-color="ui.surface" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius=(alert ? "ui.radius-panel" : "0px") box-shadow="ui.shadow-panel"
              animation=(reduced ? "none" : alert ? "uiModalZoomIn ui.duration-normal ui.easing both" : side == "left" ? "uiPanelLeftIn ui.duration-enter ui.easing both" : side == "right" ? "uiPanelRightIn ui.duration-enter ui.easing both" : side == "top" ? "uiPanelTopIn ui.duration-enter ui.easing both" : "uiPanelBottomIn ui.duration-enter ui.easing both")
              -exact-exit-animation=(reduced ? "none" : alert ? "uiModalZoomOut ui.duration-normal ui.easing both" : side == "left" ? "uiPanelLeftOut ui.duration-slow ui.easing both" : side == "right" ? "uiPanelRightOut ui.duration-slow ui.easing both" : side == "top" ? "uiPanelTopOut ui.duration-slow ui.easing both" : "uiPanelBottomOut ui.duration-slow ui.easing both")
              if not alert
                button id=(id + "-close") testId=(id + "-close") class=Control font-weight=uiWeights.medium autofocus=true press=dismiss aria-label="Close" position="absolute" top="ui.space-16" right="ui.space-16" z-index=1 width="ui.space-16" height="ui.space-16" min-height="ui.space-16" 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"
              scroll flex="1 1 auto" min-height="0px"
                column gap="ui.space-16" min-width="0px" padding=(alert ? "0px" : "ui.space-16")
                  column gap="ui.space-8" padding-right=(alert ? "0px" : "ui.space-24")
                    text title id=(id + "-title") role="heading" aria-level=2 hyphens="auto" font-size=(alert ? "ui.text-lg" : "ui.text-base") font-weight=uiWeights.semibold color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
                    text description id=(id + "-description") hyphens="auto" font-size="ui.text-sm" line-height="ui.leading-normal" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"
                  children

component Sheet
  props
    id: string
    open: bool
    title: string
    description: string
    side: string
    reduced: bool
    focusIds: string[]
    dismiss: action
  slot
  view
    ModalFrame(id=id, open=open, title=title, description=description, side=side, alert=false, reduced=reduced, focusIds=focusIds, dismiss=dismiss)
      children

component AlertDialog
  props
    id: string
    open: bool
    title: string
    description: string
    confirmLabel: string
    reduced: bool
    dismiss: action
    confirm: action
  inject
    uiWeights: FontWeights
    uiFont: string
  view
    ModalFrame(id=id, open=open, title=title, description=description, side="center", alert=true, reduced=reduced, focusIds=[id + "-cancel", id + "-confirm"], dismiss=dismiss)
      row gap="ui.space-8" flex-wrap="wrap" justify-content="flex-end"
        button id=(id + "-cancel") testId=(id + "-cancel") class=Control font-weight=uiWeights.medium autofocus=true press=dismiss padding="ui.space-8 ui.space-16" border-width="ui.border-width" border-style="solid" border-color="ui.border" background-color="ui.surface" color="ui.foreground"
          text "Cancel" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        button id=(id + "-confirm") testId=(id + "-confirm") class=Control font-weight=uiWeights.medium press=confirm padding="ui.space-8 ui.space-16" border-width="ui.border-width" border-style="solid" border-color="ui.border" background-color="ui.primary" color="ui.primary-foreground"
          text confirmLabel font-family=uiFontFamily(uiFont) data-rightui-font="body"

```
