跳转到内容

Alert 警告提示 ​

页面内常驻的一条提示,说明一件与当前上下文有关的事。

用法 ​

在中性抬升表面中说明当前状态与影响

部署已排队
构建完成后会自动发布。

组件结构 ​

加粗的是必需部件。

data-scope="alert":root · indicator · content · title · description · action · close-trigger

示例 ​

颜色 ​

tone 只改配色,语义仍由内容与 role 决定

保存成功
配额即将用尽
发布失败

可关闭 ​

closable 开启后才渲染关闭按钮;open 受控时由宿主决定去留

点右侧关闭

图标 ​

icon 部件排在标题前面,颜色取当前语气的强调色;内容由作者放置,字形与内联 svg 均可

发布完成
三个节点都已切到新版本。
发布失败
第 2 个节点健康检查未通过。

操作 ​

将与提示直接相关的短操作放在尾端

配额即将用尽
本月还可处理 120 次请求。

设计指引 ​

何时使用 ​

  • 表单顶部的整体错误、页面级的状态说明、功能公告。
  • 信息需要持续存在,直到用户处理或关闭。

何时不用 ​

  • 一次操作的结果反馈使用轻提示,它会自动消失。
  • 需要用户当场决定并阻断流程时使用对话框。
  • 单个字段的错误使用表单字段的错误文本。

特性 ​

  • 默认使用中性描边表面,语气只强调标题与图标,说明保持次级前景。
  • content 是标题与说明共用的必需文本列,操作和关闭入口排在尾端。
  • closable 显示关闭按钮,关闭状态可受控。

组合 ​

最佳实践 ​

  • 说明发生了什么、影响是什么、用户可以做什么,三项缺一不可。
  • 严重程度不能只靠颜色表达,标题文字本身应说明。

反模式 ​

  • 同一屏堆叠多条提示,用户会全部略过。
  • 将提示用作营销位。

API 参考 ​

产物 ​

层值
自定义元素<xh-alert>
Vue 组件XhAlertAction XhAlertCloseTrigger XhAlertContent XhAlertDescription XhAlertIndicator XhAlertRoot XhAlertTitle
状态机alertMachine
皮肤@xihan-ui/styles/alert.css

Props ​

属性类型必填说明
toneTone语气:brand / neutral / success / warning / danger / info,决定使用哪族颜色,默认 info。 danger / warning 使用 role="alert",其余使用 role="status"。
closableboolean关闭按钮是否可用,默认 true。false 时该按钮同时被禁用与收起。
openboolean受控显隐;未提供该 prop 即非受控。
defaultOpenboolean非受控初始显隐,默认显示。
onOpenChange(details: AlertOpenChangeDetails) => voidopen 变化意图回调;受控时是唯一出口,非受控时随内部转移一并通知。
translationsPartial<AlertTranslations>

事件 ​

自定义元素将载荷放在 detail;Vue 使用同名 emit。

事件载荷说明
open-changeAlertOpenChangeDetailsopen 状态变化;detail 为 { open: boolean }

React 适配器 props ​

只列各组件自己声明的那些:继承自 ComponentPropsWithRef 的 DOM 属性不在其中,根组件上与上面 Props 表同名的也不重复列。Vue 的对应物是上面的插槽表。

React 组件属性类型必填说明
XhAlertRootchildrenReactNode

状态 ​

公开状态写入 data-state。

部件取值
root'open' | 'closed'

以下名称仅用于内部状态机。

状态:open · closed

事件:OPEN · CLOSE · CONTROLLED.OPEN · CONTROLLED.CLOSE · PRESS.START · PRESS.END

判据:isOpenControlled · canPress

connect API ​

getXxxProps() 返回对应部件的宿主属性。

成员类型说明
openboolean
tonestring
closableboolean
setOpen(next: boolean) => void
getRootProps() => T['element']
getIndicatorProps() => T['element']
getContentProps() => T['element']文本列容器:标题与说明纵向排列。
getTitleProps() => T['element']
getDescriptionProps() => T['element']
getActionProps() => T['element']操作槽:划定按钮区,按钮本身由作者提供。
getCloseTriggerProps() => T['button']

无障碍 ​

键盘 ​

规格出处:W3C APG

按键生效条件行为
Enter / Spacefocus 在 close-trigger 上且 closable收起提示并通知 open=false
Enter / Spaceheld on close-trigger, closable按住期间关闭按钮投影 data-pressed,与指针 :active 同一副按压面;抬起、失焦或提示收起撤下

ARIA ​

以下属性由 connect 生成。

部件属性值
rootaria-atomic'true'
rootaria-describedbydescription 部件的 id
rootaria-labelledbytitle 部件的 id
rootaria-livelive
rootrolerole
indicatoraria-hidden'true'
close-triggeraria-labelprops.translations.close

样式参考 ​

皮肤 ​

@xihan-ui/styles/alert.css 使用 [data-scope="alert"][data-part="root"] 部件选择器,位于 xihan.components 层。覆盖样式使用 xihan.overrides。

forced-colors: active 下另有一套规则:颜色交给系统,边框与状态标记改用系统色关键字。

