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 Popover

part ของ Popover ให้พฤติกรรมเดียวกับ Popover และ Toggletip แบบมี style แต่ไม่มี style เลย ได้แก่ เนื้อหาของคุณใน popup ใน top layer ข้าง trigger พลิกเมื่อที่ไม่พอ และวางตำแหน่งใหม่เมื่อเลื่อนหน้า trigger ได้ aria-expanded และ aria-controls Esc ที่ trigger หรือใน popup และการกดข้างนอกปิด popup และหลังกด Esc โฟกัสจะกลับไปที่ trigger

ส่วนที่คุณทำเองคือหน้าตาของ trigger เนื้อหา ลูกศร และ style ทั้งหมด part ต่าง ๆ บอกสถานะผ่าน attribute data-* ให้คุณใช้ใส่ style

ติดตั้ง

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

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

วิธีใช้

import { Popover } from '@purpurabog/ui/headless'; const filters = ['ของฉัน', 'รอลงนาม', 'เก็บถาวรแล้ว']; export default function Example() { return ( <Popover.Root> <Popover.Trigger 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 data-open:bg-interactive data-open:text-on-primary"> ตัวกรอง </Popover.Trigger> <Popover.Popup aria-label="กรองคำขอ" className="w-60 bg-layer-1 p-4 text-sm text-ink shadow-lg"> <p className="mb-2 font-semibold">กรองคำขอ</p> {filters.map((filter) => ( <label key={filter} className="flex items-center gap-2 py-1"> <input type="checkbox" name="filter" value={filter} defaultChecked={filter === 'ของฉัน'} className="size-4 accent-interactive" /> {filter} </label> ))} </Popover.Popup> </Popover.Root> ); }

เนื้อหาของ popup ยัง render อยู่ตอนปิด checkbox จึงจำค่าไว้ ตั้งชื่อ popup ด้วย aria-label เพราะมันเป็น role="group"

โครงสร้าง

<Popover.Root> <Popover.Trigger /> <Popover.Popup> <Popover.Close /> </Popover.Popup> </Popover.Root> // หรือใช้ trigger ของคุณเอง <Popover.Root> <Popover.Anchor> <YourTrigger /> <Popover.Popup /> </Popover.Anchor> </Popover.Root>

ฟอร์มข้างใน

Close คือปุ่มที่ปิด popup แล้วคืนโฟกัสไปที่ trigger ถ้าเป็นโค้ดของคุณเองข้างใน popup ให้เรียก dismiss('close') จาก Popover.usePopoverContext() ซึ่งทำงานเหมือนกัน ใช้หลัง submit

import { useState, type FormEvent } from 'react'; import { Popover } from '@purpurabog/ui/headless'; function RenameForm({ name, onSave }: { name: string; onSave: (name: string) => void }) { const { dismiss } = Popover.usePopoverContext(); const save = (e: FormEvent<HTMLFormElement>) => { e.preventDefault(); onSave(String(new FormData(e.currentTarget).get('name'))); dismiss('close'); }; return ( <form onSubmit={save} className="grid gap-4"> <label className="grid gap-2 text-xs text-ink-muted"> ชื่อสัญญา <input name="name" defaultValue={name} required className="h-10 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" /> </label> <div className="flex gap-2"> <button type="submit" className="h-8 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> <Popover.Close className="h-8 bg-secondary px-4 text-sm text-on-primary hover:bg-secondary-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus">ยกเลิก</Popover.Close> </div> </form> ); } export default function Example() { const [name, setName] = useState('สัญญาเช่า สำนักงานสีลม'); return ( <div className="grid justify-items-start gap-2"> <Popover.Root placement="bottom-start"> <Popover.Trigger className="h-8 px-3 text-sm text-link hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus">เปลี่ยนชื่อ</Popover.Trigger> <Popover.Popup aria-label="เปลี่ยนชื่อสัญญา" className="w-72 bg-layer-1 p-4 shadow-lg"> <RenameForm name={name} onSave={setName} /> </Popover.Popup> </Popover.Root> <p className="text-xs text-ink-muted">ชื่อ: {name}</p> </div> ); }

ตำแหน่งและลูกศร

placement รับ bottom (ค่าเริ่มต้น), top, left, right แต่ละแบบเติม -start / -end ได้ ถ้าที่ไม่พอ popup จะพลิก และ data-side บอกว่าไปอยู่ฝั่งไหน gap คือระยะห่างจาก trigger (ค่าเริ่มต้น 8px แบบมี style ใช้ 10 เมื่อมีลูกศร และ 0 เมื่อไม่มี) เมื่อมี caret popup จะได้ --_caret-start คือจุดเริ่มของลูกศรที่กว้าง --_caret-w (12px ถ้าไม่ตั้ง) เพื่อให้ชี้ไปกลาง trigger

import { Popover } from '@purpurabog/ui/headless'; const caret = [ 'pointer-events-none absolute h-1.5 w-3 bg-inherit', 'group-data-[side=top]:top-full group-data-[side=top]:left-(--_caret-start) group-data-[side=top]:[clip-path:polygon(0_0,100%_0,50%_100%)]', 'group-data-[side=bottom]:bottom-full group-data-[side=bottom]:left-(--_caret-start) group-data-[side=bottom]:[clip-path:polygon(50%_0,100%_100%,0_100%)]', 'group-data-[side=left]:top-(--_caret-start) group-data-[side=left]:left-full group-data-[side=left]:h-3 group-data-[side=left]:w-1.5 group-data-[side=left]:[clip-path:polygon(0_0,100%_50%,0_100%)]', 'group-data-[side=right]:top-(--_caret-start) group-data-[side=right]:right-full group-data-[side=right]:h-3 group-data-[side=right]:w-1.5 group-data-[side=right]:[clip-path:polygon(100%_0,0_50%,100%_100%)]', ].join(' '); const placements = [ { placement: 'top', label: 'บน', text: 'เปิดด้านบน' }, { placement: 'bottom-start', label: 'ล่าง ชิดต้น', text: 'ชิดขอบต้นของปุ่ม' }, { placement: 'right', label: 'ขวา', text: 'เปิดด้านขวา' }, ] as const; export default function Example() { return ( <div className="flex flex-wrap items-center gap-4"> {placements.map(({ placement, label, text }) => ( <Popover.Root key={placement} placement={placement} gap={10} caret> <Popover.Trigger className="h-8 border border-interactive px-3 text-sm text-interactive hover:bg-interactive hover:text-on-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> {label} </Popover.Trigger> <Popover.Popup aria-label={text} className="group w-max overflow-visible bg-layer-1 p-4 text-sm text-ink drop-shadow-md"> {text} <span aria-hidden="true" className={caret} /> </Popover.Popup> </Popover.Root> ))} </div> ); }

Trigger ของคุณเอง

ถ้า trigger เป็น component ของคุณเอง ให้ครอบด้วย Anchor แทนการใช้ Trigger popup จะวางข้าง element ตัวแรกใน anchor และการกดตรงไหนก็ได้ใน anchor ไม่นับเป็นการกดข้างนอก ส่ง triggerProps (aria-expanded, aria-controls) ให้ trigger ของคุณ และสลับเปิดปิดด้วย setOpen จาก Popover.usePopoverContext() onOpenChange บอกว่าเปลี่ยนเพราะอะไร

import { useState } from 'react'; import { Popover } from '@purpurabog/ui/headless'; function AssigneeChip() { const { open, setOpen, triggerProps } = Popover.usePopoverContext(); return ( <button type="button" {...triggerProps} onClick={() => setOpen(!open, 'trigger')} className="flex h-8 items-center gap-2 rounded-full bg-layer-2 pe-3 ps-1 text-sm text-ink hover:bg-layer-hover-2 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus"> <span aria-hidden="true" className="grid size-6 place-items-center rounded-full bg-interactive text-xs text-on-primary">อน</span> อนัญญา พ. </button> ); } export default function Example() { const [last, setLast] = useState('ยังไม่มี'); const log = (open: boolean, reason: Popover.PopoverChangeReason) => setLast(`${open ? 'เปิด' : 'ปิด'} เพราะ ${reason}`); return ( <div className="grid justify-items-start gap-2"> <Popover.Root onOpenChange={log}> <Popover.Anchor className="inline-block"> <AssigneeChip /> <Popover.Popup aria-label="อนัญญา พ." className="w-64 bg-layer-1 p-4 text-sm text-ink shadow-lg"> <p className="font-semibold">อนัญญา พ.</p> <p className="text-ink-muted">ฝ่ายกฎหมาย สำนักงานกรุงเทพฯ</p> <p className="mt-2 text-ink-muted">มี 3 สัญญารอให้เธอลงนาม</p> </Popover.Popup> </Popover.Anchor> </Popover.Root> <p className="text-xs text-ink-muted">เปลี่ยนล่าสุด: {last}</p> </div> ); }

Popover.PopoverChangeReason คือ 'trigger', 'escape', 'outside', 'focus-out' หรือ 'close'

ปิดเอง

dismissible={false} ปิดการทำงานของ Esc และการกดข้างนอก จะเหลือแค่ trigger, Close หรือ open ของคุณที่ปิด popup ได้ ใช้เมื่อผู้ใช้ต้องตอบ เช่น คำแนะนำฟีเจอร์ใหม่

import { Popover } from '@purpurabog/ui/headless'; export default function Example() { return ( <Popover.Root dismissible={false} placement="right"> <Popover.Trigger 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"> ลงนามหลายฉบับ </Popover.Trigger> <Popover.Popup aria-label="ฟีเจอร์ใหม่" className="grid w-60 gap-2 bg-layer-1 p-4 text-sm text-ink shadow-lg"> <p className="font-semibold">ใหม่: ลงนามได้ครั้งละ 50 สัญญา</p> <p className="text-ink-muted">เลือกสัญญาในรายการ แล้วกด ลงนาม</p> <Popover.Close className="h-8 justify-self-start 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">เข้าใจแล้ว</Popover.Close> </Popover.Popup> </Popover.Root> ); }

Toggletip

toggletip คือ popover ที่อยู่หลังปุ่ม "i" และปิดทันทีที่โฟกัสออกไป closeOnFocusOut เพิ่มกฎนี้ คือโฟกัสออกจากทั้ง trigger และ popup แล้วปิด (focus-out) วาง popup ต่อจาก trigger เพื่อให้กด Tab ครั้งถัดไปไปถึงลิงก์ข้างใน และตั้งชื่อให้ทั้ง trigger และ popup

import { Popover } from '@purpurabog/ui/headless'; export default function Example() { return ( <div className="flex items-center gap-1 text-sm text-ink"> ระยะเวลาเก็บเอกสาร <Popover.Root closeOnFocusOut gap={10} caret> <Popover.Trigger aria-label="เกี่ยวกับระยะเวลาเก็บเอกสาร" className="grid size-6 place-items-center text-ink-muted hover:text-ink aria-expanded:text-ink 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="M8.5 11V6.5h-2v1h1V11H6v1h4v-1h-1.5ZM8 3.5a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Z" /> <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14ZM8 2a6 6 0 1 0 0 12A6 6 0 0 0 8 2Z" /> </svg> </Popover.Trigger> <Popover.Popup aria-label="เกี่ยวกับระยะเวลาเก็บเอกสาร" className="group w-max max-w-72 overflow-visible bg-layer-1 p-4 text-sm text-ink drop-shadow-md"> สำเนาที่ลงนามแล้วเก็บไว้ 10 ปี{' '} <a href="#toggletip" className="text-link underline focus-visible:outline-2 focus-visible:outline-focus">อ่านนโยบาย</a> <span aria-hidden="true" className="pointer-events-none absolute bottom-full left-(--_caret-start) h-1.5 w-3 bg-inherit [clip-path:polygon(50%_0,100%_100%,0_100%)] group-data-[side=top]:top-full group-data-[side=top]:bottom-auto group-data-[side=top]:[clip-path:polygon(0_0,100%_0,50%_100%)]" /> </Popover.Popup> </Popover.Root> </div> ); }

ใส่ style

import { Popover } from '@purpurabog/ui/headless'; export default function Example() { return ( <Popover.Root placement="bottom-end"> <Popover.Trigger aria-label="ตั้งค่าตาราง" className="grid size-12 place-items-center text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-open:bg-layer-1 data-open:shadow-md"> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"> <path d="M13.5 8.4a5.4 5.4 0 0 0 0-.8l1.4-1.1-1.3-2.3-1.7.6a5 5 0 0 0-.7-.4L10.9 2.6H8.3L8 4.4l-.7.4-1.7-.6-1.3 2.3 1.4 1.1a5.4 5.4 0 0 0 0 .8L4.3 9.5l1.3 2.3 1.7-.6.7.4.3 1.8h2.6l.3-1.8.7-.4 1.7.6 1.3-2.3zM9.6 10a2 2 0 1 1 0-4 2 2 0 0 1 0 4Z" /> </svg> </Popover.Trigger> <Popover.Popup aria-label="ตั้งค่าตาราง" className="w-52 bg-layer-1 p-4 text-sm text-ink shadow-md data-[side=top]:shadow-[0_-2px_6px_rgb(0_0_0/0.3)]"> <p className="mb-2 font-semibold">คอลัมน์</p> <label className="flex items-center gap-2 py-1"><input type="checkbox" defaultChecked className="size-4 accent-interactive" />เจ้าของ</label> <label className="flex items-center gap-2 py-1"><input type="checkbox" defaultChecked className="size-4 accent-interactive" />วันหมดอายุ</label> <label className="flex items-center gap-2 py-1"><input type="checkbox" className="size-4 accent-interactive" />แผนก</label> </Popover.Popup> </Popover.Root> ); } /* className="trigger" ที่ Trigger, "box" ที่ Popup */ .trigger[data-open] { background: #f4f4f4; box-shadow: 0 2px 6px rgb(0 0 0 / 0.3); } .box { width: 208px; padding: 16px; background: #f4f4f4; box-shadow: 0 2px 6px rgb(0 0 0 / 0.3); } .box[data-side='top'] { box-shadow: 0 -2px 6px rgb(0 0 0 / 0.3); }

Parts

Popover.Root

ไม่ render element ของตัวเอง เก็บสถานะเปิด วาง popup และปิดเมื่อกด Esc หรือกดข้างนอก

Prop Type ค่าเริ่มต้น
open boolean แสดงอยู่ (ควบคุมเอง)
defaultOpen boolean false แสดงตั้งแต่แรก (ไม่ควบคุมเอง)
onOpenChange (open: boolean, reason: PopoverChangeReason) => void หลังเปิดหรือปิด (เฉพาะเมื่อเปลี่ยน) พร้อมเหตุผล
dismissible boolean true Esc ที่ trigger หรือใน popup และการกดนอกทั้งสอง ปิด popup
closeOnFocusOut boolean false โฟกัสออกจาก trigger และ popup แล้วปิด (toggletip)
placement Placement 'bottom' top, bottom, left, right แต่ละแบบเติม -start / -end ได้
gap number 8 ระยะห่างระหว่าง trigger กับ popup หน่วย px
caret boolean false เขียน --_caret-start ที่ popup

Popover.usePopoverContext() คืนสถานะของ Root ให้ component ใดก็ได้ที่อยู่ข้างใน ได้แก่ open, popupId, triggerProps, setOpen(open, reason) และ dismiss(reason) (ปิด และคืนโฟกัสไปที่ trigger ถ้าโฟกัสอยู่ข้างใน ยกเว้นหลังการกดข้างนอก)

Popover.Trigger

<button type="button"> ที่สลับเปิดปิด popup (เหตุผล trigger) มี aria-expanded และ aria-controls popup วางข้างมัน รับ prop ทุกตัวของ button ยกเว้น type

Attribute เมื่อ
data-open popup เปิดอยู่

Popover.Anchor

<span> ที่ครอบ trigger ของคุณเอง ใช้แทน Trigger popup วางข้าง element ลูกตัวแรกของมัน การกดตรงไหนก็ได้ข้างในไม่นับเป็นการกดข้างนอก หลังกด Esc โฟกัสจะกลับไปที่ element ลูกตัวนั้น (หรือ element แรกที่รับโฟกัสได้ข้างใน)

Attribute เมื่อ
data-open popup เปิดอยู่

Popover.Popup

<div role="group" popover="manual"> ใน top layer มี id ที่ aria-controls ของ trigger ชี้มา role ของคุณมาก่อน เนื้อหายัง render อยู่ตอนปิด รับ prop ทุกตัวของ div ยกเว้น popover และ id

Attribute เมื่อ
data-open เปิดอยู่
data-side top, bottom, left หรือ right คือฝั่งที่ไปอยู่

เมื่อ Root มี caret popup จะได้ CSS variable --_caret-start ด้วย

Popover.Close

<button type="button"> ที่ปิด popup (เหตุผล close) แล้วคืนโฟกัสไปที่ trigger รับ prop ทุกตัวของ button ยกเว้น type และไม่มี data attribute

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

ปุ่ม ผลลัพธ์
Enter, Space ที่ trigger: เปิดหรือปิด
Tab ไล่ผ่าน trigger และเนื้อหาตามลำดับในหน้า ถ้ามี closeOnFocusOut ออกจากทั้งสองแล้วปิด
Esc เมื่อ dismissible และกดจาก trigger หรือใน popup: ปิด popup เว้นแต่มีอย่างอื่นจัดการปุ่มนี้ไปก่อน (ตรวจ defaultPrevented) และคืนโฟกัสไปที่ trigger

part ต่าง ๆ ตั้ง aria-expanded และ aria-controls ที่ trigger และ role="group" ที่ popup โฟกัสไม่ย้ายเข้า popup ตอนเปิด

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

  • ชื่อของ popup (aria-label หรือ aria-labelledby) และชื่อของ trigger ที่มีแต่ไอคอน
  • วาง popup ต่อจาก trigger ใน DOM เพื่อให้กด Tab ครั้งถัดไปไปถึงเนื้อหา
  • ถ้าเป็นงานที่ผู้ใช้ต้องทำให้เสร็จก่อนไปต่อ ให้ใช้ modal แทน