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

Modal

Modal ดึงความสนใจทั้งหมดของผู้ใช้ไว้ที่ตัวมัน มันเปิดทับหน้าเว็บ หน้าเว็บข้างหลังใช้งานไม่ได้ และโฟกัสอยู่ข้างในจนกว่าจะปิด ใช้กับงานสั้น ๆ หรือการตัดสินใจ ถ้าเป็นฟอร์มยาวหรือมีหลายขั้นตอน ให้ใช้ Tearsheet ถ้าต้องการแสดงรายละเอียดข้างหน้าเว็บ ให้ใช้ Side panel

เปิดสัญญา CT-2026-0142

สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด มีผลตั้งแต่ 2 ตุลาคม 2569

ยกเลิก บันทึก
HTMLReactReact + Tailwind<ui-button id="modal-open">เปิดสัญญา</ui-button> <ui-modal id="modal-contract" heading="รายละเอียดสัญญา"> <span slot="label">CT-2026-0142</span> <p style="margin: 0">สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด มีผลตั้งแต่ 2 ตุลาคม 2569</p> <ui-button slot="footer" kind="secondary">ยกเลิก</ui-button> <ui-button slot="footer">บันทึก</ui-button> </ui-modal> <script type="module"> const modal = document.getElementById('modal-contract'); document.getElementById('modal-open').addEventListener('click', () => modal.show()); for (const button of modal.querySelectorAll('[slot="footer"]')) button.addEventListener('click', () => modal.close()); </script> const [open, setOpen] = useState(false); <UiButton onClick={() => setOpen(true)}>เปิดสัญญา</UiButton> <UiModal open={open} heading="รายละเอียดสัญญา" onUiClose={() => setOpen(false)}> <span slot="label">CT-2026-0142</span> <p>สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด มีผลตั้งแต่ 2 ตุลาคม 2569</p> <UiButton slot="footer" kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</UiButton> <UiButton slot="footer" onClick={() => setOpen(false)}>บันทึก</UiButton> </UiModal> const [open, setOpen] = useState(false); <Button onClick={() => setOpen(true)}>เปิดสัญญา</Button> <Modal open={open} onClose={() => setOpen(false)} label="CT-2026-0142" heading="รายละเอียดสัญญา" footer={<> <Button kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</Button> <Button onClick={() => setOpen(false)}>บันทึก</Button> </>} > <p>สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด มีผลตั้งแต่ 2 ตุลาคม 2569</p> </Modal>

ติดตั้ง

npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui npx shadcn@4 add @kanok-ui/modal npx shadcn@4 add @kanok-ui/tw-modal # Web component # 1. ติดตั้ง dependency npm install lit@^3.3.0 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/modal.json # ไปไว้ที่ target ของมัน: # components/ui/primitives/modal/modal.ts # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # core-modal, core-overlay, shared-element, shared-events, shared-field, shared-focus-ring, shared-localize, shared-quiet-focus, shared-slots, 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-modal.json # ไปไว้ที่ target ของมัน: # components/ui/react-tw/modal.tsx # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # core-modal, tw-i18n, tw-theme, tw-utils

npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง

วิธีใช้

HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/modal';</script> <ui-button onclick="document.getElementById('contract').show()">เปิดสัญญา</ui-button> <ui-modal id="contract" heading="รายละเอียดสัญญา"> <p>สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด</p> </ui-modal> import { UiModal } from '@purpurabog/ui/react'; <UiModal open={open} heading="รายละเอียดสัญญา" onUiClose={() => setOpen(false)}> <p>สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด</p> </UiModal> import { Modal } from '@/components/ui/react-tw/modal'; <Modal open={open} onClose={() => setOpen(false)} heading="รายละเอียดสัญญา"> <p>สัญญาบริการหลักกับ บริษัท สยามโลจิสติกส์ จำกัด</p> </Modal>

Modal สร้างบน <dialog> ของ browser: browser วางมันไว้ใน top layer ทำให้หน้าเว็บข้างหลังเป็น inert และให้ Tab วนอยู่ข้างใน เปิดได้ด้วย attribute open, show() หรือ open = true ปิดได้ด้วย close() หรือ open = false เมื่อปิดแล้วจะส่ง ui-close ไม่ว่าจะปิดด้วยวิธีไหน ระหว่างที่เปิด หน้าเว็บจะเลื่อนไม่ได้

