Dialog · 模态对话框
用 open 控制对话框的可见性,通过 onOpenChange 接收关闭请求。屏幕阅读器使用 title 作为对话框名称。简短的操作说明放入 description,较长的内容及表单放入子节点,确认按钮放入 footer。
Live example
Open playground ↗App.tsx
'use client'
import { useState } from 'react'
import { Button, Dialog } from '@ancrel/ui'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
export default function App() {
const [open, setOpen] = useState(false)
const [busy, setBusy] = useState(false)
return <>
<Button onClick={() => setOpen(true)}>Review changes</Button>
<Dialog open={open} onOpenChange={setOpen} busy={busy} title="Publish changes"
description="Review the summary before publishing."
footer={<Button onClick={() => setBusy(!busy)}>{busy ? 'Finish' : 'Start review'}</Button>}>
<p>Two pages have changed.</p>
</Dialog>
</>
}
Expected behavior
Expected behavior
Open Review changes. The dialog is named Publish changes and describes the review. Start review locks dismissal. Choose Finish to release the lock, then press Escape to close.
等待操作完成
提交时把 busy 绑定到请求状态,这将阻止 Escape、遮罩按下和关闭按钮。由调用方在成功后更新 open,在失败后清除 busy 并显示错误信息。自行提供的页脚按钮也应根据请求状态控制重复提交。
打开时焦点进入对话框,Tab 和 Shift+Tab 在其中循环。若从抽屉内再打开对话框,按一次 Escape 只关闭当前对话框,并将焦点还给抽屉内的触发控件。
--ac-sys-layer-modal
Props
| Prop | Type | Required | Default |
|---|---|---|---|
busy | boolean | No | — |
children | ReactNode | No | — |
className | string | No | — |
description | ReactNode | No | — |
footer | ReactNode | No | — |
key | React.Key | null | undefined | No | — |
onOpenChange | (open: boolean) => void | Yes | — |
open | boolean | Yes | — |
ref | React.Ref<HTMLDivElement> | undefined | No | — |
title | ReactNode | Yes | — |
State matrix
| State | Theme / density |
|---|---|
| default | dark/compact, dark/comfortable, light/compact, light/comfortable |
Constraints
- Provide a meaningful title and update open in onOpenChange.
- Bind busy to the operation that must finish before dismissal.
Anti-patterns
- Closing the parent dialog when a nested layer handles Escape.
- Putting all dismissal controls behind a permanently busy state.