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.