Skip to main content

吐司组件

最后更新:

仅 CSS Tailwind CSS toast 用于通知定位:顶部、底部、开始、结束和中心放置。没有用于布局的 JavaScript。 WCAG AA 可访问,可与 Django、HTMX、Laravel、React 和任何堆栈配合使用。

Toast 组件在屏幕边缘显示简短的、非侵入性的通知。 Toast 采用固定定位和警报组件构建,可传达系统反馈,例如成功确认、错误消息或状态更新。 Frutjam toast 系统支持六个放置位置,非常适合表单提交、异步操作和实时通知。

仅 CSS,无需 JavaScript。 WCAG AA 可访问且与框架无关:适用于 Django、HTMX、Laravel、React 和任何堆栈。

班级类型描述
toast根据固定位置通知容器
toast-top修饰符锚定到视口顶部
toast-bottom修饰符锚定到视口底部(默认)
toast-start修饰符锚点向左
toast-center修饰符水平居中
toast-end修饰符锚点位于右侧(默认)
toast-middle修饰符垂直居中
toast-slide-up修饰符从下方进入,按其自身高度
toast-slide-down修饰符从上方进入
toast-slide-start修饰符从左进入,RTL 从右进入
toast-slide-end修饰符从右进入,RTL 中从左进入
--toast--宽度尺寸每个吐司的最大宽度, 24rem 默认情况下

基本用法

New message arrived.
1
2
3
4
5
<div role="status" aria-live="polite" class="toast">
  <div class="alert alert-info">
    <span>New message arrived.</span>
  </div>
</div>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import { useToast } from 'frutjam/react'

export default function ToastDemo() {
  const toast = useToast()
  return (
    <>
      <button className="btn" onClick={() => toast.info('New message arrived.')}>Show Toast</button>
      <div role="status" aria-live="polite" className="toast">
        {toast.toasts.map(t => (
          <div key={t.id} className={`alert${t.type ? ` alert-${t.type}` : ''}`}>
            <span>{t.message}</span>
          </div>
        ))}
      </div>
    </>
  )
}

敬酒姿势

最高职位

Top Start
Top Center
Top End
html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<div role="status" aria-live="polite" class="toast toast-top toast-start">
  <div class="alert alert-info"><span>Top Start</span></div>
</div>

<div role="status" aria-live="polite" class="toast toast-top toast-center">
  <div class="alert alert-info"><span>Top Center</span></div>
</div>

<div role="status" aria-live="polite" class="toast toast-top toast-end">
  <div class="alert alert-info"><span>Top End</span></div>
</div>

底部位置

Bottom Start
Bottom Center
Bottom End
html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<div role="status" aria-live="polite" class="toast toast-bottom toast-start">
  <div class="alert alert-info"><span>Bottom Start</span></div>
</div>

<div role="status" aria-live="polite" class="toast toast-bottom toast-center">
  <div class="alert alert-info"><span>Bottom Center</span></div>
</div>

<div role="status" aria-live="polite" class="toast toast-bottom toast-end">
  <div class="alert alert-info"><span>Bottom End</span></div>
</div>

居中

水平和垂直修改器结合在一起,所以 toast-center toast-middle 在屏幕中间放一个吐司。

Middle Center
html
1
2
3
<div role="status" aria-live="polite" class="toast toast-middle toast-center">
  <div class="alert alert-info"><span>Middle Center</span></div>
</div>

入场动画

每片吐司都会自行褪色并升起。没有什么可以添加的,也没有类可以打开它。

这 toast-slide-* 当您想要与默认上升不同的方向时,可以使用修改器。选择吐司所在的边缘,这样它就会从屏幕外部进入,而不是穿过屏幕: toast-slide-end 属于 toast-end, toast-slide-down 和 toast-top.

Drops in from the top
html
1
2
3
<div role="status" aria-live="polite" class="toast toast-top toast-end toast-slide-down">
  <div class="alert alert-info"><span>Drops in from the top</span></div>
</div>

该距离是 toast 自身的宽度或高度,而不是到屏幕边缘的距离,因此居中的 toast 从中间附近开始,而不是在屏幕外。

没有退出动画。 Toast 从页面中删除后就会消失,因为 CSS 没有可以转换的状态。淡出一个意味着将其保留在 DOM 中并自己为其设置动画。

对于任何要求减少动作的人来说,动画都会被跳过。

宽度

每个吐司的上限为 24rem 因此长消息不会跨越手机屏幕。覆盖 --toast--width 改变它。

html
1
2
3
<div role="status" aria-live="polite" class="toast [--toast--width:32rem]">
  <div class="alert alert-info"><span>A wider toast.</span></div>
</div>

吐司颜色

File saved successfully.
Storage is almost full.
Connection failed.
Update available.
html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<div role="status" aria-live="polite" class="toast toast-top toast-end">
  <div class="alert alert-success">
    <span>File saved successfully.</span>
  </div>
  <div class="alert alert-warning">
    <span>Storage is almost full.</span>
  </div>
  <div class="alert alert-error">
    <span>Connection failed.</span>
  </div>
  <div class="alert alert-info">
    <span>Update available.</span>
  </div>
</div>

多次吐司

Profile updated.
2 new notifications.
html
1
2
3
4
5
6
7
8
<div role="status" aria-live="polite" class="toast toast-top toast-end">
  <div class="alert alert-success">
    <span>Profile updated.</span>
  </div>
  <div class="alert alert-info">
    <span>2 new notifications.</span>
  </div>
</div>

使用 Claude Code、Cursor 或其他 AI 编辑器?

Cherry MCP 为您的 AI 编辑器按需提供准确的 吐司 类名称和结构。不再有幻觉课程。

尝试Cherry MCP

Found this useful?

Star Frutjam on GitHub. It's how other developers discover it.