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

Tearsheet

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

สัญญาใหม่
ยกเลิก บันทึกร่าง สร้าง
HTMLReactReact + Tailwind<ui-button id="tearsheet-open">สัญญาใหม่</ui-button> <ui-tearsheet id="tearsheet-create" width="wide" heading="สัญญาใหม่" label="กฎหมาย" description="กรอกคู่สัญญาและระยะเวลา เพิ่มข้อสัญญาได้หลังสร้างสัญญาแล้ว" selector-initial-focus="#tearsheet-create-name"> <div style="display: grid; gap: 16px; padding: 16px 32px"> <ui-text-input id="tearsheet-create-name" label="ชื่อสัญญา"></ui-text-input> <ui-text-input label="คู่สัญญา"></ui-text-input> <ui-date-picker label="วันเริ่มต้น"></ui-date-picker> </div> <ui-button slot="actions" kind="ghost">ยกเลิก</ui-button> <ui-button slot="actions" kind="secondary">บันทึกร่าง</ui-button> <ui-button slot="actions">สร้าง</ui-button> </ui-tearsheet> <script type="module"> const sheet = document.getElementById('tearsheet-create'); document.getElementById('tearsheet-open').addEventListener('click', () => (sheet.open = true)); for (const button of sheet.querySelectorAll('[slot="actions"]')) button.addEventListener('click', () => (sheet.open = false)); </script> const [open, setOpen] = useState(false); <UiButton onClick={() => setOpen(true)}>สัญญาใหม่</UiButton> <UiTearsheet open={open} width="wide" heading="สัญญาใหม่" label="กฎหมาย" description="กรอกคู่สัญญาและระยะเวลา เพิ่มข้อสัญญาได้หลังสร้างสัญญาแล้ว" selectorInitialFocus="#contract-name" onUiClose={() => setOpen(false)}> <UiTextInput id="contract-name" label="ชื่อสัญญา" /> <UiTextInput label="คู่สัญญา" /> <UiDatePicker label="วันเริ่มต้น" /> <UiButton slot="actions" kind="ghost" onClick={() => setOpen(false)}>ยกเลิก</UiButton> <UiButton slot="actions" kind="secondary" onClick={saveDraft}>บันทึกร่าง</UiButton> <UiButton slot="actions" onClick={create}>สร้าง</UiButton> </UiTearsheet> const [open, setOpen] = useState(false); <Button onClick={() => setOpen(true)}>สัญญาใหม่</Button> <Tearsheet open={open} onRequestClose={() => setOpen(false)} width="wide" heading="สัญญาใหม่" label="กฎหมาย" description="กรอกคู่สัญญาและระยะเวลา เพิ่มข้อสัญญาได้หลังสร้างสัญญาแล้ว" selectorInitialFocus="#contract-name" actions={[ { label: 'ยกเลิก', kind: 'ghost', onClick: () => setOpen(false) }, { label: 'บันทึกร่าง', kind: 'secondary', onClick: saveDraft }, { label: 'สร้าง', onClick: create }, ]} > <TextInput id="contract-name" label="ชื่อสัญญา" /> <TextInput label="คู่สัญญา" /> <DatePicker label="วันเริ่มต้น" /> </Tearsheet>

ติดตั้ง

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

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

วิธีใช้

HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/tearsheet';</script> <ui-button onclick="document.getElementById('new-contract').open = true">สัญญาใหม่</ui-button> <ui-tearsheet id="new-contract" width="wide" heading="สัญญาใหม่"> … <ui-button slot="actions">สร้าง</ui-button> </ui-tearsheet> import { UiTearsheet } from '@purpurabog/ui/react'; <UiTearsheet open={open} width="wide" heading="สัญญาใหม่" onUiClose={() => setOpen(false)}> … <UiButton slot="actions" onClick={create}>สร้าง</UiButton> </UiTearsheet> import { Tearsheet } from '@/components/ui/react-tw/tearsheet'; <Tearsheet open={open} onRequestClose={() => setOpen(false)} width="wide" heading="สัญญาใหม่" actions={[{ label: 'สร้าง', onClick: create }]}> … </Tearsheet>

