Toast 通知
实时预览
引入
html
<link rel="stylesheet" href="/tokens/tokens.css">
<link rel="stylesheet" href="/components/toast/toast.css">代码
HTML
html
<!-- Toast Component — 4 variants: default, success, error, loading -->
<!-- Demo: Toast 组件 -->
<div style="padding: 20px;">
<h3 style="margin: 0 0 16px 0; font-size: var(--text-lg);">Toast 通知示例</h3>
<p style="margin: 0 0 16px 0; color: var(--color-text-secondary);">实际使用时,Toast 会出现在屏幕右上角。</p>
<!-- 模拟 Toast 容器 -->
<div class="toast-container" style="position: relative; top: auto; right: auto; max-width: 400px;">
<!-- 默认 Toast -->
<div class="toast toast--default">
<svg class="toast__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"></circle>
<path d="M12 16v-4M12 8h.01"></path>
</svg>
<div class="toast__content">
<h4 class="toast__title">新消息</h4>
<p class="toast__description">您有一条新的系统通知。</p>
</div>
<button class="toast__close" aria-label="关闭">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"></path>
</svg>
</button>
<div class="toast__progress">
<div class="toast__progress-bar"></div>
</div>
</div>
<!-- 成功 Toast -->
<div class="toast toast--success">
<svg class="toast__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
<polyline points="22 4 12 14.01 9 11.01"></polyline>
</svg>
<div class="toast__content">
<h4 class="toast__title">保存成功</h4>
<p class="toast__description">您的更改已成功保存。</p>
</div>
<button class="toast__close" aria-label="关闭">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"></path>
</svg>
</button>
<div class="toast__progress">
<div class="toast__progress-bar"></div>
</div>
</div>
<!-- 错误 Toast -->
<div class="toast toast--error">
<svg class="toast__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"></circle>
<line x1="15" y1="9" x2="9" y2="15"></line>
<line x1="9" y1="9" x2="15" y2="15"></line>
</svg>
<div class="toast__content">
<h4 class="toast__title">操作失败</h4>
<p class="toast__description">无法连接到服务器,请稍后重试。</p>
</div>
<button class="toast__close" aria-label="关闭">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"></path>
</svg>
</button>
<div class="toast__progress">
<div class="toast__progress-bar"></div>
</div>
</div>
<!-- 加载 Toast -->
<div class="toast toast--loading">
<svg class="toast__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4M4.93 19.07l2.83-2.83M16.24 7.76l2.83-2.83"></path>
</svg>
<div class="toast__content">
<h4 class="toast__title">处理中</h4>
<p class="toast__description">正在上传文件,请稍候...</p>
</div>
<button class="toast__close" aria-label="关闭">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M18 6L6 18M6 6l12 12"></path>
</svg>
</button>
</div>
</div>
</div>CSS
css
/* Toast.css */
.toast-container {
position: fixed;
top: var(--space-4);
right: var(--space-4);
z-index: var(--z-toast);
display: flex;
flex-direction: column;
gap: var(--space-3);
max-width: 400px;
width: 100%;
pointer-events: none;
}
.toast {
display: flex;
align-items: flex-start;
gap: var(--space-3);
padding: var(--space-4);
border-radius: var(--radius-md);
background: var(--color-surface);
color: var(--color-text);
box-shadow: var(--shadow-lg);
border: 1px solid var(--color-border);
font-family: var(--font-sans);
font-size: var(--text-base);
line-height: var(--leading-normal);
pointer-events: auto;
animation: toast-slide-in var(--duration-normal) var(--ease-spring);
transition: all var(--duration-normal) var(--ease-default);
}
.toast:hover {
box-shadow: var(--shadow-xl);
}
.toast:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
/* ── 动画 ──────────────────────────── */
@keyframes toast-slide-in {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
@keyframes toast-slide-out {
from {
opacity: 1;
transform: translateX(0);
}
to {
opacity: 0;
transform: translateX(100%);
}
}
/* ── 图标 ──────────────────────────── */
.toast__icon {
width: 20px;
height: 20px;
flex-shrink: 0;
margin-top: 2px;
}
/* ── 内容区域 ──────────────────────────── */
.toast__content {
flex: 1;
min-width: 0;
}
.toast__title {
font-weight: var(--weight-semibold);
margin: 0 0 var(--space-1) 0;
color: var(--color-text);
}
.toast__description {
margin: 0;
color: var(--color-text-secondary);
font-size: var(--text-sm);
}
/* ── 关闭按钮 ──────────────────────────── */
.toast__close {
background: none;
border: none;
padding: var(--space-1);
cursor: pointer;
color: var(--color-text-muted);
border-radius: var(--radius-sm);
transition: all var(--duration-fast) var(--ease-default);
flex-shrink: 0;
}
.toast__close:hover {
background: var(--color-bg-muted);
color: var(--color-text);
}
.toast__close svg {
width: 16px;
height: 16px;
display: block;
}
/* ── 进度条 ──────────────────────────── */
.toast__progress {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 3px;
background: var(--color-bg-muted);
border-radius: 0 0 var(--radius-md) var(--radius-md);
overflow: hidden;
}
.toast__progress-bar {
height: 100%;
background: var(--color-primary);
width: 100%;
transform-origin: left;
animation: toast-progress 5s linear forwards;
}
@keyframes toast-progress {
from {
transform: scaleX(1);
}
to {
transform: scaleX(0);
}
}
/* ── 变体样式 ──────────────────────────── */
.toast--default {
/* 默认样式已定义 */
}
.toast--default .toast__icon {
color: var(--color-primary);
}
.toast--success {
border-left: 4px solid var(--color-success);
}
.toast--success .toast__icon {
color: var(--color-success);
}
.toast--success .toast__progress-bar {
background: var(--color-success);
}
.toast--error {
border-left: 4px solid var(--color-danger);
}
.toast--error .toast__icon {
color: var(--color-danger);
}
.toast--error .toast__progress-bar {
background: var(--color-danger);
}
.toast--loading {
border-left: 4px solid var(--color-info);
}
.toast--loading .toast__icon {
color: var(--color-info);
animation: toast-spin 1s linear infinite;
}
@keyframes toast-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
/* ── 暗黑模式适配 ──────────────────────────── */
[data-theme="dark"] .toast {
background: var(--color-surface);
color: var(--color-text);
border-color: var(--color-border);
box-shadow: var(--shadow-xl);
}
[data-theme="dark"] .toast__title {
color: var(--color-text);
}
[data-theme="dark"] .toast__description {
color: var(--color-text-secondary);
}
[data-theme="dark"] .toast__progress {
background: var(--color-bg-muted);
}AI 使用说明
组件名: toast
选择器: .toast
依赖: /tokens/tokens.css
文件: /components/toast/toast.html