Alert Dialog
A modal dialog that interrupts the user with important content and expects a response.
Overview
标题 + 描述 + Cancel/Continue 的基础确认框,点击 Show Dialog 打开。
GPUI · Live previewClick the trigger
Installation
Alert Dialog 是单个 Rust 文件(src/alert_dialog.rs),构建在 gpui-base 的模态原语之上。复制到你的 crate,或下载完整示例直接运行。
[dependencies]gpui-kit = "0.6.6"# alert_dialog.rs builds on the modal primitives# re-exported at gpui_kit::base:# AlertDialog, AlertDialogTrigger, AlertDialogBackdrop,# AlertDialogPopup, AlertDialogTitle, AlertDialogDescription,# AlertDialogCancel, AlertDialogAction, DialogHandle# Download the runnable example crate, then:cargo run --locked -- --alert-dialog# Other demos:cargo run --locked -- --alert-dialog smcargo run --locked -- --alert-dialog mediacargo run --locked -- --alert-dialog sm-mediacargo run --locked -- --alert-dialog destructiveUsage
宿主视图持有 DialogHandle,用 AlertDialogSpec 描述内容,渲染时触发器与对话框本体都通过 handle 关联。
use justdo_command::alert_dialog::{AlertDialogSpec, alert_dialog};use justdo_command::button::Button;use gpui_kit::base::{AlertDialogTrigger, DialogHandle};// Keep the handle on your host view.struct Host { handle: DialogHandle }// In render: the trigger opens the dialog; the host renders it.// (any IntoElement works as the trigger — our Button is one)AlertDialogTrigger::new(Button::new("open").label("Show Dialog")) .handle(self.handle.clone())// Plus the composed dialog itself (backdrop + popup + parts):alert_dialog(&spec, &self.handle, intro, |confirmed, _, _| { // true = confirm pressed, false = cancel / Escape}, cx)网页预览与桌面端运行同一份 Rust / GPUI 组件;点按 Show Dialog 打开,遮罩不可点击关闭。
Composition
组合结构与 shadcn/ui 的 Base UI 版本一一对应。模态行为、焦点围栏与遮罩由 gpui-base 原语提供,样式由本模块统一提供。
AlertDialog modal host · focus trap├── AlertDialogTrigger opens on press└── AlertDialogPopup centered card ├── AlertDialogMedia optional icon box ├── AlertDialogTitle ├── AlertDialogDescription └── Footer ├── AlertDialogCancel -> Cancel action └── AlertDialogAction -> Confirm actionAccessibility
Keyboard
- Tab 聚焦触发器,Enter Space 打开对话框
- Escape 触发 Cancel 关闭对话框
- Enter 在弹层内触发 Confirm 关闭
- 打开时焦点移入弹层并被围栏,关闭后还给触发器;遮罩按下不会关闭
Semantics
弹层携带 role="alertdialog";Cancel 派发 Cancel、Action 派发 Confirm action。打开时背景淡出 + 卡片上浮入场,reduced-motion 下动画自动关闭。
API Reference
本模块暴露的配置与组合函数。模态原语由 gpui_kit::base 提供。
| API | Signature | Notes |
|---|---|---|
AlertDialogSpec::new() | -> AlertDialogSpec | Default size, no media, Cancel/Continue labels. |
.trigger/.title/.description(..) | impl Into<SharedString> | Trigger label and popup copy. |
.cancel/.confirm(..) | impl Into<SharedString> | Footer button labels. |
.size(..) | AlertDialogSize | Default | Sm — Sm narrows the card and stacks the footer. |
.media(..) | AlertDialogMedia | Plus | Bluetooth | Trash icon box above the title. |
.destructive(bool) | bool | Red confirm button and tinted media icon. |
.dark(bool) | bool | Dark color scheme for scrim, card and buttons. |
alert_dialog(..) | spec, handle, intro, on_done, cx | Composes backdrop + popup; on_done receives confirmed flag. |
DialogHandle | ::new(open) | Open state shared by trigger, host and imperative close. |
// on_ok / on_cancel run when Action / Cancel / Enter / Escape resolve// the dialog; returning true lets it close.AlertDialog::new(cx) .handle(self.handle.clone()) .on_ok(|_, _, _| true) .on_cancel(|_, _, _| true) .on_open_change(|open, reason, _, _| { /* reopen, backdrop, ... */ })Source & inspiration
结构与视觉参考 shadcn/ui Alert Dialog (Base UI 组合方式)。本页交互由 Rust / GPUI 绘制,模态原语来自 gpui-base 。