Notification
全局通知:位置、显示时间、操作、更新和队列。
何时使用
notification 显示事件的标题、详细信息和后续步骤。它适合后台导出、完成长时间运行的操作或应用程序消息。对于简短的响应,Message 就足够了;在主要内容中也放置重要的永久警告。
notification CSS 前缀 显示全局 ConfigProvider.prefixCls 和本地 prefixCls,无需重置状态。 NotificationHolder 更改卡的所有展示位置和部分的类。App 使用 notification.prefixCls。队列、计时器、焦点和本机表面都被保留。
实例、内容和位置
创建一个 createNotification 并将一个 NotificationHolder 放置在所需的 ConfigProvider 中。实例所有位置都有独立的队列;某些区域需要单独的实例。
| 问题 | 机会 | 示例 |
|---|---|---|
| 显示详细活动 | open、title、description |
基本通知 |
| 选择语义状态 | success/info/warning/error |
类型 |
| 显示距离关闭的剩余时间 | duration、showProgress、pauseOnHover |
显示时长 |
| 折叠同一位置的通知 | stack |
通知堆叠 |
| 选择显示位置和挂载容器 | placement、getContainer |
放置位置 |
| 用结果替换期望 | 相同的 key、actions |
更新 |
| 运行后台任务 | 应用模型 | 出口报告 |
title 和 description 可以接收文本、数字或片段。按钮和链接放在 actions 中。placement 可选择视口边缘的六个位置之一。启用 rtl 后,文字从右向左排列,通知仍在选定的一侧。
更新并显示时间
使用同一 key 重复 open 会更新条目,包括文本、actions、type 和 placement。要在更改状态时保留 DOM 描述,请在打开时设置 type,例如 info:图标的出现或删除可以重新创建内容包装器。
通知默认在 4.5 秒后关闭。设置 duration: 0 或 duration: false 可以让通知保持打开。时长为正数时,showProgress 显示剩余时间。启用 pauseOnHover 后将指针移入通知,或将焦点移入卡片,进度条与计时器都会暂停。导出示例中的 Progress 显示报表的生成进度。
使用 closable 可以更换关闭图标、设置无障碍名称或禁用关闭按钮。如果隐藏常驻通知的关闭按钮,请提供其他关闭方式。卡片开始关闭时会调用 onClose,最后一张卡片从 DOM 中移除后会调用 onAllRemoved。
默认情况下,同一位置出现第四张卡片时,列表会折叠。将指针或键盘焦点移入堆叠区域即可查看全部卡片。可用 stack.threshold 调整阈值,或用 stack: false 始终展开列表。要在达到数量上限时关闭最早的通知,请设置 maxCount。同一实例的所有位置共享这个上限。
的背景、设计和局限性
Holder 从 ConfigProvider 获取本地主题和方向。 theme.components.Notification 更改 width、zIndexPopup 和状态背景而无需重新打开。本地 CSS 持有者和 post 变量优先。
在没有组件覆盖的情况下,状态背景使用 colorBgElevated 而不是常见的 colorInfoBg/colorSuccessBg。空配置返回默认宽度 384px。显式 width:undefined 是一种特殊情况;对于给定的大小,使用数字或 CSS 长度,而不假设 max-content 模式。
在 actions 中放置支持键盘操作的按钮和链接。通过 props 可为卡片设置 data-* 或 ARIA 等 HTML 属性。点击卡片时会调用 onClick,关闭按钮单独处理点击事件。
通过 ConfigProvider.notification 可以设置共用的关闭图标、类名和样式。classNames 和 styles 可以是对象,也可以是接收 { props } 的函数。样式依次来自配置提供者、实例和单张卡片。自定义样式示例。
getContainer 在浏览器中执行。如果未返回节点,组件会尝试 ConfigProvider.getPopupContainer,最后使用 document.body。两者都未指定时,列表保留在挂载组件旁。
您可以使用 App 代替手动 createNotification/NotificationHolder 对:App.useApp() 从最近的区域返回 notification。它的默认值由字段继承,父队列和嵌套队列是独立的。 App.notification.placement 更改适用于新通知。
示例
定期通知
createNotification 和一个持有者。
<script lang="ts">
import { Button, NotificationHolder, createNotification } from '@woodger/antd';
const notification = createNotification();
</script>
<NotificationHolder {notification} top={240} id="notification-basic-notices" />
<Button onclick={() => notification.open(({
title: "文档已更新",
description: "目录中出现了新的组件示例。"
}))}>{("显示定期通知")}</Button>
标准图标
success、info、warning 和 error。
<script lang="ts">
import { Button, NotificationHolder, createNotification } from '@woodger/antd';
const notification = createNotification();
const types = ['success', 'info', 'warning', 'error'] as const;
</script>
<style>
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
</style>
<NotificationHolder {notification} top={240} id="notification-types-notices" />
<div class="actions">{#each types as type (type)}
<Button onclick={() => notification[type](({
key: type,
title: `地位: ${type}`,
description: "标准状态图标。",
duration: 0
}))}>{(type)}</Button>
{/each}<Button
onclick={() => notification.destroy()}
>{("删除类型通知")}</Button></div>
开演时间
进度条显示通知关闭前的剩余时间。
<script lang="ts">
import {
Button,
Checkbox,
InputNumber,
NotificationHolder,
createNotification
} from '@woodger/antd';
const notification = createNotification();
let duration = $state(4.5);
let permanent = $state(false);
let pauseOnHover = $state(true);
let showProgress = $state(true);
let closed = $state(0);
</script>
<style>
.settings {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<NotificationHolder {notification} top={240} id="notification-duration-notices" />
<div class="settings">
<InputNumber
aria-label={("通知持续时间")}
min={0}
max={10}
step={0.5}
value={duration}
onValueChange={next => { duration = next ?? 0; }}
/>
<Checkbox bind:checked={permanent}>{("没有自动关闭")}</Checkbox>
<Checkbox bind:checked={showProgress} disabled={permanent || duration === 0}>
{("显示剩余时间")}
</Checkbox>
<Checkbox
bind:checked={pauseOnHover}
>{("悬停时暂停通知")}</Checkbox>
<Button onclick={() => notification.info(({
key: 'duration',
title: "开演时间",
description: "将鼠标悬停在或导航至关闭按钮。",
duration: permanent ? false : duration,
pauseOnHover,
showProgress,
onClose: () => { closed++; }
}))}>{("显示选定的持续时间")}</Button>
</div>
<output
aria-label={("结束通知")}
>{("关闭: ")}{closed}</output>
它的关闭
文本、可访问名称、disabled 和隐藏按钮。
<script lang="ts">
import { Button, Checkbox, NotificationHolder, createNotification } from '@woodger/antd';
const notification = createNotification({ placement: 'bottomLeft' });
let disabled = $state(false);
let hidden = $state(false);
</script>
<style>
.settings {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
}
</style>
<NotificationHolder {notification} top={240} id="notification-close-notices" />
<div
class="settings"
><Checkbox
bind:checked={disabled}
>{("禁用关闭按钮")}</Checkbox>
<Checkbox bind:checked={hidden}>{("隐藏关闭按钮")}</Checkbox>
<Button onclick={() => notification.open(({
key: 'close',
title: "它的关闭",
description: "显示或更新帖子时将应用这些设置。",
duration: 0,
closable: hidden ? false : {
disabled,
closeIcon: "准备好",
'aria-label': "结束通知"
}
}))}>{("显示你的关闭")}</Button>
<Button
onclick={() => notification.destroy('close')}
>{("手动删除通知")}</Button></div>
您自己的图标和设计
统一设置标题和边框样式,再为单张卡片设置图标颜色。
<script lang="ts">
import { Button, ConfigProvider, NotificationHolder, createNotification } from '@woodger/antd';
const notification = createNotification();
const appearance = {
classNames: { title: 'notification-custom-title' },
styles: { root: 'border: 1px dashed var(--ant-color-warning)' }
};
function showNotice() {
notification.open({
title: ("自己设计"),
description,
icon,
duration: 5,
class: 'notification-custom',
styles: {
root: 'border-radius: 12px',
icon: 'color: var(--ant-color-warning)'
}
});
}
</script>
<style>
:global(.notification-custom-title) { font-weight: 600; }
</style>
{#snippet icon()}<span aria-hidden="true">★</span>{/snippet}
{#snippet description()}
<span>{("签名来自 ")}<strong>Svelte Snippet</strong>{(" 与您自己的设计。")}</span>
{/snippet}
<ConfigProvider notification={appearance}>
<NotificationHolder {notification} top={240} id="notification-custom-notices" />
<Button onclick={showNotice}>{("显示您的通知图标")}</Button>
</ConfigProvider>
规定
选择挂载容器、六种屏幕位置之一以及文字方向。
<script lang="ts">
import {
Button,
Checkbox,
NotificationHolder,
RadioGroup,
createNotification,
type NotificationPlacement
} from '@woodger/antd';
const notification = createNotification();
const placements: NotificationPlacement[] = [
'top', 'topLeft', 'topRight', 'bottom', 'bottomLeft', 'bottomRight'
];
let placement = $state<NotificationPlacement>('topRight');
let container = $state<HTMLDivElement>();
let rtl = $state(false);
</script>
<style>
.settings {
display: grid;
gap: 20px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
</style>
<NotificationHolder {notification} rtl={rtl || undefined} getContainer={() => container}
top={240} bottom={24} id="notification-placement-notices" />
<div bind:this={container} class="settings">
<RadioGroup
aria-label={("通知示例位置")}
bind:value={placement}
options={(placements)}
/>
<Checkbox bind:checked={rtl}>{("文字从右向左")}</Checkbox>
<div class="actions"><Button onclick={() => notification.info(({
key: placement,
title: `位置: ${placement}`,
description: "屏幕的物理区域。",
placement,
duration: 0
}))}>{("在选定位置显示")}</Button>
<Button
onclick={() => notification.destroy()}
>{("明确的通知规定")}</Button></div></div>
更新和行动
一 key,新结果并接受按钮。
<script lang="ts">
import { Button, NotificationHolder, createNotification } from '@woodger/antd';
const notification = createNotification();
let accepted = $state(0);
</script>
<style>
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
output {
display: block;
margin-top: 16px;
}
</style>
{#snippet actions()}<Button type="primary" onclick={() => {
accepted++;
notification.destroy('review');
}}>{("接受通知结果")}</Button>{/snippet}
<NotificationHolder {notification} top={240} id="notification-update-notices" />
<div class="actions">
<Button onclick={() => notification.info(({
key: 'review',
title: "检查文件",
description: "我们正在等待检查结果。",
duration: 0
}))}>{("开始通知检查")}</Button>
<Button onclick={() => notification.success(({
key: 'review',
title: "文件已验证",
description: "你可以接受这个结果。",
actions,
duration: 0
}))}>{("完成通知检查")}</Button>
<Button onclick={() => notification.destroy('review')}>
{("删除通知检查")}
</Button>
</div>
<output
aria-label={("行动通知")}
>{("公认: ")}{accepted}</output>
队列限制
添加第四条通知时,最早的一条会关闭。
<script lang="ts">
import { Button, NotificationHolder, createNotification } from '@woodger/antd';
const notification = createNotification({ maxCount: 3, duration: 0, placement: 'bottomLeft' });
let sequence = $state(0);
</script>
<style>
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
</style>
<NotificationHolder {notification} top={240} id="notification-queue-notices" />
<div class="actions"><Button onclick={() => notification.info(({
title: `队列通知号${++sequence}`,
description: "总限制为三项。"
}))}>{("添加到通知队列")}</Button>
<Button
onclick={() => notification.destroy()}
>{("清除通知队列")}</Button></div>
通知堆叠
添加四份报表,然后将指针或键盘焦点移入堆叠区域。
<script lang="ts">
import {
Button, Checkbox, Flex, InputNumber, NotificationHolder, createNotification
} from '@woodger/antd';
const thresholdId = $props.id();
const notification = createNotification({ duration: 0, placement: 'bottomRight' });
let enabled = $state(true);
let threshold = $state(3);
let hasReports = $state(false);
let sequence = 0;
function reportReady() {
sequence += 1;
notification.success({
title: ("报告已准备好"),
description: `report-${sequence}.csv`
});
hasReports = true;
}
</script>
<NotificationHolder {notification} stack={enabled ? { threshold } : false}
onAllRemoved={() => { hasReports = false; }} />
<Flex vertical align="start" gap="medium">
<Flex align="center" wrap gap="small">
<Checkbox bind:checked={enabled}>{("折叠列表")}</Checkbox>
<label for={thresholdId}>{("折叠阈值")}</label>
<InputNumber id={thresholdId} value={threshold} min={1} max={10} step={1} precision={0}
disabled={!enabled} onValueChange={value => { threshold = value ?? 1; }} />
</Flex>
<Flex wrap gap="small">
<Button type="primary" onclick={reportReady}>
{("发送报表就绪通知")}
</Button>
<Button disabled={!hasReports} onclick={() => notification.destroy()}>
{("清空通知")}
</Button>
</Flex>
</Flex>
notification 组件主题
宽度、状态背景和图层。更改主题会将评论保存在 actions、队列和结束日期中。
<script lang="ts">
import {
Button,
Checkbox,
ConfigProvider,
Input,
NotificationHolder,
RadioGroup,
createNotification
} from '@woodger/antd';
import type { NotificationType } from '@woodger/antd';
const notification = createNotification({ pauseOnHover: false });
let enabled = $state(true);
let resetWidth = $state(false);
let status = $state<NotificationType>('info');
let comment = $state(("评论草案"));
const types = ['info', 'success', 'warning', 'error'] as const;
const tokens = {
zIndexPopup: 2400,
progressBg: '#d48806',
colorInfoBg: '#fffbe6',
colorSuccessBg: '#f6ffed',
colorWarningBg: '#fff7e6',
colorErrorBg: '#fff1f0'
};
</script>
<style>
.settings {
display: grid;
gap: 24px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.status-settings, .notice-actions {
display: grid;
gap: 12px;
min-width: 0;
max-width: 100%;
}
label {
display: grid;
gap: 8px;
}
</style>
{#snippet actions()}
<div class="notice-actions">
<label>{("评论\n ")}<Input bind:value={comment} aria-label={("通知线程评论")} />
</label>
<Button onclick={() => notification.destroy('theme')}>{("准备好")}</Button>
</div>
{/snippet}
<ConfigProvider direction="rtl" theme={{
cssVar: { prefix: 'notification-demo' },
token: { colorInfoBg: '#fffbe6' },
components: { Notification: enabled ? { ...tokens, width: resetWidth ? undefined : 480 } : {} }
}}>
<NotificationHolder {notification} top={240} bottom={24} id="notification-theme-notices" />
<div class="settings">
<Checkbox bind:checked={enabled}>{("应用通知令牌")}</Checkbox>
<Checkbox bind:checked={resetWidth} disabled={!enabled}>
{("通行宽度:未定义")}
</Checkbox>
<div class="status-settings">
<span>{("通知状态")}</span>
<RadioGroup
aria-label={("通知主题状态")}
bind:value={status}
options={(types)}
style="
display: flex;
flex-wrap: wrap;
gap: 8px;
"
/>
</div>
<div class="actions">
<Button onclick={() => notification.open(({
key: 'theme', type: status, title: "通知主题",
description: "更新主题时无需重置评论和操作。", actions, duration: 0
}))}>{("检查通知线程")}</Button>
<Button onclick={() => notification.info(({
key: 'timed-theme', title: "两秒通知",
description: "切换主题不会延长演出时间。", duration: 2, showProgress: true,
class: 'notification-theme-timed'
}))}>{("显示2秒")}</Button>
</div>
</div>
</ConfigProvider>
出口报告
操作进度、成功、错误、重试、取消和真实的 CSV。
NotificationExample.svelte<script lang="ts"> import { onDestroy } from 'svelte'; import { Button, Checkbox, NotificationHolder, Progress, Select, createNotification, type NotificationPlacement } from '@woodger/antd'; import { createNotificationExport } from './notification-export.model.svelte'; const notification = createNotification({ maxCount: 3 }); let failNext = $state(false); let placement = $state<NotificationPlacement>('topRight'); let result = $state(("准备报告并从通知中下载。")); let downloadUrl: string | undefined; const placements = ['top', 'topLeft', 'topRight', 'bottom', 'bottomLeft', 'bottomRight'].map( (value) => ({ value: value as NotificationPlacement, label: value }) ); const model = createNotificationExport({ onReady() { result = ("该报告已可供下载。"); notification.success( ({ key: 'export', title: "报告已准备好", description: progress, placement, duration: 0, actions: ready, role: 'status' }) ); }, onError() { result = ("未能准备报告。请再试一次。"); notification.error( ({ key: 'export', title: "未能准备报告", description: progress, placement, duration: 0, actions: retry }) ); } }); function releaseDownload() { if (downloadUrl) URL.revokeObjectURL(downloadUrl); downloadUrl = undefined; } function start() { const failed = failNext; if (!model.start(failed)) { return; } failNext = false; releaseDownload(); result = ("我们正在准备一份报告..."); notification.info( ({ key: 'export', title: "准备报告", description: progress, actions: pending, placement, duration: 0, role: 'status' }) ); } function cancel() { if (!model.cancel()) { return; } result = ("导出已取消。"); notification.info( ({ key: 'export', title: "出口取消", description: progress, placement, duration: 0, actions: retry, role: 'status' }) ); } function download() { const report = model.report; if (!report) { return; } releaseDownload(); downloadUrl = URL.createObjectURL(new Blob([report], { type: 'text/csv;charset=utf-8' })); const link = document.createElement('a'); link.href = downloadUrl; link.download = 'report.csv'; document.body.append(link); link.click(); link.remove(); result = ("报告已下载。"); } function reset() { model.reset(); releaseDownload(); notification.destroy(); result = ("导出示例重置。"); } onDestroy(() => { model.destroy(); releaseDownload(); }); </script> <style> .actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; } .placement { width: 180px; } output { display: block; margin-top: 20px; color: var(--ant-color-text-secondary); } .phase { margin-block: 0 var(--ant-margin-sm); } </style> {#snippet progress()}<p class="phase">{(model.phase)}</p><Progress percent={model.percent} status={model.status === 'preparing' ? 'active' : model.status === 'error' ? 'exception' : undefined} aria-label={("报告准备")} />{/snippet} {#snippet pending()}<Button onclick={cancel}>{("取消导出")}</Button>{/snippet} {#snippet retry()}<Button type="primary" onclick={start} >{("重复导出")}</Button>{/snippet} {#snippet ready()} <span class="actions"><Button type="primary" onclick={download}> {("下载报告")} </Button><Button onclick={() => notification.destroy('export')} >{("关闭导出结果")}</Button></span> {/snippet} <NotificationHolder {notification} id="showcase-notifications" /> <div class="actions" ><Button type="primary" loading={model.status === 'preparing'} onclick={start} >{("出口报告")}</Button><Checkbox bind:checked={failNext} >{("下次导出失败")}</Checkbox><div class="placement" ><Select aria-label={("通知声明")} bind:value={placement} options={(placements)} /></div><Button onclick={reset} >{("重置通知示例")}</Button></div> <output aria-label={("通知示例的结果")}>{result}</output>notification-export.model.svelte.tstype ExportState = | { status: 'idle'; percent: 0; phase: string } | { status: 'preparing'; percent: number; phase: string } | { status: 'ready'; percent: 100; phase: string; report: string } | { status: 'error' | 'cancelled'; percent: number; phase: string }; export function createNotificationExport(callbacks: { onReady: () => void; onError: () => void }) { let state = $state.raw<ExportState>({ status: 'idle', percent: 0, phase: "准备一份报告。" }); let generation = 0; let active = true; const timers = new Set<ReturnType<typeof setTimeout>>(); function clear() { generation++; for (const timer of timers) clearTimeout(timer); timers.clear(); } function start(failed = false): boolean { if (!active || state.status === 'preparing') { return false; } clear(); const current = generation; state = { status: 'preparing', percent: 0, phase: "准备好后,此处将出现一个下载按钮。" }; function schedule(delay: number, update: () => void) { const timer = setTimeout( () => { timers.delete(timer); if (active && current === generation && state.status === 'preparing') update(); }, delay ); timers.add(timer); } for (const stage of [ { delay: 200, percent: 25, phase: "正在收集数据..." }, { delay: 450, percent: 65, phase: "正在生成 CSV..." }, { delay: 600, percent: 90, phase: "正在检查文件..." } ]) { schedule( stage.delay, () => { state = { status: 'preparing', percent: stage.percent, phase: stage.phase }; } ); } schedule(700, () => { if (failed) { state = { status: 'error', percent: 90, phase: "数据已保存。再次尝试导出。" }; callbacks.onError(); } else { state = { status: 'ready', percent: 100, phase: "CSV 文件包含两个条目。现在就可以下载了。", report: '\uFEFFname,framework\r\nAnna,Svelte\r\nBoris,TypeScript\r\n' }; callbacks.onReady(); } }); return true; } return { get status() { return state.status; }, get percent() { return state.percent; }, get phase() { return state.phase; }, get report() { return state.status === 'ready' ? state.report : undefined; }, start, cancel() { if (state.status !== 'preparing') { return false; } clear(); state = { status: 'cancelled', percent: state.percent, phase: "报告的准备工作已被取消。" }; return true; }, reset() { clear(); state = { status: 'idle', percent: 0, phase: "准备一份报告。" }; }, destroy() { active = false; clear(); } }; }
NotificationHolder 更改卡的所有展示位置和部分的类。应用程序使用 notification.prefixCls。队列、计时器、焦点和本机表面都被保留。
notification CSS 前缀
通用命名空间ConfigProvider和本地 prefixCls;当名称更改时,组件的状态将被保留。
<script lang="ts">
import {
Checkbox,
ConfigProvider,
NotificationHolder,
createNotification,
Button
} from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-notification' : '');
const notification = createNotification({ duration: 0 });
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("通用前缀通知套件")}</Checkbox>
<Checkbox bind:checked={local}>{("本地前缀通知")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'notification-theme' } }}>
<div class="prefix-stage">
<NotificationHolder {notification} prefixCls={localPrefix} top={240} />
<Button onclick={() => notification.success(({
key: 'prefix',
title: "带前缀的通知",
description: "更改命名空间会保存当前卡。"
}))}>{("显示带前缀的通知")}</Button>
<Button onclick={() => notification.destroy()}>
{("关闭带前缀的通知")}
</Button>
</div>
</ConfigProvider>
<output
aria-label={("CSS 前缀通知")}
>{("一般的: ")}{prefix}{(";当地的: ")}{(localPrefix
|| ("来自提供商"))}</output>
<p>
{("NotificationHolder 更改卡的所有展示位置和部分的类。应用程序使用 notification.prefixCls。队列、计时器、焦点和本机表面都被保留。")}
</p>
</div>
API
| 主题 | 示例或限制 |
|---|---|
| Hooks usage (recommended) | Svelte:在所需上下文中使用 createNotification 和一个NotificationHolder。 |
| Duration after which the notification box is closed | 秒, false/0 并在悬停时暂停。 |
| Notification with icon | 四种标准状态。 |
| Custom close button | 自定义签名、可访问名称、disabled 和隐藏按钮。 |
| Customized Icon | 图标和说明的片段。 |
| Placement | 所有六个物理屏幕区域。 |
| Update Message Content | 通过通知内的 key 和操作进行更新。 |
| Stack | 设置折叠阈值,使用键盘或鼠标展开通知列表。 |
| Show with progress | 进度条显示通知剩余的展示时间。 |
| Customize progress bar color | notification 主题中的 progressBg 控制进度条颜色。 |
| Custom semantic styles | 在配置提供者的共用类名和样式基础上,为单张卡片追加设置。 |
NotificationInstance
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
open | 通过 key 创建或更新记录。 | (config: NotificationConfig) => void | — |
info | 信息状态。 | (config) => void | info |
success | 手术成功。 | (config) => void | success |
warning | 警告。 | (config) => void | warning |
error | 错误。 | (config) => void | error |
destroy | 关闭一个条目或整个队列。 | (key?: string | number) => void | — |
NotificationConfig
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
title | 标题和可用名称。 | NotificationContent | — |
description | 通知详情。 | NotificationContent | — |
actions | 用于进一步操作的按钮和链接。 | NotificationContent | — |
type | 标准状态图标。 | success | info | warning | error | — |
icon | 自己的图标。 | NotificationContent | 按类型 |
key | 更新 ID,而不重复条目。 | string | number | 生成的 |
placement | top/bottom, topLeft/topRight, bottomLeft/bottomRight. | NotificationPlacement | topRight |
duration | 秒; false 和 0 禁用计时器。 | number | false | 4.5 |
pauseOnHover | 将计时器暂停在指针下方。 | boolean | true |
closable | 标准关闭按钮的设置。 | boolean | NotificationClosable | true |
closeIcon | 您的签名; false/null 隐藏按钮。 | NotificationContent | 叉 |
role | 公告的紧迫性。 | alert | status | alert |
onClose | 开始关闭。未调用卸载持有者回调。 | () => void | — |
class | 记录根类。 | string | — |
style | 帖子根的 CSS。 | string | — |
classNames | 卡片各部分的 CSS 类名。 | NotificationSemantic | — |
styles | 卡片各部分的样式。 | NotificationSemantic | — |
showProgress | 显示距离关闭的剩余时间。 | boolean | false |
props | 卡片的原生属性,包括 data-* 和 ARIA。 | NotificationRootAttributes | — |
onClick | 处理卡片点击。关闭按钮单独处理点击事件。 | (event: MouseEvent) => void | — |
NotificationClosable
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
disabled | 禁用标准按钮。 | boolean | false |
closeIcon | 优先于 closeIcon 配置。 | NotificationContent | 叉 |
aria-label | 可用按钮名称。 | string | locale.closeText |
onClose | 点击关闭按钮或倒计时结束时,会在卡片的 onClose 之前调用。 | () => void | — |
createNotification options
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
placement | 标本的一般位置。 | NotificationPlacement | topRight |
duration | 总持续时间。 | number | false | 4.5 |
pauseOnHover | 一般定时器暂停。 | boolean | true |
maxCount | 所有展示位置的一般限制。 | number | 没有限制 |
showProgress | 显示距离关闭的剩余时间。 | boolean | false |
prefixCls | 列表及卡片的完整 CSS 前缀。 | string | ConfigProvider.prefixCls + "-notification" |
top | 表面的顶部凹痕。 | number | 24 |
bottom | 底面压痕。 | number | 24 |
stack | 每个位置的折叠阈值。设为 false 可始终展开列表。 | boolean | { threshold?: number } | { threshold: 3 } |
rtl | 文字方向。卡片在屏幕上的位置由 placement 决定。 | boolean | ConfigProvider.direction |
getContainer | 选择浏览器中挂载通知列表的节点。 | () => HTMLElement | ShadowRoot | null | undefined | — |
closeIcon | 所有卡片共用的自定义关闭图标。 | NotificationContent | ConfigProvider.notification.closeIcon |
classNames | 此实例的列表和卡片的 CSS 类名。 | NotificationSemantic | — |
styles | 此实例的列表和卡片的样式。 | NotificationSemantic | — |
onAllRemoved | 最后一张卡片从 DOM 中移除后调用。 | () => void | — |
NotificationHolder
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 列表及卡片的完整 CSS 前缀。 | string | ConfigProvider.prefixCls + "-notification" |
top | 表面的顶部凹痕。 | number | 24 |
bottom | 底面压痕。 | number | 24 |
stack | 每个位置的折叠阈值。设为 false 可始终展开列表。 | boolean | { threshold?: number } | { threshold: 3 } |
rtl | 文字方向。卡片在屏幕上的位置由 placement 决定。 | boolean | ConfigProvider.direction |
getContainer | 选择浏览器中挂载通知列表的节点。 | () => HTMLElement | ShadowRoot | null | undefined | — |
closeIcon | 所有卡片共用的自定义关闭图标。 | NotificationContent | ConfigProvider.notification.closeIcon |
classNames | 此实例的列表和卡片的 CSS 类名。 | NotificationSemantic | — |
styles | 此实例的列表和卡片的样式。 | NotificationSemantic | — |
onAllRemoved | 最后一张卡片从 DOM 中移除后调用。 | () => void | — |
notification | 队列实例;一个连接的支架。 | NotificationInstance | 必需的 |
id | 带有所有展示位置的表面标识符。 | string | — |
class | 表面等级。 | string | — |
style | CSS 表面。 | string | — |
使用独立的 createNotification 和NotificationHolder来代替React钩子/静态方法。打开方法返回 void;关闭是通过 destroy(key) 或标准按钮完成的。
Semantic DOM
list 和 listContent 插槽用于设置列表,其余插槽用于设置单张卡片。
| 插槽 | CSS 类名 | 目的 |
|---|---|---|
list | .ant-notification-list | 个人 placement 小组。 |
listContent | .ant-notification-list-content | 条目列表。 |
root | .ant-notification-notice | 包含 role、可用名称、class/style 的条目。 |
wrapper | .ant-notification-notice-wrapper | 图标和文字;仅当有图标时才会创建。 |
section | .ant-notification-notice-section | title/description 对;仅当两个块都存在时才创建。 |
icon | .ant-notification-notice-icon | 装饰图标。 |
title | .ant-notification-notice-title | 标题。 |
description | .ant-notification-notice-description | 描述。 |
actions | .ant-notification-notice-actions | 直接在帖子根部、包装或文本旁边进行操作。 |
close | .ant-notification-notice-close | 关闭按钮。 |
progress | .ant-notification-notice-progress | 显示剩余时间的进度条。 |
注册令牌
在主题示例中,progressBg 控制进度条的颜色。点击“显示 2 秒”即可查看。
| 代币 | 目的 |
|---|---|
width | 卡片宽度:以 px 或 CSS 长度为单位的数字;仅限于视口。 |
zIndexPopup | CSS 组展示层;本机弹出窗口保留在 top 层。 |
progressBg | 倒计时进度条的颜色。 |
colorInfoBg | 显式信息记录背景。 |
colorSuccessBg | 明确的成功记录背景。 |
colorWarningBg | 警告记录的明确背景。 |
colorErrorBg | 错误记录的明确背景。 |
| 代币 | 意义 |
|---|---|
colorBgElevated | #ffffff |
colorText | rgba(0,0,0,0.88) |
colorTextHeading | rgba(0,0,0,0.88) |
colorSuccess | #52c41a |
colorError | #ff4d4f |
colorInfo | #1677ff |
colorWarning | #faad14 |
borderRadiusLG | 8 |
FAQ
如何获取ConfigProvider上下文?
将 createNotification 的 NotificationHolder 放置在提供程序内。一个实例需要一个持有者。
notification 和消息有什么区别?
notification 专为标题、详细信息和操作而设计。消息给出简短的临时响应。
如何更新现有条目?
使用相同的 key 和新设置重复 open。
状态改变时如何保存DOM描述?
打开时已设置 type。添加或删除图标可以重新创建文本包装。
计时器什么时候暂停?
当使用 pauseOnHover 悬停并且焦点位于 actions 内时。 false/0 禁用自动关闭。
如何更改倒计时进度条的颜色?
设置 theme.components.Notification.progressBg 并启用 showProgress。在主题示例中,选择显示两秒的通知。
如何查看先前的通知?
将焦点移入通知堆叠即可展开卡片。如果设置了队列上限,添加新通知时可能会关闭最早的通知。
更改主题后是否需要重新创建通知?
不会。设计令牌(design tokens)会被动刷新,维持队列和截止日期。
为什么状态背景都一样?
默认为 colorBgElevated。可以使用 notification 组件令牌设置不同的背景。
如何返回标准宽度?
删除覆盖或传递空组件配置。显式 width:undefined 与缺失字段不同。