数据属性 ​

由 connect 生成;条件不成立时不输出无值属性。

部件属性值
rootdata-state'open' | 'closed'
rootdata-toneprops.tone
close-triggerdata-disabled''(条件成立时才出现)
close-triggerdata-pressed''(条件成立时才出现)
close-triggerdata-xh-action-control''
close-triggerdata-xh-action-display'always'
close-triggerdata-xh-action-profile'icon'
close-triggerdata-xh-action-size'sm'
close-triggerdata-xh-action-variant'ghost'

CSS 变量 ​

本组件公开覆盖槽由独立皮肤的实际消费位生成;默认来源、作用部件和状态均与 CSS 同源。

变量部件CSS 属性状态默认来源说明
--xh-alert-action-gapactiongapdefault--xh-space-2alert 的 action 部件 gap 覆盖槽。
--xh-alert-bgrootbackgrounddefault--xh-bg-surfacealert 的 root 部件 background 覆盖槽。
--xh-alert-borderrootborderdefault--xh-border-defaultalert 的 root 部件 border 覆盖槽。
--xh-alert-close-bg-activeclose-triggerbackground-colordisabled
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-_action-variant-bg-pressedalert 的 close-trigger 部件 background-color 覆盖槽。
--xh-alert-close-bg-hoverclose-triggerbackground-colordisabled
hover
loading
not([data-disabled])
not([data-loading])
--xh-_action-variant-bg-hoveralert 的 close-trigger 部件 background-color 覆盖槽。
--xh-alert-close-fgclose-triggercolordefault--xh-fg-mutedalert 的 close-trigger 部件 color 覆盖槽。
--xh-alert-close-fg-hoverclose-triggercolordisabled
hover
is(:active, [data-pressed])
loading
not([data-disabled])
not([data-loading])
pressed
--xh-fg-defaultalert 的 close-trigger 部件 color 覆盖槽。
--xh-alert-close-radiusclose-triggerborder-radiusdefault--xh-shape-controlalert 的 close-trigger 部件 border-radius 覆盖槽。
--xh-alert-close-sizeclose-triggerblock-size
inline-size
min-inline-size
default
xh-action-profile=icon
--xh-_action-profile-visual-sizealert 的 close-trigger 部件 block-size、inline-size、min-inline-size 覆盖槽。
--xh-alert-content-gapcontentgapdefault--xh-space-1alert 的 content 部件 gap 覆盖槽。
--xh-alert-description-fgdescriptioncolordefault--xh-fg-mutedalert 的 description 部件 color 覆盖槽。
--xh-alert-description-font-sizedescriptionfont-sizedefault--xh-text-secondary-sizealert 的 description 部件 font-size 覆盖槽。
--xh-alert-fgrootcolordefault--xh-fg-defaultalert 的 root 部件 color 覆盖槽。
--xh-alert-font-sizerootfont-sizedefault--xh-text-body-sizealert 的 root 部件 font-size 覆盖槽。
--xh-alert-gaprootgapdefault--xh-space-4alert 的 root 部件 gap 覆盖槽。
--xh-alert-icon-sizeclose-trigger
root
--xh-icon-sizedefault--xh-_action-profile-glyph-size
--xh-glyph-size-md
alert 的 close-trigger、root 部件 --xh-icon-size 覆盖槽。
--xh-alert-indicator-fgindicatorcolordefault--xh-_tone-fgalert 的 indicator 部件 color 覆盖槽。
--xh-alert-indicator-pindicatorpaddingdefault--xh-space-1alert 的 indicator 部件 padding 覆盖槽。
--xh-alert-leadingrootline-heightdefault--xh-leading-normalalert 的 root 部件 line-height 覆盖槽。
--xh-alert-pxrootpadding-inlinedefault--xh-surface-px-smalert 的 root 部件 padding-inline 覆盖槽。
--xh-alert-pyrootpadding-blockdefault--xh-surface-py-smalert 的 root 部件 padding-block 覆盖槽。
--xh-alert-radiusrootborder-radiusdefault--xh-shape-surfacealert 的 root 部件 border-radius 覆盖槽。
--xh-alert-shadowrootbox-shadowdefaultnonealert 的 root 部件 box-shadow 覆盖槽。
--xh-alert-title-fgtitlecolordefault--xh-_tone-fgalert 的 title 部件 color 覆盖槽。
--xh-alert-title-font-sizetitlefont-sizedefault--xh-text-label-sizealert 的 title 部件 font-size 覆盖槽。
--xh-alert-title-font-weighttitlefont-weightdefault--xh-font-weight-semiboldalert 的 title 部件 font-weight 覆盖槽。
--xh-alert-title-leadingtitleline-heightdefault--xh-leading-tightalert 的 title 部件 line-height 覆盖槽。

动效 ​

动效角色:按压 · 状态(见动效规范)。

本组件皮肤不含过渡与关键帧,也没有脚本驱动的动效:状态一变,外观立即到位。

RTL ​

皮肤用逻辑属性排布(inline-start 一族),dir="rtl" 下自动镜像。

Released under The MIT License