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 Card

Card คือพฤติกรรมของ Card แบบมี style แยกเป็น React part ไม่มี style สองชิ้น Card.Root คือตัว card ถ้าใส่ href จะเป็นลิงก์เดียว (<a>) ถ้าใส่ clickable จะเป็นปุ่มเดียว (เป็น <div role="button"> เพราะ card มีเนื้อหาแบบ block ซึ่ง <button> ใส่ไม่ได้) นอกนั้นเป็นกล่องธรรมดา (<div>) การคลิก หรือกด Enter หรือ Space ที่ตัว card เอง จะเรียก onClick Card.Actions ใช้ครอบปุ่มเล็ก ๆ ของ card เอง การคลิกหรือกดปุ่มที่เริ่มข้างในจะเป็นของปุ่มนั้น ไม่ใช่ของ card

ส่วนที่เหลือเป็นของคุณทั้งหมด ทั้งรูป header หัวข้อ เนื้อหา footer และ style ทุกอย่าง part อื่นของ card แบบมี style (รูป header หัวข้อ เนื้อหา footer) เป็นแค่ layout ที่นี่จึงเป็น element ของคุณเอง

ติดตั้ง

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

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

วิธีใช้

import { Card } from '@purpurabog/ui/headless'; export default function Example() { return ( <Card.Root className="max-w-sm bg-layer-1 text-ink"> <div className="p-4"> <p className="text-xs text-ink-muted">สัญญา</p> <h3 className="text-base font-semibold">สัญญาเช่าสำนักงาน สาขาสีลม</h3> </div> <p className="p-4 pt-0 text-sm">รอผู้ให้เช่าลงนาม</p> </Card.Root> ); }

โครงสร้าง

<Card.Root> <Card.Actions /> </Card.Root>

Card ที่เป็นลิงก์

ถ้าใส่ href ทั้ง card จะเป็น <a> เดียว (มี tabIndex={0} เพื่อให้ Safari focus เมื่อคลิกและหยุดเมื่อกด Tab) target="_blank" จะเติม rel="noopener noreferrer" ให้ onClick ก็ทำงานด้วย แอปแบบ single-page จึงจัดการ route เองได้ ตัวอย่างนี้ยกเลิกการเปลี่ยนหน้าแล้วแสดงที่อยู่แทน ตั้งชื่อลิงก์ด้วย aria-label เพราะข้อความทั้งหมดจะเป็นชื่อที่ยาวมาก

import { useState } from 'react'; import { Card } from '@purpurabog/ui/headless'; export default function Example() { const [opened, setOpened] = useState(''); return ( <div className="grid gap-2"> <Card.Root href="/policies/procurement" aria-label="เปิดระเบียบการจัดซื้อ" onClick={(event) => { event.preventDefault(); setOpened('/policies/procurement'); }} className="block max-w-sm border border-border-tile-1 bg-layer-1 p-4 text-ink no-underline hover:bg-layer-hover-1 focus:outline-2 focus:outline-focus" > <p className="text-xs text-ink-muted">ระเบียบ</p> <h3 className="mb-4 text-base font-semibold">ระเบียบการจัดซื้อ</h3> <p className="text-sm">ใครอนุมัติการจัดซื้อได้ และได้ถึงวงเงินเท่าไร</p> </Card.Root> <p className="text-xs text-ink-muted">router จะเปิด: {opened || 'ยังไม่มี'}</p> </div> ); }

Card ที่คลิกได้

ถ้าใส่ clickable card จะเป็น <div role="button" tabIndex={0}> การคลิก หรือกด Enter หรือ Space ขณะที่ตัว card มี focus จะเรียก onClick ตั้งชื่อด้วย aria-label

