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

Headless Toggle

part แบบ headless ชื่อ Switch ตาม Base UI ซึ่งคำว่า "Toggle" หมายถึงปุ่มกดค้าง Switch.Root คือ <button role="switch"> ที่มี aria-checked ปุ่ม Space และ Enter จึงสลับได้ การคลิกทำให้มันได้โฟกัสทุก browser รวมถึง Safari นอกจากนั้นมันเพิ่มสถานะอ่านอย่างเดียว onCheckedChange การส่งฟอร์มผ่าน hidden input และ attribute data-* ให้คุณใช้ใส่ style

ส่วนที่คุณทำเองคือรางและปุ่มเลื่อน label ข้อความ "เปิด" / "ปิด" และ style ทั้งหมด แบบมี style ดูที่ Toggle

ติดตั้ง

ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):

npm install @purpurabog/ui import { Switch } from '@purpurabog/ui/headless';

วิธีใช้

import { Switch } from '@purpurabog/ui/headless'; export default function Example() { return ( <Switch.Root aria-label="แจ้งเตือนทางอีเมล" className="relative h-6 w-12 shrink-0 rounded-full bg-toggle-off transition-colors focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus data-checked:bg-success"> <Switch.Thumb className="absolute start-[3px] top-[3px] size-[18px] rounded-full bg-on-primary transition-transform data-checked:translate-x-6 rtl:data-checked:-translate-x-6" /> </Switch.Root> ); }

โครงสร้าง

<Switch.Root> <Switch.Thumb /> </Switch.Root>

เปิดและปิด

เก็บสถานะเองด้วย checked คู่กับ onCheckedChange หรือให้ค่าเริ่มต้นด้วย defaultChecked onCheckedChange ยิงหลังผู้ใช้สลับ

import { useState } from 'react'; import { Switch } from '@purpurabog/ui/headless'; export default function Example() { const [on, setOn] = useState(false); return ( <div className="grid justify-items-start gap-3"> <Switch.Root aria-label="แจ้งเตือนทางอีเมล" checked={on} onCheckedChange={setOn} className="relative h-6 w-12 shrink-0 rounded-full bg-toggle-off transition-colors focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus data-checked:bg-success"> <Switch.Thumb className="absolute start-[3px] top-[3px] size-[18px] rounded-full bg-on-primary transition-transform data-checked:translate-x-6 rtl:data-checked:-translate-x-6" /> </Switch.Root> <p className="text-xs text-ink-muted">เปิดอยู่: {String(on)}</p> </div> ); }

Label และข้อความสถานะ

Switch ไม่วาดข้อความให้ ตั้งชื่อด้วย aria-label หรือ aria-labelledby และแสดงข้อความสถานะเอง screen reader ได้ยิน "เปิด" หรือ "ปิด" จาก role อยู่แล้ว จึงซ่อนข้อความสถานะจาก screen reader

import { useState } from 'react'; import { Switch } from '@purpurabog/ui/headless'; export default function Example() { const [on, setOn] = useState(true); return ( <div className="grid justify-items-start gap-3"> <span id="toggle-visibility" className="text-xs text-ink-muted">การมองเห็น</span> <div className="flex items-center gap-2"> <Switch.Root aria-labelledby="toggle-visibility" checked={on} onCheckedChange={setOn} className="relative h-6 w-12 shrink-0 rounded-full bg-toggle-off transition-colors focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus data-checked:bg-success"> <Switch.Thumb className="absolute start-[3px] top-[3px] size-[18px] rounded-full bg-on-primary transition-transform data-checked:translate-x-6 rtl:data-checked:-translate-x-6" /> </Switch.Root> <span aria-hidden="true" className="text-sm text-ink">{on ? 'สาธารณะ' : 'ส่วนตัว'}</span> </div> </div> ); }

<label htmlFor> ที่ชี้ไปที่ id ของสวิตช์ก็ใช้ได้ คลิกที่ label จะคลิกสวิตช์ด้วย

ใช้ในฟอร์ม

ถ้ามี name สวิตช์ที่เปิดอยู่จะส่ง value (ค่าเริ่มต้นคือ on) ผ่าน hidden input ที่อยู่ต่อจากปุ่ม ถ้าปิดหรือปิดใช้งานจะไม่ส่งอะไร การ reset ฟอร์มคืนสวิตช์แบบ uncontrolled กลับเป็น defaultChecked

import { useState } from 'react'; import { Switch } from '@purpurabog/ui/headless'; export default function Example() { 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))); }} > <Switch.Root aria-label="ต่ออายุอัตโนมัติ" name="autoRenew" value="yes" className="relative h-6 w-12 shrink-0 rounded-full bg-toggle-off transition-colors focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus data-checked:bg-success"> <Switch.Thumb className="absolute start-[3px] top-[3px] size-[18px] rounded-full bg-on-primary transition-transform data-checked:translate-x-6 rtl:data-checked:-translate-x-6" /> </Switch.Root> <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> ); }

อ่านอย่างเดียวและปิดใช้งาน

  • readOnly คงสถานะไว้และยังส่งไปกับฟอร์ม แต่การคลิก Space และ Enter ไม่สลับ สวิตช์ได้ aria-readonly
  • disabled คือ attribute ปกติของปุ่ม ใช้งานไม่ได้และไม่ถูกส่งไปกับฟอร์ม
