Headless Modal
part ของ Modal ให้พฤติกรรมเดียวกับ Modal แบบมี style แต่ไม่มี style เลย ได้แก่ <dialog> ของ browser ที่เปิดด้วย showModal() (top layer หน้าเว็บข้างหลังเป็น inert Tab วนอยู่ข้างใน และโฟกัสกลับไปที่ตัวที่เปิดเมื่อปิด) Esc และการคลิกข้างนอกที่ขอปิด โฟกัสแรกแบบ Carbon ส่วนเนื้อหาที่กลายเป็น region โฟกัสได้เมื่อยาวเกิน และหน้าเว็บที่เลื่อนไม่ได้ระหว่างที่เปิด
ส่วนที่คุณทำเองคือ markup ข้อความหัวข้อ ปุ่ม และ style ทั้งหมด Root เป็นแบบ controlled: มันบอกเหตุผลที่ขอปิด และจะปิดเมื่อคุณตั้ง open เป็น false เท่านั้น
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Modal } from '@purpurabog/ui/headless';
วิธีใช้
import { useState } from 'react';
import { Modal } from '@purpurabog/ui/headless';
const button = 'h-10 px-4 text-sm focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus';
export default function Example() {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)} className={`${button} bg-interactive text-on-primary hover:bg-blue-hover`}>
เปิดสัญญา
</button>
<Modal.Root open={open} onClose={() => setOpen(false)} className="m-auto w-[min(100%-2rem,42rem)] bg-layer-1 text-ink backdrop:bg-overlay open:flex open:flex-col">
<header className="relative px-4 pt-4 pb-2 pe-12">
<p className="text-xs text-ink-muted">CT-2026-0142</p>
<Modal.Title className="text-xl">รายละเอียดสัญญา</Modal.Title>
<Modal.Close aria-label="ปิด" className="absolute end-0 top-0 grid size-12 place-items-center hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
<svg viewBox="0 0 32 32" aria-hidden="true" className="size-5 fill-current">
<path d="M24 9.4 22.6 8 16 14.6 9.4 8 8 9.4l6.6 6.6L8 22.6 9.4 24l6.6-6.6 6.6 6.6 1.4-1.4-6.6-6.6z" />
</svg>
</Modal.Close>
</header>
<Modal.Body className="px-4 pt-2 pb-12 text-sm">
สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด มีผลตั้งแต่ 2 ตุลาคม 2569
</Modal.Body>
<Modal.Footer className="flex">
<button type="button" data-kind="secondary" onClick={() => setOpen(false)} className={`${button} h-16 flex-1 bg-secondary text-start text-on-primary hover:bg-secondary-hover`}>
ยกเลิก
</button>
<button type="button" data-kind="primary" onClick={() => setOpen(false)} className={`${button} h-16 flex-1 bg-interactive text-start text-on-primary hover:bg-blue-hover`}>
บันทึก
</button>
</Modal.Footer>
</Modal.Root>
</>
);
}
onClose(reason) ได้ค่า 'close-button', 'escape', 'backdrop' หรือ 'api' (<form method="dialog"> ข้างในปิด dialog) preflight ของ CSS reset ส่วนใหญ่ลบ margin: auto ของ dialog ออก จึงต้องใส่กลับ (m-auto) เพื่อให้อยู่กลางจอ
โครงสร้าง
<Modal.Root open={open} onClose={onClose}>
<Modal.Title />
<Modal.Close />
<Modal.Body />
<Modal.Footer />
</Modal.Root>
ยืนยันก่อนลบ
alert ทำให้ dialog มี role="alertdialog" และใช้ Body เป็นคำอธิบาย โฟกัสจะเริ่มที่คำสั่งที่ปลอดภัยที่สุดใน Footer คือปุ่มแรกที่ data-kind ไม่ใช่ primary และไม่ใช่ kind แบบ danger จึงต้องใส่ data-kind ให้ทุกปุ่มด้านล่าง
import { useState } from 'react';
import { Modal } from '@purpurabog/ui/headless';
const button = 'h-10 px-4 text-sm focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus';
export default function Example() {
const [open, setOpen] = useState(false);
const [result, setResult] = useState('');
const answer = (value: string) => {
setResult(value);
setOpen(false);
};
return (
<div className="grid justify-items-start gap-2">
<button type="button" onClick={() => setOpen(true)} className={`${button} bg-button-danger text-on-primary hover:bg-error-hover`}>
ลบสัญญา
</button>
<p className="text-xs text-ink-muted">ผลลัพธ์: {result || '(ยังไม่มี)'}</p>
<Modal.Root open={open} alert closeOnOutsideClick={false} onClose={() => setOpen(false)} className="m-auto w-[min(100%-2rem,30rem)] bg-layer-1 text-ink backdrop:bg-overlay open:flex open:flex-col">
<Modal.Title className="px-4 pt-4 pb-2 text-xl">ลบสัญญานี้?</Modal.Title>
<Modal.Body className="px-4 pt-2 pb-12 text-sm">สัญญา CT-2026-0142 และลายเซ็นทั้งหมดจะถูกลบถาวร</Modal.Body>
<Modal.Footer className="flex">
<button type="button" data-kind="secondary" onClick={() => answer('เก็บไว้')} className={`${button} h-16 flex-1 bg-secondary text-start text-on-primary hover:bg-secondary-hover`}>
ยกเลิก
</button>
<button type="button" data-kind="danger" onClick={() => answer('ลบแล้ว')} className={`${button} h-16 flex-1 bg-button-danger text-start text-on-primary hover:bg-error-hover`}>
ลบ
</button>
</Modal.Footer>
</Modal.Root>
</div>
);
}
ตัวอย่างนี้ไม่มี Close คีย์บอร์ดจึงออกได้ด้วย Esc
ฟอร์ม และการคลิกข้างนอก
ตอนเปิด โฟกัสจะไปที่ช่องที่มี autoFocus ถ้าไม่มีจะไปที่ช่องแรกใน Body ถ้าไม่มีอีกจะไปที่ตัวแรกใน Footer แล้วจึงเป็น Close ค่าเริ่มต้น การคลิกที่ backdrop จะขอปิด ('backdrop') แต่ Carbon ให้ทำแบบนี้เฉพาะ modal แบบ passive จึงควรใส่ closeOnOutsideClick={false} เมื่อมีปุ่มด้านล่าง <form method="dialog"> จะปิด dialog เมื่อส่งฟอร์ม หลังจาก browser ตรวจช่องที่ต้องกรอกแล้ว และ onClose จะได้ 'api'
import { useState } from 'react';
import { Modal } from '@purpurabog/ui/headless';
const button = 'h-16 flex-1 px-4 text-start text-sm text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus';
const field = 'h-10 w-full border-b border-border-strong-1 bg-field-1 px-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus';
export default function Example() {
const [open, setOpen] = useState(false);
const [sent, setSent] = useState<Record<string, FormDataEntryValue>>();
return (
<div className="grid justify-items-start gap-2">
<button type="button" onClick={() => setOpen(true)} 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>
{sent ? <p className="text-xs text-ink-muted">เพิ่มแล้ว: {JSON.stringify(sent)}</p> : null}
<Modal.Root open={open} closeOnOutsideClick={false} onClose={() => setOpen(false)} className="m-auto w-[min(100%-2rem,42rem)] bg-layer-1 text-ink backdrop:bg-overlay">
<form method="dialog" onSubmit={(e) => setSent(Object.fromEntries(new FormData(e.currentTarget)))} className="flex flex-col">
<Modal.Title className="px-4 pt-4 pb-2 text-xl">เพิ่มผู้ลงนาม</Modal.Title>
<Modal.Body className="grid gap-4 px-4 pt-2 pb-12">
<label className="grid gap-2 text-xs text-ink-muted">
ชื่อ-นามสกุล
<input name="name" required className={field} />
</label>
<label className="grid gap-2 text-xs text-ink-muted">
อีเมล
<input name="email" type="email" required className={field} />
</label>
</Modal.Body>
<Modal.Footer className="flex">
<button type="button" data-kind="secondary" onClick={() => setOpen(false)} className={`${button} bg-secondary hover:bg-secondary-hover`}>
ยกเลิก
</button>
<button type="submit" data-kind="primary" className={`${button} bg-interactive hover:bg-blue-hover`}>
เพิ่มผู้ลงนาม
</button>
</Modal.Footer>
</form>
</Modal.Root>
</div>
);
}
เนื้อหายาว
เมื่อ Body สูงกว่าพื้นที่ที่มี มันจะได้ tabIndex={0} และ data-scrollable และใช้ Title เป็นชื่อ ผู้ใช้คีย์บอร์ดจึงเลื่อนอ่านได้ ให้กำหนดความสูงสูงสุดของ dialog แล้วให้ Body เลื่อน
import { useState } from 'react';
import { Modal } from '@purpurabog/ui/headless';
const clauses = Array.from({ length: 12 }, (_, n) => `ข้อ ${n + 1} ผู้ประมวลผลจะใช้ข้อมูลส่วนบุคคลตามคำสั่งเป็นหนังสือของผู้ควบคุมข้อมูลเท่านั้น และเก็บรักษาเป็นความลับ`);
export default function Example() {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(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">
อ่านข้อตกลง
</button>
<Modal.Root open={open} onClose={() => setOpen(false)} className="m-auto max-h-[min(100%-6rem,30rem)] w-[min(100%-2rem,42rem)] bg-layer-1 text-ink backdrop:bg-overlay open:flex open:flex-col">
<header className="relative px-4 pt-4 pb-2 pe-12">
<Modal.Title className="text-xl">ข้อตกลงการประมวลผลข้อมูล</Modal.Title>
<Modal.Close aria-label="ปิด" className="absolute end-0 top-0 grid size-12 place-items-center hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
<svg viewBox="0 0 32 32" aria-hidden="true" className="size-5 fill-current">
<path d="M24 9.4 22.6 8 16 14.6 9.4 8 8 9.4l6.6 6.6L8 22.6 9.4 24l6.6-6.6 6.6 6.6 1.4-1.4-6.6-6.6z" />
</svg>
</Modal.Close>
</header>
<Modal.Body className="min-h-0 flex-1 overflow-y-auto px-4 pb-8 text-sm data-scrollable:focus-visible:outline-2 data-scrollable:focus-visible:-outline-offset-2 data-scrollable:focus-visible:outline-focus">
<div className="grid gap-4">
{clauses.map((text) => (
<p key={text}>{text}</p>
))}
</div>
</Modal.Body>
</Modal.Root>
</>
);
}
Body ดูขนาดของลูกตัวแรก จึงควรห่อเนื้อหาด้วย element ตัวเดียวอย่างในตัวอย่าง
ขอยืนยันก่อนปิด
onClose แค่แจ้ง ถ้าจะให้ modal เปิดค้างไว้ ก็ไม่ต้องตั้ง open เป็น false ในตัวอย่างนี้ ครั้งแรกที่ขอปิดตอนยังมีข้อความที่ไม่ได้บันทึกจะแสดงคำเตือน ครั้งที่สองจึงปิด
import { useState } from 'react';
import { Modal } from '@purpurabog/ui/headless';
export default function Example() {
const [open, setOpen] = useState(false);
const [note, setNote] = useState('');
const [warned, setWarned] = useState(false);
const [reason, setReason] = useState<Modal.ModalCloseReason>();
const show = () => {
setNote('');
setWarned(false);
setOpen(true);
};
const close = (why: Modal.ModalCloseReason) => {
setReason(why);
if (note && !warned) setWarned(true);
else setOpen(false);
};
return (
<div className="grid justify-items-start gap-2">
<button type="button" onClick={show} 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>
<p className="text-xs text-ink-muted">ขอปิดด้วย: {reason ?? '(ยังไม่มี)'}</p>
<Modal.Root open={open} onClose={close} className="m-auto w-[min(100%-2rem,42rem)] bg-layer-1 text-ink backdrop:bg-overlay open:flex open:flex-col">
<header className="relative px-4 pt-4 pb-2 pe-12">
<Modal.Title className="text-xl">แก้ไขบันทึก</Modal.Title>
<Modal.Close aria-label="ปิด" className="absolute end-0 top-0 grid size-12 place-items-center hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
<svg viewBox="0 0 32 32" aria-hidden="true" className="size-5 fill-current">
<path d="M24 9.4 22.6 8 16 14.6 9.4 8 8 9.4l6.6 6.6L8 22.6 9.4 24l6.6-6.6 6.6 6.6 1.4-1.4-6.6-6.6z" />
</svg>
</Modal.Close>
</header>
<Modal.Body className="grid gap-4 px-4 pt-2 pb-12">
{warned ? (
<p role="status" className="border-s-4 border-warning bg-notification-background-warning px-4 py-3 text-sm text-ink">
ยังไม่ได้บันทึก ปิดอีกครั้งเพื่อทิ้งการแก้ไข
</p>
) : null}
<label className="grid gap-2 text-xs text-ink-muted">
บันทึกถึงผู้อนุมัติ
<textarea value={note} onChange={(e) => setNote(e.target.value)} rows={4} className="w-full border-b border-border-strong-1 bg-field-1 p-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" />
</label>
</Modal.Body>
</Modal.Root>
</div>
);
}
ใส่ style
ui-modal-motion (ใน theme ของ React + Tailwind) ให้การเคลื่อนไหวแบบ Carbon: กล่องเลื่อนลงมา 24px พร้อมกล่องและพื้นหลังค่อย ๆ ปรากฏ และค่อย ๆ หายไปหลังปิด dialog ที่กำลังปิดยังอยู่บนจอระหว่างที่ค่อย ๆ หายไป จึงต้องใส่ layout (flex-col) ไว้ที่ dialog เลย และใส่แค่ display ไว้ใต้ data-open
import { useState } from 'react';
import { Modal } from '@purpurabog/ui/headless';
export default function Example() {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)} 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>
<Modal.Root
open={open}
onClose={() => setOpen(false)}
className="m-auto w-[min(100%-2rem,30rem)] bg-layer-1 text-ink backdrop:bg-overlay flex-col data-open:flex ui-modal-motion"
>
<header className="relative px-4 pt-4 pb-2 pe-12">
<Modal.Title className="text-xl">อัตราอากรแสตมป์</Modal.Title>
<Modal.Close aria-label="ปิด" className="absolute end-0 top-0 grid size-12 place-items-center hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">
<svg viewBox="0 0 32 32" aria-hidden="true" className="size-5 fill-current">
<path d="M24 9.4 22.6 8 16 14.6 9.4 8 8 9.4l6.6 6.6L8 22.6 9.4 24l6.6-6.6 6.6 6.6 1.4-1.4-6.6-6.6z" />
</svg>
</Modal.Close>
</header>
<Modal.Body className="px-4 pt-2 pb-12 text-sm">สัญญาเช่า: 1 บาททุก 1,000 บาทของค่าเช่าตลอดอายุสัญญา เศษของ 1,000 บาทคิดเป็น 1,000 บาท</Modal.Body>
</Modal.Root>
</>
);
}
/* className="modal" ที่ Root และ "body" ที่ Body */
.modal[data-open] { display: flex; flex-direction: column; margin: auto; }
.modal::backdrop { background: rgb(22 22 22 / 0.5); }
.body[data-scrollable] { overflow-y: auto; }
.body[data-scrollable]:focus-visible { outline: 2px solid #0f62fe; outline-offset: -2px; }
Parts
Modal.Root
<dialog> ที่เปิดด้วย showModal() ระหว่างที่ open เป็น true รับ prop ของ dialog ได้ทุกตัว ยกเว้น open, onClose และ role ระหว่างที่เปิด หน้าเว็บ (<html>) จะเลื่อนไม่ได้
| Prop |
Type |
ค่าเริ่มต้น |
|
open |
boolean |
|
แสดง modal (ต้องใส่) |
onClose |
(reason: Modal.ModalCloseReason) => void |
|
Esc, Close, การคลิกข้างนอก หรือ <form method="dialog"> ขอปิด: 'escape', 'close-button', 'backdrop', 'api' |
alert |
boolean |
false |
role="alertdialog" ใช้ Body เป็นคำอธิบาย และโฟกัสเริ่มที่คำสั่งที่ปลอดภัยที่สุดด้านล่าง |
closeOnOutsideClick |
boolean |
true |
กดและปล่อยบน backdrop จะขอปิด ('backdrop') |
aria-labelledby ของมันคือ id ของ Title และเมื่อเป็น alert aria-describedby คือ id ของ Body ถ้าคุณไม่ได้ใส่เอง
| Attribute |
เมื่อ |
data-open |
open เป็น true |
Modal.Title
<h2> ที่มี id (ของคุณ หรือ <id>-title) ใช้เป็นชื่อของ dialog
Modal.Body
<section> สำหรับเนื้อหา ระหว่างที่เนื้อหายาวเกิน มันจะได้ tabIndex={0} และ aria-labelledby ที่ชี้ไปที่ Title จึงเป็น region ที่มีชื่อและคีย์บอร์ดเลื่อนได้ โฟกัสแรกจะหาช่องในนี้ก่อน
| Attribute |
เมื่อ |
data-scrollable |
เนื้อหาสูงกว่าส่วนเนื้อหา |
<footer> สำหรับปุ่มคำสั่ง โฟกัสแรกจะหาที่นี่ต่อจาก Body เมื่อเป็น alert มันอ่าน data-kind ของปุ่ม (primary, secondary, danger …) เพื่อเริ่มที่ปุ่มที่ปลอดภัยที่สุด
Modal.Close
<button type="button"> ที่ขอปิด ('close-button') กด Tab ไปถึงได้แม้ใน Safari (tabIndex={0}) ต้องใส่ aria-label
คีย์บอร์ดและ accessibility
| ปุ่ม |
ทำอะไร |
| Tab / Shift+Tab |
เลื่อนไปมาเฉพาะช่องและปุ่มใน dialog (dialog แบบ modal ของ browser) |
| Esc |
เรียก onClose('escape') dialog จะเปิดอยู่จนกว่าคุณตั้ง open เป็น false |
dialog แบบ modal ของ browser ทำให้ส่วนอื่นของหน้าเป็น inert และคืนโฟกัสให้ element ที่โฟกัสอยู่ก่อนเปิดเมื่อ dialog ปิด part ต่าง ๆ เพิ่มชื่อจาก Title, alertdialog พร้อมคำอธิบาย และ Body ที่โฟกัสได้เมื่อเลื่อน
ส่วนที่คุณเพิ่มเอง
Title หรือ aria-label ที่ Root เมื่อไม่มีหัวข้อที่มองเห็น
aria-label ที่ Close
data-kind ที่ปุ่มด้านล่างของ modal แบบ alert