ui
ธีม รูปแบบ CarbonKanok ภาษา English
ภาพรวมติดตั้งการย้ายไป 1.0ธีมและ layerดีไซน์ Kanokปรับแต่งหน้าตาไอคอนฟอร์มและการตรวจค่าใช้กับ frameworkใช้กับ AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeแนะนำText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place

Headless Toast

part ของ Toaster ให้พฤติกรรมเดียวกับ region ของ Toast แบบมี style แต่ไม่มี style เลย ได้แก่ คิวของ toast (ไม่เกิน max ตัว ตัวใหม่ปิดตัวที่เก่าที่สุด และ id เดียวกันแทนที่ตัวที่เปิดอยู่) live region แบบสุภาพหนึ่งอันที่อยู่ในหน้าก่อนมี toast ใด ๆ ทุกตัวจึงถูกอ่าน ซึ่งอยู่ใน top layer ตั้งแต่แรก และถูกยกขึ้นเหนือ dialog แบบ modal เมื่อมี toast เข้ามาระหว่างที่ modal เปิดอยู่

ส่วนที่คุณทำเองคือ markup ของ toast แต่ละตัว (มักใช้ part ของ Notification) ตำแหน่งของ region และ style ทั้งหมด

ติดตั้ง

ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):

npm install @purpurabog/ui import { Notification, Toaster } from '@purpurabog/ui/headless';

วิธีใช้

import { Notification, Toaster } from '@purpurabog/ui/headless'; type Toast = { title: string; subtitle?: string }; // สร้างครั้งเดียวต่อแอป โค้ดส่วนไหนก็เพิ่ม toast ได้ const queue = new Toaster.ToastQueue<Toast>(); export default function Example() { const toasts = Toaster.useToasts(queue); return ( <> <button type="button" onClick={() => queue.add({ title: 'บันทึกสัญญาแล้ว', subtitle: 'CT-2026-0142' })} className="h-10 bg-interactive px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> บันทึกสัญญา </button> <Toaster.Root queue={queue} aria-label="การแจ้งเตือน" className="pointer-events-none fixed inset-auto end-4 top-16 m-0 flex flex-col-reverse gap-2 overflow-visible bg-transparent p-0"> {toasts.map(({ key, data }) => ( <Notification.Root key={key} variant="toast" role="none" timeout={Toaster.toastTimeout('success')} onClose={() => queue.remove(key)}> <Notification.Box className="pointer-events-auto relative w-72 border-s-[3px] border-success bg-inverse-canvas py-4 ps-3 pe-12 text-sm text-inverse-ink shadow-lg"> <Notification.Title className="block font-semibold">{data.title}</Notification.Title> <Notification.Subtitle className="block">{data.subtitle}</Notification.Subtitle> <Notification.Close aria-label="ปิดการแจ้งเตือน" className="absolute end-0 top-0 grid size-12 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink"> <svg viewBox="0 0 20 20" aria-hidden="true" className="size-5 fill-current"> <path d="M15 6.4 13.6 5 10 8.6 6.4 5 5 6.4 8.6 10 5 13.6 6.4 15l3.6-3.6 3.6 3.6 1.4-1.4-3.6-3.6z" /> </svg> </Notification.Close> </Notification.Box> </Notification.Root> ))} </Toaster.Root> </> ); }

toast แต่ละตัวคือ Notification ที่มี role="none" เพราะ region เป็นตัวประกาศ และลบออกจากคิวใน onClose ของมัน Root เป็น popover จึงอยู่ใน top layer ให้จัดตำแหน่งด้วย fixed และล้างค่า inset, margin และพื้นหลังของ popover อย่างในตัวอย่าง

โครงสร้าง

<Toaster.Root queue={queue} aria-label="การแจ้งเตือน"> <Notification.Root> <Notification.Box> <Notification.Title /> <Notification.Subtitle /> <Notification.Close /> </Notification.Box> </Notification.Root> </Toaster.Root>

ชนิดและเวลาที่อยู่

Toaster.toastTimeout(kind, timeout?) คือกฎที่ toaster แบบมี style ใช้: ใช้ timeout ที่ให้มา ถ้าไม่ให้จะเป็น 5 วินาทีสำหรับ info และ success และ 0 (อยู่จนกว่าจะปิด) สำหรับ error และ warning