ตั้ง open เพื่อแสดง และตั้งกลับเป็น false เพื่อซ่อน มันคือ <dialog> แบบ modal ของ browser: หน้าเว็บข้างหลังเป็น inert และเลื่อนไม่ได้ และ Tab วนอยู่ใน sheet ตอนเปิด โฟกัสจะไปที่ selector-initial-focus (selector ของ element ใน sheet) ถ้าไม่ตั้งจะไปที่ × ถ้าไม่มี × จะไปที่ช่องแรก ตอนปิดโฟกัสจะกลับไปที่ตัวเดิม และส่ง ui-close เมื่อ sheet เลื่อนลงไปแล้ว

เนื้อหาไม่มี padding ของตัวเอง ให้ใส่เองรอบเนื้อหาอย่างในตัวอย่าง React + Tailwind เป็นแบบ controlled: onRequestClose(reason) บอกว่ามีการกด × กด Esc หรือคลิกข้างนอก แล้วคุณตั้ง open เป็น false

แบบแคบ

width="narrow" (ค่าเริ่มต้น) กว้างเท่า modal ขนาดเล็กของ Carbon คือ 60%, 42% หรือ 36% ของจอ ตั้งแต่จอขนาดกลาง ใหญ่ และใหญ่พิเศษขึ้นไป เริ่มที่ 130px จากด้านบน vertical-position="normal" เลื่อนขึ้นไปที่ 90px เหมือนแบบกว้าง หัวข้อเล็กลงหนึ่งขนาด และเนื้อหาอยู่บน layer แทนพื้นหลัง React + Tailwind มี TearsheetNarrow ให้ด้วย

เปลี่ยนชื่อหน่วยงาน
ยกเลิก เปลี่ยนชื่อ
HTMLReactReact + Tailwind<ui-button id="tearsheet-narrow-open" kind="tertiary">เปลี่ยนชื่อหน่วยงาน</ui-button> <ui-tearsheet id="tearsheet-narrow" heading="เปลี่ยนชื่อหน่วยงาน" description="ชื่อใหม่จะแสดงในทุกสัญญาที่หน่วยงานนี้เป็นเจ้าของ"> <div style="padding: 16px"> <ui-text-input label="ชื่อหน่วยงาน" value="กฎหมาย"></ui-text-input> </div> <ui-button slot="actions" kind="secondary">ยกเลิก</ui-button> <ui-button slot="actions">เปลี่ยนชื่อ</ui-button> </ui-tearsheet> <script type="module"> const sheet = document.getElementById('tearsheet-narrow'); document.getElementById('tearsheet-narrow-open').addEventListener('click', () => (sheet.open = true)); for (const button of sheet.querySelectorAll('[slot="actions"]')) button.addEventListener('click', () => (sheet.open = false)); </script> <UiTearsheet open={open} heading="เปลี่ยนชื่อหน่วยงาน" description="ชื่อใหม่จะแสดงในทุกสัญญาที่หน่วยงานนี้เป็นเจ้าของ" onUiClose={() => setOpen(false)}> <UiTextInput label="ชื่อหน่วยงาน" value="กฎหมาย" /> <UiButton slot="actions" kind="secondary" onClick={() => setOpen(false)}>ยกเลิก</UiButton> <UiButton slot="actions" onClick={rename}>เปลี่ยนชื่อ</UiButton> </UiTearsheet> import { TearsheetNarrow } from '@/components/ui/react-tw/tearsheet'; <TearsheetNarrow open={open} onRequestClose={() => setOpen(false)} heading="เปลี่ยนชื่อหน่วยงาน" description="ชื่อใหม่จะแสดงในทุกสัญญาที่หน่วยงานนี้เป็นเจ้าของ" actions={[ { label: 'ยกเลิก', kind: 'secondary', onClick: () => setOpen(false) }, { label: 'เปลี่ยนชื่อ', onClick: rename }, ]} > <TextInput label="ชื่อหน่วยงาน" defaultValue="กฎหมาย" /> </TearsheetNarrow>

ปุ่มในส่วนหัวและแท็บ

tearsheet แบบกว้างใส่ปุ่มเล็ก ๆ ในส่วนหัวได้ (slot="header-actions") และใส่แท็บใต้ข้อความส่วนหัวได้ (slot="header-navigation") ให้สลับเนื้อหาเองตาม ui-tab-change ของแท็บ React + Tailwind ใส่ใน headerActions และ navigation