import { useState } from 'react'; import { Card } from '@purpurabog/ui/headless'; const templates = [ { id: 'sale', title: 'สัญญาซื้อขาย', text: 'สินค้า ราคา และการส่งมอบ' }, { id: 'lease', title: 'สัญญาเช่า', text: 'ทรัพย์สิน ค่าเช่า และระยะเวลา' }, ]; export default function Example() { const [chosen, setChosen] = useState(''); return ( <div className="grid gap-2"> <div className="grid grid-cols-[repeat(auto-fill,minmax(200px,1fr))] gap-4"> {templates.map((template) => ( <Card.Root key={template.id} clickable aria-label={`เริ่ม${template.title}`} onClick={() => setChosen(template.title)} className="cursor-pointer border border-border-tile-1 bg-layer-1 p-4 text-ink hover:bg-layer-hover-1 active:bg-layer-active-1 focus:outline-2 focus:outline-focus" > <p className="text-xs text-ink-muted">แม่แบบ</p> <h3 className="mb-4 text-base font-semibold">{template.title}</h3> <p className="text-sm">{template.text}</p> </Card.Root> ))} </div> <p className="text-xs text-ink-muted">เลือกแล้ว: {chosen || 'ยังไม่มี'}</p> </div> ); }

ปุ่มใน card

แนวทางของ Carbon คือไม่ใส่ control อื่นใน card ที่เป็นลิงก์หรือคลิกได้ ให้ใส่ปุ่มใน card ธรรมดา หรือใช้ Headless Product card ที่มี click zone ถ้าจำเป็นต้องมีปุ่มข้างในจริง ๆ ให้ครอบด้วย Card.Actions (เป็น <div data-card-actions>) การคลิกข้างในเป็นของปุ่มนั้น onClick ของ card จะไม่ทำงาน การกด Enter หรือ Space บนปุ่มที่มี focus ก็ไม่กด card เพราะ card รับปุ่มคีย์บอร์ดเฉพาะตอนที่ตัวมันเองมี focus screen reader จัดการ control ที่อยู่ใน role="button" ได้ไม่ดี Carbon จึงแนะนำว่าไม่ควรทำ

import { useState } from 'react'; import { Card } from '@purpurabog/ui/headless'; export default function Example() { const [log, setLog] = useState('ยังไม่มี'); const [starred, setStarred] = useState(false); return ( <div className="grid gap-2"> <Card.Root clickable aria-label="เปิดโครงการปรับปรุงสาขา" onClick={() => setLog('card: เปิดโครงการ')} className="relative max-w-sm cursor-pointer border border-border-tile-1 bg-layer-1 p-4 text-ink hover:bg-layer-hover-1 focus:outline-2 focus:outline-focus" > <p className="text-xs text-ink-muted">โครงการ</p> <h3 className="mb-4 text-base font-semibold">ปรับปรุงสาขา</h3> <p className="text-sm">สาขาสีลม ชั้นสอง</p> <Card.Actions className="absolute end-2 top-2"> <button type="button" aria-label="ติดดาว" aria-pressed={starred} onClick={() => { setStarred(!starred); setLog(starred ? 'ดาว: ปิด' : 'ดาว: เปิด'); }} className="flex size-8 items-center justify-center text-ink hover:bg-layer-hover-1 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus" > {starred ? '★' : '☆'} </button> </Card.Actions> </Card.Root> <p className="text-xs text-ink-muted">กดล่าสุด: {log}</p> </div> ); }

ปิดใช้งาน

disabled หยุด onClick card ที่เป็นลิงก์แต่ปิดใช้งานจะกลายเป็นกล่องธรรมดา (หรือเป็นปุ่ม ถ้าใส่ clickable ด้วย) card ที่คลิกได้แต่ปิดใช้งานยังคง role ไว้ ได้ aria-disabled และออกจากลำดับของ Tab (tabIndex={-1}) กล่องและปุ่มจะมี data-disabled

import { Card } from '@purpurabog/ui/headless'; export default function Example() { return ( <Card.Root clickable disabled aria-label="เริ่มสัญญากู้ยืม (ยังใช้ไม่ได้)" onClick={() => alert('never runs')} className="max-w-sm border border-border-tile-1 bg-layer-1 p-4 text-ink data-disabled:cursor-not-allowed data-disabled:border-border-disabled data-disabled:text-ink-disabled" > <p className="text-xs">แม่แบบ</p> <h3 className="mb-4 text-base font-semibold">สัญญากู้ยืม</h3> <p className="text-sm">ฝ่ายกฎหมายกำลังแก้ไขถึงวันที่ 15 ตุลาคม</p> </Card.Root> ); }

แต่ง style