import { Switch } from '@purpurabog/ui/headless'; const track = 'relative h-6 w-12 shrink-0 rounded-full bg-toggle-off transition-colors focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus data-checked:bg-success data-readonly:cursor-default data-readonly:border data-readonly:border-ink-disabled data-readonly:bg-transparent data-disabled:cursor-not-allowed data-disabled:bg-button-disabled'; const knob = 'absolute start-[3px] top-[3px] size-[18px] rounded-full bg-on-primary transition-transform data-checked:translate-x-6 rtl:data-checked:-translate-x-6 data-readonly:start-[2px] data-readonly:top-[2px] data-readonly:bg-ink data-disabled:bg-on-color-disabled'; export default function Example() { return ( <div className="flex gap-6"> <Switch.Root aria-label="ต่ออายุอัตโนมัติ" defaultChecked readOnly className={track}><Switch.Thumb className={knob} /></Switch.Root> <Switch.Root aria-label="ต่ออายุอัตโนมัติ" disabled className={track}><Switch.Thumb className={knob} /></Switch.Root> </div> ); }

ไม่มีขนาดให้เลือก รางและปุ่มเลื่อนเป็น CSS ของคุณ

ใส่ style

import { useId, useState } from 'react'; import { Switch } from '@purpurabog/ui/headless'; function EmailAlerts({ label, ...props }: { label: string; readOnly?: boolean; disabled?: boolean; defaultChecked?: boolean }) { const [on, setOn] = useState(!!props.defaultChecked); const id = useId(); return ( <div className="flex items-center gap-2"> <Switch.Root {...props} aria-labelledby={id} onCheckedChange={setOn} className="relative h-6 w-12 shrink-0 rounded-full bg-toggle-off transition-colors focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus data-checked:bg-success data-readonly:cursor-default data-readonly:border data-readonly:border-ink-disabled data-readonly:bg-transparent data-disabled:cursor-not-allowed data-disabled:bg-button-disabled" > <Switch.Thumb className="absolute start-[3px] top-[3px] size-[18px] rounded-full bg-on-primary transition-transform data-checked:translate-x-6 rtl:data-checked:-translate-x-6 data-readonly:start-[2px] data-readonly:top-[2px] data-readonly:bg-ink data-disabled:bg-on-color-disabled" /> </Switch.Root> <span id={id} className="text-sm text-ink">{label}</span> <span aria-hidden="true" className="text-sm text-ink-muted">{on ? 'เปิด' : 'ปิด'}</span> </div> ); } export default function Example() { return ( <div className="grid gap-3"> <EmailAlerts label="แจ้งเตือนทางอีเมล" /> <EmailAlerts label="แจ้งเตือนทางอีเมล (อ่านอย่างเดียว)" defaultChecked readOnly /> <EmailAlerts label="แจ้งเตือนทางอีเมล (ปิดใช้งาน)" disabled /> </div> ); } /* className="track" ที่ Root, "knob" ที่ Thumb */ .track { width: 48px; height: 24px; border-radius: 12px; background: #8d8d8d; } .track[data-checked] { background: #24a148; } .track[data-readonly] { background: #e0e0e0; } .knob { display: block; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: translate 70ms; } .knob[data-checked] { translate: 24px; }

Parts

Switch.Root

<button type="button" role="switch"> ตามด้วย hidden input เมื่อมี name และสวิตช์เปิดอยู่และใช้งานได้ รับ prop ของ button ได้ทุกตัว ยกเว้น type, role, value และ defaultChecked

Prop Type ค่าเริ่มต้น
checked boolean สถานะ (controlled)
defaultChecked boolean false สถานะเริ่มต้น (uncontrolled)
onCheckedChange (checked: boolean, event: MouseEvent<HTMLButtonElement>) => void หลังผู้ใช้สลับ
readOnly boolean false คงสถานะไว้ การคลิก Space และ Enter ไม่เปลี่ยนอะไร (aria-readonly)
name string สวิตช์ที่เปิดอยู่ส่ง value ไปกับฟอร์ม
value string 'on' ค่าที่ส่งตอนเปิด
Attribute เมื่อ
data-checked เปิดอยู่
data-disabled ปิดใช้งาน
data-readonly อ่านอย่างเดียว

Switch.useSwitchContext() คืน checked, disabled และ readOnly ให้ component ที่อยู่ใน Root

Switch.Thumb

<span aria-hidden="true"> ใน Root สำหรับปุ่มเลื่อน มี data-checked, data-disabled และ data-readonly ชุดเดียวกับ Root

คีย์บอร์ดและ accessibility

ปุ่ม ทำอะไร
Space, Enter สลับสวิตช์
Tab ไปตัวควบคุมถัดไป

part ตั้ง role="switch", aria-checked และ aria-readonly และให้สวิตช์ได้โฟกัสเมื่อคลิก

ส่วนที่คุณเพิ่มเอง

  • ชื่อ: aria-label, aria-labelledby หรือ <label htmlFor>
  • ข้อความสถานะ โดยซ่อนจาก screen reader
  • style ของโฟกัสที่มองเห็นได้
  • เขียน label เป็นชื่อการตั้งค่า ("แจ้งเตือนทางอีเมล") ไม่ใช่คำสั่ง ("เปิดการแจ้งเตือนทางอีเมล")