React + Tailwind เป็นแบบ controlled: onClose(reason) บอกว่ามีการกด × กด Esc คลิกข้างนอก หรือ <form method="dialog"> ข้างในสั่งปิด ให้ตั้ง open เป็น false เพื่อปิด หรือไม่ตั้งเพื่อให้เปิดค้างไว้

Modal ที่ไม่มีปุ่มด้านล่าง (passive) ใช้แสดงข้อมูลเท่านั้น ปิดได้ด้วย × กด Esc หรือคลิกข้างนอก

อัตราอากรแสตมป์ กรมสรรพากร

สัญญาเช่า: 1 บาททุก 1,000 บาทของค่าเช่าตลอดอายุสัญญา เศษของ 1,000 บาทคิดเป็น 1,000 บาท

HTMLReactReact + Tailwind<ui-button id="modal-passive-open" kind="tertiary">อัตราอากรแสตมป์</ui-button> <ui-modal id="modal-passive" heading="อัตราอากรแสตมป์" size="sm"> <span slot="label">กรมสรรพากร</span> <p style="margin: 0">สัญญาเช่า: 1 บาททุก 1,000 บาทของค่าเช่าตลอดอายุสัญญา เศษของ 1,000 บาทคิดเป็น 1,000 บาท</p> </ui-modal> <script type="module"> const modal = document.getElementById('modal-passive'); document.getElementById('modal-passive-open').addEventListener('click', () => modal.show()); </script> <UiButton kind="tertiary" onClick={() => setOpen(true)}>อัตราอากรแสตมป์</UiButton> <UiModal open={open} heading="อัตราอากรแสตมป์" size="sm" onUiClose={() => setOpen(false)}> <span slot="label">กรมสรรพากร</span> <p>สัญญาเช่า: 1 บาททุก 1,000 บาทของค่าเช่าตลอดอายุสัญญา เศษของ 1,000 บาทคิดเป็น 1,000 บาท</p> </UiModal> <Button kind="tertiary" onClick={() => setOpen(true)}>อัตราอากรแสตมป์</Button> <Modal open={open} onClose={() => setOpen(false)} label="กรมสรรพากร" heading="อัตราอากรแสตมป์" size="sm"> <p>สัญญาเช่า: 1 บาททุก 1,000 บาทของค่าเช่าตลอดอายุสัญญา เศษของ 1,000 บาทคิดเป็น 1,000 บาท</p> </Modal>

ขนาด

size: sm 480px, md 672px (ค่าเริ่มต้น), lg 1056px ความกว้างจะไม่เกินหน้าจอลบด้วยขอบข้างละ 16px และความสูงไม่เกิน 768px

เล็ก กลาง ใหญ่

อนัญญา พ. ขออนุมัติเมื่อ 2 ตุลาคม 2569 ทีมกฎหมายอนุมัติเมื่อ 3 ตุลาคม 2569

HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button kind="tertiary" data-size="sm">เล็ก</ui-button> <ui-button kind="tertiary" data-size="md">กลาง</ui-button> <ui-button kind="tertiary" data-size="lg">ใหญ่</ui-button> </div> <ui-modal id="modal-size" heading="ประวัติการอนุมัติ"> <p style="margin: 0">อนัญญา พ. ขออนุมัติเมื่อ 2 ตุลาคม 2569 ทีมกฎหมายอนุมัติเมื่อ 3 ตุลาคม 2569</p> </ui-modal> <script type="module"> const modal = document.getElementById('modal-size'); for (const button of document.querySelectorAll('[data-size]')) { button.addEventListener('click', () => { modal.size = button.dataset.size; modal.show(); }); } </script> <UiModal open={open} size="sm" heading="ประวัติการอนุมัติ" onUiClose={() => setOpen(false)}>…</UiModal> <UiModal open={open} heading="ประวัติการอนุมัติ" onUiClose={() => setOpen(false)}>…</UiModal> <UiModal open={open} size="lg" heading="ประวัติการอนุมัติ" onUiClose={() => setOpen(false)}>…</UiModal> <Modal open={open} onClose={() => setOpen(false)} size="sm" heading="ประวัติการอนุมัติ">…</Modal> <Modal open={open} onClose={() => setOpen(false)} heading="ประวัติการอนุมัติ">…</Modal> <Modal open={open} onClose={() => setOpen(false)} size="lg" heading="ประวัติการอนุมัติ">…</Modal>

