Notification通知提醒框
通知是包含标题、文字以及可选按钮的提示框。它显示在窗口的角落或上、下边缘,一段时间后自动关闭,并且不抢占焦点。它比消息更重:能传达更多内容,并停靠在窗口边缘而不是内容上方。你可以在代码的任何位置通过调用来打开它,而不是在模板里写一个元素。
何时使用
- 用于需要标题和文字的反馈:上传完成、同步失败、有新版本、收到某人的消息。
- 只说一件事的简短反馈应使用
Message。需要用户做出决定时应使用对话框(Modal)。 - 通知会自动消失,因此不要把用户之后还需要再次查看的信息放在其中;或者设置
duration: 0,让用户自行关闭。
代码演示
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
<!-- A call from anywhere shows a notification at the top right: no setup and no markup. It goes after 4.5 s. -->
<Button
type="primary"
onclick={() =>
notification.open({
title: 'Notification title',
description: 'This is the content of the notification. A title, and a text under it.'
})}
>
Open the notification
</Button>
open: no type icon
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
const text = 'The type gives the icon, and a screen reader the type word before the title.';
</script>
<!-- Four kinds with an icon, and `open`, which has none. `error` and `warning` are read out at once, the others wait. -->
<div class="stack">
<div class="row">
<Button onclick={() => notification.success({ title: 'Saved', description: text })}>success</Button>
<Button onclick={() => notification.info({ title: 'New version', description: text })}>info</Button>
<Button onclick={() => notification.warning({ title: 'Almost full', description: text })}>warning</Button>
<Button onclick={() => notification.error({ title: 'Upload failed', description: text })}>error</Button>
</div>
<div class="field">
<span class="caption">open: no type icon</span>
<Button onclick={() => notification.open({ title: 'No icon', description: text })}>open</Button>
</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, notification, type NotificationPlacement } from 'svelte-ant-design';
const top: NotificationPlacement[] = ['topLeft', 'top', 'topRight'];
const bottom: NotificationPlacement[] = ['bottomLeft', 'bottom', 'bottomRight'];
const show = (placement: NotificationPlacement) =>
notification.info({ title: placement, description: 'The newest stands nearest the window edge.', placement, duration: 6 });
</script>
<!-- Six places. Each is its own list: the newest notification is nearest the edge, the older ones are pushed away from it. -->
<div class="stack">
<div class="row">
{#each top as placement (placement)}
<Button onclick={() => show(placement)}>{placement}</Button>
{/each}
</div>
<div class="row">
{#each bottom as placement (placement)}
<Button onclick={() => show(placement)}>{placement}</Button>
{/each}
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
</style>
duration 10
duration 0: until it is closed
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
let close: (() => void) | undefined;
</script>
<!-- `duration` is in seconds (default 4.5). `0` or `false` keeps the notification until it is closed. -->
<div class="stack">
<div class="field">
<span class="caption">duration 10</span>
<Button onclick={() => notification.info({ title: 'Ten seconds', description: 'It waits longer than usual.', duration: 10 })}>10 s</Button>
</div>
<div class="field">
<span class="caption">duration 0: until it is closed</span>
<div class="row">
<Button onclick={() => (close = notification.warning({ title: 'Stays', description: 'Close it with the cross, or with the other button.', duration: 0 }))}>
Open
</Button>
<Button onclick={() => close?.()}>Close it by code</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, notification } from 'svelte-ant-design';
// The same key updates the notification in place: the box stays where it is, and a screen reader reads the new text.
function run() {
notification.info({ title: 'Uploading', description: 'The file is on its way.', key: 'upload', duration: 0 });
setTimeout(() => notification.success({ title: 'Uploaded', description: 'The file is on the server.', key: 'upload', duration: 4 }), 1800);
}
</script>
<Button onclick={run}>Upload</Button>
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
const key = 'actions';
const undo = () => notification.destroy(key);
</script>
<!-- `actions` is content under the text, at the end: usually buttons. They are your elements, so the keyboard reaches them
after the text and before the close button. -->
{#snippet actions()}
<Button size="small" onclick={undo}>Undo</Button>
<Button size="small" type="primary" onclick={undo}>Keep</Button>
{/snippet}
<Button onclick={() => notification.info({ title: 'Message moved', description: 'It is in the archive now.', actions, key, duration: 0 })}>
With actions
</Button>
icon: a string
icon: a snippet
icon: false
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
{#snippet star()}<span style="color: gold; font-size: 24px">★</span>{/snippet}
<!-- `icon` replaces the icon of the type: a string (an emoji), a snippet, or `false` for none. -->
<div class="stack">
<div class="field">
<span class="caption">icon: a string</span>
<Button onclick={() => notification.success({ title: 'Launched', description: 'The icon is an emoji.', icon: '🚀' })}>string</Button>
</div>
<div class="field">
<span class="caption">icon: a snippet</span>
<Button onclick={() => notification.open({ title: 'Starred', description: 'The icon is a snippet.', icon: star })}>snippet</Button>
</div>
<div class="field">
<span class="caption">icon: false</span>
<Button onclick={() => notification.error({ title: 'Without icon', description: 'The type word is still read by a screen reader.', icon: false })}>false</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>
closable: false
closeIcon: a string
closeIcon: a snippet
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
{#snippet word()}Close{/snippet}
<!-- The close button is there by default. `closable: false` or `closeIcon: false` removes it; `closeIcon` replaces the cross. -->
<div class="stack">
<div class="field">
<span class="caption">closable: false</span>
<Button onclick={() => notification.info({ title: 'No close button', description: 'It goes by itself.', closable: false })}>closable false</Button>
</div>
<div class="field">
<span class="caption">closeIcon: a string</span>
<Button onclick={() => notification.info({ title: 'A text button', description: 'The name of the button stays "Close".', closeIcon: '✕', duration: 8 })}>string</Button>
</div>
<div class="field">
<span class="caption">closeIcon: a snippet</span>
<Button onclick={() => notification.info({ title: 'A snippet button', description: 'Any content is the icon.', closeIcon: word, duration: 8 })}>snippet</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>
showProgress, duration 8: hold the pointer over it
showProgress, pauseOnHover: false
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
<!-- `showProgress` draws a strip that runs down with the time left. It is drawn from the timer that closes the notification, so it
stands still while the pointer or the focus holds it. It is decorative: it is not announced. -->
<div class="stack">
<div class="field">
<span class="caption">showProgress, duration 8: hold the pointer over it</span>
<Button onclick={() => notification.info({ title: 'Progress', description: 'The strip waits when the timer waits.', showProgress: true, duration: 8 })}>
with a strip
</Button>
</div>
<div class="field">
<span class="caption">showProgress, pauseOnHover: false</span>
<Button onclick={() => notification.info({ title: 'No pause', description: 'It goes on while you hold the pointer.', showProgress: true, pauseOnHover: false, duration: 8 })}>
does not pause
</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>
pauseOnHover (the default), duration 5: keep the pointer over it
pauseOnHover: false
duration 5: Tab to the close button, and it waits for you
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
<!-- Hover (a mouse or a pen, not a touch) and focus inside 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 5: keep the pointer over it</span>
<Button onclick={() => notification.info({ title: 'Hover me', description: 'I stay while you do.', duration: 5 })}>pauses</Button>
</div>
<div class="field">
<span class="caption">pauseOnHover: false</span>
<Button onclick={() => notification.info({ title: 'I go on', description: 'Whatever you do.', duration: 5, pauseOnHover: false })}>does not pause</Button>
</div>
<div class="field">
<span class="caption">duration 5: Tab to the close button, and it waits for you</span>
<Button onclick={() => notification.warning({ title: 'Focus holds it', description: 'Focus inside the box stops the timer.', duration: 5 })}>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>
stack on (the default), threshold 3
<script lang="ts">
import { Button, Switch, notification } from 'svelte-ant-design';
let stacked = $state(true);
let opened = $state(0);
// `stack` is a setting of notification.config(): on by default, `{ threshold }` sets the number, `false` turns it off.
$effect(() => {
notification.config({ stack: stacked ? undefined : false });
return () => notification.config({ stack: undefined });
});
</script>
<!-- Past the threshold (3) the older notifications of a place lie behind the newest one; the pointer or the 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 (the default), threshold 3' : 'off'}</span>
</div>
<div class="row">
<Button onclick={() => notification.info({ title: `Notification ${++opened}`, description: 'This is a stacked notification.', duration: 12 })}>Open</Button>
<Button onclick={() => notification.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>
maxCount not set
<script lang="ts">
import { Button, Switch, notification } from 'svelte-ant-design';
let limited = $state(false);
let opened = $state(0);
let pushed = $state(0);
// maxCount is a setting of notification.config(): it counts the notifications of all places together. undefined takes it back
// to the default (no limit), so the other examples of the page are not limited.
$effect(() => {
notification.config({ maxCount: limited ? 3 : undefined });
return () => notification.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={() =>
notification.info({
title: `Notification ${++opened}`,
description: 'The oldest closes to make room.',
duration: 0,
onclose: (reason) => reason === 'replaced' && pushed++
})}
>
Open one
</Button>
<Button onclick={() => notification.destroy()}>Close all</Button>
</div>
<span role="status">{opened} opened, pushed out by the limit: {pushed}</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>
class, style: on the box
classNames, styles: the parts (root, icon, section, title, description, actions, close, progress)
props: data-* and aria-* on the box
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
<div class="stack">
<div class="field">
<span class="caption">class, style: on the box</span>
<Button onclick={() => notification.info({ title: 'Styled box', description: 'A class and a style.', class: 'fancy', style: { 'border-radius': '24px' } })}>
class and style
</Button>
</div>
<div class="field">
<span class="caption">classNames, styles: the parts (root, icon, section, title, description, actions, close, progress)</span>
<Button
onclick={() =>
notification.success({
title: 'Styled parts',
description: 'The box, the title and the strip.',
showProgress: 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 class="field">
<span class="caption">props: data-* and aria-* on the box</span>
<Button onclick={() => notification.info({ title: 'With attributes', props: { 'data-testid': 'my-note', 'aria-label': 'Custom label' } })}>props</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 notification 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>
box clicks: 0; closed by: —
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
let clicked = $state(0);
let reason = $state('');
</script>
<!-- `onclick` is called when the box is clicked (not the close button). `onclose` is called once, when the notification starts to
close, with the reason: 'timeout', 'user' (the close button, Escape), 'replaced' (the limit) or 'destroy' (code). -->
<div class="stack">
<div class="field">
<Button
onclick={() =>
notification.info({
title: 'Click me, or close me',
description: 'Wait, close it, or press Escape inside it.',
duration: 6,
onclick: () => clicked++,
onclose: (why) => (reason = why)
})}
>
Open
</Button>
<span role="status">box clicks: {clicked}; closed by: {reason || '—'}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
</style>
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
</script>
<!-- Title, description and actions are a string, a number or a snippet. A snippet is drawn in the layer, so it sees the context of the
place where the host is, not of the component that wrote it. -->
{#snippet title()}<strong>Rich</strong> <em>title</em>{/snippet}
{#snippet description()}A link to <a href="#content">the docs</a> and <code>code</code>.{/snippet}
<Button onclick={() => notification.info({ title, description, duration: 10 })}>Snippets</Button>
<NotificationHost placement="bottomLeft" bottom={72} ... /> not mounted
<script lang="ts">
import { Button, NotificationHost, Switch, notification } from 'svelte-ant-design';
let mounted = $state(false);
</script>
<!-- <NotificationHost> draws the notifications where you put it. Its props are the settings and win over notification.config(). Without one the library
makes its own layer. Only one host is active at a time. -->
{#if mounted}
<NotificationHost placement="bottomLeft" bottom={72} duration={8} labels={{ close: 'Dismiss', success: 'Done', error: 'Failed' }} />
{/if}
<div class="stack">
<div class="row">
<Switch bind:checked={mounted} aria-label="NotificationHost" />
<span><NotificationHost placement="bottomLeft" bottom={72} ... /> {mounted ? 'mounted' : 'not mounted'}</span>
</div>
<div class="row">
<Button onclick={() => notification.success({ title: 'Saved' })}>success</Button>
<Button onclick={() => notification.error({ title: '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>
<script lang="ts">
import { Button, notification } from 'svelte-ant-design';
let opened = $state(0);
</script>
<!-- `notification.destroy(key)` closes one, `notification.destroy()` closes every notification. -->
<div class="stack">
<div class="row">
<Button onclick={() => notification.info({ title: `Notification ${++opened}`, key: `n${opened}`, duration: 0 })}>Open</Button>
<Button onclick={() => notification.destroy(`n${opened}`)}>Close the last</Button>
<Button onclick={() => notification.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>
a modal dialog
<script lang="ts">
import { tick } from 'svelte';
import { Button, notification } 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 notification.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 notification 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;
notification.config({ container: inDialog ? dialog : undefined });
notification.info({
key: KEY,
title: inDialog ? 'In the dialog' : 'In the body',
description: inDialog ? 'Above the dialog, and it can be closed.' : 'Behind the dialog.',
duration: 8,
props: { 'data-demo': 'container' }
});
await tick();
placed = dialog.contains(document.querySelector('[data-demo="container"]')) ? 'dialog' : 'body';
}
function release() {
notification.destroy(KEY);
notification.config({ container: undefined });
placed = null;
}
$effect(() => {
return () => notification.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 notification</Button>
<span class="out" role="status">
{#if placed === 'body'}
The layer is in the body. The dialog covers it, and the notification 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 notification</Button>
<span class="out" role="status">
{#if placed === 'dialog'}
The layer is in the dialog. The notification 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
通知选项
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| title | 第一行:字符串、数字(0 会显示)或 snippet。必填。这是 Ant Design React 6 使用的名称 title(那里的 message 已弃用)。 | NotificationContent | — |
| description | 标题下方的文字。可选:只有标题也是有效的通知。 | NotificationContent | — |
| actions | 文字下方、行末位置的内容:通常是按钮。你放在这里的元素就是键盘可以到达的元素(React 的 actions,Vue 的 btn)。 | NotificationContent | — |
| icon | 替换类型图标;false 会移除图标。open 默认没有图标,除非传入一个。 | string | Snippet | false | the icon of the type |
| duration | 通知消失前的秒数。0 或 false 表示一直显示,直到被关闭。负数或非有限数值会回退为默认值(开发时会有警告)。Angular 使用毫秒,这里不是。 | number | false | 4.5 |
| key | 使用已打开通知的 key 调用会原地更新该通知(新的类型、文字和选项,计时器重新开始)。未提供时会自动生成。 | string | number | generated |
| placement | 通知所在的位置:它自己的位置,或宿主与 notification.config() 的默认位置。 | 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight' | 'topRight' |
| closable | 显示关闭按钮。未设置时使用宿主和 notification.config() 的默认值。 | boolean | true |
| closeIcon | 替换关闭按钮的叉号;false 会移除按钮。按钮对屏幕阅读器的名称仍为 labels.close。未采用 React closable 的对象形式:对应的是 closeIcon 和 onclose。 | string | Snippet | false | the cross |
| showProgress | 在通知框底部显示一条随剩余时间缩短的进度条。计时器暂停时它也停下,duration: 0 时没有进度条。 | boolean | false |
| pauseOnHover | 指针(鼠标或触控笔)在通知框上时暂停计时。焦点在框内、堆叠展开或页面被隐藏时,无论此值如何都会暂停。 | boolean | true |
| role | 覆盖角色。默认 error 和 warning 为 alert(立即朗读),其余为 status。 | 'alert' | 'status' | by type |
| class | 通知框的额外类名。 | string | — |
| style | 通知框的内联样式:字符串或 CSS 属性对象(kebab-case 或自定义属性;数字按原样写入)。 | string | Record<string, string | number> | — |
| classNames | 为各部分添加的类名。此处的值优先于宿主和 notification.config() 中的值。 | { root?, icon?, section?, title?, description?, actions?, close?, progress? } | — |
| styles | 内联样式的同样设置,使用对象。 | { root?, icon?, section?, title?, description?, actions?, close?, progress? } | — |
| props | 放在通知框上的属性(React 的 props):data-* 和 aria-*。 | Record<`data-${string}` | `aria-${string}`, string> | — |
| onclick | 点击通知框(而非其关闭按钮)时带着事件调用。点击不会关闭通知。 | (event: MouseEvent) => void | — |
| onclose | 通知开始关闭时调用一次,并带有原因:时间到、用户关闭(关闭按钮或 Escape)、被数量限制挤出,或由代码关闭(destroy 或返回的函数)。Angular 只告知是否由用户关闭;React 和 Vue 不传任何参数。 | (reason: 'timeout' | 'user' | 'replaced' | 'destroy') => void | — |
方法
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| notification.open(options) | 打开一条没有类型图标的通知,或更新具有相同 key 的已打开通知。返回一个用于关闭它的函数。它不是 thenable(Message 的是)。 | () => void | — |
| notification.info | success | error | warning(options) | 每种类型一个方法,选项相同。没有窗口时(服务器端)调用不会有任何效果,并返回一个什么都不做的函数。 | () => void | — |
| notification.destroy(key?) | 关闭该 key 对应的通知,或关闭所有通知。每条通知都会执行正常的离场过程,并以原因 'destroy' 调用其 onclose。 | void | — |
| notification.config(options) | 设置此后所有通知的默认值(见下方设置表)。以 { top: undefined } 传入的设置会还原为默认值。 | void | — |
设置:notification.config() 与 NotificationHost 的 props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| top | 顶部位置距窗口顶部的距离。数字表示像素。 | number | string | 24 |
| bottom | 底部位置距窗口底部的距离。数字表示像素。 | number | string | 24 |
| placement | 未指定位置的通知所在的位置。 | 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight' | 'topRight' |
| duration | 默认时长(秒)。0 和 false 表示不自动关闭。 | number | false | 4.5 |
| maxCount | 最多同时打开这么多条通知(所有位置合计);会关闭最早的一条(原因 'replaced')。Ant Design React 和 Angular 的计数方式相同,Vue 则按位置分别计数。 | number | — |
| container | 放置浮层的元素。只决定它在页面中的位置:浮层仍为 position: fixed,绘制在窗口处(对应 Ant Design 的 getContainer)。适用于原生模态 <dialog>,它会盖住其外部的所有内容。 | HTMLElement | (() => HTMLElement) | document.body |
| closable | closable 的默认值。 | boolean | true |
| closeIcon | closeIcon 的默认值。 | string | Snippet | false | the cross |
| showProgress | showProgress 的默认值。 | boolean | false |
| pauseOnHover | pauseOnHover 的默认值。 | boolean | true |
| labels | 标题前朗读的类型词(空字符串表示不读,open 默认如此)以及关闭按钮的名称。请在这里传入翻译:库目前还没有语言包系统。 | { open?, info?, success?, error?, warning?, close? } | { info: "Info", success: "Success", error: "Error", warning: "Warning", close: "Close" } |
| classNames | 每条通知各部分的默认类名,以及位置的两个部分:list(可滚动的列)和 listContent(各行)。 | { root?, icon?, section?, title?, description?, actions?, close?, progress?, list?, listContent? } | — |
| styles | 内联样式的同样设置,使用对象。 | { root?, icon?, section?, title?, description?, actions?, close?, progress?, list?, listContent? } | — |
| stack | 默认开启(与 Ant Design React 相同):超过阈值(3)后,该位置较早的通知会叠在最新通知后面,指针或焦点会展开堆叠。false 可关闭,{ threshold } 可更改数量。 | boolean | { threshold?: number } | true |
与 Ant Design 的差异:浮层由库自己创建(React 的
useNotification 和 contextHolder 由 <NotificationHost> 取代);title 和 actions 是 React 当前使用的名称(那里的 message 和 btn 已弃用);与 React 一样默认开启 stack;与 React 一样,在每个位置最新的通知都最靠近窗口边缘;按类型设置角色并加入隐藏的类型词(React:全部为 role="alert");计时器还会因焦点、展开的堆叠和页面隐藏而暂停;onclose 带有原因,调用会返回一个用于关闭通知的函数;duration 与 React、Vue 一样以秒为单位(Angular 为毫秒)。未采用:Vue 的 warn 别名、prefixCls、rtl(方向由环境继承)以及 closable 的对象形式。API 受 Ant Design(MIT)启发;实现为面向 Svelte 的原创实现。设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-notification-z-index | 通知的层级,高于消息(2010)和所有弹出层(文字提示为 1070)。 | 2050 |
| --ads-notification-width | 通知框宽度。不会超过窗口宽度。 | 384px |
| --ads-notification-top | 未设置 top 时,顶部位置距窗口顶部的距离。 | 24px |
| --ads-notification-bottom | 未设置 bottom 时,底部位置距窗口底部的距离。 | 24px |
| --ads-notification-margin | 窗口两侧以及位置远端保留的空白(同时为阴影留出空间)。 | 24px |
| --ads-notification-gap | 两条通知之间的间距。 | 16px |
| --ads-notification-bg | 通知框背景。深色主题为 #1f1f1f。 | #ffffff |
| --ads-notification-color | 文字颜色。深色主题为 85% 白色。 | rgba(0, 0, 0, 0.88) |
| --ads-notification-padding-block | 通知框上下内边距。 | 20px |
| --ads-notification-padding-inline | 通知框左右内边距。关闭按钮距边缘的距离也是它。 | 24px |
| --ads-notification-padding | 通知框内边距(即上面两个)。 | var(--ads-notification-padding-block) var(--ads-notification-padding-inline) |
| --ads-notification-line-height | 描述文字的行高。 | 22px |
| --ads-notification-radius | 通知框圆角半径。 | 8px |
| --ads-notification-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-notification-title-size | 标题字号。 | 16px |
| --ads-notification-title-line-height | 标题的行高;图标与关闭按钮以它为中心对齐。 | 24px |
| --ads-notification-description-gap | 标题与描述之间的间距。 | 8px |
| --ads-notification-actions-gap | 文字与操作区之间的间距。 | 12px |
| --ads-notification-icon-size | 图标大小。 | 24px |
| --ads-notification-icon-gap | 图标与文字之间的间距。 | 12px |
| --ads-notification-icon-info | info 图标的颜色。 | #1677ff |
| --ads-notification-icon-success | success 图标的颜色。tokens-accessible.css 会将其加深为 #389e0d(在白色通知框上达到 3:1)。 | #52c41a |
| --ads-notification-icon-error | error 图标的颜色。 | #ff4d4f |
| --ads-notification-icon-warning | warning 图标的颜色。tokens-accessible.css 会将其加深为 #c27c00。 | #faad14 |
| --ads-notification-close-size | 关闭按钮大小:24px,WCAG 2.5.8 要求的最小目标尺寸(Ant Design 为 22px)。 | 24px |
| --ads-notification-close-icon-size | 关闭按钮中叉号的大小。 | 14px |
| --ads-notification-close-room | 标题第一行末尾为关闭按钮预留的空白。 | 26px |
| --ads-notification-close-padding | 关闭按钮内容多于叉号(例如一个词)时两端的留白;叉号仍是 24 px 的方块。 | 4px |
| --ads-notification-close-color | 关闭按钮颜色。tokens-accessible.css 会把它改为 rgba(0, 0, 0, 0.65)(在通知框上达到 4.5:1)。 | rgba(0, 0, 0, 0.45) |
| --ads-notification-close-color-hover | 关闭按钮悬停时的颜色。 | rgba(0, 0, 0, 0.88) |
| --ads-notification-close-bg | 关闭按钮平时的背景。 | transparent |
| --ads-notification-close-bg-hover | 关闭按钮悬停时的背景。 | rgba(0, 0, 0, 0.06) |
| --ads-notification-progress-bg | 进度条的填充。 | linear-gradient(90deg, #69b1ff, #1677ff) |
| --ads-notification-progress-track | 进度条的轨道。 | rgba(0, 0, 0, 0.04) |
| --ads-notification-progress-height | 进度条高度。 | 2px |
| --ads-notification-progress-inset | 进度条距通知框两侧的距离。 | 8px |
| --ads-notification-progress-radius | 进度条圆角半径。 | 8px |
| --ads-notification-motion-offset | 通知进入时的起始位置与离开时的终止位置距其所在位置的距离(从各自一侧:右侧位置从右边,top 从上方)。 | 64px |
| --ads-notification-stack-offset | 堆叠中各行之间的偏移。 | 8px |
| --ads-notification-stack-scale-step | 堆叠中每一行比前一行缩小的比例(缩放在第二级停止)。 | 0.06 |
无障碍
error和warning使用role="alert",会被立即朗读;info、success和open使用role="status",会等屏幕阅读器读完。Ant Design React 给全部通知都设置了role="alert",这会让读屏软件因为一句普通的“已保存”而被打断;role选项可为每条通知单独设置。- 图标仅作装饰。标题前有一个隐藏的词来说明类型(“Error: Upload failed”),因此类型不会只靠颜色和形状传达。请通过
labels传入翻译:库目前还没有语言包系统,默认是英文。 - 通知不会抢占焦点。其中可聚焦的是关闭按钮和你放在
actions中的元素;关闭按钮为 24px,即 WCAG 2.5.8 要求的目标尺寸。Escape会关闭焦点所在的通知。 - 指针在通知上、焦点在通知内、堆叠展开,或页面被隐藏时,计时器都会暂停。需要一直保留的通知要设置
duration: 0,再由关闭按钮让用户自行关闭(WCAG 2.2.1,时间限制)。 - 浮层是其容器中的最后一个元素,因此键盘要经过页面其余内容之后才能到达通知中的按钮,也没有把焦点移到通知的快捷键。在堆叠中,较早的通知是
inert的,并对读屏软件隐藏;焦点进入堆叠时它会展开,所以键盘用户可以到达这些通知。 - 对比度:文字在通知框上约为 15:1;各类型图标保持在 3:1 以上,关闭按钮保持在 4.5:1 以上(深色主题中如此,浅色主题需引入
tokens-accessible.css,它会加深绿色、琥珀色和关闭按钮)。 - 在 Windows 高对比度等 forced-colors 模式下,通知框、关闭按钮和进度条会带边框显示。在
prefers-reduced-motion下动画(通知框从其位置一侧进入并淡入)会关闭,计时器与进度条不变。触摸不会暂停计时器,也不会展开堆叠。 - 已用真实读屏软件(Orca 搭配 Firefox)验证:它能读出角色、类型词、标题、描述、按 key 更新后的新文字以及关闭按钮的名称。未验证:读屏软件在通知出现时是否会播报(我们的 Orca 环境连普通的
role="alert"都不会朗读)、NVDA、JAWS、VoiceOver、手机读屏软件、真实触摸设备(触摸仅在 Chromium 中模拟)以及真实的 Safari。