import { Notification, Toaster } from '@purpurabog/ui/headless'; type Kind = 'success' | 'info' | 'warning' | 'error'; type Toast = { kind: Kind; title: string }; const queue = new Toaster.ToastQueue<Toast>(); const border: Record<Kind, string> = { success: 'border-success', info: 'border-info', warning: 'border-warning', error: 'border-error' }; const samples: Toast[] = [ { kind: 'success', title: 'ส่งคำขออนุมัติแล้ว' }, { kind: 'info', title: 'มีแบบฟอร์มใหม่' }, { kind: 'warning', title: 'ยังไม่ได้ชำระอากรแสตมป์' }, { kind: 'error', title: 'บันทึกไม่สำเร็จ' }, ]; export default function Example() { const toasts = Toaster.useToasts(queue); return ( <div className="flex flex-wrap gap-4"> {samples.map((toast) => ( <button key={toast.kind} type="button" onClick={() => queue.add(toast)} className="h-10 border border-interactive px-4 text-sm text-interactive capitalize hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> {toast.kind} </button> ))} <Toaster.Root queue={queue} aria-label="การแจ้งเตือน" className="pointer-events-none fixed inset-auto end-4 top-16 m-0 flex flex-col-reverse gap-2 overflow-visible bg-transparent p-0"> {toasts.map(({ key, data }) => ( <Notification.Root key={key} variant="toast" role="none" timeout={Toaster.toastTimeout(data.kind)} onClose={() => queue.remove(key)}> <Notification.Box className={`pointer-events-auto relative w-72 border-s-[3px] ${border[data.kind]} bg-inverse-canvas py-4 ps-3 pe-12 text-sm text-inverse-ink shadow-lg`}> <Notification.Title className="block font-semibold">{data.title}</Notification.Title> <Notification.Subtitle className="block text-inverse-ink-muted">{Toaster.toastTimeout(data.kind) ? 'ปิดเองใน 5 วินาที' : 'อยู่จนกว่าคุณจะปิด'}</Notification.Subtitle> <Notification.Close aria-label="ปิดการแจ้งเตือน" className="absolute end-0 top-0 grid size-12 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink"> <svg viewBox="0 0 20 20" aria-hidden="true" className="size-5 fill-current"> <path d="M15 6.4 13.6 5 10 8.6 6.4 5 5 6.4 8.6 10 5 13.6 6.4 15l3.6-3.6 3.6 3.6 1.4-1.4-3.6-3.6z" /> </svg> </Notification.Close> </Notification.Box> </Notification.Root> ))} </Toaster.Root> </div> ); }

แทนที่ และจำนวนที่แสดงพร้อมกัน

queue.add(data, id) แทนที่ toast ที่เปิดอยู่ซึ่งมี id เดียวกัน แทนที่จะซ้อนอีกตัว เช่น "กำลังซิงก์…" แล้วเป็น "ซิงก์แล้ว" queue.max (ค่าเริ่มต้น 3) คือจำนวนที่แสดงพร้อมกัน ตัวใหม่จะทิ้งตัวที่เก่าที่สุด queue.remove(key) และ queue.clear() ใช้เอา toast ออก

import { Notification, Toaster } from '@purpurabog/ui/headless'; type Toast = { title: string; done?: boolean }; const queue = new Toaster.ToastQueue<Toast>(); queue.max = 2; let signers = 0; export default function Example() { const toasts = Toaster.useToasts(queue); const sync = () => { queue.add({ title: 'กำลังซิงก์…' }, 'sync'); setTimeout(() => queue.add({ title: 'ซิงก์แล้ว', done: true }, 'sync'), 2000); }; const addSigner = () => { signers += 1; queue.add({ title: `เพิ่มผู้ลงนามคนที่ ${signers} แล้ว`, done: true }); }; return ( <div className="flex flex-wrap gap-4"> <button type="button" onClick={sync} className="h-10 border border-interactive px-4 text-sm text-interactive hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> ซิงก์สัญญา </button> <button type="button" onClick={addSigner} className="h-10 border border-interactive px-4 text-sm text-interactive hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> เพิ่มผู้ลงนาม </button> <button type="button" onClick={() => queue.clear()} className="h-10 px-4 text-sm text-link hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> ล้างทั้งหมด </button> <Toaster.Root queue={queue} aria-label="การแจ้งเตือน" className="pointer-events-none fixed inset-auto end-4 top-16 m-0 flex flex-col-reverse gap-2 overflow-visible bg-transparent p-0"> {toasts.map(({ key, data }) => ( <Notification.Root key={key} variant="toast" role="none" timeout={data.done ? 5000 : 0} onClose={() => queue.remove(key)}> <Notification.Box className="pointer-events-auto relative w-72 border-s-[3px] border-info bg-inverse-canvas py-4 ps-3 pe-12 text-sm text-inverse-ink shadow-lg"> <Notification.Title className="block font-semibold">{data.title}</Notification.Title> <Notification.Close aria-label="ปิดการแจ้งเตือน" className="absolute end-0 top-0 grid size-12 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink"> <svg viewBox="0 0 20 20" aria-hidden="true" className="size-5 fill-current"> <path d="M15 6.4 13.6 5 10 8.6 6.4 5 5 6.4 8.6 10 5 13.6 6.4 15l3.6-3.6 3.6 3.6 1.4-1.4-3.6-3.6z" /> </svg> </Notification.Close> </Notification.Box> </Notification.Root> ))} </Toaster.Root> </div> ); }

queue.add คืนค่า key ของ toast key ใหม่ทุกครั้งทำให้ Notification เริ่มใหม่ด้วย toast ที่ถูกแทนที่จึงเริ่มนับเวลาใหม่

มีปุ่มคำสั่ง

สำหรับ toast ที่มีปุ่มคำสั่ง ไม่ต้องใส่ role="none" และใส่ actionable มันจะยังเป็น alertdialog ดึงโฟกัส กัก Tab ไว้ข้างใน และไม่ปิดเอง ใช้เฉพาะเมื่อคำสั่งนั้นคุ้มกับการขัดจังหวะผู้ใช้

import { Notification, Toaster } from '@purpurabog/ui/headless'; type Toast = { title: string; retry?: boolean }; const queue = new Toaster.ToastQueue<Toast>(); function RetryAction() { const { requestClose } = Notification.useNotificationContext(); return ( <Notification.Action onClick={() => { requestClose('close-button'); queue.add({ title: 'กำลังลองอีกครั้ง…' }); }} className="ms-0 mt-4 h-8 border border-notification-action-tertiary-inverse px-4 text-notification-action-tertiary-inverse hover:bg-notification-action-tertiary-inverse-hover hover:text-notification-action-tertiary-inverse-text focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink" > ลองอีกครั้ง </Notification.Action> ); } export default function Example() { const toasts = Toaster.useToasts(queue); return ( <> <button type="button" onClick={() => queue.add({ title: 'อัปโหลดไม่สำเร็จ', retry: true })} className="h-10 border border-interactive px-4 text-sm text-interactive hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> อัปโหลด contract.pdf </button> <Toaster.Root queue={queue} aria-label="การแจ้งเตือน" className="pointer-events-none fixed inset-auto end-4 top-16 m-0 flex flex-col-reverse gap-2 overflow-visible bg-transparent p-0"> {toasts.map(({ key, data }) => ( <Notification.Root key={key} variant="toast" role={data.retry ? undefined : 'none'} actionable={data.retry} timeout={data.retry ? 0 : 5000} onClose={() => queue.remove(key)}> <Notification.Box className={`pointer-events-auto relative w-72 border-s-[3px] ${data.retry ? 'border-warning' : 'border-info'} bg-inverse-canvas py-4 ps-3 pe-12 text-sm text-inverse-ink shadow-lg`}> <Notification.Title className="block font-semibold">{data.title}</Notification.Title> <Notification.Subtitle className="block">contract.pdf</Notification.Subtitle> {data.retry ? <RetryAction /> : null} <Notification.Close aria-label="ปิดการแจ้งเตือน" className="absolute end-0 top-0 grid size-12 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink"> <svg viewBox="0 0 20 20" aria-hidden="true" className="size-5 fill-current"> <path d="M15 6.4 13.6 5 10 8.6 6.4 5 5 6.4 8.6 10 5 13.6 6.4 15l3.6-3.6 3.6 3.6 1.4-1.4-3.6-3.6z" /> </svg> </Notification.Close> </Notification.Box> </Notification.Root> ))} </Toaster.Root> </> ); }

ใส่ style

ตำแหน่งของ region เป็น CSS ของคุณ: มุมบนด้านท้ายใต้ header (ตัวใหม่สุดอยู่บนสุด flex-col-reverse) หรือมุมล่างด้านท้าย (ตัวใหม่สุดอยู่ล่างสุด) Toaster.ToasterPlacement ('top-end' | 'bottom-end') คือ type ที่ toaster แบบมี style ใช้เรื่องนี้

import { useState } from 'react'; import { Notification, Toaster } from '@purpurabog/ui/headless'; const queue = new Toaster.ToastQueue<{ title: string }>(); const corner: Record<Toaster.ToasterPlacement, string> = { 'top-end': 'top-16 flex-col-reverse', 'bottom-end': 'bottom-8 flex-col', }; export default function Example() { const toasts = Toaster.useToasts(queue); const [placement, setPlacement] = useState<Toaster.ToasterPlacement>('bottom-end'); return ( <div className="flex flex-wrap gap-4"> {(['top-end', 'bottom-end'] as const).map((side) => ( <button key={side} type="button" onClick={() => { setPlacement(side); queue.add({ title: `คัดลอกลิงก์แล้ว (${side})` }); }} className="h-10 border border-interactive px-4 text-sm text-interactive hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus" > {side} </button> ))} <Toaster.Root queue={queue} aria-label="การแจ้งเตือน" className={`pointer-events-none fixed inset-auto end-4 m-0 flex gap-2 overflow-visible bg-transparent p-0 ${corner[placement]}`}> {toasts.map(({ key, data }) => ( <Notification.Root key={key} variant="toast" role="none" timeout={5000} onClose={() => queue.remove(key)}> <Notification.Box className="pointer-events-auto relative w-72 border-s-[3px] border-info bg-inverse-canvas py-4 ps-3 pe-12 text-sm text-inverse-ink shadow-lg"> <Notification.Title className="block font-semibold">{data.title}</Notification.Title> <Notification.Close aria-label="ปิดการแจ้งเตือน" className="absolute end-0 top-0 grid size-12 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink"> <svg viewBox="0 0 20 20" aria-hidden="true" className="size-5 fill-current"> <path d="M15 6.4 13.6 5 10 8.6 6.4 5 5 6.4 8.6 10 5 13.6 6.4 15l3.6-3.6 3.6 3.6 1.4-1.4-3.6-3.6z" /> </svg> </Notification.Close> </Notification.Box> </Notification.Root> ))} </Toaster.Root> </div> ); } /* className="toaster" ที่ Root */ .toaster { position: fixed; inset: auto; inset-block-start: 64px; inset-inline-end: 16px; margin: 0; padding: 0; border: 0; background: transparent; overflow: visible; display: flex; flex-direction: column-reverse; gap: 8px; pointer-events: none; } .toaster > * { pointer-events: auto; }

Parts

Toaster.Root

<section popover="manual" aria-live="polite" aria-relevant="additions"> ที่แสดงใน top layer ทันทีที่ mount รับ prop ของ section ได้ทุกตัว ยกเว้น popover เมื่อมี toast เพิ่มเข้ามาระหว่างที่ dialog แบบ modal เปิดอยู่ มันจะแสดงตัวเองใหม่เพื่อให้อยู่เหนือ modal

Prop Type ค่าเริ่มต้น
queue Toaster.ToastQueue<T> toast ที่ region นี้แสดง (ต้องใส่) render มันเป็น children

ใส่ aria-label ให้มัน เช่น "การแจ้งเตือน"

Toaster.ToastQueue

class: new Toaster.ToastQueue<T>() เก็บ toast ที่เปิดอยู่ เรียงจากเก่าไปใหม่

Member
add(data, id?) เพิ่ม toast และคืนค่า key ของมัน แทนที่ตัวที่เปิดอยู่ซึ่งมี id เดียวกัน และทิ้งตัวที่เก่าที่สุดเมื่อเกิน max
remove(key) เอา toast หนึ่งตัวออก
clear() เอา toast ทั้งหมดออก
max จำนวน toast ที่แสดงพร้อมกัน (ค่าเริ่มต้น 3)
toasts toast ที่เปิดอยู่: { key, id?, data }[]
subscribe(listener) เรียก listener ทุกครั้งที่มีการเปลี่ยน และคืนฟังก์ชันยกเลิก

Toaster.useToasts

Toaster.useToasts(queue) คืน toast ในคิว และ render ใหม่เมื่อมีการเปลี่ยน

Toaster.toastTimeout

Toaster.toastTimeout(kind, timeout?) คืน timeout ถ้าให้มา ถ้าไม่ให้จะเป็น 5000 สำหรับ info และ success และ 0 สำหรับ error, warning และ warning-alt

คีย์บอร์ดและ accessibility

region ประกาศ toast แต่ละตัวแบบสุภาพ ตัว toast ไม่มี role ของตัวเอง (role="none") ยกเว้นแบบมีปุ่มคำสั่ง ซึ่งเป็น alertdialog ที่ดึงโฟกัสและกัก Tab ไว้จนกว่าจะปิด ปุ่มคีย์บอร์ดใน toast เป็นของ Notification

ส่วนที่คุณเพิ่มเอง

  • aria-label ที่ Root
  • role="none" ที่ toast ที่ไม่มีปุ่มคำสั่ง
  • การแจ้งผลใน modal ให้ใช้ notification แบบ inline เพราะหน้าเว็บนอก dialog แบบ modal เป็น inert toast ที่วาดทับ modal จึงคลิกไม่ได้และ screen reader อ่านไม่ได้