ยืนยันก่อนลบ

alert ใช้กับการยืนยันที่ขัดจังหวะผู้ใช้ เช่น การลบ dialog จะได้ role="alertdialog" เนื้อหาจะเป็นคำอธิบายของ dialog และโฟกัสเริ่มที่ปุ่มด้านล่างที่ปลอดภัยที่สุด (ปุ่มแรกที่ kind ไม่ใช่ primary และไม่ใช่ kind แบบ danger) กด Enter จึงไม่ลบโดยไม่ตั้งใจ

ลบสัญญา สัญญา CT-2026-0142 และลายเซ็นทั้งหมดจะถูกลบถาวร ยกเลิก ลบ

ผลลัพธ์:

HTMLReactReact + Tailwind<ui-button id="modal-alert-open" kind="danger">ลบสัญญา</ui-button> <ui-modal id="modal-alert" alert heading="ลบสัญญานี้?" size="sm"> สัญญา CT-2026-0142 และลายเซ็นทั้งหมดจะถูกลบถาวร <ui-button slot="footer" kind="secondary" data-answer="kept">ยกเลิก</ui-button> <ui-button slot="footer" kind="danger" data-answer="deleted">ลบ</ui-button> </ui-modal> <p>ผลลัพธ์: <output id="modal-alert-out"></output></p> <script type="module"> const modal = document.getElementById('modal-alert'); const out = document.getElementById('modal-alert-out'); document.getElementById('modal-alert-open').addEventListener('click', () => modal.show()); for (const button of modal.querySelectorAll('[data-answer]')) { button.addEventListener('click', () => { out.value = button.dataset.answer; modal.close(); }); } </script> <UiButton kind="danger" onClick={() => setOpen(true)}>ลบสัญญา</UiButton> <UiModal open={open} alert heading="ลบสัญญานี้?" size="sm" onUiClose={() => setOpen(false)}> สัญญา CT-2026-0142 และลายเซ็นทั้งหมดจะถูกลบถาวร <UiButton slot="footer" kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</UiButton> <UiButton slot="footer" kind="danger" onClick={remove}>ลบ</UiButton> </UiModal> <Button kind="danger" onClick={() => setOpen(true)}>ลบสัญญา</Button> <Modal open={open} onClose={() => setOpen(false)} alert heading="ลบสัญญานี้?" size="sm" footer={<> <Button kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</Button> <Button kind="danger" onClick={remove}>ลบ</Button> </>} > สัญญา CT-2026-0142 และลายเซ็นทั้งหมดจะถูกลบถาวร </Modal>

คลิกข้างนอก

เหมือน Carbon คือ modal ที่มีปุ่มด้านล่างจะไม่ปิดเมื่อคลิกข้างนอก ข้อมูลที่พิมพ์ไว้จึงไม่หาย ส่วน modal แบบ passive จะปิด ยกเว้นตั้ง persistent ซึ่งจะปิดได้แค่ด้วย × กด Esc หรือปุ่มของคุณเอง

อ่านเงื่อนไข

คลิกข้างนอกแล้ว modal นี้ไม่ปิด ให้ใช้ × หรือกด Esc

HTMLReactReact + Tailwind<ui-button id="modal-persistent-open" kind="tertiary">อ่านเงื่อนไข</ui-button> <ui-modal id="modal-persistent" heading="เงื่อนไขการใช้งาน" persistent> <p style="margin: 0">คลิกข้างนอกแล้ว modal นี้ไม่ปิด ให้ใช้ × หรือกด Esc</p> </ui-modal> <script type="module"> const modal = document.getElementById('modal-persistent'); document.getElementById('modal-persistent-open').addEventListener('click', () => modal.show()); </script> <UiModal open={open} heading="เงื่อนไขการใช้งาน" persistent onUiClose={() => setOpen(false)}> <p>คลิกข้างนอกแล้ว modal นี้ไม่ปิด ให้ใช้ × หรือกด Esc</p> </UiModal> <Modal open={open} onClose={() => setOpen(false)} heading="เงื่อนไขการใช้งาน" persistent> <p>คลิกข้างนอกแล้ว modal นี้ไม่ปิด ให้ใช้ × หรือกด Esc</p> </Modal>

