Alert
带有状态的消息:设计、描述、操作和顺利结束。
何时使用
状态、标题和操作
title 设置主要消息,description - 详细信息,action - 附近的操作。这些区域采用值和 Svelte 片段。type 选择 success/info/warning/error,variant - outlined/filled。
| 问题 | 机会 | 示例 |
|---|---|---|
| 显示简短结果 | title、type |
关键信息 |
| 解释原因和下一步 | description |
详细留言 |
| 允许关闭 | 对象 closable |
闭幕式 |
| 添加动作 | snippet action |
行动 |
| 放置横幅 | banner |
页面横幅 |
| 处理渲染失败 | svelte:boundary |
误差限度 |
showIcon 包含一个图标。图标允许您设置自己的;可以在 ConfigProvider.alert 中配置按 type 划分的默认值。banner 更改标准类型/showIcon:如有必要,请明确设置它们。颜色和图标应与清晰的文本相辅相成。
关闭并恢复
closable 接受 boolean 或带有 closeIcon、disabled、ARIA 属性、onClose 和 afterClose 的对象。 onClose 报告关闭请求; preventDefault 不会覆盖它。 afterClose 在 DOM 被删除后调用,除非先卸载组件。
Alert 没有单独的 open/bind:open。闭包存储在实例中。要重新显示,请创建一个新实例,例如带关键帧的 block,如演示中所示。如果删除后焦点按钮消失,请提前选择一个合理的位置返回。
成分和设计
React Alert.ErrorBoundary 未导出。 错误示例 在常规 svelte:boundary 的失败片段中使用警报。移动横幅也是示例的逻辑:具有切换和减少运动的 CSS 动画,无需外部选框库。
classNames/styles 配置 root/icon/section/title/description/actions/close; styles 包含 CSS 字符串。对于没有 description 的长 title,默认对齐方式可以是居中。对于第一行,设置语义样式以匹配您的字体和内容,而不是通用的计算偏移量。
ConfigProvider.alert 设置外观、关闭和图标的默认值。警报设计令牌(design tokens)通过主题应用。在公共包中,半径称为 borderRadius;官方文档中的新名称 containerBorderRadius 还不是它的契约。
默认情况下,root 具有 role:alert。对于平静的信息消息,您可以选择适当的 role/aria-live;除非必要,否则不要将大型不可变块声明为紧急事件。
非空本地 prefixCls 指定警报 CSS 类。空字符串和 undefined 继承ConfigProvider.prefixCls,带有 -alert 后缀,默认为 ant-alert。更改命名空间不会重置关闭状态、语义 styles 和标记。 示例 ConfigProvider 显示了共同的前缀以及 Collapse 和 Affix。
示例
短信
正常的成功消息。
<script lang="ts">
import { Alert } from '@woodger/antd';
</script>
<Alert title={("设置已保存")} type="success" role="status" />
消息类型
success、info、warning 和 error。
<script lang="ts">
import { Alert } from '@woodger/antd';
const types = ['success', 'info', 'warning', 'error'] as const;
const titles = {
success: "数据已保存",
info: "可用更新",
warning: "检查您的设置",
error: "保存失败"
};
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
</style>
<div
class="examples"
>{#each types as type (type)}<Alert
{type}
title={(titles[type])}
role="status"
/>{/each}</div>
Filled
无框填充设计。
<script lang="ts">
import { Alert } from '@woodger/antd';
const types = ['success', 'info', 'warning', 'error'] as const;
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
</style>
<div class="examples">{#each types as type (type)}<Alert {type} title={("Filled: " + String((type)) + "")} variant="filled" showIcon role="status" />{/each}</div>
关闭与恢复
disabled,本人签名并完成关闭。
<script lang="ts">
import { Alert, Button, Checkbox } from '@woodger/antd';
let disabled = $state(false);
let revision = $state(0);
let phase = $state(("该消息已打开。"));
</script>
<style>
.settings {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
margin-bottom: 24px;
}
output {
display: block;
margin-top: 16px;
}
</style>
<div
class="settings"
>
<Checkbox
bind:checked={disabled}
>{("拒绝关闭")}</Checkbox>
<Button onclick={() => { revision++; phase = ("该消息已打开。"); }}>
{("恢复警报")}
</Button>
</div>
{#key revision}
<Alert title={("结束语")} description={("该按钮报告请求和关闭完成情况。")}
type="info" showIcon role="status" aria-label={("可关闭警报")} closable={{
disabled,
'aria-label': "隐藏消息",
closeIcon: "隐藏",
onClose: () => { phase = ("关闭..."); },
afterClose: () => { phase = ("消息已关闭。"); }
}} />
{/key}
<output aria-label={("警报关闭状态")}>{phase}</output>
细节
文本和片段描述。
<script lang="ts">
import { Alert } from '@woodger/antd';
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
</style>
{#snippet description()}
<span>{("打开 ")}<strong>
{("项目设置")}
</strong>{(" 并检查会员访问权限。")}</span>
{/snippet}
<div
class="examples"
>
<Alert
type="warning"
title={("有未保存的更改")}
description={(description)}
showIcon
role="status"
/>
<Alert type="error" title={("出版未完成")} description={("请检查连接后重试。")} showIcon role="status" />
</div>
徽章
标准、自定义和共享图标。
<script lang="ts">
import { Alert, ConfigProvider } from '@woodger/antd';
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
</style>
{#snippet icon()}<span aria-hidden="true">★</span>{/snippet}
<div class="examples">
<Alert title={("标准图标")} type="success" showIcon role="status" />
<Alert title={("你的图标")} type="info" showIcon {icon} role="status" />
<ConfigProvider
alert={{ warningIcon: icon }}
><Alert
title={("来自提供商的图标")}
type="warning"
showIcon
role="status"
/></ConfigProvider>
</div>
移动横幅
示例 CSS、开关和简化运动。
<script lang="ts">
import { Alert, Checkbox } from '@woodger/antd';
let moving = $state(false);
</script>
<style>
.settings { margin-bottom: 20px; }
.line {
overflow: hidden;
min-width: 0;
}
.text { display: inline-block; }
.moving {
white-space: nowrap;
animation: ticker 18s linear infinite;
}
@keyframes ticker {
from {
transform: translateX(100%);
}
to {
transform: translateX(-100%);
}
}
@media (prefers-reduced-motion: reduce) {
.moving {
animation: none;
white-space: normal;
}
}
</style>
{#snippet title()}<div class="line"><span class="text" class:moving>{("预定工作:更新期间文档将保持可用。")}</span></div>{/snippet}
<div
class="settings"
><Checkbox
bind:checked={moving}
>{("横幅运动")}</Checkbox></div>
<Alert banner title={(title)} role="status" />
消息中的操作
接受和取消设置。
<script lang="ts">
import { Alert, Button } from '@woodger/antd';
let accepted = $state(false);
</script>
{#snippet action()}<Button size="small" onclick={() => { accepted = !accepted; }}>{(accepted ? "取消选择" : "接受设置")}</Button>{/snippet}
<Alert
type={accepted ? 'success' : 'info'}
title={(accepted ? "接受设置" : "检查您的设置")}
description={("Alert 内的操作会更新示例状态。")}
{action}
showIcon
role="status"
/>
对齐长 title
第一行通过语义 styles。
<script lang="ts">
import { Alert, Button } from '@woodger/antd';
</script>
{#snippet action()}
<Button size="small" href={("#alert-demo-application")}>
{("设置")}
</Button>
{/snippet}
<Alert
title={("关于在发布新版本文档之前需要检查项目设置的长警告。")}
type="warning"
showIcon
{action}
role="status"
closable={{ 'aria-label': "隐藏长警告" }}
styles={{
root: 'align-items: flex-start',
icon: 'margin-top: 4px',
close: 'margin-top: 4px'
}}
/>
部分内容可用。
Svelte 中的渲染错误
在失败代码段中应用了 svelte:boundary 和 Alert。
Boundary.svelte<script lang="ts"> import { Alert, Button } from '@woodger/antd'; import FragileContent from './FragileContent.svelte'; let fail = $state(false); </script> <style> .settings { margin-bottom: 20px; } </style> <div class="settings"> <Button onclick={() => { fail = true; }}> {("检查渲染错误")} </Button> </div> <svelte:boundary> <FragileContent {fail} /> {#snippet failed(error: unknown, reset: () => void)} {#snippet action()} <Button size="small" onclick={() => { fail = false; reset(); }}> {("恢复分区")} </Button> {/snippet} <Alert type="error" title={("部分不可用")} description={(error instanceof Error ? error.message : "渲染错误")} {action} showIcon /> {/snippet} </svelte:boundary>FragileContent.svelte<script lang="ts"> let { fail }: { fail: boolean } = $props(); let text = $derived.by(() => { if (fail) { throw new Error(("无法构建分区内容。")); } return ("部分内容可用。"); }); </script> <p>{text}</p>
Semantic DOM
样式对象和功能。
<script lang="ts">
import { Alert, ConfigProvider } from '@woodger/antd';
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
</style>
<ConfigProvider alert={{ styles: { title: 'font-weight: 600' } }}>
<div class="examples">
<Alert title={("样式对象")} showIcon role="status" classNames={{
root: 'alert-object'
}}
styles={{
root: 'border-style: dashed',
icon: 'color: #d48806'
}} />
<Alert title={("款式特色")} type="warning" role="status" classNames={{
root: 'alert-function'
}}
styles={({ props }) => ({ title: props.type === 'warning' ? 'font-weight: 700' : '' })} />
</div>
</ConfigProvider>
代币和 RTL
半径、填充和图标大小。
<script lang="ts">
import { Alert, ConfigProvider } from '@woodger/antd';
</script>
<style>
.examples {
display: grid;
gap: 16px;
}
</style>
<ConfigProvider direction="rtl" theme={{
components: {
Alert: {
borderRadius: 12,
defaultPadding: '12px 18px',
withDescriptionPadding: '24px 28px',
withDescriptionIconSize: 30
}
}
}}>
<div
class="examples"
><Alert
title={("主题讯息")}
type="info"
role="status"
/>
<Alert
title={("专题描述")}
description={("这些设置将应用于嵌套主题。")}
type="warning"
showIcon
role="status"
/></div>
</ConfigProvider>
通知设置
带有动作和重播的原始应用程序示例。
<script lang="ts">
import { Alert, Button, Checkbox, type AlertType, type AlertVariant } from '@woodger/antd';
let type = $state<AlertType>('info');
let variant = $state<AlertVariant>('outlined');
let banner = $state(false);
let showIcon = $state(true);
let description = $state(true);
let closable = $state(true);
let customIcon = $state(false);
let closeText = $state(false);
let polite = $state(true);
let revision = $state(0);
let result = $state(("应用设置或关闭消息。"));
const titles: Record<AlertType, string> = {
success: "设置已保存",
info: "通知设置",
warning: "有未保存的更改",
error: "保存设置失败"
};
const descriptions: Record<AlertType, string> = {
success: "新设置将用于将来的通知。",
info: "保存前检查您的通知设置。",
warning: "应用设置以保存更改。",
error: "再次保存。输入的参数被保存。"
};
function apply() {
type = 'success';
result = ("设置已应用。");
}
</script>
<style>
.settings {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 24px;
}
.settings label {
display: flex;
align-items: center;
gap: 8px;
}
select {
padding: 4px 8px;
color: var(--ant-color-text);
background: var(--ant-color-bg-container);
border: 1px solid var(--ant-color-border);
border-radius: 6px;
font: inherit;
}
.footer {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
margin-top: 24px;
}
output { color: var(--ant-color-text-secondary); }
</style>
{#snippet icon()}<span>★</span>{/snippet}
{#snippet action()}
<Button
size="small"
style="
max-width: 100%;
height: auto;
min-height: var(--ant-control-height-sm);
white-space: normal;
"
onclick={apply}
>
{(type === 'error' ? "再次保存" : "应用设置")}
</Button>
{/snippet}
<div class="settings">
<label>{("消息类型 ")}<select bind:value={type}>
<option value="info">
{("信息")}
</option>
<option
value="success"
>{("成功")}</option>
<option
value="warning"
>{("警告")}</option>
<option
value="error"
>{("错误")}</option>
</select></label>
<label>{("设置消息格式 ")}<select
bind:value={variant}
><option
value="outlined"
>{("带框架")}</option><option
value="filled"
>{("无框")}</option></select></label>
<Checkbox bind:checked={showIcon}>{("消息图标")}</Checkbox>
<Checkbox bind:checked={description}>{("消息描述")}</Checkbox>
<Checkbox bind:checked={banner}>{("通知栏")}</Checkbox>
<Checkbox bind:checked={closable}>{("关闭消息按钮")}</Checkbox>
<Checkbox bind:checked={customIcon}>{("自定义消息图标")}</Checkbox>
<Checkbox bind:checked={closeText}>{("文字而不是十字")}</Checkbox>
<Checkbox bind:checked={polite}>{("软消息公告")}</Checkbox>
</div>
{#key revision}
<Alert
aria-label={("自定义消息")}
role={polite ? 'status' : 'alert'}
{type}
{variant}
{banner}
{showIcon}
title={(titles[type])}
description={(description ? descriptions[type] : undefined)}
icon={customIcon ? icon : undefined}
{action}
closable={closable
&& {
closeIcon: closeText ? "隐藏" : undefined,
onClose: () => { result = ("关闭消息..."); },
afterClose: () => { result = ("消息已关闭。"); }
}}
/>
{/key}
<div class="footer"><Button
onclick={() => {
revision++;
result = ("消息已恢复。");
}}
>
{("再次显示消息")}
</Button><output aria-label={("通知结果")}>
{result}
</output></div>
API
13 个 ant.design 6.7.0 主题映射到公共 Svelte API。 Loop banner 和 ErrorBoundary 适配与警报功能是分开的。
| 主题 | 示例或限制 |
|---|---|
| Basic | 短消息。 |
| More types | success、info、warning 和 error。 |
| Filled | 无框设计。 |
| Closable | disabled、按钮签名和回调。 |
| Description | 文本和片段中的详细信息。 |
| Icon | 标准、本机和提供商 icon。 |
| Banner | 警告栏和显式 type。 |
| Loop Banner | 运动 - CSS 示例,具有控件和减少的运动。 |
| Smoothly Unmount | afterClose 报告关闭完成。 |
| ErrorBoundary | 通过 svelte:boundary 进行适配。包中缺少 React Alert.ErrorBoundary。 |
| Custom action | 该按钮更改消息的状态。 |
| Custom title alignment | 语义 styles 将元素与第一行对齐。 |
| Custom semantic dom styling | 带有 props 的对象和回调。 |
Alert
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 警报及其状态的完整本地 CSS 前缀。空字符串和 undefined 继承ConfigProvider.prefixCls;默认为 ant-alert。 | string | ConfigProvider + -alert |
title | 文本、数字、片段或内容数组。 | AlertContent | — |
type | 含义、背景和标准消息图标。 | success | info | warning | error | 横幅中的信息/警告 |
variant | 带或不带框架的设计。 | outlined | filled | outlined |
description | 留言详情。 | AlertContent | — |
showIcon | 显示图标。 | boolean | 横幅中的 false / true |
icon | 您的图标位于 showIcon。 | AlertContent | 按类型 |
action | 操作按钮和链接。 | AlertContent | — |
banner | 消息栏有自己的 default type/showIcon。 | boolean | false |
closable | 关闭按钮和生命周期。 | boolean | AlertClosable | false |
role | 对于安静的静态示例,您可以使用 status。 | HTML div role | alert |
classNames | 语义槽类。 | AlertSemanticSlots | callback | — |
styles | CSS 线槽;回调获取允许的道具。 | AlertSemanticSlots | callback | — |
nativeElement | 关闭后 bind:nativeElement 被清除。 | HTMLDivElement | — |
AlertClosable
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
disabled | 禁用关闭按钮。 | boolean | false |
closeIcon | 文本或片段按钮。 | AlertContent | 叉 |
onClose | 关闭请求。 preventDefault 不是否决权。 | (event: MouseEvent) => void | — |
afterClose | 移除 DOM 后;当所有者卸载时被取消。 | () => void | — |
aria-label | 可用按钮名称。 | string | 本土化 |
标题通过 title 指定;关闭回调是 closable 的。 ConfigProvider.alert 指定 variant、外观、close 默认值和类型图标。
Semantic DOM
默认情况下,root 具有 role=alert。操作保留了本机按钮和链接,close 是具有可访问名称的真实按钮。title 是消息的一部分,而不是页面的标题。
| Slot | 目的 |
|---|---|
root | 消息容器。 |
icon | 图标。 |
section | 文本区域。 |
title | 消息标题。 |
description | 细节。 |
actions | 行动。 |
close | 关闭按钮。 |
注册令牌
安装的包支持 borderRadius、defaultPadding、withDescriptionPadding 和 withDescriptionIconSize。 6.7 文档中的名称 containerBorderRadius 尚未出现在公共包类型中。
| 代币 | 目的 |
|---|---|
borderRadius | 已安装的 Svelte 包中容器的半径。 |
defaultPadding | 短消息缩进。 |
withDescriptionPadding | 带有 description 的缩进。 |
withDescriptionIconSize | 图标大小及 description。 |
| 代币 | 意义 |
|---|---|
colorInfoBg | #e6f4ff |
colorSuccessBg | #f6ffed |
colorWarningBg | #fffbe6 |
colorErrorBg | #fff2f0 |
colorText | rgba(0,0,0,0.88) |
lineHeight | 1.5714285714285714 |
fontSize | 14 |
FAQ
为什么深色主题中的信息警报保持浅色?
在当前原生路径中,仅通过 mode/primary/compact 设置,info/outlined 具有浅色文本,但背景保持浅色。标准前缀和通用前缀都可以观察到这一点。测试示例中带有 cssVar 和 token.colorPrimary 的源配置使用深色调色板。 主题指南 中描述了算法的设置;常规 prefixCls 不会切换调色板。
如何再次显示已关闭的警报?
通过键或组合状态创建一个新实例。它没有 open/bind:open。
删除后使用什么事件?
closable.afterClose。 onClose 也在 closable 对象内部;标题由 title 设置。
有 Alert.ErrorBoundary 吗?
否。在 failed-snippet 中使用 svelte:boundary 和 Alert,如示例所示。
Loop banner 的流量从哪里来?
来自 CSS 演示。有一个开关,减少运动会使文本保持静止。
半径令牌叫什么?
在已安装的公共 API - borderRadius 中。新官方页面的 ContainerBorderRadius 尚不支持。
如何将长 title 与第一行对齐?
为特定的构图设置语义 styles。没有 description 的标准警报可以使内容居中。