gpuicn
Theme

Theme the previews

Every preview on the site is live GPUI and follows these.

Base
Accent
Radius
gpuicn theme set --base-color neutral --radius 0.625rem

Overlays

Alert Dialog

A modal dialog that expects a response from the user.

Added in 0.2.0 87 lines of Rust

Gallery

Starting the preview. GPUI compiles to WebAssembly, so the first load takes a few seconds. This browser has neither WebGPU nor WebGL 2, so the live preview cannot run. The code on this page still works. The preview did not start.

Installation

After the one-time setup, run the command from your app root. The CLI writes the modules and keeps src/ui/mod.rs in step.

gpuicn add alert-dialog

If you prefer to copy the files by hand, declare them yourself:

// src/ui/mod.rs
pub mod theme;
pub mod button;
pub mod dialog;
pub mod alert_dialog;

// src/main.rs
mod ui;

Usage

Put this in a view module and return example(handle, window, cx) from its render method.

use crate::ui::alert_dialog::*;
use crate::ui::dialog::*;
use gpui_kit::{App, IntoElement, ParentElement, Window};

fn example(handle: &DialogHandle, window: &mut Window, cx: &mut App) -> impl IntoElement {
    let popup = dialog_popup("profile.popup", "Edit profile", cx)
        .child(dialog_title("profile.title", cx).child("Edit profile"))
        .child(dialog_action("profile.save", handle).label("Save"));
    gpui_kit::div()
        .child(dialog_trigger("profile.open", handle).label("Edit profile"))
        .child(alert_dialog("profile", handle, popup, window, cx))
}

Examples

Destructive

An alert dialog only closes through its buttons. Style the action to match what it does.

Starting the preview. GPUI compiles to WebAssembly, so the first load takes a few seconds. This browser has neither WebGPU nor WebGL 2, so the live preview cannot run. The code on this page still works. The preview did not start.

One button

With only an action, the dialog is a notice the person has to acknowledge.

Starting the preview. GPUI compiles to WebAssembly, so the first load takes a few seconds. This browser has neither WebGPU nor WebGL 2, so the live preview cannot run. The code on this page still works. The preview did not start.

API reference

pub fn alert_dialog_with<E: IntoElement>(
    id: impl Into<ElementId>,
    handle: &DialogHandle,
    build: impl FnOnce(&mut Window, &mut App) -> E,
    window: &mut Window,
    cx: &mut App,
) -> AlertDialog

Like [`alert_dialog`], but builds the popup only while the dialog is open.

pub fn alert_dialog(
    id: impl Into<ElementId>,
    handle: &DialogHandle,
    popup: impl IntoElement,
    window: &mut Window,
    cx: &mut App,
) -> AlertDialog

Keep this host mounted while closed; validate the action in `.on_ok(...)`. The caller builds `popup` on every frame, open or closed; use [`alert_dialog_with`] to build it only while the dialog is open.

pub fn alert_dialog_action(id: impl Into<ElementId>) -> DialogControl

Explicit confirm button; the host's callback may veto dismissal.

pub fn alert_dialog_cancel(id: impl Into<ElementId>) -> DialogControl

Cancel button with keyboard and focus behavior from Kit.

Installed source, 87 lines
//! Nova confirmation dialog using Kit's non-dismissible alert host.
use super::{
    button::ButtonVariant,
    dialog::{
        DialogControl, DialogControlAction, DialogHandle, dialog_backdrop, modal_focus,
        modal_viewport,
    },
    theme::{UiTheme, primitives::dismiss},
};
pub use gpui_kit::base::AlertDialog;
use gpui_kit::{
    App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, Styled, Window, div,
};

/// Like [`alert_dialog`], but builds the popup only while the dialog is open.
pub fn alert_dialog_with<E: IntoElement>(
    id: impl Into<ElementId>,
    handle: &DialogHandle,
    build: impl FnOnce(&mut Window, &mut App) -> E,
    window: &mut Window,
    cx: &mut App,
) -> AlertDialog {
    let popup = super::dialog::lazy_popup(handle, build, window, cx);
    alert_dialog(id, handle, popup, window, cx)
}

/// Keep this host mounted while closed; validate the action in `.on_ok(...)`.
///
/// The caller builds `popup` on every frame, open or closed; use
/// [`alert_dialog_with`] to build it only while the dialog is open.
pub fn alert_dialog(
    id: impl Into<ElementId>,
    handle: &DialogHandle,
    popup: impl IntoElement,
    window: &mut Window,
    cx: &mut App,
) -> AlertDialog {
    let id = id.into();
    let focus = modal_focus::prepare(id.clone(), handle.is_open(), window, cx);
    let scope = modal_focus::ModalFocus::new(id.clone());
    // Escape cancels, as in shadcn's alert dialog; only a press outside is
    // ignored. Kit's own Escape binding is off so an app's Escape shortcut
    // cannot take it first, as in `dialog`.
    let popup = dismiss::on_escape(scope.trap(div(), true), |window, cx| {
        window.dispatch_action(Box::new(gpui_kit::base::actions::Cancel), cx);
        true
    })
    .id(id.clone())
    .track_focus(&focus)
    .w_full()
    .max_w(UiTheme::read(cx).space(96.))
    .child(scope.boundary(false))
    .child(scope.boundary(true))
    .child(modal_viewport((id, "viewport"), popup, window, cx));
    AlertDialog::new(cx)
        .handle(handle.clone())
        .close_on_escape(false)
        .backdrop(dialog_backdrop(cx))
        .popup(
            div()
                .absolute()
                .inset_0()
                .flex()
                .items_center()
                .justify_center()
                .p(UiTheme::read(cx).space(4.))
                .child(popup),
        )
}
/// Explicit confirm button; the host's callback may veto dismissal.
pub fn alert_dialog_action(id: impl Into<ElementId>) -> DialogControl {
    DialogControl::new(
        id,
        DialogControlAction::Confirm,
        "gpuicn-alert-dialog-action-anchor",
    )
}
/// Cancel button with keyboard and focus behavior from Kit.
pub fn alert_dialog_cancel(id: impl Into<ElementId>) -> DialogControl {
    DialogControl::new(
        id,
        DialogControlAction::Cancel,
        "gpuicn-alert-dialog-cancel-anchor",
    )
    .variant(ButtonVariant::Outline)
}

Platform notes

Visual reference: shadcn/ui 4.19.0, Neutral Nova. Implementation: crates/ui/registry/alert_dialog/alert_dialog.rs, using GPUI Kit 0.6.4.

A retained Kit DialogHandle owns visibility. Explicit confirm and cancel buttons route actions through the alert host, which can veto confirmation. Tab traversal stays within the popup and closing restores focus. Enter activates the focused button; the host does not convert every Enter press into confirmation.

The Rust source and Usage example define the supported API. Browser DOM/CSS behavior, exact exit animations and cross-platform screen-reader parity are not implied by the native component. See migration qualification for the checks completed on this revision.