跳转到内容
Nacre
组件配色
EN

对话框 Dialog

stableDialog · DialogPanel · TouchSheet · TouchSearchField · TouchTrigger在画廊运行
Dialog
在浏览器里运行真实组件。

要用户做决定或填一段表单时打断当前操作。居中卡片 + 遮罩;标题、说明、可滚动正文、底部按钮行。

状态:stable · 组件:Dialog、DialogPanel、TouchSheet、TouchSearchField、TouchTrigger

import { Dialog, DialogPanel, TouchSheet, TouchSearchField, TouchTrigger } from 'nacre'

结束、删除这类不可撤销的动作:alertdialog + 警示图标,确认按钮用 danger 红底并写清后果。

import { signal } from 'nsh'
import type { ViewRef } from 'nsh'
import { Button, Dialog } from 'nacre'
export function DialogConfirm() {
const open = signal(false)
let trigger: ViewRef | undefined
return (
<>
<Button testID="demo-dialog-confirm-open" viewRef={(node) => (trigger = node)} onPress={() => open.set(true)}>
结束会话
</Button>
<Dialog
testID="demo-dialog-confirm"
open={open()}
onClose={() => open.set(false)}
returnFocus={() => trigger}
role="alertdialog"
width="sm"
icon="warning"
tone="warning"
title="还没写小记,确定结束会话吗?"
description="结束后客户需要重新发起会话。"
footer={
<>
<Button onPress={() => open.set(false)}>取消</Button>
<Button variant="primary" tone="danger" onPress={() => open.set(false)}>
结束会话
</Button>
</>
}
/>
</>
)
}

TouchTrigger 包住有名称的入口。搜索框固定在面板顶部,选一行或向下滑动即可收起。

import { signal, View } from 'nsh'
import { TouchSheet, TouchTrigger, TouchSearchField, TouchRow, SelectTrigger, TouchGroup } from 'nacre'
export function DialogTouchSheet() {
const open = signal(false)
const query = signal('')
const value = signal('')
const title = () => '语言'
const languages = ['English', '日本語', '简体中文']
return <View>
<TouchTrigger label={value() ? `${title()}:${value()}` : title()} expanded={open()} onPress={() => { query.set(''); open.set(true) }}>
<SelectTrigger accessibilityLabel={title()} placeholder={title()} value={value()} expanded={open()} />
</TouchTrigger>
<TouchSheet open={open()} title={title()} contentHeight={176} onClose={() => open.set(false)}
top={<TouchSearchField surface="card" value={query()} onChange={(v) => query.set(v)} />}>
<TouchGroup><Each of={languages.filter((name) => name.toLowerCase().includes(query().trim().toLowerCase()))} key={(name) => name}>
{(name) => <TouchRow label={name()} checked={name() === value()} onPress={() => { value.set(name()); open.set(false) }} />}
</Each></TouchGroup>
</TouchSheet>
</View>
}

第一个字段 autoFocus,打开就能输入;提交按钮在必填项没填时禁用,原因写在 footerHint;提交中 dismissible=false,Esc 与遮罩都不关。

手机:第一个字段自动聚焦并弹出键盘;必填项没填时提交按钮禁用,原因写在按钮上方;提交中点遮罩不关。

import { signal } from 'nsh'
import type { ViewRef } from 'nsh'
import { Button, Dialog, Field, TextField, toast } from 'nacre'
export function DialogForm() {
const open = signal(false)
const reason = signal('')
const busy = signal(false)
let trigger: ViewRef | undefined
const submit = () => {
busy.set(true)
setTimeout(() => {
busy.set(false)
open.set(false)
toast.show({ kind: 'success', message: '已转派' })
}, 900)
}
return (
<>
<Button testID="demo-dialog-form-open" viewRef={(node) => (trigger = node)} onPress={() => open.set(true)}>
转派客诉
</Button>
<Dialog
testID="demo-dialog-form"
open={open()}
onClose={() => open.set(false)}
returnFocus={() => trigger}
dismissible={!busy()}
title="转派客诉"
description="转给其他坐席处理,原处理人会收到通知。"
footerHint={reason().trim() ? undefined : '请填写转派原因'}
footer={
<>
<Button size="md" disabled={busy()} onPress={() => open.set(false)}>
取消
</Button>
<Button size="md" variant="primary" loading={busy()} disabled={!reason().trim()} onPress={submit}>
转派
</Button>
</>
}
>
<Field label="原因" required>
<TextField multiline rows={4} maxLength={200} counter autoFocus value={reason()} onChange={(v) => reason.set(v)} accessibilityLabel="原因" />
</Field>
</Dialog>
</>
)
}

调用方自己用 Presence 挂载时用 DialogPanel:关闭动画期间说明里的值(这里是名字)还在。