เปิดสัญญา ส่งออก PDF คู่สัญญา เงื่อนไข ประวัติ

บริษัท สยามโลจิสติกส์ จำกัด และบริษัทของเรา

เสร็จสิ้น
HTMLReactReact + Tailwind<ui-button id="tearsheet-tabs-open" kind="tertiary">เปิดสัญญา</ui-button> <ui-tearsheet id="tearsheet-tabs" width="wide" heading="CT-2026-0142" description="เช่าสำนักงานชั้น 3" has-close-icon> <ui-button slot="header-actions" kind="secondary" size="sm">ส่งออก PDF</ui-button> <ui-tabs slot="header-navigation" id="tearsheet-tabs-nav" label="ส่วนของสัญญา"> <ui-tab panel="parties">คู่สัญญา</ui-tab> <ui-tab panel="terms">เงื่อนไข</ui-tab> <ui-tab panel="history">ประวัติ</ui-tab> </ui-tabs> <div style="padding: 16px 32px"> <p data-section="parties">บริษัท สยามโลจิสติกส์ จำกัด และบริษัทของเรา</p> <p data-section="terms" hidden>24 เดือน ตั้งแต่ 1 พฤศจิกายน 2569 เดือนละ 45,000 บาท</p> <p data-section="history" hidden>ทั้งสองฝ่ายลงนามเมื่อ 9 ตุลาคม 2569</p> </div> <ui-button slot="actions">เสร็จสิ้น</ui-button> </ui-tearsheet> <script type="module"> const sheet = document.getElementById('tearsheet-tabs'); document.getElementById('tearsheet-tabs-open').addEventListener('click', () => (sheet.open = true)); sheet.querySelector('[slot="actions"]').addEventListener('click', () => (sheet.open = false)); document.getElementById('tearsheet-tabs-nav').addEventListener('ui-tab-change', (event) => { for (const section of sheet.querySelectorAll('[data-section]')) section.hidden = section.dataset.section !== event.detail.name; }); </script> <UiTearsheet open={open} width="wide" heading="CT-2026-0142" description="เช่าสำนักงานชั้น 3" hasCloseIcon onUiClose={() => setOpen(false)}> <UiButton slot="header-actions" kind="secondary" size="sm">ส่งออก PDF</UiButton> <UiTabs slot="header-navigation" label="ส่วนของสัญญา" onUiTabChange={(e) => setSection(e.detail.name)}> <UiTab panel="parties">คู่สัญญา</UiTab> <UiTab panel="terms">เงื่อนไข</UiTab> <UiTab panel="history">ประวัติ</UiTab> </UiTabs> <Section name={section} /> <UiButton slot="actions" onClick={() => setOpen(false)}>เสร็จสิ้น</UiButton> </UiTearsheet> <Tearsheet open={open} onRequestClose={() => setOpen(false)} width="wide" heading="CT-2026-0142" description="เช่าสำนักงานชั้น 3" hasCloseIcon headerActions={<Button kind="secondary" size="sm">ส่งออก PDF</Button>} navigation={ <Tabs value={section} onValueChange={setSection}> <TabList label="ส่วนของสัญญา"> <Tab value="parties">คู่สัญญา</Tab> <Tab value="terms">เงื่อนไข</Tab> <Tab value="history">ประวัติ</Tab> </TabList> </Tabs> } actions={[{ label: 'เสร็จสิ้น', onClick: () => setOpen(false) }]} > <Section name={section} /> </Tearsheet>

ทั้งสอง slot แสดงเฉพาะใน tearsheet แบบกว้าง

บานข้าง

slot="influencer" เพิ่มบานข้างเนื้อหาของ tearsheet แบบกว้าง เช่น สรุป ขั้นตอนของงาน หรือตัวอย่าง ค่าเริ่มต้นอยู่ทางขวา influencer-placement="left" ย้ายไปทางซ้าย influencer-width เป็น narrow (256px ค่าเริ่มต้น) หรือ wide (320px) บานข้างเลื่อนได้แยกจากเนื้อหา

