用 title 指出哪个操作遇到了什么问题。需要更多上下文时,用 detail 说明用户可以怎样继续。可选的 code 使用等宽字体,方便核对或抄录。
Live example
Open playground ↗Document title already in use
E_DUPLICATEChoose another title to save a separate copy.
App.tsx
'use client'
import { useState } from 'react'
import { Button, ErrorCard } from '@ancrel/ui'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
export default function App() {
const [editing, setEditing] = useState(false)
return editing ? <label>Document title <input defaultValue="Untitled" /></label> :
<ErrorCard title="Document title already in use" tone="warning" code="E_DUPLICATE"
detail="Choose another title to save a separate copy."
actions={<Button onClick={() => setEditing(true)}>Rename document</Button>} />
}
Expected behavior
The warning explains the duplicate title and displays its code. Rename document opens a title field.
Build @ancrel/tokens and @ancrel/ui from the workspace root, then run pnpm --filter @ancrel/example-error-card test.
选择语气和操作
错误默认使用 danger,可继续处理的提醒可设置为 warning。卡片以 alert 区域播报内容,标题提供可访问名称。用文字说明严重程度,方便高对比度模式下的阅读。
通过 actions 传入带有明确标签的按钮或链接,点击后的行为由调用方实现。若用户只需阅读说明,可以省略操作区域。
Props
| Prop | Type | Required | Default |
|---|---|---|---|
actions | ReactNode | No | — |
className | string | No | — |
code | string | No | — |
detail | ReactNode | No | — |
key | React.Key | null | undefined | No | — |
ref | React.Ref<HTMLDivElement> | undefined | No | — |
title | ReactNode | Yes | — |
tone | Tone | No | — |
--ac-sys-status-warning-fgConstraints
- Choose danger or warning and state the problem in the title.
- Provide only actions the caller can perform in the current context.
Anti-patterns
- Using a code as the only explanation.
- Inferring available actions from the displayed message.