import { signal } from 'nsh'
import type { ViewRef } from 'nsh'
import { Button, DialogPanel, Presence } from 'nacre'
export function DialogWithPresence() {
const doomed = signal<string | null>(null)
let trigger: ViewRef | undefined
return (
<>
<Button testID="demo-dialog-panel-open" variant="destructive" viewRef={(node) => (trigger = node)} onPress={() => doomed.set('常用回复 A')}>
删除常用回复
</Button>
<Presence when={doomed()}>
{(name, leave) => (
<DialogPanel
testID="demo-dialog-panel"
leave={leave}
onClose={() => doomed.set(null)}
returnFocus={() => trigger}
role="alertdialog"
width="sm"
icon="trash"
tone="destructive"
title="删除这条常用回复?"
description={name()}
footer={
<>
<Button onPress={() => doomed.set(null)}>取消</Button>
<Button variant="destructive" onPress={() => doomed.set(null)}>
删除
</Button>
</>
}
/>
)}
</Presence>
</>
)
}
键 行为
Tab / ⇧Tab 焦点在对话框内循环,不会走到底层页面(平台模态层)
Esc 关闭(onClose(“escape”)),焦点在输入框里也一样;dismissible=false 时不关
  • role 默认 dialog;确认、警示用 alertdialog,读屏会立刻读出标题与说明。
  • title 是读屏名,description 是读屏描述;自定义 header 时 title 仍要给。
  • 关闭后焦点回到打开前有焦点的控件;要还给别处时传 returnFocus。
  • 表单型给第一个字段 autoFocus;不给时打开后焦点在对话框上,按 Tab 进第一个控件。

该用

  • 按钮写动作与后果(「结束会话」而不是「确定」)
  • 危险动作用 destructive 主按钮
  • 长表单给 width=“lg”,正文自己滚

不该用

  • 用对话框显示纯信息(用 toast 或页内提示)
  • 对话框里再开对话框
  • 提交中还允许关闭
属性 类型 默认 说明
open * boolean — 开着吗(受控)。关闭时先播退场(卡片 popOut + 遮罩淡出),播完才卸载
onClose * (reason: DialogCloseReason) => void — 用户想关(Esc、点遮罩);dismissible 为 false 时不会调用(提交中不可关)
title * string —
description string — 标题下一行说明(12 muted),也是读屏的描述
icon IconName — 标题前的图标(确认型弹窗的警示);颜色跟 tone
tone 'default' | 'warning' | 'destructive' — 图标语义色:warning 黄、destructive 红;默认 foreground
width 'sm' | 'md' | 'lg' | number — 宽:sm 420(确认)、md 480(默认,短表单)、lg 640(长表单、列表);或点数
dismissible boolean — 默认 true;提交中给 false:Esc 与遮罩不关
role 'dialog' | 'alertdialog' — 确认、警示类用 alertdialog(读屏会立刻读出标题与说明)
header JSX.Element — 换掉默认的标题行(图标 + 标题 + 说明):标题旁要放标签、翻页、倒计时这类东西时用;title 仍是读屏名。 关于面板这种居中排版也用它
returnFocus () => ViewRef | undefined — 关闭后把键盘焦点还给谁;不给时还给打开前有焦点的视图(平台的模态层自己还)
footer JSX.Element — 底部按钮行(右对齐);不给就没有按钮行
footerHint string — 按钮行左侧的一句话(「确认」点不了的原因):禁用的按钮不响应悬停,提示出不来,所以写在这里
testID string —
children JSX.Element — 正文:放在可滚动区里(窗口矮时只滚正文,标题与按钮行不动),边缘渐隐

* 必填

属性 类型 默认 说明
onClose * (reason: DialogCloseReason) => void — 用户想关(Esc、点遮罩);dismissible 为 false 时不会调用(提交中不可关)
title * string —
description string — 标题下一行说明(12 muted),也是读屏的描述
icon IconName — 标题前的图标(确认型弹窗的警示);颜色跟 tone
tone 'default' | 'warning' | 'destructive' — 图标语义色:warning 黄、destructive 红;默认 foreground
width 'sm' | 'md' | 'lg' | number — 宽:sm 420(确认)、md 480(默认,短表单)、lg 640(长表单、列表);或点数
dismissible boolean — 默认 true;提交中给 false:Esc 与遮罩不关
role 'dialog' | 'alertdialog' — 确认、警示类用 alertdialog(读屏会立刻读出标题与说明)
header JSX.Element — 换掉默认的标题行(图标 + 标题 + 说明):标题旁要放标签、翻页、倒计时这类东西时用;title 仍是读屏名。 关于面板这种居中排版也用它
returnFocus () => ViewRef | undefined — 关闭后把键盘焦点还给谁;不给时还给打开前有焦点的视图(平台的模态层自己还)
footer JSX.Element — 底部按钮行(右对齐);不给就没有按钮行
footerHint string — 按钮行左侧的一句话(「确认」点不了的原因):禁用的按钮不响应悬停,提示出不来,所以写在这里
testID string —
children JSX.Element — 正文:放在可滚动区里(窗口矮时只滚正文,标题与按钮行不动),边缘渐隐
leave Leave — 外层 Presence 给的退场句柄:调用方自己用 Presence 挂载时传(值在关闭期间仍要显示的场景)

* 必填

属性 类型 默认 说明
open * boolean —
onClose * () => void —
title * string —
testID string —
left TouchSheetAction —
right TouchSheetAction —
top JSX.Element — 列表上方固定不滚的一行(搜索框)
contentHeight * number — 首次显示时的列表高度估算。布局完成后使用实际内容高度,整张面板最高为屏高的 85%。
children JSX.Element —

* 必填

属性 类型 默认 说明
value * string —
onChange * (value: string) => void —
testID string —
placeholder string —
inputRef (node: InputRef) => void —
onFocus () => void —
onBlur () => void —
surface 'secondary' | 'card' — 放在 plane 底(弹层、分组页)上时用 card:secondary 与 plane 几乎同色,看不出框

* 必填

属性 类型 默认 说明
onPress * () => void —
disabled boolean —
block boolean —
label string —
expanded boolean —
viewRef (node: ViewRef) => void —
children JSX.Element —

* 必填