Headless Checkbox
Checkbox.Root คือ <input type="checkbox"> จริงที่ไม่มี style เลย การส่งฟอร์ม required ปุ่ม Space label และ screen reader จึงมาจาก browser นอกจากนั้นมันเพิ่ม indeterminate เป็น prop สถานะอ่านอย่างเดียว onCheckedChange และ attribute data-* ให้คุณใช้ใส่ style
ส่วนที่คุณทำเองคือ <label> กล่องที่วาดเอง กลุ่มที่ครอบ checkbox ข้อความช่วยเหลือหรือ error และ style ทั้งหมด แบบมี style ดูที่ Checkbox
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { Checkbox } from '@purpurabog/ui/headless';
วิธีใช้
ซ่อน input ไว้ใต้กล่องที่คุณวาดข้าง ๆ แล้วครอบทั้งสองด้วย <label> เพื่อให้คลิกได้ทั้งแถว
import { Checkbox } from '@purpurabog/ui/headless';
export default function Example() {
return (
<label className="relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink">
<Checkbox.Root name="terms" required className="peer sr-only" />
<span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse">
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
ฉันยอมรับเงื่อนไข
</label>
);
}
โครงสร้าง
<label>
<Checkbox.Root />
{/* กล่องและข้อความของคุณ */}
</label>
สถานะติ๊ก
เก็บสถานะเองด้วย checked คู่กับ onCheckedChange (หรือ onChange) หรือให้ค่าเริ่มต้นด้วย defaultChecked ช่องที่ติ๊กไว้จะส่ง value (ค่าเริ่มต้นคือ on) ภายใต้ name ช่องที่ไม่ติ๊กไม่ส่งอะไร การ reset ฟอร์มจะคืนช่องแบบ uncontrolled กลับเป็น defaultChecked และ data-checked เปลี่ยนตาม
import { useState } from 'react';
import { Checkbox } from '@purpurabog/ui/headless';
// ช่องที่วาดต่อจาก input ที่ซ่อนไว้ (เป็น peer ของมัน)
function Box() {
return (
<span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse">
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
);
}
export default function Example() {
const [on, setOn] = useState(false);
const [sent, setSent] = useState<Record<string, FormDataEntryValue>>();
return (
<form
className="grid justify-items-start gap-3"
onSubmit={(e) => {
e.preventDefault();
setSent(Object.fromEntries(new FormData(e.currentTarget)));
}}
>
<label className="relative flex cursor-pointer items-center gap-2 text-sm text-ink">
<Checkbox.Root name="newsletter" value="yes" checked={on} onCheckedChange={setOn} className="peer sr-only" />
<Box />
ส่งจดหมายข่าวให้ฉัน
</label>
<p className="text-xs text-ink-muted">ติ๊กแล้ว: {String(on)}</p>
<button type="submit" 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}
</form>
);
}
กลุ่มของ checkbox
ไม่มี part สำหรับกลุ่ม ให้ใช้ <fieldset> กับ <legend> จัดวาง checkbox เอง และตรวจ "อย่างน้อยหนึ่งช่อง" ใน validation ของคุณเอง แต่ละช่องส่ง name และ value ของตัวเอง จึงอ่านด้วย FormData.getAll()
import { useState } from 'react';
import { Checkbox } from '@purpurabog/ui/headless';
const docs = [
{ value: 'id', label: 'บัตรประชาชน' },
{ value: 'house', label: 'ทะเบียนบ้าน' },
{ value: 'bank', label: 'สมุดบัญชีธนาคาร' },
];
function Box() {
return (
<span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse">
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
);
}
export default function Example() {
const [sent, setSent] = useState<FormDataEntryValue[]>();
return (
<form
className="grid justify-items-start gap-3"
onSubmit={(e) => {
e.preventDefault();
setSent(new FormData(e.currentTarget).getAll('docs'));
}}
>
<fieldset className="flex flex-wrap gap-x-6 gap-y-2">
<legend className="mb-2 text-xs text-ink-muted">เอกสารที่ได้รับ</legend>
{docs.map((d) => (
<label key={d.value} className="relative flex cursor-pointer items-center gap-2 text-sm text-ink">
<Checkbox.Root name="docs" value={d.value} className="peer sr-only" />
<Box />
{d.label}
</label>
))}
</fieldset>
<button type="submit" 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">ส่งแล้ว: docs = {JSON.stringify(sent)}</p> : null}
</form>
);
}
Indeterminate
indeterminate แสดงสถานะ mixed ของ checkbox แม่ที่ checkbox ลูกถูกติ๊กไว้บางส่วน ค่านี้ตั้งเป็น DOM property screen reader จึงได้ยิน "mixed" โค้ดของคุณต้องคอยปรับ checkbox แม่ให้ตรงกับลูก
import { useState } from 'react';
import { Checkbox } from '@purpurabog/ui/headless';
// ติ๊ก: เครื่องหมายถูก, indeterminate: ขีด (::after ของมัน)
function Box() {
return (
<span aria-hidden="true" className="relative grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent after:absolute after:top-[6px] after:left-[3px] after:hidden after:h-0.5 after:w-2 after:bg-icon-inverse peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse peer-data-indeterminate:bg-ink peer-data-indeterminate:after:block">
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
);
}
const row = 'relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink';
const all = ['email', 'sms'];
export default function Example() {
const [chosen, setChosen] = useState(['email']);
const some = chosen.length > 0 && chosen.length < all.length;
const pick = (value: string, on: boolean) => setChosen((c) => (on ? [...c, value] : c.filter((v) => v !== value)));
return (
<div className="grid gap-2">
<label className={row}>
<Checkbox.Root checked={chosen.length === all.length} indeterminate={some} onCheckedChange={(on) => setChosen(on ? all : [])} className="peer sr-only" />
<Box />
ทุกช่องทาง
</label>
<div className="grid gap-2 ps-6">
<label className={row}>
<Checkbox.Root checked={chosen.includes('email')} onCheckedChange={(on) => pick('email', on)} className="peer sr-only" />
<Box />
อีเมล
</label>
<label className={row}>
<Checkbox.Root checked={chosen.includes('sms')} onCheckedChange={(on) => pick('sms', on)} className="peer sr-only" />
<Box />
SMS
</label>
</div>
</div>
);
}
ไม่มี label ที่มองเห็น
เมื่อบริบทรอบ ๆ บอกอยู่แล้วว่าช่องนี้คืออะไร เช่น checkbox ในแถวของตาราง ให้ใส่ aria-label แทนข้อความ label
import { Checkbox } from '@purpurabog/ui/headless';
const contracts = ['เช่า สีลม', 'ซื้อขาย อโศก', 'จำนอง เชียงใหม่'];
export default function Example() {
return (
<table className="w-full max-w-sm text-sm text-ink">
<tbody>
{contracts.map((contract, i) => (
<tr key={contract} className="border-b border-border-subtle-1">
<td className="w-10 p-3">
<label className="relative flex w-fit cursor-pointer">
<Checkbox.Root aria-label={`เลือกแถวที่ ${i + 1}`} className="peer sr-only" />
<span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse">
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
</label>
</td>
<td className="p-3">{contract}</td>
</tr>
))}
</tbody>
</table>
);
}
อ่านอย่างเดียวและปิดใช้งาน
readOnly คงสถานะไว้และยังส่งไปกับฟอร์ม แต่การคลิกและ Space ไม่เปลี่ยนอะไร input ได้ aria-readonly การคลิกถูกกันไว้ก่อน onClick ของคุณจะทำงาน handler ของคุณจึงเห็น event.defaultPrevented
disabled คือ attribute ปกติของ input ใช้งานไม่ได้และไม่ถูกส่งไปกับฟอร์ม
import { Checkbox } from '@purpurabog/ui/headless';
// อ่านอย่างเดียว: กรอบสีเทาและเครื่องหมายถูกสีเข้ม, ปิดใช้งาน: ทุกอย่างเป็นสีเทา
function Box() {
return (
<span aria-hidden="true" className="grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse peer-data-readonly:border-ink-subtle peer-data-readonly:peer-data-checked:bg-transparent peer-data-readonly:peer-data-checked:text-ink peer-data-disabled:border-ink-disabled peer-data-disabled:peer-data-checked:bg-ink-disabled">
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
);
}
const row = 'relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink has-data-readonly:cursor-default has-data-disabled:cursor-not-allowed has-data-disabled:text-ink-disabled';
export default function Example() {
return (
<div className="grid gap-2">
<label className={row}>
<Checkbox.Root defaultChecked readOnly className="peer sr-only" />
<Box />
ลงนามครบทั้งสองฝ่าย
</label>
<label className={row}>
<Checkbox.Root disabled className="peer sr-only" />
<Box />
เก็บถาวรแล้ว
</label>
</div>
);
}
invalid ตั้ง aria-invalid และ data-invalid ส่วนข้อความ error เป็นของคุณ (ฟอร์มและการตรวจค่า)
ใส่ style
import { useState } from 'react';
import { Checkbox } from '@purpurabog/ui/headless';
function Box() {
return (
<span
aria-hidden="true"
className="relative grid size-4 shrink-0 place-items-center rounded-xs border border-ink text-transparent after:absolute after:top-[6px] after:left-[3px] after:hidden after:h-0.5 after:w-2 after:bg-icon-inverse peer-focus-visible:outline-2 peer-focus-visible:outline-offset-1 peer-focus-visible:outline-focus peer-data-checked:bg-ink peer-data-checked:text-icon-inverse peer-data-indeterminate:bg-ink peer-data-indeterminate:after:block peer-data-invalid:border-2 peer-data-invalid:border-error peer-data-readonly:border-ink-subtle peer-data-disabled:border-ink-disabled peer-data-disabled:peer-data-checked:bg-ink-disabled"
>
<svg viewBox="0 0 16 16" className="size-3.5 fill-current"><path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /></svg>
</span>
);
}
const row = 'relative flex w-fit cursor-pointer items-center gap-2 text-sm text-ink has-data-disabled:cursor-not-allowed has-data-disabled:text-ink-disabled';
export default function Example() {
const [accepted, setAccepted] = useState(false);
return (
<div className="grid gap-2">
<label className={row}>
<Checkbox.Root checked={accepted} onCheckedChange={setAccepted} invalid={!accepted} aria-describedby={accepted ? undefined : 'terms-style-error'} className="peer sr-only" />
<Box />
ฉันยอมรับเงื่อนไข
</label>
{accepted ? null : <p id="terms-style-error" className="ps-6 text-xs text-error-text">ยอมรับเงื่อนไขก่อนจึงจะไปต่อได้</p>}
<label className={row}>
<Checkbox.Root defaultChecked disabled className="peer sr-only" />
<Box />
ส่งอีเมลถึงฉัน (ปิดใช้งาน)
</label>
</div>
);
}
/* className="box" ที่ span ที่คุณวาดต่อจาก input */
input[data-checked] + .box { background: #161616; }
input[data-indeterminate] + .box { background: #6f6f6f; }
input[data-invalid] + .box { border-color: #da1e28; }
input[data-readonly] + .box { border-color: #c6c6c6; }
input:focus-visible + .box { outline: 2px solid #0f62fe; }
Parts
Checkbox.Root
<input type="checkbox"> รับ prop ของ input ได้ทุกตัว ยกเว้น type
| Prop |
Type |
ค่าเริ่มต้น |
|
indeterminate |
boolean |
false |
สถานะ mixed ตั้งเป็น DOM property |
readOnly |
boolean |
false |
คงสถานะไว้ การคลิกและ Space ไม่เปลี่ยนอะไร (aria-readonly) |
invalid |
boolean |
|
aria-invalid และ data-invalid |
onCheckedChange |
(checked: boolean, event: ChangeEvent<HTMLInputElement>) => void |
|
หลังผู้ใช้ติ๊กหรือเอาติ๊กออก |
| Attribute |
เมื่อ |
data-checked |
ติ๊กอยู่ |
data-indeterminate |
ตั้ง indeterminate |
data-disabled |
ปิดใช้งาน |
data-readonly |
อ่านอย่างเดียว |
data-invalid |
ตั้ง invalid |
คีย์บอร์ดและ accessibility
| ปุ่ม |
ทำอะไร |
| Space |
ติ๊กหรือเอาติ๊กออกจากช่องที่โฟกัสอยู่ |
| Tab |
ไปช่องถัดไป checkbox แต่ละช่องเป็นจุดหยุดของ Tab แยกกัน |
browser ให้ role สถานะติ๊กและ mixed โฟกัส และปุ่ม Space ส่วน part เพิ่ม aria-readonly และ aria-invalid
ส่วนที่คุณเพิ่มเอง
- label โดยครอบ input ด้วย
<label> หรือใส่ aria-label
<fieldset> กับ <legend> รอบกลุ่ม
- ข้อความช่วยเหลือและ error ผูกด้วย
aria-describedby
- style ของโฟกัสที่มองเห็นได้บนกล่องที่คุณวาด (เพราะ input ถูกซ่อนไว้)