ฟอร์มใน modal

ตอนเปิด โฟกัสจะไปที่ช่องแรกในเนื้อหา ถ้าไม่มีจะไปที่ปุ่มแรกด้านล่าง ถ้าไม่มีอีกจะไปที่ × ใส่ autofocus ที่ช่องอื่นถ้าต้องการให้เริ่มที่ช่องนั้น ปุ่มด้านล่างอยู่นอก <form> ของคุณ ปุ่มหลักจึงเรียก requestSubmit() เพื่อให้ browser ตรวจช่องที่ต้องกรอกก่อน กด Enter ในช่องก็ส่งฟอร์มได้เช่นกัน

เพิ่มผู้ลงนาม CT-2026-0142 ยกเลิก เพิ่มผู้ลงนาม

เพิ่มแล้ว:

HTMLReactReact + Tailwind<ui-button id="modal-form-open">เพิ่มผู้ลงนาม</ui-button> <ui-modal id="modal-form" heading="เพิ่มผู้ลงนาม"> <span slot="label">CT-2026-0142</span> <form id="modal-form-fields" style="display: grid; gap: 16px"> <ui-text-input label="ชื่อ-นามสกุล" name="name" required></ui-text-input> <ui-text-input label="อีเมล" name="email" type="email" required></ui-text-input> <ui-select label="บทบาท" name="role"> <option value="signer">ผู้ลงนาม</option> <option value="witness">พยาน</option> </ui-select> </form> <ui-button slot="footer" kind="secondary" id="modal-form-cancel">ยกเลิก</ui-button> <ui-button slot="footer" id="modal-form-add">เพิ่มผู้ลงนาม</ui-button> </ui-modal> <p>เพิ่มแล้ว: <output id="modal-form-out"></output></p> <script type="module"> const modal = document.getElementById('modal-form'); const form = document.getElementById('modal-form-fields'); document.getElementById('modal-form-open').addEventListener('click', () => { form.reset(); modal.show(); }); document.getElementById('modal-form-cancel').addEventListener('click', () => modal.close()); document.getElementById('modal-form-add').addEventListener('click', () => form.requestSubmit()); form.addEventListener('submit', (event) => { event.preventDefault(); document.getElementById('modal-form-out').value = [...new FormData(form)].map(([key, value]) => `${key}=${value}`).join(', '); modal.close(); }); </script> const form = useRef<HTMLFormElement>(null); <UiModal open={open} heading="เพิ่มผู้ลงนาม" onUiClose={() => setOpen(false)}> <span slot="label">CT-2026-0142</span> <form ref={form} onSubmit={(e) => { e.preventDefault(); add(new FormData(e.currentTarget)); setOpen(false); }}> <UiTextInput label="ชื่อ-นามสกุล" name="name" required /> <UiTextInput label="อีเมล" name="email" type="email" required /> <UiSelect label="บทบาท" name="role"> <option value="signer">ผู้ลงนาม</option> <option value="witness">พยาน</option> </UiSelect> </form> <UiButton slot="footer" kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</UiButton> <UiButton slot="footer" onClick={() => form.current?.requestSubmit()}>เพิ่มผู้ลงนาม</UiButton> </UiModal> <Modal open={open} onClose={() => setOpen(false)} label="CT-2026-0142" heading="เพิ่มผู้ลงนาม" footer={<> <Button kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</Button> <Button type="submit" form="signer-form">เพิ่มผู้ลงนาม</Button> </>} > <form id="signer-form" onSubmit={(e) => { e.preventDefault(); add(new FormData(e.currentTarget)); setOpen(false); }}> <TextInput label="ชื่อ-นามสกุล" name="name" required /> <TextInput label="อีเมล" name="email" type="email" required /> <Select label="บทบาท" name="role"> <option value="signer">ผู้ลงนาม</option> <option value="witness">พยาน</option> </Select> </form> </Modal>

ใน React + Tailwind ปุ่มด้านล่างเป็น <button> ของ browser จึงใช้ form="signer-form" ผูกปุ่มส่งเข้ากับฟอร์มได้

ขอยืนยันก่อนปิด

× กด Esc และคลิกข้างนอก จะส่ง ui-request-close ก่อน ซึ่งยกเลิกได้ และมี reason เป็น close-button, escape หรือ backdrop เรียก preventDefault() เพื่อให้ modal เปิดค้างไว้ เช่น ตอนที่ยังมีการแก้ไขที่ไม่ได้บันทึก ลองพิมพ์บันทึกด้านล่าง แล้วลองปิด modal

