Headless Notification
part ของ Notification ให้พฤติกรรมเดียวกับ Notification แบบมี style แต่ไม่มี style เลย ได้แก่ role ที่ถูกต้อง (status ไม่มี role สำหรับ callout และ alertdialog เมื่อมีปุ่มคำสั่ง) การย้ายโฟกัสไปที่ปุ่มคำสั่งของแบบ actionable และให้ Tab วนอยู่ในตัวมัน Esc ที่ปิดมันระหว่างที่โฟกัสอยู่ข้างใน timeout ที่หยุดนับระหว่างที่เมาส์ชี้หรือโฟกัสอยู่ข้างใน และการคืนโฟกัสเมื่อมันปิดตัวเอง
ส่วนที่คุณทำเองคือ markup ไอคอน ข้อความ และ style ทั้งหมด Root ไม่ render อะไรเลยระหว่างที่ notification ปิดอยู่
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Notification } from '@purpurabog/ui/headless';
วิธีใช้
import { Notification } from '@purpurabog/ui/headless';
export default function Example() {
return (
<Notification.Root>
<Notification.Box className="flex max-w-xl items-start gap-4 border-s-[3px] border-error bg-inverse-canvas ps-3 text-sm text-inverse-ink">
<svg viewBox="0 0 20 20" role="img" aria-label="ไอคอนข้อผิดพลาด" className="mt-3.5 size-5 shrink-0 fill-error">
<path d="M10 1a9 9 0 1 0 0 18 9 9 0 0 0 0-18Zm3.5 13.5L10 11l-3.5 3.5L5 13l3.5-3.5L5 6l1.5-1.5L10 8l3.5-3.5L15 6l-3.5 3.5L15 13z" />
</svg>
<p className="flex-1 py-[15px]">
<Notification.Title className="me-1 font-semibold">อัปโหลดไม่สำเร็จ</Notification.Title>
<Notification.Subtitle>contract.pdf มีขนาดเกิน 25 MB</Notification.Subtitle>
</p>
<Notification.Close aria-label="ปิดการแจ้งเตือน" className="grid size-12 shrink-0 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>
);
}
ถ้าไม่ตั้ง open Root จะซ่อน notification เองเมื่อปิด Box เป็นตัวที่มี role ในตัวอย่างนี้คือ status screen reader จึงอ่านสิ่งที่เปลี่ยนแบบสุภาพ
โครงสร้าง
<Notification.Root>
<Notification.Box>
<Notification.Title />
<Notification.Subtitle />
<Notification.Action />
<Notification.Close />
</Notification.Box>
</Notification.Root>
มีปุ่มคำสั่ง
actionable (พร้อม Action ข้างใน) ทำให้ Box เป็น alertdialog ที่ใช้ Title เป็นชื่อและ Subtitle เป็นคำอธิบาย ตอนแสดงขึ้นมา โฟกัสจะย้ายไปที่ Action (autoFocus={false} ให้โฟกัสอยู่ที่เดิม) Tab และ Shift+Tab วนอยู่ในตัวมัน Esc ปิดมันระหว่างที่โฟกัสอยู่ข้างใน (closeOnEscape={false} ปิดความสามารถนี้) เมื่อปิดด้วย Close, Esc, การครบเวลา หรือ requestClose ระหว่างที่โฟกัสอยู่ข้างใน โฟกัสจะกลับไปที่เดิม ส่วนการตั้ง open เป็น false เองจะไม่ย้ายโฟกัส (Carbon ก็ไม่ย้าย) Action ไม่ปิด notification เอง ให้เรียก requestClose จาก Notification.useNotificationContext() ใน onClick ซึ่งจะปิดแบบเดียวกับ Close และคืนโฟกัสด้วย
import { useState } from 'react';
import { Notification } from '@purpurabog/ui/headless';
function UndoAction({ onUndo }: { onUndo: () => void }) {
const { requestClose } = Notification.useNotificationContext();
return (
<Notification.Action
onClick={() => {
onUndo();
requestClose('close-button');
}}
className="h-8 px-4 text-link-inverse hover:bg-inverse-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-inverse-ink"
>
เลิกทำ
</Notification.Action>
);
}
export default function Example() {
const [shown, setShown] = useState(false);
const [result, setResult] = useState('');
return (
<div className="grid justify-items-start gap-2">
<button type="button" onClick={() => { setShown(true); setResult(''); }} className="h-10 bg-button-danger px-4 text-sm text-on-primary hover:bg-error-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus">
ลบร่าง
</button>
<p className="text-xs text-ink-muted">ผลลัพธ์: {result || '(ยังไม่มี)'}</p>
<Notification.Root actionable open={shown} onClose={() => setShown(false)}>
<Notification.Box className="flex max-w-xl flex-wrap items-center gap-x-4 border-s-[3px] border-info bg-inverse-canvas ps-3 text-sm text-inverse-ink">
<p className="flex-1 py-[15px]">
<Notification.Title className="me-1 font-semibold">ลบร่างแล้ว</Notification.Title>
<Notification.Subtitle>ร่างสัญญาเช่าชั้น 3</Notification.Subtitle>
</p>
<UndoAction onUndo={() => setResult('กู้คืนแล้ว')} />
<Notification.Close aria-label="ปิดการแจ้งเตือน" className="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>
</div>
);
}
Callout
variant="callout" ทำให้ Box ไม่มี role เพราะเป็นคำแนะนำที่อยู่กับหน้าเว็บ Action ของมันใช้ Title เป็นคำอธิบาย ไม่ต้องใส่ Close และ actionable เพราะ callout อยู่ตลอด
import { Notification } from '@purpurabog/ui/headless';
export default function Example() {
return (
<Notification.Root variant="callout">
<Notification.Box className="flex max-w-xl flex-wrap items-center gap-x-4 border border-s-[3px] border-info bg-notification-background-info ps-3 text-sm text-ink">
<p className="flex-1 py-[15px]">
<Notification.Title className="me-1 font-semibold">ปิดรับการลงนามวันศุกร์</Notification.Title>
<Notification.Subtitle>ผู้ลงนามสองคนยังไม่ได้เปิดเอกสาร</Notification.Subtitle>
</p>
<Notification.Action className="me-4 h-8 px-4 text-link hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
ส่งการแจ้งเตือน
</Notification.Action>
</Notification.Box>
</Notification.Root>
);
}
ปิดเองเมื่อครบเวลา
timeout ปิดมันเมื่อครบจำนวนมิลลิวินาทีที่ตั้ง ('timeout') การนับเวลาหยุดชั่วคราวระหว่างที่เมาส์ชี้ที่ Box หรือโฟกัสอยู่ข้างใน และแบบ actionable จะไม่ปิดเอง ลองชี้ที่การ์ดเพื่อให้มันค้างไว้
import { useState } from 'react';
import { Notification } from '@purpurabog/ui/headless';
export default function Example() {
const [shown, setShown] = useState(true);
return (
<div className="grid justify-items-start gap-4">
<Notification.Root variant="toast" timeout={5000} open={shown} onClose={() => setShown(false)}>
<Notification.Box className="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">บันทึกแล้ว</Notification.Title>
<Notification.Subtitle className="block">ข้อความนี้จะปิดเองใน 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>
<button type="button" onClick={() => setShown(true)} className="h-8 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>
</div>
);
}
ทุกครั้งที่แสดงอีกครั้ง จะเริ่มนับเวลาใหม่
ให้เปิดค้างไว้
onClose(reason) ทำงานเมื่อกด Close กด Esc หรือครบเวลา ('close-button', 'escape', 'timeout') คืนค่า false เพื่อให้ notification เปิดค้างไว้ ถ้าตั้ง open มันจะปิดเมื่อคุณตั้ง open เป็น false เท่านั้น
import { useState } from 'react';
import { Notification } from '@purpurabog/ui/headless';
export default function Example() {
const [pinned, setPinned] = useState(true);
const [reason, setReason] = useState<Notification.NotificationCloseReason>();
return (
<div className="grid justify-items-start gap-2">
<Notification.Root
onClose={(why) => {
setReason(why);
return !pinned;
}}
>
<Notification.Box className="flex max-w-xl items-start gap-4 border border-s-[3px] border-warning bg-notification-background-warning ps-3 text-sm text-ink">
<p className="flex-1 py-[15px]">
<Notification.Title className="me-1 font-semibold">ยังไม่ได้ชำระอากรแสตมป์</Notification.Title>
<Notification.Subtitle>ชำระก่อนยื่นสัญญา</Notification.Subtitle>
</p>
<Notification.Close aria-label="ปิดการแจ้งเตือน" className="grid size-12 shrink-0 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
<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>
<label className="flex items-center gap-2 text-sm">
<input type="checkbox" checked={pinned} onChange={(e) => setPinned(e.target.checked)} className="size-4 accent-interactive" />
แสดงไว้จนกว่าจะชำระอากร
</label>
<p className="text-xs text-ink-muted">ขอปิดด้วย: {reason ?? '(ยังไม่มี)'}</p>
</div>
);
}
ใส่ style
part ต่าง ๆ ไม่ตั้ง attribute data-* เลย ชนิดและหน้าตาเป็นของคุณ ใส่ชนิดที่ Box เอง เช่น data-kind ในตัวอย่างนี้ แล้วใส่ style ตามนั้น
import { Notification } from '@purpurabog/ui/headless';
const kinds = [
{ kind: 'success', title: 'ลงนามครบแล้ว', text: 'ทุกฝ่ายลงนามแล้ว' },
{ kind: 'info', title: 'มีแบบฟอร์มใหม่', text: 'สัญญาเช่า 2569 พร้อมใช้แล้ว' },
{ kind: 'warning', title: 'ใกล้หมดอายุ', text: 'สัญญาเช่าสิ้นสุด 31 ตุลาคม 2569' },
{ kind: 'error', title: 'ชำระเงินไม่สำเร็จ', text: 'ธนาคารปฏิเสธการโอน' },
];
export default function Example() {
return (
<div className="grid gap-4">
{kinds.map(({ kind, title, text }) => (
<Notification.Root key={kind}>
<Notification.Box
data-kind={kind}
className="flex max-w-xl items-start gap-4 border border-s-[3px] ps-3 text-sm text-ink data-[kind=error]:border-error data-[kind=error]:bg-notification-background-error data-[kind=info]:border-info data-[kind=info]:bg-notification-background-info data-[kind=success]:border-success data-[kind=success]:bg-notification-background-success data-[kind=warning]:border-warning data-[kind=warning]:bg-notification-background-warning"
>
<p className="flex-1 py-[15px]">
<Notification.Title className="me-1 font-semibold">{title}</Notification.Title>
<Notification.Subtitle>{text}</Notification.Subtitle>
</p>
<Notification.Close aria-label="ปิดการแจ้งเตือน" className="grid size-12 shrink-0 place-items-center focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
<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>
))}
</div>
);
}
/* className="notification" และ data-kind ที่ Box */
.notification { display: flex; border-inline-start: 3px solid; }
.notification[data-kind='error'] { border-color: #da1e28; background: #fff1f1; }
.notification[data-kind='success'] { border-color: #24a148; background: #defbe6; }
.notification[role='alertdialog']:focus-within { outline: 2px solid #0f62fe; }
Parts
Notification.Root
ไม่ render element ของตัวเอง render แค่ children ระหว่างที่ notification เปิดอยู่ มันเก็บสถานะเปิดปิด ย้ายโฟกัส รับ Esc และนับเวลา
| Prop |
Type |
ค่าเริ่มต้น |
|
open |
boolean |
|
ควบคุมการแสดงเอง ถ้าไม่ตั้ง notification จะซ่อนตัวเองเมื่อปิด |
onClose |
(reason: Notification.NotificationCloseReason) => boolean | void |
|
Close, Esc หรือครบเวลา: 'close-button', 'escape', 'timeout' คืนค่า false เพื่อให้เปิดค้าง |
variant |
'inline' | 'toast' | 'callout' |
'inline' |
callout ไม่มี role |
role |
'status' | 'alert' | 'log' | 'alertdialog' | 'none' |
|
role ของ Box แทนตัวที่เลือกให้ |
actionable |
boolean |
false |
มี Action: เป็น alertdialog ที่ไม่ปิดเอง |
timeout |
number |
0 |
มิลลิวินาทีก่อนปิดเอง 0 = ไม่ปิดเอง |
closeOnEscape |
boolean |
true |
Esc ปิดแบบ alertdialog ระหว่างที่โฟกัสอยู่ข้างใน |
autoFocus |
boolean |
true |
แบบ alertdialog ย้ายโฟกัสไปที่ Action ตอนแสดง |
Notification.useNotificationContext() คืนสถานะของ Root (open, variant, role, requestClose …) ให้ component ที่อยู่ข้างใน
Notification.Box
<div> ที่มี role: ค่าเริ่มต้น status callout ไม่มี role actionable เป็น alertdialog หรือ role ที่คุณตั้ง (none คือไม่มี role) แบบ alertdialog ใช้ Title เป็นชื่อ Subtitle เป็นคำอธิบาย และให้ Tab วนอยู่ในลิงก์และปุ่มของมัน การนับเวลาหยุดชั่วคราวระหว่างที่เมาส์ชี้หรือโฟกัสอยู่ข้างใน
Notification.Title, Notification.Subtitle
<span> ที่มี id (ของคุณ หรือ <id>-title, <id>-subtitle) Title เป็นชื่อของแบบ alertdialog และเป็นคำอธิบายของ Action ใน callout ส่วน Subtitle เป็นคำอธิบายของแบบ alertdialog
Notification.Action
<button type="button"> เป็นโฟกัสแรกของแบบ alertdialog และกด Tab ไปถึงได้แม้ใน Safari (tabIndex={0}) ใน callout มันใช้ Title เป็นคำอธิบาย มันไม่ปิด notification เอง
Notification.Close
<button type="button"> ที่ขอปิด ('close-button') ต้องใส่ aria-label
คีย์บอร์ดและ accessibility
| ปุ่ม |
ทำอะไร |
| Tab / Shift+Tab |
ในแบบ alertdialog วนอยู่ในลิงก์และปุ่มของมัน แบบอื่นใช้ลำดับปกติของหน้า |
| Esc |
ปิดแบบ alertdialog ระหว่างที่โฟกัสอยู่ข้างใน (closeOnEscape) |
part ต่าง ๆ ตั้ง role และสำหรับ alertdialog ตั้ง aria-labelledby และ aria-describedby
ส่วนที่คุณเพิ่มเอง
- ไอคอนบอกสถานะพร้อมชื่อ (
role="img" และ aria-label เช่น "ไอคอนข้อผิดพลาด") หรือข้อความที่บอกชนิด
aria-label ที่ Close
- live region ที่มีอยู่ก่อนข้อความ สำหรับข้อความที่ต้องถูกอ่าน: render
Root ไว้ในหน้าแล้วเปลี่ยนข้อความ หรือใช้ region ของ Toast