Message全局提示
消息是一种简短、轻量的提示:在用户操作之后出现在页面顶部(例如“已保存”“上传失败”“加载中…”),并会自动消失。它不抢占焦点,也不打断用户。你可以在代码的任何位置通过调用来打开它,而不是在模板里写一个元素。
何时使用
- 用于反馈用户的操作结果:成功、错误、警告,或正在进行中的工作。
- 消息适用于不阻塞任何操作的简短反馈。需要用户做出决定时应使用对话框,带标题的较长文字应使用通知(二者均在计划中)。
- 消息会自动消失,因此不要把用户之后还需要再次查看的信息放在其中。
代码演示
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
</script>
<!-- A call from anywhere in the code: no setup, no markup. The library makes a layer on the page by itself. -->
<Button onclick={() => message.info('This is a normal message')}>Show message</Button>
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
</script>
<!-- Five kinds. error and warning are read out at once by a screen reader; the others wait for it to finish. -->
<div class="row">
<Button onclick={() => message.info('This is an information message')}>info</Button>
<Button onclick={() => message.success('This is a success message')}>success</Button>
<Button onclick={() => message.error('This is an error message')}>error</Button>
<Button onclick={() => message.warning('This is a warning message')}>warning</Button>
<Button onclick={() => message.loading('Action in progress..')}>loading</Button>
</div>
<style>
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
duration 1 (seconds; the default is 3)
duration 6
duration 0: until it is closed from code
<script lang="ts">
import { Button, message, type MessageHandle } from 'svelte-ant-design';
// The function that a call returns closes the message.
let hide: MessageHandle | undefined;
</script>
<div class="stack">
<div class="field">
<span class="caption">duration 1 (seconds; the default is 3)</span>
<Button onclick={() => message.info('Gone in a second', 1)}>1 second</Button>
</div>
<div class="field">
<span class="caption">duration 6</span>
<Button onclick={() => message.info('Stays for six seconds', 6)}>6 seconds</Button>
</div>
<div class="field">
<span class="caption">duration 0: until it is closed from code</span>
<div class="row">
<Button onclick={() => (hide = message.loading('Stays until you close it', 0))}>Open</Button>
<Button onclick={() => hide?.()}>Close it</Button>
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
// The same key updates the message in place: the box stays where it is, and a screen reader reads the new text.
function run() {
message.loading({ content: 'Loading...', key: 'updatable', duration: 0 });
setTimeout(() => message.success({ content: 'Loaded!', key: 'updatable', duration: 2 }), 1500);
}
</script>
<Button onclick={run}>Load</Button>
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
// The handle is thenable: the promise resolves when the message starts to close, so the next one comes in as
// the first one goes. (For the same reason `await message.info(...)` waits for the message to close.)
function run() {
message
.loading('Action in progress..', 2.5)
.then(() => message.success('Loading finished', 2.5))
.then(() => message.info('Loading finished is finished', 2.5));
}
</script>
<Button onclick={run}>Display sequential messages</Button>
content: string, number, snippet
content: a snippet with a button Undo pressed 0 times
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
let undone = $state(0);
</script>
<!-- A snippet can hold a link or a button. It is drawn in the layer, so it sees the context of the place where the
MessageHost is, not of this component. -->
{#snippet saved()}
Saved
<Button size="small" type="link" onclick={() => undone++}>Undo</Button>
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">content: string, number, snippet</span>
<div class="row">
<Button onclick={() => message.info('A string')}>string</Button>
<Button onclick={() => message.info(0)}>number 0</Button>
</div>
</div>
<div class="field">
<span class="caption">content: a snippet with a button</span>
<Button onclick={() => message.success({ content: saved, duration: 6 })}>snippet</Button>
<span role="status">Undo pressed {undone} times</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
</script>
<!-- A string is shown as text (an emoji, for example), a snippet is drawn as it is, false removes the icon. -->
{#snippet star()}
<svg viewBox="0 0 16 16" width="16" height="16" fill="currentColor" aria-hidden="true">
<path d="M8 1l2.1 4.6 5 .5-3.8 3.4 1.1 4.9L8 11.8 3.6 14.4l1.1-4.9L.9 6.1l5-.5z" />
</svg>
{/snippet}
<div class="row">
<Button onclick={() => message.info({ content: 'A string icon', icon: '🎉' })}>string</Button>
<Button onclick={() => message.warning({ content: 'A snippet icon', icon: star })}>snippet</Button>
<Button onclick={() => message.success({ content: 'No icon', icon: false })}>false</Button>
</div>
<style>
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
class, style: on the box
classNames, styles: the parts (root, wrapper, icon, title, close)
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">class, style: on the box</span>
<Button
onclick={() => message.info({ content: 'Styled box', class: 'fancy', style: { 'border-radius': '24px' } })}
>
class and style
</Button>
</div>
<div class="field">
<span class="caption">classNames, styles: the parts (root, wrapper, icon, title, close)</span>
<Button
onclick={() =>
message.success({
content: 'Styled parts',
closable: true,
classNames: { title: 'fancy-title' },
styles: { root: { background: 'var(--ads-color-primary-solid)', color: '#fff' }, icon: { color: '#fff' }, close: { color: '#fff' } }
})}
>
classNames and styles
</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
/* The message is drawn outside this component, so a class that styles it is global here. */
:global(.fancy) {
outline: 2px dashed var(--ads-color-primary);
}
:global(.fancy-title) {
font-weight: 600;
letter-spacing: 0.02em;
}
</style>
pauseOnHover (the default), duration 3: keep the pointer over it
pauseOnHover: false
closable, duration 3: Tab into it, and it waits for you
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
</script>
<!-- Hover (a mouse or a pen, not a touch) and focus inside the message stop the timer; it goes on with the time that
was left. The timer also waits while the page is hidden. -->
<div class="stack">
<div class="field">
<span class="caption">pauseOnHover (the default), duration 3: keep the pointer over it</span>
<Button onclick={() => message.info('Hover me: I stay while you do')}>pauses</Button>
</div>
<div class="field">
<span class="caption">pauseOnHover: false</span>
<Button onclick={() => message.info({ content: 'I go on, whatever you do', pauseOnHover: false })}>does not pause</Button>
</div>
<div class="field">
<span class="caption">closable, duration 3: Tab into it, and it waits for you</span>
<Button onclick={() => message.warning({ content: 'Focus inside holds the timer', closable: true })}>holds on focus</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
clicked 0, closed 0
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
let clicks = $state(0);
let closes = $state(0);
</script>
<!-- onclick is for a mouse or a finger only: a box cannot be reached with the keyboard. Put a button in the content
(see Content) for an action. onclose is called once, when the message starts to close, however it was closed. -->
<div class="field">
<Button onclick={() => message.info({ content: 'Click me', onclick: () => clicks++, onclose: () => closes++ })}>Open</Button>
<span role="status">clicked {clicks}, closed {closes}</span>
</div>
<style>
.field {
display: grid;
justify-items: start;
gap: 8px;
}
</style>
closable, duration 0: stays until the button is pressed
closable, duration 3
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
</script>
<!-- closable adds a button. Enter and Space press it, and Escape closes the message that has focus inside it. -->
<div class="stack">
<div class="field">
<span class="caption">closable, duration 0: stays until the button is pressed</span>
<Button onclick={() => message.info({ content: 'A message that stays', closable: true, duration: 0 })}>stays</Button>
</div>
<div class="field">
<span class="caption">closable, duration 3</span>
<Button onclick={() => message.error({ content: 'Upload failed', closable: true })}>can be closed</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
maxCount not set
<script lang="ts">
import { Button, Switch, message } from 'svelte-ant-design';
let limited = $state(false);
let opened = $state(0);
// maxCount is a setting of message.config(); undefined takes it back to the default.
$effect(() => {
message.config({ maxCount: limited ? 3 : undefined });
return () => message.config({ maxCount: undefined });
});
</script>
<div class="stack">
<div class="row">
<Switch bind:checked={limited} aria-label="maxCount 3" />
<span>maxCount {limited ? 3 : 'not set'}</span>
</div>
<div class="field">
<div class="row">
<Button onclick={() => message.info({ content: `Message ${++opened}`, duration: 0 })}>Open one</Button>
<Button onclick={() => message.destroy()}>Close all</Button>
</div>
<span role="status">The oldest closes to make room: {opened} opened</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
top: 8 (px)
<script lang="ts">
import { Button, message } from 'svelte-ant-design';
let top = $state<number | string>(8);
$effect(() => {
message.config({ top });
return () => message.config({ top: undefined });
});
</script>
<!-- top is a number (pixels) or any CSS length. -->
<div class="stack">
<div class="field">
<span class="caption">top: {typeof top === 'number' ? `${top} (px)` : `'${top}'`}</span>
<div class="row">
<Button onclick={() => (top = 8)}>8</Button>
<Button onclick={() => (top = 80)}>80</Button>
<Button onclick={() => (top = '30vh')}>'30vh'</Button>
</div>
</div>
<Button onclick={() => message.info({ content: 'Where am I?', duration: 5 })}>Show message</Button>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
stack off
<script lang="ts">
import { Button, Switch, message } from 'svelte-ant-design';
let stacked = $state(false);
let opened = $state(0);
// stack is a setting of message.config(): true collapses the messages into a pile after 3 of them (or { threshold }).
$effect(() => {
message.config({ stack: stacked ? { threshold: 3 } : undefined });
return () => message.config({ stack: undefined });
});
</script>
<!-- The newest message is the first row. Past the threshold the others lie behind it; the pointer or focus opens the
pile, and every timer waits while it is open. -->
<div class="stack">
<div class="row">
<Switch bind:checked={stacked} aria-label="stack" />
<span>stack {stacked ? 'on, threshold 3' : 'off'}</span>
</div>
<div class="row">
<Button onclick={() => message.info({ content: `Message ${++opened}: This is a stacked message.`, closable: true, duration: 10 })}>Open</Button>
<Button onclick={() => message.destroy()}>Destroy all</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
<MessageHost top={72} closable labels={...} /> not mounted
<script lang="ts">
import { Button, MessageHost, Switch, message } from 'svelte-ant-design';
let mounted = $state(false);
</script>
<!-- <MessageHost> draws the messages where you put it. Its props are the settings and win over message.config().
Without one the library makes its own layer. Only one host is active at a time. -->
{#if mounted}
<MessageHost top={72} closable labels={{ close: 'Dismiss', success: 'Done', error: 'Failed' }} />
{/if}
<div class="stack">
<div class="row">
<Switch bind:checked={mounted} aria-label="MessageHost" />
<span><MessageHost top={72} closable labels={...} /> {mounted ? 'mounted' : 'not mounted'}</span>
</div>
<div class="row">
<Button onclick={() => message.success('Saved')}>success</Button>
<Button onclick={() => message.error('Upload')}>error</Button>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
a modal dialog
<script lang="ts">
import { tick } from 'svelte';
import { Button, message } from 'svelte-ant-design';
const KEY = 'container-dialog';
let dialog = $state<HTMLDialogElement | null>(null);
// Null before the first press; then where the layer was found: in the body or in the dialog.
let placed = $state<'body' | 'dialog' | null>(null);
// `container` is a setting of message.config(): the element that the layer is put in (the body by default). A native modal
// <dialog> is shown in the top layer of the browser, over everything that is outside of it, and nothing outside it can be reached.
// So a layer in the body is covered by the dialog, and a layer in the dialog stands above it and can be used. The setting is
// given back (and the message closed) when the dialog closes, and when the example is left, so that the page is not touched.
async function show(inDialog: boolean) {
if (!dialog) return;
message.config({ container: inDialog ? dialog : undefined });
message.info({
key: KEY,
content: inDialog ? 'In the dialog' : 'In the body',
duration: 8,
closable: true,
class: 'container-demo'
});
await tick();
placed = dialog.contains(document.querySelector('.container-demo')) ? 'dialog' : 'body';
}
function release() {
message.destroy(KEY);
message.config({ container: undefined });
placed = null;
}
$effect(() => {
return () => message.config({ container: undefined });
});
</script>
<div class="stack">
<div class="field">
<span class="caption">a modal dialog</span>
<div class="row">
<Button onclick={() => dialog?.showModal()}>Open the dialog</Button>
</div>
</div>
</div>
<dialog bind:this={dialog} aria-label="The container example" onclose={release}>
<div class="stack">
<div class="field">
<span class="caption">container: not set (the body)</span>
<Button onclick={() => show(false)}>Show a message</Button>
<span class="out" role="status">
{#if placed === 'body'}
The layer is in the body. The dialog covers it, and the message cannot be reached.
{:else}
Press to see where the layer goes.
{/if}
</span>
</div>
<div class="field">
<span class="caption">container: the dialog</span>
<Button onclick={() => show(true)}>Show a message</Button>
<span class="out" role="status">
{#if placed === 'dialog'}
The layer is in the dialog. The message stands above it and can be closed.
{:else}
Press to see where the layer goes.
{/if}
</span>
</div>
<div class="row">
<Button onclick={() => dialog?.close()}>Close the dialog</Button>
</div>
</div>
</dialog>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
.out {
min-height: 36px;
color: var(--ads-color-text-secondary);
font-size: 12px;
line-height: 18px;
}
dialog {
box-sizing: border-box;
width: min(420px, calc(100vw - 32px));
padding: 20px 24px;
border: 1px solid var(--ads-color-border);
border-radius: var(--ads-radius-lg);
background: var(--ads-color-bg-container);
color: var(--ads-color-text);
}
/* Dark enough to show that what stands under it is covered. */
dialog::backdrop {
background: rgb(0 0 0 / 55%);
}
</style>
API
消息选项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type | 消息类型。仅用于 message.open;带类型的方法已固定类型。 | 'info' | 'success' | 'error' | 'warning' | 'loading' | 'info' |
| content | 文字或内容:字符串、数字(0 会显示)或 snippet。 | string | number | Snippet | — |
| duration | 消息消失前的秒数。0 表示一直显示直到被关闭。负数或非有限值会回退到默认值,开发时会给出警告。 | number | 3 |
| icon | 替换类型对应的图标:字符串(按文字显示)、snippet,或 false 表示不显示。 | string | Snippet | false | the icon of the type |
| key | 使用已打开消息的 key 调用会原地更新该消息(新的类型、内容和选项,计时器重新开始)。未提供时会自动生成。 | string | number | generated |
| pauseOnHover | 指针(鼠标或触控笔)在消息框上时暂停计时。焦点在框内或页面被隐藏时,无论此值如何都会暂停。 | boolean | true |
| closable | 显示关闭按钮。Ant Design 没有此选项。 | boolean | false |
| class | 消息框的额外类名。 | string | — |
| style | 消息框的内联样式:字符串或 CSS 属性对象(kebab-case 或自定义属性;数字按原样写入)。 | string | Record<string, string | number> | — |
| classNames | 为 root、wrapper、icon、title 和 close 部分添加的类名。此处的值优先于宿主和 message.config() 中的值。 | { root?, wrapper?, icon?, title?, close? } | — |
| styles | 同样各部分的内联样式,使用对象。 | { root?, wrapper?, icon?, title?, close? } | — |
| onclick | 点击消息框时(而非其关闭按钮时)带事件调用。键盘无法到达消息框:需要操作时请把按钮放进内容。 | (event: MouseEvent) => void | — |
| onclose | 在消息开始关闭时调用一次,无论以何种方式关闭(计时器、返回的函数、destroy、maxCount、关闭按钮、Escape)。 | () => void | — |
方法
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| message.open(options) | 打开 options.type 类型的消息(默认 info),或更新具有相同 key 的已打开消息。 | MessageHandle | — |
| message.info | success | error | warning | loading(content, duration?, onclose?) | 每种类型一个方法。没有窗口时(服务器端)调用不会有任何效果,并返回一个无效的 handle。 | MessageHandle | — |
| message.info | success | error | warning | loading(options) | 同上,参数为选项对象(不含 type)。 | MessageHandle | — |
| message.destroy(key?) | 关闭该 key 对应的消息,或关闭所有消息。每条消息都会执行正常的离场过程并调用其 onclose。 | void | — |
| message.config(options) | 设置此后所有消息的默认值(见下方设置表)。以 { top: undefined } 传入的设置会还原为默认值。 | void | — |
| handle() | 调用返回的内容:一个用于关闭消息的函数。关闭正在关闭或已关闭的消息不会有任何效果。 | () => void | — |
| handle.then(onfulfilled) | handle 是 thenable:promise 在消息开始关闭时完成。因此 await handle 会等到消息关闭。 | PromiseLike<void> | — |
设置:message.config() 与 MessageHost 的 props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| top | 第一条消息距窗口顶部的距离。数字表示像素。未设置时使用令牌 --ads-message-top(8px)。 | number | string | 8 |
| duration | 每条消息的默认 duration,单位为秒。 | number | 3 |
| maxCount | 最多同时打开这么多条消息:最早的一条会被关闭以腾出位置。小于 1 或非有限的值会被忽略。 | number | — |
| container | 浮层移入的位置:一个元素,或返回元素的函数。默认 document.body。 | HTMLElement | (() => HTMLElement) | document.body |
| closable | 每条消息 closable 的默认值。 | boolean | false |
| labels | 屏幕阅读器在文字之前听到的词(info、success、error、warning、loading,例如 "Error: Upload failed")以及关闭按钮的名称(close)。请传入翻译;空字符串会去掉类型词。 | Partial<Record<'info' | 'success' | 'error' | 'warning' | 'loading' | 'close', string>> | 'Info', 'Success', 'Error', 'Warning', 'Loading', 'Close' |
| classNames | 每条消息各部分的默认类名,以及浮层的两个部分:list(固定浮层)和 listContent(行的列)。 | { root?, wrapper?, icon?, title?, close?, list?, listContent? } | — |
| styles | 内联样式的同样设置,使用对象。 | { root?, wrapper?, icon?, title?, close?, list?, listContent? } | — |
| stack | true 或 { threshold }:最新消息显示在第一行,超过阈值(3)后其余消息叠在它后面,指针或焦点会将其展开。React 6.4.0 有此功能,Vue 和 Angular 没有。 | boolean | { threshold?: number } | false |
与 Ant Design 的差异:浮层由库自己创建(React 的
useMessage 和 contextHolder 由 <MessageHost> 取代);按类型设置角色并加入隐藏的类型词(React:全部为 role="alert",Vue 和 Angular:均无);closable 是新增的,计时器还会因焦点和页面隐藏而暂停;duration 与 React、Vue 一样以秒为单位(Angular 为毫秒);stack 仅 React 6.4.0 有。未采用:Vue 的 warn 别名、prefixCls 和 rtl(方向由环境继承)。API 受 Ant Design(MIT)启发;实现为面向 Svelte 的原创实现。设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-message-z-index | 消息的层级,高于所有弹出层(文字提示为 1070)。 | 2010 |
| --ads-message-top | 未设置 top 时,第一条消息距顶部的距离。 | 8px |
| --ads-message-gap | 两条消息之间的间距。 | 16px |
| --ads-message-margin-inline | 窗口左右边缘保留的空白。 | 24px |
| --ads-message-bg | 消息框背景。深色主题为 #1f1f1f。 | #ffffff |
| --ads-message-color | 文字颜色。深色主题为 85% 白色。 | rgba(0, 0, 0, 0.88) |
| --ads-message-padding | 消息框内边距。 | 9px 12px |
| --ads-message-line-height | 文字行与图标的高度,因此无论有无图标消息框都是 40px 高。 | 22px |
| --ads-message-radius | 消息框圆角半径。 | 8px |
| --ads-message-shadow | 消息框阴影。 | 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05) |
| --ads-message-icon-size | 图标大小。 | 16px |
| --ads-message-icon-gap | 图标、文字与关闭按钮之间的间距。 | 8px |
| --ads-message-icon-info | info 与 loading 图标的颜色。 | #1677ff |
| --ads-message-icon-success | success 图标的颜色。tokens-accessible.css 会将其加深为 #389e0d(在白色消息框上达到 3:1)。 | #52c41a |
| --ads-message-icon-error | error 图标的颜色。 | #ff4d4f |
| --ads-message-icon-warning | warning 图标的颜色。tokens-accessible.css 会将其加深为 #c27c00。 | #faad14 |
| --ads-message-close-size | 关闭按钮大小:24px,WCAG 2.5.8 要求的最小目标尺寸。 | 24px |
| --ads-message-close-color | 关闭按钮颜色(在消息框上达到 4.5:1)。 | rgba(0, 0, 0, 0.65) |
| --ads-message-close-color-hover | 关闭按钮悬停时的颜色。 | rgba(0, 0, 0, 0.88) |
| --ads-message-close-bg | 关闭按钮平时的背景:用来显示按钮所在的位置。 | rgba(0, 0, 0, 0.04) |
| --ads-message-close-bg-hover | 关闭按钮悬停时的背景。 | rgba(0, 0, 0, 0.1) |
| --ads-message-motion-offset | 消息进入时的起始位置与离开时的终止位置距其所在位置的上方距离。 | 64px |
| --ads-message-stack-offset | 堆叠中各行之间的偏移。 | 8px |
| --ads-message-stack-scale-step | 堆叠中每一行比前一行缩小的比例。 | 0.06 |
无障碍
error和warning使用role="alert",会被立即朗读;info、success和loading使用role="status",会等屏幕阅读器读完。Ant Design React 给全部五种类型都设置了role="alert",Vue 和 Angular 则都没有设置。- 图标仅作装饰。文字前有一个隐藏的词来说明类型(“Error: Upload failed”),因此类型不会只靠颜色和形状传达。请通过
labels传入翻译:库目前还没有语言包系统,默认是英文。 - 消息不会抢占焦点。没有
closable时其中没有可聚焦的内容;有的话只有关闭按钮可聚焦(24px,WCAG 2.5.8 要求的目标尺寸)。Escape会关闭焦点所在的消息。 - 指针在消息上、焦点在消息内,或页面被隐藏时,计时器都会暂停。需要一直保留的消息要设置
duration: 0,再用closable让用户自行关闭。 - 浮层是其容器中的最后一个元素,因此键盘要经过页面其余内容之后才能到达关闭按钮,也没有把焦点移到消息的快捷键。屏幕阅读器会在消息打开时朗读它。
- 对比度:文字在消息框上约为 15:1,各类型图标保持在 3:1 以上(深色主题中如此,浅色主题需引入
tokens-accessible.css,它会加深绿色和琥珀色)。 - 在 Windows 高对比度等 forced-colors 模式下,消息框和关闭按钮会带边框显示。在
prefers-reduced-motion下动画(消息框从上方进入并淡入)会关闭,计时器不变。 - 已用真实读屏软件(Orca 搭配 Firefox)验证:它能读出角色、类型词、文字、更新后的新文字以及关闭按钮的名称。未验证:读屏软件在消息出现时是否会播报(我们的 Orca 环境连普通的
role="alert"都不会朗读)、NVDA、JAWS、VoiceOver、手机读屏软件、真实触摸设备以及真实的 Safari。