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 Content switcher

ContentSwitcher คือพฤติกรรมของ Content switcher แบบมี style ในรูป React part ที่ไม่มี style: tablist ที่เลือกได้ทีละหนึ่งรายการเสมอ มีจุดหยุดของ Tab จุดเดียวที่รายการที่เลือก ปุ่มลูกศร (วนรอบและข้ามรายการที่ปิดใช้งาน) Home และ End และการเลือกแบบอัตโนมัติหรือ manual สถานะแสดงเป็น attribute data-* ไว้แต่ง style

markup และ style ทั้งหมดเป็นของคุณ ทั้งแถบพื้น พื้นของรายการที่เลือก เส้นคั่น ไอคอนและ tooltip และเนื้อหาที่เปลี่ยนตาม

ติดตั้ง

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

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

วิธีใช้

import { ContentSwitcher } from '@purpurabog/ui/headless'; const item = 'flex h-10 min-w-0 flex-1 cursor-pointer items-center border-0 border-y border-solid border-border-inverse px-4 text-sm whitespace-nowrap text-ink-muted first:rounded-s-[4px] first:border-s last:rounded-e-[4px] last:border-e hover:bg-ghost-hover hover:text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-selected:rounded-[4px] data-selected:bg-layer-selected-inverse data-selected:text-inverse-ink data-disabled:cursor-not-allowed data-disabled:border-border-disabled data-disabled:text-ink-disabled'; export default function Example() { return ( <ContentSwitcher.Root aria-label="คำขอ" className="flex max-w-xl"> <ContentSwitcher.Item name="all" className={item}>คำขอทั้งหมด</ContentSwitcher.Item> <ContentSwitcher.Item name="mine" className={item}>มอบหมายให้ฉัน</ContentSwitcher.Item> <ContentSwitcher.Item name="archived" className={item}>เก็บถาวร</ContentSwitcher.Item> </ContentSwitcher.Root> ); }

โครงสร้าง

<ContentSwitcher.Root> <ContentSwitcher.Item /> <ContentSwitcher.Item /> </ContentSwitcher.Root>

child โดยตรงของ Root แต่ละตัวคือหนึ่งรายการตามตำแหน่ง Root อ่าน prop name และ disabled ของ children ดังนั้น component ของคุณที่ครอบ Item ต้องรับสอง prop นี้ด้วย

เปลี่ยนเนื้อหา

onChange ได้ { index, name } ของรายการที่ผู้ใช้เลือก การแสดงเนื้อหาที่ตรงกันเป็นหน้าที่ของคุณ ให้วางไว้ต่อจาก switcher ทันที

import { useState } from 'react'; import { ContentSwitcher } from '@purpurabog/ui/headless'; const item = 'flex h-10 min-w-0 flex-1 cursor-pointer items-center border-0 border-y border-solid border-border-inverse px-4 text-sm whitespace-nowrap text-ink-muted first:rounded-s-[4px] first:border-s last:rounded-e-[4px] last:border-e hover:bg-ghost-hover hover:text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-selected:rounded-[4px] data-selected:bg-layer-selected-inverse data-selected:text-inverse-ink'; const requests = [ { title: 'ต่อสัญญาเช่าเซิร์ฟเวอร์', mine: true, overdue: false }, { title: 'อนุมัติสัญญาจัดเลี้ยง', mine: false, overdue: true }, { title: 'ลงนาม NDA กับสยามออฟฟิศซัพพลาย', mine: true, overdue: true }, { title: 'ตรวจเงื่อนไขสัญญาเช่ารถ', mine: false, overdue: false }, ]; export default function Example() { const [filter, setFilter] = useState('all'); const shown = requests.filter((r) => filter === 'all' || r[filter as 'mine' | 'overdue']); return ( <div className="grid max-w-xl gap-4"> <ContentSwitcher.Root aria-label="คำขอ" onChange={({ name }) => setFilter(name)} className="flex"> <ContentSwitcher.Item name="all" className={item}>คำขอทั้งหมด</ContentSwitcher.Item> <ContentSwitcher.Item name="mine" className={item}>มอบหมายให้ฉัน</ContentSwitcher.Item> <ContentSwitcher.Item name="overdue" className={item}>เกินกำหนด</ContentSwitcher.Item> </ContentSwitcher.Root> <ul className="m-0 list-disc ps-6 text-sm text-ink"> {shown.map((r) => ( <li key={r.title}>{r.title}</li> ))} </ul> </div> ); }

ควบคุมการเลือกเอง

ใช้ selectedIndex คู่กับ onChange เมื่อต้องการควบคุมเอง หรือ defaultSelectedIndex (ค่าเริ่มต้น 0) ถ้าไม่ต้องการควบคุม

