Headless Tearsheet
part ของ Tearsheet ให้พฤติกรรมเดียวกับ Tearsheet แบบมี style แต่ไม่มี style เลย ได้แก่ <dialog> ของ browser ที่เปิดด้วย showModal() (top layer หน้าเว็บข้างหลังเป็น inert) ซึ่ง sheet เลื่อนขึ้นมาจากด้านล่างตอนเปิดและเลื่อนลงก่อนถูกถอดออก Tab ที่วนอยู่ใน sheet Esc ปุ่มปิด และการคลิกนอก sheet ที่ขอปิด โฟกัสแรกและโฟกัสกลับ หน้าเว็บที่เลื่อนไม่ได้ และการซ้อน tearsheet แบบ Carbon
ส่วนที่คุณทำเองคือ markup ส่วนหัว ปุ่มคำสั่ง และ style ทั้งหมด Root เป็นแบบ controlled: มันบอกเหตุผลที่ขอปิด และจะปิดเมื่อคุณตั้ง open เป็น false เท่านั้น
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Tearsheet } from '@purpurabog/ui/headless';
วิธีใช้
import { useState } from 'react';
import { Tearsheet } from '@purpurabog/ui/headless';
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';
const action = 'h-20 flex-1 px-4 pt-4 text-start align-top text-sm text-on-primary focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus';
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>
<Tearsheet.Root open={open} onRequestClose={() => setOpen(false)} selectorInitialFocus="#tearsheet-use-name">
<Tearsheet.Dialog className="m-0 size-full max-h-none max-w-none bg-transparent backdrop:bg-overlay open:flex open:flex-col open:items-center open:justify-end">
<Tearsheet.Sheet className="flex h-[calc(100%-5.5rem)] w-[calc(100%-2rem)] max-w-5xl flex-col border border-border-subtle-2 bg-layer-1 text-ink">
<header className="relative border-b border-border-subtle-2 px-8 py-6 pe-20">
<p className="text-xs text-ink-muted">กฎหมาย</p>
<Tearsheet.Title className="text-2xl">สัญญาใหม่</Tearsheet.Title>
<p className="pt-4 text-sm">กรอกคู่สัญญาและระยะเวลา</p>
<Tearsheet.Close aria-label="ปิด" className="absolute end-4 top-2 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>
</Tearsheet.Close>
</header>
<div className="grid flex-1 content-start gap-4 overflow-y-auto bg-canvas px-8 py-4">
<label className="grid gap-2 text-xs text-ink-muted">
ชื่อสัญญา
<input id="tearsheet-use-name" className={field} />
</label>
<label className="grid gap-2 text-xs text-ink-muted">
คู่สัญญา
<input className={field} />
</label>
</div>
<div className="flex border-t border-border-subtle-2">
<button type="button" onClick={() => setOpen(false)} className={`${action} bg-secondary hover:bg-secondary-hover`}>
ยกเลิก
</button>
<button type="button" onClick={() => setOpen(false)} className={`${action} bg-interactive hover:bg-blue-hover`}>
สร้าง
</button>
</div>
</Tearsheet.Sheet>
</Tearsheet.Dialog>
</Tearsheet.Root>
</>
);
}
onRequestClose(reason) ได้ค่า 'close-button', 'escape' หรือ 'outside' โฟกัสแรกไปที่ selectorInitialFocus ถ้าไม่ตั้งจะไปที่ Close ถ้าไม่มีจะไปที่ช่องแรกของ sheet ตอนปิดโฟกัสจะกลับไปที่ตัวเดิม Dialog เต็มจอ ส่วน Sheet คือส่วนที่มองเห็น: ส่วนที่เลื่อนขึ้นลง ที่ที่ Tab วนอยู่ และขอบเขตที่ใช้ตัดสินว่าคลิกข้างนอกหรือไม่
โครงสร้าง
<Tearsheet.Root open={open} onRequestClose={onRequestClose}>
<Tearsheet.Dialog>
<Tearsheet.Sheet>
<Tearsheet.Title />
<Tearsheet.Close />
</Tearsheet.Sheet>
</Tearsheet.Dialog>
</Tearsheet.Root>
Tearsheet ซ้อนกัน
tearsheet ที่เปิดทับอีกตัวจะซ้อนบนตัวนั้น ระหว่างที่เปิดมากกว่าหนึ่งตัว data-stack ที่ Dialog และ Sheet บอกตำแหน่งของแต่ละตัว: 1-of-2, 2-of-2, 1-of-3 … ซ้อนได้ถึงสามชั้น ให้ย่อตัวที่อยู่ข้างใต้ และหรี่หน้าเว็บน้อยลง แบบที่ Carbon ทำ
import { useState, type ReactNode } from 'react';
import { Tearsheet } from '@purpurabog/ui/headless';
function Sheet({ open, onClose, title, children }: { open: boolean; onClose: () => void; title: string; children: ReactNode }) {
return (
<Tearsheet.Root open={open} onRequestClose={onClose}>
<Tearsheet.Dialog className="m-0 size-full max-h-none max-w-none bg-transparent backdrop:bg-overlay data-[stack=1-of-2]:backdrop:opacity-70 data-[stack=1-of-3]:backdrop:opacity-20 open:flex open:flex-col open:items-center open:justify-end">
<Tearsheet.Sheet className="flex h-[calc(100%-5.5rem)] w-[calc(100%-2rem)] max-w-4xl flex-col border border-border-subtle-2 bg-layer-1 text-ink transition-transform data-[stack=1-of-2]:scale-95 data-[stack=1-of-3]:scale-90 data-[stack=2-of-3]:scale-95">
<header className="relative border-b border-border-subtle-2 px-8 py-6 pe-20">
<Tearsheet.Title className="text-2xl">{title}</Tearsheet.Title>
<Tearsheet.Close aria-label="ปิด" className="absolute end-4 top-2 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>
</Tearsheet.Close>
</header>
<div className="flex-1 px-8 py-4 text-sm">{children}</div>
</Tearsheet.Sheet>
</Tearsheet.Dialog>
</Tearsheet.Root>
);
}
const next = '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';
export default function Example() {
const [depth, setDepth] = useState(0);
return (
<>
<button type="button" onClick={() => setDepth(1)} className={next}>
เปิดสัญญา
</button>
<Sheet open={depth >= 1} onClose={() => setDepth(0)} title="CT-2026-0142">
<button type="button" onClick={() => setDepth(2)} className={next}>เพิ่มคู่สัญญา</button>
</Sheet>
<Sheet open={depth >= 2} onClose={() => setDepth(1)} title="เพิ่มคู่สัญญา">
<button type="button" onClick={() => setDepth(3)} className={next}>เพิ่มบริษัทใหม่</button>
</Sheet>
<Sheet open={depth >= 3} onClose={() => setDepth(2)} title="บริษัทใหม่">
ชั้นบนสุด กด Esc เพื่อปิดและกลับไปที่ชั้นข้างใต้
</Sheet>
</>
);
}
ขอยืนยันก่อนปิด
onRequestClose แค่แจ้ง tearsheet จะปิดเมื่อคุณตั้ง open เป็น false closeOnOutsideClick={false} ทำให้การคลิกนอก sheet ไม่ขอปิดเลย เหมาะกับฟอร์มที่ผู้ใช้พิมพ์ข้อมูล ลองพิมพ์ข้อความ แล้วกด Esc
import { useState } from 'react';
import { Tearsheet } from '@purpurabog/ui/headless';
export default function Example() {
const [open, setOpen] = useState(false);
const [memo, setMemo] = useState('');
const [warned, setWarned] = useState(false);
const [reason, setReason] = useState<Tearsheet.TearsheetCloseReason>();
const show = () => {
setMemo('');
setWarned(false);
setOpen(true);
};
const close = (why: Tearsheet.TearsheetCloseReason) => {
setReason(why);
if (memo && !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>
<Tearsheet.Root open={open} onRequestClose={close} closeOnOutsideClick={false}>
<Tearsheet.Dialog className="m-0 size-full max-h-none max-w-none bg-transparent backdrop:bg-overlay open:flex open:flex-col open:items-center open:justify-end">
<Tearsheet.Sheet className="flex h-[calc(100%-8rem)] w-[min(100%-2rem,40rem)] flex-col border border-border-subtle-2 bg-layer-1 text-ink">
<header className="relative border-b border-border-subtle-2 p-4 pe-16">
<Tearsheet.Title className="text-xl">บันทึกข้อความใหม่</Tearsheet.Title>
<Tearsheet.Close aria-label="ปิด" className="absolute end-2 top-2 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>
</Tearsheet.Close>
</header>
<div className="grid flex-1 content-start gap-4 p-4">
{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={memo} onChange={(e) => setMemo(e.target.value)} rows={5} 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>
</div>
</Tearsheet.Sheet>
</Tearsheet.Dialog>
</Tearsheet.Root>
</div>
);
}
ใส่ style
sheet เลื่อนขึ้นด้วย Web Animations API (240ms) และเลื่อนลงตอนปิด backdrop จางไปพร้อมกัน จึงไม่ต้องทำ animation ใน CSS ใช้ data-stack กำหนด style ตอนซ้อนกัน
import { useState } from 'react';
import { Tearsheet } 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>
<Tearsheet.Root open={open} onRequestClose={() => setOpen(false)}>
<Tearsheet.Dialog className="m-0 size-full max-h-none max-w-none bg-transparent backdrop:bg-overlay data-open:flex data-open:flex-col data-open:items-center data-open:justify-end">
<Tearsheet.Sheet className="h-[calc(100%-8rem)] w-[min(100%-2rem,36rem)] border border-border-subtle-2 bg-layer-1 text-ink data-closing:pointer-events-none">
<header className="relative border-b border-border-subtle-2 p-4 pe-16">
<Tearsheet.Title className="text-xl">บันทึกการลงนาม</Tearsheet.Title>
<Tearsheet.Close aria-label="ปิด" className="absolute end-2 top-2 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>
</Tearsheet.Close>
</header>
<ul className="grid gap-2 p-4 text-sm">
<li>อนัญญา พ. ลงนามเมื่อ 8 ตุลาคม 2569 เวลา 10:42 น.</li>
<li>บุญมี ส. ลงนามเมื่อ 9 ตุลาคม 2569 เวลา 14:05 น.</li>
</ul>
</Tearsheet.Sheet>
</Tearsheet.Dialog>
</Tearsheet.Root>
</>
);
}
/* className="dialog" ที่ Dialog และ "sheet" ที่ Sheet */
.dialog { inline-size: 100%; block-size: 100%; max-inline-size: none; max-block-size: none; margin: 0; background: transparent; }
.dialog[data-open] { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; }
.dialog::backdrop { background: rgb(0 0 0 / 0.6); }
.dialog[data-stack='1-of-2']::backdrop { opacity: 0.67; }
.sheet[data-stack='1-of-2'],
.sheet[data-stack='2-of-3'] { transform: scale(0.95); }
.sheet[data-stack='1-of-3'] { transform: scale(0.9); }
.sheet[data-closing] { pointer-events: none; }
Parts
Tearsheet.Root
ไม่ render element ของตัวเอง render แค่ children ระหว่างที่ tearsheet เปิดอยู่และระหว่างที่เลื่อนลง
| Prop |
Type |
ค่าเริ่มต้น |
|
open |
boolean |
|
แสดง tearsheet (ต้องใส่) |
onRequestClose |
(reason: Tearsheet.TearsheetCloseReason) => void |
|
Esc, Close หรือการคลิกนอก sheet ขอปิด: 'escape', 'close-button', 'outside' |
closeOnOutsideClick |
boolean |
true |
กดและปล่อยนอก Sheet แล้วขอปิด |
selectorInitialFocus |
string |
|
element ที่รับโฟกัสตอนเปิด (selector ใน dialog) ถ้าไม่ตั้งจะเป็น Close แล้วจึงเป็นช่องแรกของ sheet |
Tearsheet.useTearsheetContext() คืนสถานะของ Root (open, closing, stackPlace, requestClose …) ให้ component ที่อยู่ข้างใน
Tearsheet.Dialog
<dialog> ที่เปิดด้วย showModal() ให้ใส่ style ให้เต็มจอ และใส่สีหรี่ที่ ::backdrop รับ prop ของ dialog ได้ทุกตัว ยกเว้น open และ onClose aria-labelledby ของมันคือ id ของ Title ถ้าคุณไม่ได้ใส่เอง ระหว่างที่ tearsheet เปิดอยู่ หน้าเว็บ (<html>) จะเลื่อนไม่ได้
| Attribute |
เมื่อ |
data-open |
open เป็น true |
data-closing |
กำลังเลื่อนลง |
data-stack |
1-of-2, 2-of-2, 1-of-3, 2-of-3 หรือ 3-of-3 ระหว่างที่เปิดมากกว่าหนึ่งตัว |
Tearsheet.Sheet
<div> คือ sheet ที่มองเห็น มันเลื่อนขึ้นลง Tab วนอยู่ข้างใน และการคลิกนอกมันจะขอปิด
| Attribute |
เมื่อ |
data-open |
open เป็น true |
data-closing |
กำลังเลื่อนลง |
data-stack |
เหมือนที่ Dialog |
Tearsheet.Title
<h2> ที่มี id (ของคุณ หรือ <id>-title) ใช้เป็นชื่อของ dialog
Tearsheet.Close
<button type="button"> ที่ขอปิด ('close-button') รับโฟกัสแรก ยกเว้น selectorInitialFocus กำหนดเป็นอย่างอื่น ต้องใส่ aria-label
คีย์บอร์ดและ accessibility
| ปุ่ม |
ทำอะไร |
| Tab / Shift+Tab |
เลื่อนไปมาระหว่างช่องและปุ่มใน sheet และวนอยู่ข้างใน |
| Esc |
เรียก onRequestClose('escape') tearsheet จะเปิดอยู่จนกว่าคุณตั้ง open เป็น false |
dialog แบบ modal ของ browser ทำให้ส่วนอื่นของหน้าเป็น inert part ต่าง ๆ ตั้งชื่อจาก Title กัก Tab ไว้ใน sheet และย้ายโฟกัสเข้าตอนเปิดและกลับตอนปิด
ส่วนที่คุณเพิ่มเอง
Title หรือ aria-label ที่ Dialog
aria-label ที่ Close
- ปุ่มคำสั่ง ปุ่มในส่วนหัว แท็บ และบานข้าง ซึ่งเป็น markup ธรรมดาใน
Sheet