Skip to content
Nacre
Color palette
中文

Dialog

stableDialog · DialogPanel · TouchSheet · TouchSearchField · TouchTriggerOpen in playground
Dialog
Runs the real component in your browser.

Interrupts the current task when the user has to decide or fill in a short form. A centered card over a scrim: title, description, scrolling body and a row of buttons.

Status: stable · Components: Dialog, DialogPanel, TouchSheet, TouchSearchField, TouchTrigger

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

For actions that cannot be undone (ending, deleting): alertdialog with a warning icon, and a red danger confirm button that names the outcome.

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)}>
End chat
</Button>
<Dialog
testID="demo-dialog-confirm"
open={open()}
onClose={() => open.set(false)}
returnFocus={() => trigger}
role="alertdialog"
width="sm"
icon="warning"
tone="warning"
title="No summary yet. End the chat anyway?"
description="The customer will have to start a new chat."
footer={
<>
<Button onPress={() => open.set(false)}>Cancel</Button>
<Button variant="primary" tone="danger" onPress={() => open.set(false)}>
End chat
</Button>
</>
}
/>
</>
)
}

Wrap a labelled control in TouchTrigger. The search field stays above the scrolling results. Select a row or swipe down to dismiss.

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 = () => 'Language'
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>
}

The first field has autoFocus, so typing starts at once; the submit button is disabled until the required field is filled, with the reason in footerHint; while submitting dismissible is false, so neither Escape nor the scrim closes it.

Phone: The first field takes focus and raises the keyboard; submit stays disabled until required fields are filled, with the reason shown above the buttons; while submitting, tapping the scrim does not close it.

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: 'Reassigned' })
}, 900)
}
return (
<>
<Button testID="demo-dialog-form-open" viewRef={(node) => (trigger = node)} onPress={() => open.set(true)}>
Reassign complaint
</Button>
<Dialog
testID="demo-dialog-form"
open={open()}
onClose={() => open.set(false)}
returnFocus={() => trigger}
dismissible={!busy()}
title="Reassign complaint"
description="Hand it to another agent; the current owner is notified."
footerHint={reason().trim() ? undefined : 'Give a reason to reassign'}
footer={
<>
<Button size="md" disabled={busy()} onPress={() => open.set(false)}>
Cancel
</Button>
<Button size="md" variant="primary" loading={busy()} disabled={!reason().trim()} onPress={submit}>
Reassign
</Button>
</>
}
>
<Field label="Reason" required>
<TextField multiline rows={4} maxLength={200} counter autoFocus value={reason()} onChange={(v) => reason.set(v)} accessibilityLabel="Reason" />
</Field>
</Dialog>
</>
)
}

When the caller mounts it with its own Presence, use DialogPanel: the value it shows (the name here) stays during the closing animation.

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('Quick reply A')}>
Delete quick reply
</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="Delete this quick reply?"
description={name()}
footer={
<>
<Button onPress={() => doomed.set(null)}>Cancel</Button>
<Button variant="destructive" onPress={() => doomed.set(null)}>
Delete
</Button>
</>
}
/>
)}
</Presence>
</>
)
}
Key Action
Tab / ⇧Tab Focus moves within the dialog and never reaches the page under it (the platform’s modal layer)
Esc Close (onClose(“escape”)), from inside a text field too; not while dismissible is false
  • role defaults to dialog; confirmations and warnings use alertdialog, which screen readers announce with its title and description at once.
  • title is the accessible name and description the accessible description; with a custom header, still pass title.
  • On close the focus goes back to the control focused before it opened; pass returnFocus to send it elsewhere.
  • Give a form’s first field autoFocus; without it the focus starts on the dialog and Tab enters its first control.

Do

  • Name the action and its outcome on the button (“End chat”, not “OK”)
  • Use a destructive primary button for dangerous actions
  • Give long forms width=“lg” and let the body scroll

Don’t

  • Use a dialog for plain information (use a toast or an inline note)
  • Open a dialog from a dialog
  • Allow closing while submitting

Property descriptions follow the Chinese source comments.

Prop Type Default Description
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 — 正文:放在可滚动区里(窗口矮时只滚正文,标题与按钮行不动),边缘渐隐

* required

Prop Type Default Description
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 挂载时传(值在关闭期间仍要显示的场景)

* required

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

* required

Prop Type Default Description
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 几乎同色,看不出框

* required

Prop Type Default Description
onPress * () => void —
disabled boolean —
block boolean —
label string —
expanded boolean —
viewRef (node: ViewRef) => void —
children JSX.Element —

* required