import { useState } from 'react'; import { ContentSwitcher } from '@purpurabog/ui/headless'; const item = 'flex h-10 min-w-0 flex-1 cursor-pointer items-center border-0 border-y border-solid border-border-inverse px-4 text-sm whitespace-nowrap text-ink-muted first:rounded-s-[4px] first:border-s last:rounded-e-[4px] last:border-e hover:bg-ghost-hover hover:text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-selected:rounded-[4px] data-selected:bg-layer-selected-inverse data-selected:text-inverse-ink'; export default function Example() { const [index, setIndex] = useState(1); return ( <div className="grid max-w-xl gap-2"> <ContentSwitcher.Root aria-label="ช่วงเวลา" selectedIndex={index} onChange={(change) => setIndex(change.index)} className="flex"> <ContentSwitcher.Item name="week" className={item}>สัปดาห์นี้</ContentSwitcher.Item> <ContentSwitcher.Item name="month" className={item}>เดือนนี้</ContentSwitcher.Item> <ContentSwitcher.Item name="year" className={item}>ปีนี้</ContentSwitcher.Item> </ContentSwitcher.Root> <p className="text-xs text-ink-muted">ตำแหน่งที่เลือก: {index}</p> </div> ); }

รายการที่ปิดใช้งานและการเลือกแบบ manual

รายการที่ตั้ง disabled จะได้ aria-disabled และ data-disabled ปุ่มลูกศรข้ามไป และคลิกแล้วไม่ทำอะไร เมื่อใช้ selectionMode="manual" ปุ่มลูกศรจะย้ายแค่โฟกัส แล้วกด Enter หรือ Space เพื่อเลือก

import { ContentSwitcher } from '@purpurabog/ui/headless'; const item = 'flex h-10 min-w-0 flex-1 cursor-pointer items-center border-0 border-y border-solid border-border-inverse px-4 text-sm whitespace-nowrap text-ink-muted first:rounded-s-[4px] first:border-s last:rounded-e-[4px] last:border-e hover:bg-ghost-hover hover:text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-selected:rounded-[4px] data-selected:bg-layer-selected-inverse data-selected:text-inverse-ink data-disabled:cursor-not-allowed data-disabled:border-border-disabled data-disabled:text-ink-disabled data-disabled:hover:bg-transparent'; export default function Example() { return ( <ContentSwitcher.Root aria-label="รายงาน" selectionMode="manual" defaultSelectedIndex={1} className="flex max-w-xl"> <ContentSwitcher.Item name="summary" className={item}>สรุป</ContentSwitcher.Item> <ContentSwitcher.Item name="details" className={item}>รายละเอียด</ContentSwitcher.Item> <ContentSwitcher.Item name="audit" disabled className={item}>ประวัติการตรวจสอบ</ContentSwitcher.Item> </ContentSwitcher.Root> ); }

แบบไอคอนอย่างเดียว

รายการที่แสดงแค่ไอคอนต้องมีชื่อ: aria-label ที่ Item และควรมี tooltip สำหรับผู้ที่มองเห็น ตัวอย่างนี้ใช้ title แทน tooltip