ตรวจคำขอ
PR-2026-0318 ฝ่ายไอที 128,500 บาท
ส่งกลับ อนุมัติ
HTMLReactReact + Tailwind<ui-button id="tearsheet-pane-open" kind="tertiary">ตรวจคำขอ</ui-button> <ui-tearsheet id="tearsheet-pane" width="wide" heading="ตรวจใบขอซื้อ" influencer-placement="left"> <div slot="influencer" style="display: grid; gap: 8px; padding: 16px"> <strong>PR-2026-0318</strong> <span>ฝ่ายไอที</span> <span>128,500 บาท</span> </div> <div style="display: grid; gap: 16px; padding: 16px 32px"> <ui-text-area label="ความเห็นถึงผู้ขอ"></ui-text-area> </div> <ui-button slot="actions" kind="secondary">ส่งกลับ</ui-button> <ui-button slot="actions">อนุมัติ</ui-button> </ui-tearsheet> <script type="module"> const sheet = document.getElementById('tearsheet-pane'); document.getElementById('tearsheet-pane-open').addEventListener('click', () => (sheet.open = true)); for (const button of sheet.querySelectorAll('[slot="actions"]')) button.addEventListener('click', () => (sheet.open = false)); </script> <UiTearsheet open={open} width="wide" heading="ตรวจใบขอซื้อ" influencerPlacement="left" onUiClose={() => setOpen(false)}> <div slot="influencer"> <strong>PR-2026-0318</strong> … </div> <UiTextArea label="ความเห็นถึงผู้ขอ" /> <UiButton slot="actions" kind="secondary" onClick={sendBack}>ส่งกลับ</UiButton> <UiButton slot="actions" onClick={approve}>อนุมัติ</UiButton> </UiTearsheet> <Tearsheet open={open} onRequestClose={() => setOpen(false)} width="wide" heading="ตรวจใบขอซื้อ" influencer={<div><strong>PR-2026-0318</strong> …</div>} influencerPosition="left" actions={[ { label: 'ส่งกลับ', kind: 'secondary', onClick: sendBack }, { label: 'อนุมัติ', onClick: approve }, ]} > <TextArea label="ความเห็นถึงผู้ขอ" /> </Tearsheet>

React + Tailwind เรียกตำแหน่งนี้ว่า influencerPosition

ปุ่มด้านล่างและปุ่มปิด

<ui-button slot="actions"> ใส่ได้ถึงสี่ปุ่มที่ด้านล่าง จัดวางแบบ ActionSet ของ Carbon (สูง 64px ส่วนแบบกว้างสูง 80px) ปุ่มที่เกินจะถูกซ่อน ถ้ามีปุ่มด้านล่างจะไม่มี × เว้นแต่ตั้ง has-close-icon ถ้าไม่มีปุ่มเลยจะแสดง × เสมอ sheet ที่แสดงข้อมูลอย่างเดียวจึงยังปิดได้

บันทึกการลงนาม
  • อนัญญา พ. ลงนามเมื่อ 8 ตุลาคม 2569 เวลา 10:42 น.
  • บุญมี ส. ลงนามเมื่อ 9 ตุลาคม 2569 เวลา 14:05 น.
HTMLReactReact + Tailwind<ui-button id="tearsheet-info-open" kind="tertiary">บันทึกการลงนาม</ui-button> <ui-tearsheet id="tearsheet-info" heading="บันทึกการลงนาม" description="ใครลงนามสัญญา CT-2026-0142 และเมื่อไร"> <ul style="margin: 0; padding: 16px 32px"> <li>อนัญญา พ. ลงนามเมื่อ 8 ตุลาคม 2569 เวลา 10:42 น.</li> <li>บุญมี ส. ลงนามเมื่อ 9 ตุลาคม 2569 เวลา 14:05 น.</li> </ul> </ui-tearsheet> <script type="module"> const sheet = document.getElementById('tearsheet-info'); document.getElementById('tearsheet-info-open').addEventListener('click', () => (sheet.open = true)); </script> <UiTearsheet open={open} heading="บันทึกการลงนาม" description="ใครลงนามสัญญา CT-2026-0142 และเมื่อไร" onUiClose={() => setOpen(false)}> <ul>…</ul> </UiTearsheet> <Tearsheet open={open} onRequestClose={() => setOpen(false)} heading="บันทึกการลงนาม" description="ใครลงนามสัญญา CT-2026-0142 และเมื่อไร"> <ul>…</ul> </Tearsheet>