import { useState } from 'react'; import { Card } from '@purpurabog/ui/headless'; export default function Example() { const [disabled, setDisabled] = useState(false); const [presses, setPresses] = useState(0); return ( <div className="grid gap-4"> <Card.Root clickable disabled={disabled} aria-label="เปิดสรุปประจำเดือน" onClick={() => setPresses(presses + 1)} className="max-w-sm bg-layer-1 text-ink outline-transparent data-clickable:cursor-pointer data-clickable:border data-clickable:border-border-tile-1 data-clickable:hover:bg-layer-hover-1 focus:outline-2 focus:outline-focus data-disabled:cursor-not-allowed data-disabled:border-border-disabled data-disabled:text-ink-disabled data-disabled:hover:bg-layer-1" > <div className="p-4"> <p className="text-xs">รายงาน</p> <h3 className="text-base font-semibold">สรุปสัญญาประจำเดือน</h3> </div> <div className="flex justify-end p-4" aria-hidden="true"> <svg viewBox="0 0 16 16" className="size-4 fill-current"> <path d="M9.3 3.7 13.1 7.5H1v1h12.1l-3.8 3.8.7.7 5-5-5-5z" /> </svg> </div> </Card.Root> <div className="flex items-center gap-4 text-sm text-ink"> <label className="flex items-center gap-2"> <input type="checkbox" checked={disabled} onChange={(e) => setDisabled(e.target.checked)} /> disabled </label> <span className="text-xs text-ink-muted">กดแล้ว: {presses}</span> </div> </div> ); } .card { background: #f4f4f4; } .card[data-link], .card[data-clickable] { border: 1px solid #c6c6c6; cursor: pointer; } .card[data-link]:hover, .card[data-clickable]:hover { background: #e8e8e8; } .card[data-disabled] { color: #8d8d8d; cursor: not-allowed; }

Parts

Card.Root

เป็น <a> เมื่อมี href (ยกเว้นตอนปิดใช้งาน) เป็น <div role="button"> เมื่อมี clickable นอกนั้นเป็น <div> รับ prop ของ <div> ได้ทุกตัว ref ชี้ไปที่ element ที่ render จริง

Prop Type Default
href string — ทำให้ทั้ง card เป็นลิงก์เดียว
target '_self' | '_blank' '_self' _blank เติม rel="noopener noreferrer"
clickable boolean false ทำให้ทั้ง card เป็นปุ่มเดียว (tabIndex 0, Enter และ Space)
onClick (event: MouseEvent<HTMLElement> | KeyboardEvent<HTMLElement>) => void — card ถูกกด เฉพาะ card ที่เป็นลิงก์หรือคลิกได้ ไม่นับที่มาจากใน Card.Actions และไม่ทำงานตอนปิดใช้งาน
disabled boolean false ไม่เรียก onClick ลิงก์จะกลายเป็นกล่องหรือปุ่ม
Attribute เมื่อไร
data-link render เป็นลิงก์
data-clickable render เป็นปุ่ม
data-disabled ตั้ง disabled บนกล่องหรือปุ่ม

Card.Actions

<div data-card-actions> สำหรับปุ่มที่จำเป็นต้องอยู่ใน card ที่เป็นลิงก์หรือคลิกได้ (Carbon แนะนำว่าไม่ควรทำ) รับ prop ของ <div> ได้ทุกตัว การคลิกที่เริ่มข้างในจะไม่เรียก onClick ของ card

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

ปุ่ม ทำอะไร
Tab ไปที่ card ที่เป็นลิงก์หรือคลิกได้ แล้วไปที่ปุ่มข้างใน
Enter ไปตามลิงก์ของ card ที่เป็นลิงก์ กด card ที่คลิกได้ซึ่งตัวมันเองมี focus
Space กด card ที่คลิกได้ซึ่งตัวมันเองมี focus

part ตั้ง role="button", tabIndex, aria-disabled และ rel ให้

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

  • ชื่อของ card ที่เป็นลิงก์หรือคลิกได้ ใช้ aria-label เพราะไม่อย่างนั้นข้อความทั้งหมดจะถูกอ่านเป็นชื่อ
  • heading สำหรับหัวข้อ ในระดับที่หน้านั้นต้องการ
  • alt สำหรับรูปที่มีข้อมูล และ alt="" สำหรับรูปตกแต่ง
  • style ของ focus ที่มองเห็นได้ ทั้งบน card และปุ่มข้างใน
  • ประโยคใน card ที่ปิดใช้งาน บอกว่าทำไมใช้ไม่ได้