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 Search

Search คือพฤติกรรมของ Search แบบมี style ในรูป React part ที่ไม่มี style: มี landmark role="search" ที่ใช้ label เป็นชื่อ, <input type="search">, ปุ่มล้างข้อความ และปุ่มแว่นขยายของช่องค้นหาแบบย่อขยาย part จัดการ Esc, ตำแหน่งโฟกัสหลังล้าง เปิด หรือปิด และจังหวะที่ปุ่มล้างอยู่ในลำดับ Tab ช่องแบบ uncontrolled จะกลับไปที่ defaultValue เมื่อฟอร์ม reset

markup เป็นของคุณเอง ทั้งไอคอน วิธีซ่อน label ชื่อของปุ่ม และ style ทั้งหมด part จะตั้ง attribute data-* ไว้ให้แต่ง style

ติดตั้ง

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

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

วิธีใช้

import { useState } from 'react'; import { Search } from '@purpurabog/ui/headless'; export default function Example() { const [query, setQuery] = useState(''); return ( <div className="grid max-w-sm gap-2"> <Search.Root value={query}> <Search.Landmark className="grid gap-2"> <Search.Label className="text-xs text-ink-muted">ค้นหาสัญญา</Search.Label> <div className="flex border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1"> <Search.Input name="q" onChange={(e) => setQuery(e.target.value)} className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus [&::-webkit-search-cancel-button]:appearance-none" /> <Search.Clear aria-label="ล้างคำค้นหา" className="size-10 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-hidden:invisible" > × </Search.Clear> </div> </Search.Landmark> </Search.Root> <p className="text-xs text-ink-muted">ค่า: {query}</p> </div> ); }
  • value, defaultValue, disabled และ readOnly ใส่ที่ Root ไม่ใช่ที่ Input
  • การล้างจะยิง event input จริง onChange ของคุณจึงเห็นด้วย จากนั้นจึงเรียก onClear
  • ใส่ Label เสมอ แม้จะซ่อนไว้ เพราะเป็นชื่อของ landmark และ input

โครงสร้าง

<Search.Root> <Search.Landmark> <Search.Label /> <Search.Trigger /> {/* เฉพาะแบบย่อขยาย */} <Search.Input /> <Search.Clear /> </Search.Landmark> </Search.Root>

ล้างข้อความ

Clear ล้างช่องโดยโฟกัสยังอยู่ในช่อง กด Esc ในช่องที่มีข้อความก็ได้ผลเดียวกัน ถ้าช่องว่างอยู่แล้ว Esc จะไม่ทำอะไร dialog ที่ครอบช่องค้นหาจึงยังปิดด้วย Esc ได้ ระหว่างที่ไม่มีอะไรให้ล้าง (ช่องว่างหรืออ่านอย่างเดียว) Clear จะอยู่นอกลำดับ Tab และมี data-hidden ให้ซ่อนด้วย CSS ของคุณ

import { useState } from 'react'; import { Search } from '@purpurabog/ui/headless'; export default function Example() { const [cleared, setCleared] = useState(0); return ( <div className="grid max-w-sm gap-2"> <Search.Root defaultValue="สมชาย" onClear={() => setCleared((n) => n + 1)}> <Search.Landmark className="flex border-b border-border-strong-1 bg-field-1 hover:bg-field-hover-1"> <Search.Label className="sr-only">ค้นหาผู้เช่า</Search.Label> <Search.Input placeholder="ค้นหาผู้เช่า" className="h-10 min-w-0 flex-1 bg-transparent px-4 text-sm text-ink placeholder:text-ink-placeholder focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus [&::-webkit-search-cancel-button]:appearance-none" /> {/* มี data-hidden ระหว่างที่ไม่มีอะไรให้ล้าง */} <Search.Clear aria-label="ล้างคำค้นหาผู้เช่า" className="size-10 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-hidden:invisible" > × </Search.Clear> </Search.Landmark> </Search.Root> <p className="text-xs text-ink-muted">onClear ถูกเรียก {cleared} ครั้ง</p> </div> ); }

ช่องค้นหาแบบย่อขยาย

expandable (ExpandableSearch ของ Carbon) ย่อช่องเหลือแค่ Trigger จนกว่าจะมีคนคลิกหรือกด Enter หรือ Space แล้วช่องจะเปิดและโฟกัสย้ายไปที่ช่อง ถ้าโฟกัสออกจากช่องที่เปิดอยู่และว่าง ไปที่ไหนก็ได้นอก Landmark ช่องจะยุบกลับ กด Esc ในช่องที่เปิดอยู่และว่างจะยุบช่องและย้ายโฟกัสไปที่ Trigger

import { useState } from 'react'; import { Search } from '@purpurabog/ui/headless'; export default function Example() { const [open, setOpen] = useState(false); return ( <div className="grid justify-items-start gap-2"> <Search.Root expandable onExpandedChange={setOpen}> <Search.Landmark className="flex border-b border-border-strong-1 bg-field-1 data-collapsed:border-transparent data-collapsed:bg-transparent"> <Search.Label className="sr-only">ค้นหาในตาราง</Search.Label> <Search.Trigger aria-label="ค้นหาในตาราง" className="grid size-10 shrink-0 place-items-center text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" > <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"> <path d="m15 14.3-4.3-4.3a5.5 5.5 0 1 0-.7.7l4.3 4.3zM2 6.5a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0" /> </svg> </Search.Trigger> <Search.Input className="h-10 w-56 bg-transparent pe-4 text-sm text-ink focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-collapsed:w-0 data-collapsed:pe-0 [&::-webkit-search-cancel-button]:appearance-none" /> <Search.Clear aria-label="ล้างคำค้นหา" className="size-10 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-hidden:hidden" > × </Search.Clear> </Search.Landmark> </Search.Root> <p className="text-xs text-ink-muted">ขยายอยู่: {String(open)}</p> </div> ); }
  • expanded ใช้คุมสถานะเอง ส่วน defaultExpanded กำหนดสถานะตอนเริ่ม
  • ระหว่างที่ยุบอยู่ Landmark และ Input มี data-collapsed และ input อยู่นอกลำดับ Tab การซ่อนหรือหดช่องเป็นหน้าที่ของ CSS ของคุณ

แต่ง style

import { Search } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="max-w-sm"> <Search.Root> <Search.Landmark className="group flex h-10 items-center border-b border-border-strong-1 bg-field-1 data-disabled:opacity-50"> <Search.Label className="sr-only">ค้นหา</Search.Label> <Search.Input placeholder="ค้นหา" className="h-full min-w-0 flex-1 bg-transparent px-4 text-sm text-ink placeholder:text-ink-placeholder focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus group-data-collapsed:w-0 group-data-collapsed:px-0 [&::-webkit-search-cancel-button]:appearance-none" /> <Search.Clear aria-label="ล้างคำค้นหา" className="size-10 text-ink hover:bg-field-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-hidden:invisible" > × </Search.Clear> </Search.Landmark> </Search.Root> </div> ); } [role="search"][data-collapsed] input { width: 0; padding: 0; } button[data-hidden] { visibility: hidden; }

Parts

Search.Root

ไม่ render element ใด ๆ เก็บข้อความและสถานะเปิดปิดไว้ให้ part อื่น

Prop Type ค่าเริ่มต้น
inputId string สร้างให้ id ของ input ส่วน id ของ label คือ ${inputId}-label
value string | number | readonly string[] — ข้อความ (controlled ให้อัปเดตตาม onChange ของ input)
defaultValue string | number | readonly string[] — ข้อความเริ่มต้น (uncontrolled)
disabled boolean false ปิดทั้ง input และปุ่ม
readOnly boolean false คงข้อความไว้ ไม่มีอะไรให้ล้าง
onClear () => void — หลังจาก Clear หรือ Esc ล้างช่องแล้ว
expandable boolean false ย่อเหลือ Trigger จนกว่าจะเปิด
expanded boolean — ช่องแบบย่อขยายเปิดอยู่หรือไม่ (controlled)
defaultExpanded boolean false เปิดตั้งแต่แรกหรือไม่
onExpandedChange (expanded: boolean) => void — ทุกครั้งที่เปิดหรือปิด

Search.Landmark

render <div role="search"> ที่ใช้ Label เป็นชื่อ (aria-labelledby) ไม่มี prop เพิ่ม

Attribute เมื่อ
data-empty ช่องไม่มีข้อความ
data-collapsed ช่องแบบย่อขยายยุบอยู่
data-disabled Root ถูกปิดใช้งาน
data-readonly Root เป็นแบบอ่านอย่างเดียว

Search.Label

render <label> ของ input พร้อม id ที่ landmark ชี้ไป ตั้ง htmlFor หรือ id เองไม่ได้

Search.Input

render <input type="search" autoComplete="off"> รับ prop ของ <input> ได้ทุกตัว ยกเว้น type, id, value, defaultValue, disabled และ readOnly ซึ่งมาจาก Root มี attribute data-* ชุดเดียวกับ Landmark

Search.Clear

render <button type="button"> ไม่มี prop เพิ่ม และไม่รับ disabled (ตาม Root) mousedown ของปุ่มถูกยกเลิก โฟกัสจึงอยู่ในช่องต่อ

Attribute เมื่อ
data-hidden ไม่มีอะไรให้ล้าง: ช่องว่างหรืออ่านอย่างเดียว

Search.Trigger

render <button type="button"> ที่มี aria-expanded และ aria-controls ชี้ไปที่ input ไม่มี prop เพิ่ม และไม่รับ disabled (ตาม Root) อยู่ในลำดับ Tab เฉพาะตอนที่ช่องยุบอยู่

Attribute เมื่อ
data-expanded ช่องเปิดอยู่ (เปิดเสมอ ถ้าไม่ใช่ expandable)

Search.useSearchContext()

คืนสถานะของ Root ที่ครอบอยู่ เช่น empty, clearable, open, expandable, inputId และ labelId

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

ปุ่ม ผล
Esc ล้างข้อความโดยโฟกัสยังอยู่ในช่อง ถ้าเป็นช่องแบบย่อขยายที่เปิดอยู่และว่าง จะยุบช่องและย้ายโฟกัสไปที่ Trigger
Enter ส่งฟอร์มที่ช่องค้นหาอยู่ เหมือน input ปกติ
Enter หรือ Space ที่ Trigger เปิดช่องแบบย่อขยาย ไม่ส่งฟอร์ม
Tab ไปที่ Clear เฉพาะตอนที่มีข้อความให้ล้าง
  • Landmark เป็น landmark role="search" ที่ใช้ Label เป็นชื่อ ผู้ใช้ screen reader จึงกระโดดมาที่ช่องนี้ได้
  • Carbon ซ่อน label บนหน้าจอ ให้ซ่อนด้วย class แบบ visually hidden ไม่ใช่ display: none
  • สิ่งที่คุณต้องใส่เอง: ชื่อของ Clear และ Trigger (aria-label) และ aria-hidden="true" ที่ไอคอนของปุ่ม