× มีชื่อว่า "ปิด" ตามภาษาของหน้า เปลี่ยนได้ด้วย close-icon-description

Label และคำอธิบายที่มี markup

label และ description รับข้อความธรรมดา ถ้าต้องใช้ markup ให้ใช้ slot="label" และ slot="description" แทน เช่น ใส่ลิงก์ในคำอธิบาย

คำขอข้อมูลส่วนบุคคล PDPA · ครบกำหนดในอีก 3 วัน ต้องตอบภายใน 30 วันนับจากวันที่ได้รับคำขอ อ่านนโยบาย
ส่งคำตอบ
HTMLReactReact + Tailwind<ui-button id="tearsheet-rich-open" kind="tertiary">คำขอข้อมูลส่วนบุคคล</ui-button> <ui-tearsheet id="tearsheet-rich" heading="คำขอใช้สิทธิของเจ้าของข้อมูล"> <span slot="label">PDPA · <strong>ครบกำหนดในอีก 3 วัน</strong></span> <span slot="description">ต้องตอบภายใน 30 วันนับจากวันที่ได้รับคำขอ <ui-link href="#tearsheet-rich-open">อ่านนโยบาย</ui-link></span> <div style="padding: 16px"> <ui-text-area label="คำตอบถึงเจ้าของข้อมูล"></ui-text-area> </div> <ui-button slot="actions">ส่งคำตอบ</ui-button> </ui-tearsheet> <script type="module"> const sheet = document.getElementById('tearsheet-rich'); document.getElementById('tearsheet-rich-open').addEventListener('click', () => (sheet.open = true)); sheet.querySelector('[slot="actions"]').addEventListener('click', () => (sheet.open = false)); </script> <UiTearsheet open={open} heading="คำขอใช้สิทธิของเจ้าของข้อมูล" onUiClose={() => setOpen(false)}> <span slot="label">PDPA · <strong>ครบกำหนดในอีก 3 วัน</strong></span> <span slot="description">ต้องตอบภายใน 30 วันนับจากวันที่ได้รับคำขอ <UiLink href="/policy">อ่านนโยบาย</UiLink></span> … </UiTearsheet> <Tearsheet open={open} onRequestClose={() => setOpen(false)} heading="คำขอใช้สิทธิของเจ้าของข้อมูล" label={<>PDPA · <strong>ครบกำหนดในอีก 3 วัน</strong></>} description={<>ต้องตอบภายใน 30 วันนับจากวันที่ได้รับคำขอ <Link href="/policy">อ่านนโยบาย</Link></>} actions={[{ label: 'ส่งคำตอบ', onClick: send }]} > … </Tearsheet>

Tearsheet ซ้อนกัน

tearsheet เปิดอีกตัวทับตัวเองได้ ตัวที่อยู่ข้างใต้จะย่อลง (เหลือ 95% แล้ว 90%) และหน้าเว็บจะถูกหรี่น้อยลง ซ้อนได้ถึงสามชั้น แต่ละตัวปิดของตัวเอง ตัวที่เปิดล่าสุดปิดก่อน