แก้ไขบันทึก ยกเลิก บันทึก

ปิดด้วย:

HTMLReactReact + Tailwind<ui-button id="modal-guard-open" kind="tertiary">แก้ไขบันทึก</ui-button> <ui-modal id="modal-guard" heading="แก้ไขบันทึก"> <ui-notification id="modal-guard-warning" kind="warning" low-contrast open="false" heading="ยังไม่ได้บันทึก" subtitle="ปิดอีกครั้งเพื่อทิ้งการแก้ไข"></ui-notification> <ui-text-area id="modal-guard-note" label="บันทึกถึงผู้อนุมัติ"></ui-text-area> <ui-button slot="footer" kind="secondary" id="modal-guard-cancel">ยกเลิก</ui-button> <ui-button slot="footer" id="modal-guard-save">บันทึก</ui-button> </ui-modal> <p>ปิดด้วย: <output id="modal-guard-out"></output></p> <script type="module"> const modal = document.getElementById('modal-guard'); const note = document.getElementById('modal-guard-note'); const warning = document.getElementById('modal-guard-warning'); let warned = false; document.getElementById('modal-guard-open').addEventListener('click', () => { note.value = ''; warned = false; warning.open = false; modal.show(); }); modal.addEventListener('ui-request-close', (event) => { if (event.target !== modal) return; // notification ข้างในก็ส่ง event นี้ document.getElementById('modal-guard-out').value = event.detail.reason; if (note.value && !warned) { event.preventDefault(); warned = true; warning.open = true; } }); document.getElementById('modal-guard-cancel').addEventListener('click', () => modal.requestClose('close-button')); document.getElementById('modal-guard-save').addEventListener('click', () => modal.close()); </script> <UiModal open={open} heading="แก้ไขบันทึก" onUiRequestClose={(e) => { if (dirty && !warned) { e.preventDefault(); setWarned(true); } }} onUiClose={() => setOpen(false)} > <UiNotification kind="warning" lowContrast open={warned} heading="ยังไม่ได้บันทึก" subtitle="ปิดอีกครั้งเพื่อทิ้งการแก้ไข" /> <UiTextArea label="บันทึกถึงผู้อนุมัติ" /> … </UiModal> // onClose แค่แจ้ง modal จะเปิดค้างจนกว่าคุณตั้ง open เป็น false <Modal open={open} onClose={() => { if (dirty && !warned) setWarned(true); else setOpen(false); }} heading="แก้ไขบันทึก" footer={…} > <Notification kind="warning" lowContrast open={warned} title="ยังไม่ได้บันทึก" subtitle="ปิดอีกครั้งเพื่อทิ้งการแก้ไข" /> <TextArea label="บันทึกถึงผู้อนุมัติ" /> </Modal>

ui-request-close และ ui-close bubble ขึ้นมา และ notification ที่อยู่ใน modal ก็ส่ง event ชื่อเดียวกัน จึงต้องเช็กว่า event.target คือ modal ส่วน UiModal ของ React เช็กให้แล้ว onUiRequestClose และ onUiClose ได้รับเฉพาะ event ของ modal เอง close() และ open = false ปิดทันทีโดยไม่ถาม ส่วน requestClose(reason) ถามก่อนเหมือนปุ่ม × การแจ้งผลใน modal ให้ใช้ Notification แบบ inline อย่างในตัวอย่างนี้ เพราะ toast จะอยู่หลังหน้าเว็บที่เป็น inert

เนื้อหาที่เลื่อนได้

ถ้าเนื้อหาสูงกว่า modal จะเลื่อนเฉพาะส่วนเนื้อหา ส่วนหัวและปุ่มด้านล่างอยู่กับที่ ส่วนเนื้อหาจะกลายเป็น region ที่โฟกัสได้และใช้หัวข้อเป็นชื่อ ผู้ใช้คีย์บอร์ดจึงเลื่อนอ่านได้

อ่านข้อตกลง ฉบับที่ 3 ตุลาคม 2569 ไม่ยอมรับ ยอมรับ
HTMLReactReact + Tailwind<ui-button id="modal-scroll-open" kind="tertiary">อ่านข้อตกลง</ui-button> <ui-modal id="modal-scroll" heading="ข้อตกลงการประมวลผลข้อมูล"> <span slot="label">ฉบับที่ 3 ตุลาคม 2569</span> <div id="modal-scroll-terms"></div> <ui-button slot="footer" kind="secondary">ไม่ยอมรับ</ui-button> <ui-button slot="footer">ยอมรับ</ui-button> </ui-modal> <script type="module"> const modal = document.getElementById('modal-scroll'); const terms = document.getElementById('modal-scroll-terms'); for (let n = 1; n <= 12; n++) { const clause = document.createElement('p'); clause.textContent = `ข้อ ${n} ผู้ประมวลผลจะใช้ข้อมูลส่วนบุคคลตามคำสั่งเป็นหนังสือของผู้ควบคุมข้อมูลเท่านั้น และเก็บรักษาเป็นความลับ`; terms.append(clause); } document.getElementById('modal-scroll-open').addEventListener('click', () => modal.show()); for (const button of modal.querySelectorAll('[slot="footer"]')) button.addEventListener('click', () => modal.close()); </script> <UiModal open={open} heading="ข้อตกลงการประมวลผลข้อมูล" onUiClose={() => setOpen(false)}> <span slot="label">ฉบับที่ 3 ตุลาคม 2569</span> {clauses.map((text, n) => <p key={n}>{text}</p>)} <UiButton slot="footer" kind="secondary" onClick={() => setOpen(false)}>ไม่ยอมรับ</UiButton> <UiButton slot="footer" onClick={() => setOpen(false)}>ยอมรับ</UiButton> </UiModal> <Modal open={open} onClose={() => setOpen(false)} label="ฉบับที่ 3 ตุลาคม 2569" heading="ข้อตกลงการประมวลผลข้อมูล" footer={…}> {clauses.map((text, n) => <p key={n}>{text}</p>)} </Modal>

ปุ่มในส่วนหัว

slot header-actions วางปุ่มไว้ที่ปลายส่วนหัว ข้างปุ่ม × ใส่ได้หนึ่งปุ่มขนาด 48px ส่วน React + Tailwind ใส่ใน headerActions

เปิดพร้อมปุ่มในส่วนหัว

เช่าสำนักงานชั้น 3 นาน 24 เดือน ตั้งแต่ 1 พฤศจิกายน 2569 เดือนละ 45,000 บาท

HTMLReactReact + Tailwind<ui-button id="modal-actions-open" kind="tertiary">เปิดพร้อมปุ่มในส่วนหัว</ui-button> <ui-modal id="modal-actions" heading="สรุปสัญญาเช่า"> <ui-icon-button slot="header-actions" label="แก้ไขสัญญา"> <svg viewBox="0 0 16 16"><path d="m13.7 4.3-2-2a1 1 0 0 0-1.4 0L2 10.6V14h3.4l8.3-8.3a1 1 0 0 0 0-1.4ZM5 13H3v-2l6-6 2 2Zm7-7-2-2 1-1 2 2Z"/></svg> </ui-icon-button> <p style="margin: 0">เช่าสำนักงานชั้น 3 นาน 24 เดือน ตั้งแต่ 1 พฤศจิกายน 2569 เดือนละ 45,000 บาท</p> </ui-modal> <script type="module"> const modal = document.getElementById('modal-actions'); document.getElementById('modal-actions-open').addEventListener('click', () => modal.show()); </script> import { Edit as EditIcon } from '@carbon/icons-react'; <UiModal open={open} heading="สรุปสัญญาเช่า" onUiClose={() => setOpen(false)}> <UiIconButton slot="header-actions" label="แก้ไขสัญญา"><EditIcon /></UiIconButton> <p>เช่าสำนักงานชั้น 3 นาน 24 เดือน ตั้งแต่ 1 พฤศจิกายน 2569 เดือนละ 45,000 บาท</p> </UiModal> import { Edit as EditIcon } from '@carbon/icons-react'; <Modal open={open} onClose={() => setOpen(false)} heading="สรุปสัญญาเช่า" headerActions={<IconButton label="แก้ไขสัญญา"><EditIcon /></IconButton>} > <p>เช่าสำนักงานชั้น 3 นาน 24 เดือน ตั้งแต่ 1 พฤศจิกายน 2569 เดือนละ 45,000 บาท</p> </Modal>

คีย์บอร์ด

ปุ่ม ทำอะไร
Tab / Shift+Tab เลื่อนไปมาเฉพาะช่องและปุ่มที่อยู่ใน modal
Esc ขอปิด (escape) แล้วโฟกัสกลับไปที่ตัวที่เปิด modal
Enter, Space กดปุ่มที่โฟกัสอยู่

Accessibility

  • <dialog> แบบ modal ของ browser ทำให้ส่วนอื่นของหน้าเว็บเป็น inert ทั้งกับเมาส์ คีย์บอร์ด และ screen reader และคืนโฟกัสให้ตัวที่เปิด modal
  • หัวข้อเป็นชื่อของ dialog ถ้าไม่มีหัวข้อ slot label จะเป็นชื่อแทน ถ้าไม่มีทั้งสองอย่างให้ตั้ง label (React + Tailwind ใช้ aria-label)
  • alert ทำให้เป็น alertdialog ที่ใช้เนื้อหาเป็นคำอธิบาย ใช้กับการยืนยันเท่านั้น
  • ปุ่ม × มีชื่อว่า "ปิด" ตามภาษาของหน้า เปลี่ยนได้ด้วย close-label (closeLabel)

ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Modal

API

สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)

<ui-modal> · React UiModal

Attribute

Attribute Property ชนิด ค่าเริ่มต้น คำอธิบาย
open open boolean false
heading heading string ''
label label string '' Accessible name when there is no heading (Carbon aria-label); the label slot is used before it.
size size 'sm' | 'md' | 'lg' 'md' Dialog width (Carbon modal sizes): sm 480px, md 672px, lg 1056px — never wider than the viewport minus 16px a side.
persistent persistent boolean false A passive modal (no footer) ignores outside clicks too; one with footer buttons always does (Carbon).
close-label closeLabel string '' Accessible name for the close button.
alert alert boolean false Interrupting confirmation: role="alertdialog", focus starts on the safest action.

Method

Method คำอธิบาย
requestClose(reason) Ask to close — fires cancelable ui-request-close first.
focus(options) Focus inside the open dialog (the least destructive control, as on open).

Event

Event React คำอธิบาย
ui-close onUiClose After the modal has closed
ui-request-close onUiRequestClose Cancelable; detail: { reason }. preventDefault() keeps it open.

Slot

Slot คำอธิบาย
(ค่าเริ่มต้น) Body content
label Small label above the heading (the row renders only when slotted)
footer Action buttons (laid out full-bleed, Carbon style)
header-actions Controls at the top end of the header, beside the close button (one 48px button fits)

CSS part

Part คำอธิบาย
dialog The native <dialog> panel
header Header row
header-actions Wrapper of the header-actions slot
label The small label above the heading
heading The heading
close-button The × button
body Scrollable body
footer Footer holding the action buttons

CSS custom property

Custom property ค่าเริ่มต้น คำอธิบาย
--ui-modal-radius Corner radius (default 0; Kanok: 12px)
--ui-modal-shadow box-shadow of the box (default none)
--ui-modal-scrim The overlay behind the box (default rgb(22 22 22 / 0.5))
--ui-overlay-background The box's surface, shared by every floating surface (default layer 1)
--ui-overlay-border The box's border, shared by modal, side panel and tearsheet (default none)
--ui-modal-footer-gap Gap between footer buttons (default none: they touch)
--ui-modal-footer-height Footer min-height (default 64px)
--ui-modal-footer-padding Footer padding (default 0: full-bleed buttons)
--ui-modal-footer-border Border above the footer (default none)
--ui-modal-button-flex flex of each footer button (default 0 1 50%: halves)
--ui-modal-button-height Height of the footer buttons (default 64px)
--ui-modal-button-align align-items of their content (default flex-start: text at the top)
--ui-modal-button-padding-top Their top padding (default 14px)
--ui-close-size Size of the × button (default 48px; Kanok: 32px)
--ui-close-inset Margin that sets the × in from the corner (default 0; Kanok: 8px)
--ui-icon-control-radius Corners of the ×, its hover fill and its ring (default 0; Kanok: round)
--ui-focus-offset outline-offset of the ×'s focus ring (default -2px: inside; Kanok: 2px outside)
--ui-modal-body-focus-radius Corners of the scrolling body's focus ring (default 0; Kanok: 6px)