import { ContentSwitcher } from '@purpurabog/ui/headless'; const item = 'grid size-10 cursor-pointer place-items-center border-0 border-y border-solid border-border-inverse text-icon-secondary first:rounded-s-[4px] first:border-s last:rounded-e-[4px] last:border-e hover:bg-ghost-hover hover:text-icon-primary focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-selected:rounded-[4px] data-selected:bg-layer-selected-inverse data-selected:text-icon-inverse'; export default function Example() { return ( <ContentSwitcher.Root aria-label="รูปแบบการแสดง" className="flex"> <ContentSwitcher.Item name="table" aria-label="ตาราง" title="ตาราง" className={item}> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"><path d="M13 2H3a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1ZM3 7h4v2H3Zm5 0h5v2H8Zm5-4v3H3V3ZM3 10h4v3H3Zm5 3v-3h5v3Z" /></svg> </ContentSwitcher.Item> <ContentSwitcher.Item name="grid" aria-label="กริด" title="กริด" className={item}> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"><path d="M7 2H3a1 1 0 0 0-1 1v4a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1Zm0 5H3V3h4Zm6-5H9a1 1 0 0 0-1 1v4a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1Zm0 5H9V3h4ZM7 9H3a1 1 0 0 0-1 1v4a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1v-4a1 1 0 0 0-1-1Zm0 5H3v-4h4Zm6-5H9a1 1 0 0 0-1 1v4a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1v-4a1 1 0 0 0-1-1Zm0 5H9v-4h4Z" /></svg> </ContentSwitcher.Item> <ContentSwitcher.Item name="list" aria-label="รายการ" title="รายการ" className={item}> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"><path d="M2 4h12v1H2zm0 3.5h12v1H2zM2 11h12v1H2z" /></svg> </ContentSwitcher.Item> </ContentSwitcher.Root> ); }

แต่ง style

แบบ contrast ต่ำ: แถบพื้นสีเทา และรายการที่เลือกเป็นกล่องสีอ่อนตัวหนา กรอบโฟกัสของคีย์บอร์ดอยู่ที่รายการด้วย focus-visible

import { ContentSwitcher } from '@purpurabog/ui/headless'; const item = 'flex h-8 min-w-0 flex-1 cursor-pointer items-center justify-center rounded-[4px] border-0 px-4 text-sm whitespace-nowrap text-ink-muted hover:bg-content-switcher-background-hover hover:text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-selected:bg-content-switcher-selected data-selected:font-semibold data-selected:text-ink data-selected:shadow-[inset_0_0_0_1px_var(--ui-border-inverse)] data-disabled:cursor-not-allowed data-disabled:text-ink-disabled'; export default function Example() { return ( <ContentSwitcher.Root aria-label="คำขอ" className="flex max-w-xl rounded-[4px] bg-content-switcher-background outline-1 -outline-offset-1 outline-hairline-strong"> <ContentSwitcher.Item name="all" className={item}>คำขอทั้งหมด</ContentSwitcher.Item> <ContentSwitcher.Item name="mine" className={item}>มอบหมายให้ฉัน</ContentSwitcher.Item> <ContentSwitcher.Item name="archived" disabled className={item}>เก็บถาวร</ContentSwitcher.Item> </ContentSwitcher.Root> ); } [role='tablist'] { display: flex; border-radius: 4px; background: #e8e8e8; } [role='tab'] { flex: 1; padding: 0 16px; color: #525252; cursor: pointer; } [role='tab'][data-selected] { background: #fff; color: #161616; font-weight: 600; } [role='tab'][data-disabled] { color: #a8a8a8; cursor: not-allowed; }

Parts

ContentSwitcher.Root

render <div role="tablist"> และครอบ child แต่ละตัวเพื่อให้รู้ตำแหน่งของตัวเอง รับ prop ของ <div> ได้ทุกตัว ตั้งชื่อด้วย aria-label

Prop Type ค่าเริ่มต้น
selectedIndex number — ตำแหน่งของรายการที่เลือก (ควบคุมเอง)
defaultSelectedIndex number 0 รายการเริ่มต้น (ไม่ควบคุมเอง)
onChange (change: { index: number; name: string }) => void — หลังผู้ใช้เลือกรายการอื่น name คือ prop name ของรายการ หรือ ''
selectionMode 'automatic' | 'manual' 'automatic' automatic เลือกไปพร้อมการย้ายด้วยปุ่มลูกศร manual เลือกเมื่อกด Enter หรือ Space

ContentSwitcher.Item

render <div role="tab"> ที่มี aria-selected เฉพาะจุดหยุดของ Tab (รายการที่เลือก หรือรายการแรกที่ใช้งานได้) ที่มี tabIndex={0} รับ prop ของ <div> ได้ทุกตัว

Prop Type ค่าเริ่มต้น
name string — ส่งไปใน onChange (Root เป็นคนอ่าน)
disabled boolean false aria-disabled: ยังมองเห็นและอ่านออกเสียง แต่เลือกไม่ได้และปุ่มลูกศรข้ามไป
Attribute เมื่อ
data-selected เป็นรายการที่เลือกอยู่
data-disabled ตั้ง disabled

ContentSwitcher.useContentSwitcherContext() ให้สถานะกับ part ของคุณเอง (selected, focusable, count, selectionMode, select(index)) และ ContentSwitcher.useItemIndex() ให้ตำแหน่งของรายการที่เรียกใช้อยู่

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

ปุ่ม ทำอะไร
← / → รายการก่อนหน้า / ถัดไปที่ใช้งานได้ วนรอบ (กลับด้านเมื่อเขียนจากขวาไปซ้าย) ในโหมดอัตโนมัติจะเลือกด้วย
Home / End รายการแรก / สุดท้ายที่ใช้งานได้
Enter, Space เลือกรายการที่โฟกัสอยู่
Tab ออกจาก switcher

part ตั้ง role="tablist", role="tab", aria-selected, aria-disabled และ tabIndex แบบ roving ให้แล้ว

สิ่งที่คุณต้องใส่เอง:

  • ชื่อของกลุ่ม: aria-label หรือ aria-labelledby ที่ Root
  • aria-label ที่รายการที่แสดงแค่ไอคอน และ tooltip
  • เนื้อหาที่เปลี่ยนตาม วางไว้ต่อจาก switcher ทันที รายการไม่ได้ควบคุม panel ใด จึงไม่มี aria-controls