เปิดสัญญา
เพิ่มคู่สัญญา
เสร็จสิ้น
เพิ่มบริษัทใหม่
เพิ่ม
บันทึก
HTMLReactReact + Tailwind<ui-button id="tearsheet-stack-open" kind="tertiary">เปิดสัญญา</ui-button> <ui-tearsheet id="tearsheet-stack-1" width="wide" heading="CT-2026-0142" description="ขั้นที่ 1 ของงานที่ซ้อนกัน"> <div style="padding: 16px 32px"><ui-button data-next="tearsheet-stack-2" kind="tertiary">เพิ่มคู่สัญญา</ui-button></div> <ui-button slot="actions">เสร็จสิ้น</ui-button> </ui-tearsheet> <ui-tearsheet id="tearsheet-stack-2" width="wide" heading="เพิ่มคู่สัญญา" description="ขั้นที่ 2: ซ้อนบนสัญญา"> <div style="padding: 16px 32px"><ui-button data-next="tearsheet-stack-3" kind="tertiary">เพิ่มบริษัทใหม่</ui-button></div> <ui-button slot="actions">เพิ่ม</ui-button> </ui-tearsheet> <ui-tearsheet id="tearsheet-stack-3" width="wide" heading="บริษัทใหม่" description="ขั้นที่ 3: ชั้นบนสุด"> <div style="padding: 16px 32px"><ui-text-input label="ชื่อบริษัท"></ui-text-input></div> <ui-button slot="actions">บันทึก</ui-button> </ui-tearsheet> <script type="module"> document.getElementById('tearsheet-stack-open').addEventListener('click', () => (document.getElementById('tearsheet-stack-1').open = true)); for (const n of [1, 2, 3]) { const sheet = document.getElementById(`tearsheet-stack-${n}`); sheet.querySelector('[slot="actions"]').addEventListener('click', () => (sheet.open = false)); sheet.querySelector('[data-next]')?.addEventListener('click', (event) => (document.getElementById(event.currentTarget.dataset.next).open = true)); } </script> <UiTearsheet open={contractOpen} width="wide" heading="CT-2026-0142" onUiClose={() => setContractOpen(false)}> <UiButton kind="tertiary" onClick={() => setPartyOpen(true)}>เพิ่มคู่สัญญา</UiButton> … </UiTearsheet> <UiTearsheet open={partyOpen} width="wide" heading="เพิ่มคู่สัญญา" onUiClose={() => setPartyOpen(false)}>…</UiTearsheet> <Tearsheet open={contractOpen} onRequestClose={() => setContractOpen(false)} width="wide" heading="CT-2026-0142"> <Button kind="tertiary" onClick={() => setPartyOpen(true)}>เพิ่มคู่สัญญา</Button> </Tearsheet> <Tearsheet open={partyOpen} onRequestClose={() => setPartyOpen(false)} width="wide" heading="เพิ่มคู่สัญญา">…</Tearsheet>

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

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

เขียนบันทึกข้อความ
บันทึก

ขอปิดด้วย:

HTMLReactReact + Tailwind<ui-button id="tearsheet-guard-open" kind="tertiary">เขียนบันทึกข้อความ</ui-button> <ui-tearsheet id="tearsheet-guard" heading="บันทึกข้อความใหม่" has-close-icon prevent-close-on-click-outside> <div style="display: grid; gap: 16px; padding: 16px"> <ui-notification id="tearsheet-guard-warning" kind="warning" low-contrast open="false" heading="ยังไม่ได้บันทึก" subtitle="ปิดอีกครั้งเพื่อทิ้งข้อความ"></ui-notification> <ui-text-area id="tearsheet-guard-text" label="ข้อความ"></ui-text-area> </div> <ui-button slot="actions" id="tearsheet-guard-save">บันทึก</ui-button> </ui-tearsheet> <p>ขอปิดด้วย: <output id="tearsheet-guard-out"></output></p> <script type="module"> const sheet = document.getElementById('tearsheet-guard'); const text = document.getElementById('tearsheet-guard-text'); const warning = document.getElementById('tearsheet-guard-warning'); document.getElementById('tearsheet-guard-open').addEventListener('click', () => { text.value = ''; warning.open = false; sheet.open = true; }); document.getElementById('tearsheet-guard-save').addEventListener('click', () => (sheet.open = false)); sheet.addEventListener('ui-request-close', (event) => { if (event.target !== sheet) return; // notification ข้างในก็ส่ง event นี้ document.getElementById('tearsheet-guard-out').value = event.detail.reason; if (text.value && !warning.open) { event.preventDefault(); warning.open = true; } }); </script> <UiTearsheet open={open} heading="บันทึกข้อความใหม่" hasCloseIcon preventCloseOnClickOutside onUiRequestClose={(e) => { if (dirty && !warned) { e.preventDefault(); setWarned(true); } }} onUiClose={() => setOpen(false)} > <UiNotification kind="warning" lowContrast open={warned} heading="ยังไม่ได้บันทึก" subtitle="ปิดอีกครั้งเพื่อทิ้งข้อความ" /> <UiTextArea label="ข้อความ" /> <UiButton slot="actions" onClick={save}>บันทึก</UiButton> </UiTearsheet> // onRequestClose แค่แจ้ง sheet จะเปิดค้างจนกว่าคุณตั้ง open เป็น false <Tearsheet open={open} onRequestClose={() => { if (dirty && !warned) setWarned(true); else setOpen(false); }} heading="บันทึกข้อความใหม่" hasCloseIcon preventCloseOnClickOutside actions={[{ label: 'บันทึก', onClick: save }]} > <Notification kind="warning" lowContrast open={warned} title="ยังไม่ได้บันทึก" subtitle="ปิดอีกครั้งเพื่อทิ้งข้อความ" /> <TextArea label="ข้อความ" /> </Tearsheet>

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

คีย์บอร์ด

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

Accessibility

  • มันคือ <dialog> แบบ modal ของ browser ที่ใช้ heading เป็นชื่อ หน้าเว็บข้างหลังเป็น inert ทั้งกับเมาส์ คีย์บอร์ด และ screen reader
  • ใส่ heading ให้ทุก tearsheet ถ้าไม่มี dialog จะไม่มีชื่อ
  • × มีชื่อว่า "ปิด" ตามภาษาของหน้า เปลี่ยนได้ด้วย close-icon-description

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

API

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

<ui-tearsheet> · React UiTearsheet

Attribute

Attribute Property ชนิด ค่าเริ่มต้น คำอธิบาย
open open boolean false
width width 'narrow' | 'wide' 'narrow' narrow: Carbon's small modal; wide: the whole screen but 64px each side.
vertical-position verticalPosition 'normal' | 'lower' | undefined How far from the top the sheet starts: normal 90px, lower 130px (default lower for a narrow one).
heading heading string ''
label label string '' A small label above the heading.
description description string ''
influencer-placement influencerPlacement 'left' | 'right' 'right'
influencer-width influencerWidth 'narrow' | 'wide' 'narrow'
has-close-icon hasCloseIcon boolean false Show the × even with actions (it always shows without them).
close-icon-description closeIconDescription string '' The ×'s name (default "Close", translated).
prevent-close-on-click-outside preventCloseOnClickOutside boolean false
selector-initial-focus selectorInitialFocus string '' What takes focus on open (a selector in the content), else × or the first control.

Method

Method คำอธิบาย
placeInStack(position, depth) Called by the stack: this sheet's place among the open ones (position 1 = oldest) and how many show.
requestClose(reason) Asks to close (cancelable ui-request-close), then closes.

Event

Event React คำอธิบาย
ui-close onUiClose After it closed
ui-request-close onUiRequestClose Cancelable; detail: { reason: 'escape' | 'close-button' | 'outside' }

Slot

Slot คำอธิบาย
(ค่าเริ่มต้น) The content
label Rich label (instead of label)
description Rich description (instead of description), e.g. a <ui-truncated-text>
header-actions Small buttons in the header (wide)
header-navigation Tabs under the header text (wide)
influencer A side pane beside the content (wide)
actions Up to four action buttons

CSS part

Part คำอธิบาย
dialog The full-screen dialog
container The sheet
header The header
heading The heading
close-button The ×
influencer The side pane
content The content's box
actions The row of action buttons

CSS custom property

Custom property ค่าเริ่มต้น คำอธิบาย
--ui-tearsheet-radius Corner radius of the top corners (default 0; Kanok: 12px)
--ui-actions-gap / --ui-actions-padding - Gap between the action buttons and padding round them (default none: full-bleed buttons that touch; Kanok: 8px / 16px 20px)
--ui-actions-button-flex flex of each action button (default: Carbon's widths by count; Kanok: their own width)
--ui-actions-button-align / --ui-actions-button-padding-top - Where a button's label sits (default: at the top, 16px in)
--ui-actions-stacked-direction / --ui-actions-stacked-wrap / --ui-actions-stacked-width - Where Carbon stacks the actions (narrow): flex-direction, flex-wrap and each button's width (default column-reverse, nowrap, 100%; Kanok: one row that wraps)
--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)