Toast
Toast คือข้อความสั้น ๆ ที่โผล่ขึ้นที่มุมจอหลังมีอะไรเกิดขึ้นที่อื่นในหน้า เช่น บันทึกสัญญาแล้ว หรืออัปโหลดเบื้องหลังไม่สำเร็จ toast จะซ้อนกัน ตัวใหม่สุดอยู่ใกล้มุมที่สุด และตัวที่ไม่สำคัญจะหายไปเอง Carbon ไม่มีตัวจัดการ toast ส่วน ui-toaster และ toast() เป็นของไลบรารีนี้ ถ้าข้อความควรอยู่ในหน้า ให้ใช้ Notification แบบ inline
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-save">บันทึกสัญญา</ui-button>
<ui-toaster></ui-toaster>
</div>
<script type="module">
// สิ่งที่ toast({ kind: 'success', title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' }) ทำ
const toaster = document.querySelector('ui-toaster');
document.getElementById('toast-save').addEventListener('click', () => {
const toast = document.createElement('ui-notification');
Object.assign(toast, { variant: 'toast', role: 'none', kind: 'success', heading: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142', timeout: 5000 });
toaster.add(toast);
});
</script>
import { toast } from '@purpurabog/ui/toaster';
<UiButton onClick={() => toast({ kind: 'success', title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' })}>บันทึกสัญญา</UiButton>
import { toast } from '@/components/ui/react-tw/toaster';
<Button onClick={() => toast({ kind: 'success', title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' })}>บันทึกสัญญา</Button>
ติดตั้ง
npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/toaster
npx shadcn@4 add @kanok-ui/tw-toaster
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/toaster.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/toaster/toaster.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-toaster, notification, shared-element, shared-localize, tokens
# React + Tailwind
# 1. ติดตั้ง dependency
npm install @purpurabog/ui@^2.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-toaster.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/toaster.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-notification, core-toaster, tw-i18n, tw-notification, tw-refs, tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
HTMLReactReact + Tailwind<script type="module">
import { toast } from '@purpurabog/ui/toaster';
document.getElementById('save').addEventListener('click', () => {
toast({ kind: 'success', title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' });
});
</script>
<ui-button id="save">บันทึกสัญญา</ui-button>
import { toast } from '@purpurabog/ui/toaster';
<UiButton onClick={() => toast({ kind: 'success', title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' })}>บันทึกสัญญา</UiButton>
import { Toaster, toast } from '@/components/ui/react-tw/toaster';
// ครั้งเดียว ใกล้ root ของแอป
<Toaster />
<Button onClick={() => toast({ kind: 'success', title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' })}>บันทึกสัญญา</Button>
toast(options) สร้าง <ui-notification variant="toast"> แล้วเพิ่มเข้า <ui-toaster> ของหน้า ครั้งแรกจะสร้าง toaster ไว้ท้าย <body> ให้ และคืนค่า { element, close } ตัวเลือกมี kind, title, subtitle, caption, timeout, actionLabel, onAction, lowContrast และ id
ตัวอย่างที่ใช้งานได้ในหน้านี้ import module ไม่ได้ จึงทำสิ่งที่ toast() ทำด้วยมือ คือสร้าง element แล้วส่งให้ add() ของ toaster ในแอปของคุณให้เรียก toast() React + Tailwind มี toast() ของตัวเอง และมี <Toaster> ที่ต้อง mount ครั้งเดียว toast() ของมันคืนค่า { close } และ toast.clear() ลบ toast ทั้งหมด
toaster เป็น live region แบบสุภาพหนึ่งอัน ชื่อ "การแจ้งเตือน" ซึ่งอยู่ในหน้าก่อนจะมี toast ใด ๆ screen reader จึงอ่านทุกตัว มันอยู่ใน top layer ห่างมุมบนด้านท้าย 16px หรือห่าง 16px ใต้ ui-header
ชนิดและเวลาที่อยู่
kind เริ่มต้นเป็น info toast แบบ info และ success ปิดเองหลัง 5 วินาที แบบ error และ warning อยู่จนกว่าจะปิด เพราะต้องอ่าน timeout ตั้งเวลาอื่นเป็นมิลลิวินาที หรือ 0 เพื่อให้อยู่ตลอด การนับเวลาหยุดชั่วคราวระหว่างที่เมาส์ชี้อยู่หรือโฟกัสอยู่ใน toast
Success
Info
Warning
Error
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button kind="tertiary" data-toast-kind="success">Success</ui-button>
<ui-button kind="tertiary" data-toast-kind="info">Info</ui-button>
<ui-button kind="tertiary" data-toast-kind="warning">Warning</ui-button>
<ui-button kind="danger" data-toast-kind="error">Error</ui-button>
<ui-toaster></ui-toaster>
</div>
<script type="module">
const toaster = document.querySelector('ui-toaster');
const texts = {
success: ['ส่งคำขออนุมัติแล้ว', 'ชัยยา ร. จะได้รับอีเมล'],
info: ['มีแบบฟอร์มใหม่', 'สัญญาเช่า 2569 พร้อมใช้แล้ว'],
warning: ['ยังไม่ได้ชำระอากรแสตมป์', 'ชำระก่อนยื่นสัญญา'],
error: ['บันทึกไม่สำเร็จ', 'เซิร์ฟเวอร์ไม่ตอบกลับ'],
};
for (const button of document.querySelectorAll('[data-toast-kind]')) {
button.addEventListener('click', () => {
const kind = button.dataset.toastKind;
const toast = document.createElement('ui-notification');
// toast() เลือกเวลาให้: 5 วินาทีสำหรับ success และ info, 0 (อยู่ตลอด) สำหรับ warning และ error
Object.assign(toast, { variant: 'toast', role: 'none', kind, heading: texts[kind][0], subtitle: texts[kind][1], timeout: kind === 'success' || kind === 'info' ? 5000 : 0 });
toaster.add(toast);
});
}
</script>
toast({ kind: 'success', title: 'ส่งคำขออนุมัติแล้ว', subtitle: 'ชัยยา ร. จะได้รับอีเมล' });
toast({ title: 'มีแบบฟอร์มใหม่', subtitle: 'สัญญาเช่า 2569 พร้อมใช้แล้ว' });
toast({ kind: 'warning', title: 'ยังไม่ได้ชำระอากรแสตมป์', subtitle: 'ชำระก่อนยื่นสัญญา' });
toast({ kind: 'error', title: 'บันทึกไม่สำเร็จ', subtitle: 'เซิร์ฟเวอร์ไม่ตอบกลับ' });
toast({ kind: 'success', title: 'ส่งคำขออนุมัติแล้ว', subtitle: 'ชัยยา ร. จะได้รับอีเมล' });
toast({ title: 'มีแบบฟอร์มใหม่', subtitle: 'สัญญาเช่า 2569 พร้อมใช้แล้ว' });
toast({ kind: 'warning', title: 'ยังไม่ได้ชำระอากรแสตมป์', subtitle: 'ชำระก่อนยื่นสัญญา' });
toast({ kind: 'error', title: 'บันทึกไม่สำเร็จ', subtitle: 'เซิร์ฟเวอร์ไม่ตอบกลับ' });
Caption และคอนทราสต์ต่ำ
caption เพิ่มบรรทัดเล็ก ๆ ใต้ subtitle มักเป็นเวลา lowContrast เปลี่ยน toast เป็นพื้นสีอ่อนแทนพื้นสีเข้ม
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-caption" kind="tertiary">ส่งออกเสร็จแล้ว</ui-button>
<ui-toaster></ui-toaster>
</div>
<script type="module">
const toaster = document.querySelector('ui-toaster');
document.getElementById('toast-caption').addEventListener('click', () => {
const toast = document.createElement('ui-notification');
const caption = new Date().toLocaleTimeString('th-TH', { hour: '2-digit', minute: '2-digit' });
Object.assign(toast, { variant: 'toast', role: 'none', kind: 'success', lowContrast: true, heading: 'ส่งออกเสร็จแล้ว', subtitle: 'contracts-2026.csv', caption, timeout: 5000 });
toaster.add(toast);
});
</script>
toast({ kind: 'success', lowContrast: true, title: 'ส่งออกเสร็จแล้ว', subtitle: 'contracts-2026.csv', caption: new Date().toLocaleTimeString('th-TH') });
toast({ kind: 'success', lowContrast: true, title: 'ส่งออกเสร็จแล้ว', subtitle: 'contracts-2026.csv', caption: new Date().toLocaleTimeString('th-TH') });
มีปุ่มคำสั่ง
actionLabel เพิ่มปุ่มสำหรับคำสั่งด่วนหนึ่งอย่าง และ onAction ทำงานเมื่อกดปุ่ม toast ที่มีปุ่มคำสั่งจะไม่ปิดเอง จะดึงโฟกัสตอนแสดง และกัก Tab ไว้ข้างในจนกว่าจะปิด (actionable notification ของ Carbon) ใช้เฉพาะเมื่อคำสั่งนั้นคุ้มกับการขัดจังหวะผู้ใช้
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-action" kind="tertiary">อัปโหลด contract.pdf</ui-button>
<ui-toaster></ui-toaster>
</div>
<script type="module">
const toaster = document.querySelector('ui-toaster');
const show = (options) => {
const toast = document.createElement('ui-notification');
Object.assign(toast, { variant: 'toast', ...options });
toaster.add(toast);
return toast;
};
document.getElementById('toast-action').addEventListener('click', () => {
// toast ที่มีปุ่มคำสั่งใช้ role ของตัวเอง (alertdialog)
const failed = show({ kind: 'warning', heading: 'อัปโหลดไม่สำเร็จ', subtitle: 'contract.pdf', actionLabel: 'ลองอีกครั้ง' });
failed.addEventListener('ui-action', () => {
failed.close();
show({ role: 'none', kind: 'info', heading: 'กำลังลองอีกครั้ง…', subtitle: 'contract.pdf', timeout: 5000 });
});
});
</script>
toast({
kind: 'warning',
title: 'อัปโหลดไม่สำเร็จ',
subtitle: 'contract.pdf',
actionLabel: 'ลองอีกครั้ง',
onAction: () => upload(file),
});
toast({
kind: 'warning',
title: 'อัปโหลดไม่สำเร็จ',
subtitle: 'contract.pdf',
actionLabel: 'ลองอีกครั้ง',
onAction: () => upload(file),
});
แทนที่ toast เดิม
toast ที่มี id เดียวกับตัวที่ยังเปิดอยู่จะแทนที่ตัวเดิม แทนที่จะซ้อนเพิ่มอีกตัว ใช้กับสถานะที่เปลี่ยนไป เช่น "กำลังซิงก์…" แล้วเป็น "ซิงก์แล้ว"
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-sync" kind="tertiary">ซิงก์สัญญา</ui-button>
<ui-toaster></ui-toaster>
</div>
<script type="module">
const toaster = document.querySelector('ui-toaster');
// toast({ id: 'sync', … }) ลบ toast ที่เปิดอยู่ซึ่งมี id นี้ก่อน
const show = (options) => {
toaster.querySelector('[data-toast-id="sync"]')?.remove();
const toast = document.createElement('ui-notification');
Object.assign(toast, { variant: 'toast', role: 'none', ...options });
toast.dataset.toastId = 'sync';
toaster.add(toast);
};
document.getElementById('toast-sync').addEventListener('click', () => {
show({ kind: 'info', heading: 'กำลังซิงก์…', subtitle: 'สัญญา 24 ฉบับ' });
setTimeout(() => show({ kind: 'success', heading: 'ซิงก์แล้ว', subtitle: 'สัญญา 24 ฉบับเป็นข้อมูลล่าสุด', timeout: 5000 }), 2000);
});
</script>
toast({ id: 'sync', title: 'กำลังซิงก์…', subtitle: 'สัญญา 24 ฉบับ', timeout: 0 });
await sync();
toast({ id: 'sync', kind: 'success', title: 'ซิงก์แล้ว', subtitle: 'สัญญา 24 ฉบับเป็นข้อมูลล่าสุด' });
toast({ id: 'sync', title: 'กำลังซิงก์…', subtitle: 'สัญญา 24 ฉบับ', timeout: 0 });
await sync();
toast({ id: 'sync', kind: 'success', title: 'ซิงก์แล้ว', subtitle: 'สัญญา 24 ฉบับเป็นข้อมูลล่าสุด' });
ปิดจากโค้ด
toast() คืนค่า handle ที่มี close() ไว้ปิด toast ตัวนั้น เช่น เมื่องานที่มันพูดถึงเสร็จแล้ว close() ของ element ก็ทำแบบเดียวกัน
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-handle" kind="tertiary">สร้างรายงาน</ui-button>
<ui-toaster></ui-toaster>
</div>
<script type="module">
const toaster = document.querySelector('ui-toaster');
document.getElementById('toast-handle').addEventListener('click', () => {
const toast = document.createElement('ui-notification');
Object.assign(toast, { variant: 'toast', role: 'none', kind: 'info', heading: 'กำลังสร้างรายงาน…', subtitle: 'ใช้เวลาไม่กี่วินาที' });
toaster.add(toast);
setTimeout(() => toast.close(), 3000);
});
</script>
const working = toast({ title: 'กำลังสร้างรายงาน…', subtitle: 'ใช้เวลาไม่กี่วินาที', timeout: 0 });
await generateReport();
working.close();
const working = toast({ title: 'กำลังสร้างรายงาน…', subtitle: 'ใช้เวลาไม่กี่วินาที', timeout: 0 });
await generateReport();
working.close();
แสดงได้กี่ตัวพร้อมกัน
toaster แสดง toast ได้ไม่เกิน max ตัว (ค่าเริ่มต้น 3) ตัวใหม่จะปิดตัวที่เก่าที่สุด ถ้าจะเปลี่ยนค่านี้ ให้วาง <ui-toaster> เอง toast() จะใช้ตัวแรกในหน้า ลองกดปุ่มหลาย ๆ ครั้ง
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-max" kind="tertiary">เพิ่มผู้ลงนาม</ui-button>
<ui-toaster id="toast-max-toaster" max="2"></ui-toaster>
</div>
<script type="module">
const toaster = document.getElementById('toast-max-toaster');
let count = 0;
document.getElementById('toast-max').addEventListener('click', () => {
count += 1;
const toast = document.createElement('ui-notification');
Object.assign(toast, { variant: 'toast', role: 'none', kind: 'success', heading: `เพิ่มผู้ลงนามคนที่ ${count} แล้ว`, timeout: 5000 });
toaster.add(toast);
});
</script>
// ครั้งเดียวในแอป แล้ว toast() จะใช้ตัวนี้
<UiToaster max={2} />
<Toaster max={2} />
มุมล่าง
placement="bottom-end" ให้ toast ซ้อนกันที่มุมล่างด้านท้าย ตัวใหม่สุดอยู่ล่างสุด --ui-toaster-offset กำหนดระยะห่างจากขอบ (React + Tailwind ใช้ offset)
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="toast-bottom" kind="tertiary">คัดลอกลิงก์</ui-button>
<ui-toaster id="toast-bottom-toaster" placement="bottom-end" style="--ui-toaster-offset: 32px"></ui-toaster>
</div>
<script type="module">
const toaster = document.getElementById('toast-bottom-toaster');
document.getElementById('toast-bottom').addEventListener('click', () => {
const toast = document.createElement('ui-notification');
Object.assign(toast, { variant: 'toast', role: 'none', kind: 'info', heading: 'คัดลอกลิงก์แล้ว', timeout: 5000 });
toaster.add(toast);
});
</script>
<UiToaster placement="bottom-end" style={{ '--ui-toaster-offset': '32px' } as CSSProperties} />
<Toaster placement="bottom-end" offset="32px" />
Toast กับ modal
toast ที่เพิ่มเข้ามาระหว่างที่ Modal เปิดอยู่จะถูกวาดทับ modal แต่หน้าเว็บนอก modal เป็น inert จึงไม่มีใครคลิกได้ และ screen reader เข้าไม่ถึง การแจ้งผลใน modal ให้ใช้ Notification แบบ inline แทน (ตามแนวทางของ Carbon)
แก้ไขหน่วยงาน
ปิด
บันทึก
HTMLReactReact + Tailwind<ui-button id="toast-modal-open" kind="tertiary">แก้ไขหน่วยงาน</ui-button>
<ui-modal id="toast-modal" heading="แก้ไขหน่วยงาน" size="sm">
<ui-notification id="toast-modal-saved" kind="success" low-contrast open="false" heading="บันทึกแล้ว" subtitle="ใช้ชื่อใหม่แล้ว"></ui-notification>
<ui-text-input label="ชื่อหน่วยงาน" value="กฎหมาย"></ui-text-input>
<ui-button slot="footer" kind="secondary" id="toast-modal-close">ปิด</ui-button>
<ui-button slot="footer" id="toast-modal-save">บันทึก</ui-button>
</ui-modal>
<script type="module">
const modal = document.getElementById('toast-modal');
document.getElementById('toast-modal-open').addEventListener('click', () => {
document.getElementById('toast-modal-saved').open = false;
modal.show();
});
document.getElementById('toast-modal-close').addEventListener('click', () => modal.close());
document.getElementById('toast-modal-save').addEventListener('click', () => (document.getElementById('toast-modal-saved').open = true));
</script>
<UiModal open={open} heading="แก้ไขหน่วยงาน" size="sm" onUiClose={(e) => { if (e.target === e.currentTarget) setOpen(false); }}>
<UiNotification kind="success" lowContrast open={saved} heading="บันทึกแล้ว" subtitle="ใช้ชื่อใหม่แล้ว" />
<UiTextInput label="ชื่อหน่วยงาน" value="กฎหมาย" />
<UiButton slot="footer" kind="secondary" onClick={() => setOpen(false)}>ปิด</UiButton>
<UiButton slot="footer" onClick={save}>บันทึก</UiButton>
</UiModal>
<Modal open={open} onClose={() => setOpen(false)} heading="แก้ไขหน่วยงาน" size="sm" footer={…}>
<Notification kind="success" lowContrast open={saved} title="บันทึกแล้ว" subtitle="ใช้ชื่อใหม่แล้ว" />
<TextInput label="ชื่อหน่วยงาน" defaultValue="กฎหมาย" />
</Modal>
Accessibility
- toast แต่ละตัวถูกอ่านผ่าน region ของ toaster ตัว toast เองไม่มี live role ส่วน toast ที่มีปุ่มคำสั่งยังเป็น
alertdialog และดึงโฟกัส
- toast ที่ปิดแล้วจะถูกลบออกจากหน้า
- เขียน toast ให้สั้น และอย่าใส่สิ่งที่ผู้ใช้หาได้จาก toast เท่านั้น เพราะเมื่อหายไปแล้วก็หายไปเลย
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Toast
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-toaster> · React UiToaster
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
placement |
placement |
ToasterPlacement |
'top-end' |
Corner: newest toast nearest the edge. |
max |
max |
number |
3 |
Most toasts shown at once; a new one closes the oldest. |
Method
| Method |
คำอธิบาย |
add(toast) |
Adds toast, closes the oldest beyond max, and lifts the region above anything opened since. |
Slot
| Slot |
คำอธิบาย |
| (ค่าเริ่มต้น) |
<ui-notification variant="toast"> elements (added by toast()) |
CSS custom property
| Custom property |
ค่าเริ่มต้น |
คำอธิบาย |
--ui-toaster-offset |
|
Distance from the top (or bottom) edge (default 16px; 64px under a ui-header) |