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 Code snippet

CodeSnippet คือพฤติกรรมของ Code snippet แบบมี style ในรูป React part ที่ไม่มี style Code คือ <code> ที่อ่านข้อความกลับมาหลัง render ทุกครั้ง และข้อความนั้นคือสิ่งที่คัดลอก (ถ้าไม่ได้ให้ text) สำหรับ snippet หลายบรรทัด Content คุมความสูงของโค้ดตามจำนวนแถวแบบ Carbon (แถวละ 16px แสดง 15 แถวตอนพับ) และวัดโค้ดใหม่เมื่อขนาดเปลี่ยน ส่วน Toggle ("แสดงเพิ่มเติม / แสดงน้อยลง") มีอยู่เฉพาะตอนที่โค้ดสูงกว่าจำนวนแถวตอนพับ

การคัดลอกใช้ Copy button แบบ headless: ส่ง copyText จาก context ให้ Copy.Root ส่วนที่คุณทำเองคือกล่อง ไอคอนคัดลอก tooltip ข้อความบนปุ่ม และ style ทั้งหมด

ติดตั้ง

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

npm install @purpurabog/ui import { CodeSnippet, Copy } from '@purpurabog/ui/headless';

วิธีใช้

import { CodeSnippet, Copy } from '@purpurabog/ui/headless'; function CopyCode() { const { copyText } = CodeSnippet.useCodeSnippetContext(); return ( <Copy.Root text={copyText}> <Copy.Trigger aria-label="คัดลอก" className="grid size-10 shrink-0 cursor-pointer place-items-center bg-surface-1 text-icon-primary hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-copied:text-success"> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"> <path d="M14 5v9H5V5h9m0-1H5a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h9a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1Z" /> <path d="M2 9H1V2a1 1 0 0 1 1-1h7v1H2Z" /> </svg> </Copy.Trigger> <Copy.Status className="sr-only">คัดลอกแล้ว</Copy.Status> </Copy.Root> ); } export default function Example() { return ( <CodeSnippet.Root> <div className="flex h-10 max-w-3xl items-center bg-surface-1"> <div role="textbox" aria-readonly="true" aria-label="คำสั่งติดตั้ง" tabIndex={0} className="flex h-full grow items-center overflow-x-auto ps-4 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> <pre className="m-0 type-code whitespace-pre text-ink"> <CodeSnippet.Code>npm install @purpurabog/ui</CodeSnippet.Code> </pre> </div> <CopyCode /> </div> </CodeSnippet.Root> ); }

Root render แค่ children ส่วน textbox อ่านอย่างเดียวรอบโค้ด (role="textbox", aria-readonly, tabIndex={0}) คุณใส่เองแบบเดียวกับ Carbon ผู้ใช้คีย์บอร์ดจะไปถึงและเลื่อนดูโค้ดได้

โครงสร้าง

<CodeSnippet.Root> <CodeSnippet.Content> <pre> <CodeSnippet.Code /> </pre> </CodeSnippet.Content> <CodeSnippet.Toggle /> </CodeSnippet.Root>

snippet บรรทัดเดียวและแบบ inline ใช้แค่ Root กับ Code

หลายบรรทัดพร้อมแสดงเพิ่มเติม

ครอบโค้ดด้วย Content มันตั้ง max-height และ min-height เองตามจำนวนแถว จึงต้องใส่ overflow-y-auto Toggle จะแสดงเมื่อโค้ดสูงกว่า 15 แถว ให้โค้ดมีความสูงบรรทัด 16px (type-code) แถวจะได้ตรงกัน

import { CodeSnippet } from '@purpurabog/ui/headless'; const contract = `{ "contract": "CT-2026-0142", "title": "วัสดุสำนักงาน ปีงบประมาณ 2569", "department": "ฝ่ายจัดซื้อ", "vendor": { "name": "บริษัท สยามออฟฟิศ จำกัด", "taxId": "0105556012345" }, "amount": 120000, "currency": "THB", "startDate": "2026-01-01", "endDate": "2026-12-31", "approvers": [ { "step": 1, "role": "หัวหน้าฝ่าย" }, { "step": 2, "role": "ฝ่ายการเงิน" }, { "step": 3, "role": "ฝ่ายกฎหมาย" } ], "status": "awaiting-signature", "attachments": ["quotation.pdf", "terms.pdf"] }`; function ToggleText() { const { expanded } = CodeSnippet.useCodeSnippetContext(); return <>{expanded ? 'แสดงน้อยลง' : 'แสดงเพิ่มเติม'}</>; } export default function Example() { return ( <CodeSnippet.Root> <div className="relative max-w-3xl bg-surface-1 p-4"> <CodeSnippet.Content className="overflow-y-auto transition-[max-height] duration-150 data-expanded:pb-4"> <pre tabIndex={0} role="textbox" aria-readonly="true" aria-multiline="true" aria-label="JSON ของสัญญา" className="m-0 type-code whitespace-pre text-ink focus-visible:outline-2 focus-visible:outline-focus"> <CodeSnippet.Code>{contract}</CodeSnippet.Code> </pre> </CodeSnippet.Content> <CodeSnippet.Toggle className="group absolute end-0 bottom-0 inline-flex h-8 cursor-pointer items-center gap-2 bg-surface-1 px-4 text-sm text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> <ToggleText /> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-icon-primary transition-transform group-data-expanded:rotate-180"> <path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" /> </svg> </CodeSnippet.Toggle> </div> </CodeSnippet.Root> ); }

เก็บสถานะเปิดเอง

expanded คู่กับ onExpandedChange ใช้เก็บสถานะเอง ส่วน defaultExpanded ตั้งแค่ค่าเริ่มต้น onExpandedChange ยิงทุกครั้งที่กด Toggle และเมื่อ snippet ที่เปิดอยู่พับกลับเอง (โค้ดสูงไม่เกิน minExpandedRows)

import { useState } from 'react'; import { CodeSnippet } from '@purpurabog/ui/headless'; const env = Array.from({ length: 24 }, (_, i) => `SETTING_${String(i + 1).padStart(2, '0')}=value-${i + 1}`).join('\n'); export default function Example() { const [open, setOpen] = useState(false); return ( <div className="grid max-w-3xl gap-3"> <CodeSnippet.Root expanded={open} onExpandedChange={setOpen}> <div className="relative bg-surface-1 p-4"> <CodeSnippet.Content className="overflow-y-auto data-expanded:pb-4"> <pre className="m-0 type-code whitespace-pre text-ink"> <CodeSnippet.Code>{env}</CodeSnippet.Code> </pre> </CodeSnippet.Content> <CodeSnippet.Toggle className="absolute end-0 bottom-0 h-8 cursor-pointer bg-surface-1 px-4 text-sm text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> {open ? 'แสดงแค่ส่วนต้น' : 'แสดงทั้งไฟล์'} </CodeSnippet.Toggle> </div> </CodeSnippet.Root> <p className="text-xs text-ink-muted">เปิดอยู่: {String(open)}</p> </div> ); }

จำนวนแถว

maxCollapsedRows (15) และ maxExpandedRows (0 คือไม่จำกัด) จำกัดความสูงเป็นแถวละ 16px ส่วน minCollapsedRows (3) และ minExpandedRows (16) กำหนดความสูงต่ำสุด Toggle แสดงเฉพาะเมื่อโค้ดสูงกว่าจำนวนแถวตอนพับและการเปิดจะแสดงได้มากขึ้น ถ้าลดจำนวนแถวตอนพับให้ลด minExpandedRows ด้วย ไม่อย่างนั้น snippet ที่เปิดอยู่และโค้ดสั้นจะพับกลับเมื่อวัดใหม่

import { CodeSnippet } from '@purpurabog/ui/headless'; const query = `SELECT c.id, c.title, c.amount, d.name AS department FROM contracts c JOIN departments d ON d.id = c.department_id JOIN vendors v ON v.id = c.vendor_id WHERE c.status = 'awaiting-signature' AND c.amount >= 100000 AND c.start_date >= DATE '2026-01-01' AND d.code IN ('PROC', 'LEGAL', 'FIN') AND v.blacklisted = false GROUP BY c.id, d.name HAVING count(*) > 0 ORDER BY c.amount DESC LIMIT 50;`; export default function Example() { return ( <CodeSnippet.Root maxCollapsedRows={4} maxExpandedRows={8} minExpandedRows={0}> <div className="relative max-w-3xl bg-surface-1 p-4"> <CodeSnippet.Content className="overflow-y-auto data-expanded:pb-4"> <pre className="m-0 type-code whitespace-pre text-ink"> <CodeSnippet.Code>{query}</CodeSnippet.Code> </pre> </CodeSnippet.Content> <CodeSnippet.Toggle className="absolute end-0 bottom-0 h-8 cursor-pointer bg-surface-1 px-4 text-sm text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-expanded:hidden"> แสดงเพิ่มเติม </CodeSnippet.Toggle> </div> </CodeSnippet.Root> ); }

ตัวอย่างนี้ Toggle ซ่อนตัวเองเมื่อเปิดแล้ว (data-expanded:hidden) บล็อกที่เปิดจะเลื่อนดูต่อหลัง 8 แถว

คัดลอกค่าอื่น

text ที่ Root ใช้แทนโค้ดเป็น copyText แสดงแบบสั้นหรือแบบปลอดภัย แล้วคัดลอกแบบเต็ม

import { CodeSnippet, Copy } from '@purpurabog/ui/headless'; function CopyCode() { const { copyText } = CodeSnippet.useCodeSnippetContext(); return ( <Copy.Root text={copyText}> <Copy.Trigger aria-label="คัดลอกคำสั่งพร้อม token" className="grid size-10 shrink-0 cursor-pointer place-items-center bg-surface-1 text-icon-primary hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus data-copied:text-success"> <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-current"> <path d="M14 5v9H5V5h9m0-1H5a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h9a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1Z" /> <path d="M2 9H1V2a1 1 0 0 1 1-1h7v1H2Z" /> </svg> </Copy.Trigger> <Copy.Status className="sr-only">คัดลอกพร้อม token แล้ว</Copy.Status> </Copy.Root> ); } export default function Example() { const token = '7f3c9a1e5b2d'; return ( <CodeSnippet.Root text={`curl -H 'Authorization: Bearer ${token}' https://api.example.com/contracts/CT-2026-0142`}> <div className="flex h-10 max-w-3xl items-center bg-surface-1"> <div className="flex h-full grow items-center overflow-x-auto ps-4"> <pre className="m-0 type-code whitespace-pre text-ink"> <CodeSnippet.Code>curl -H 'Authorization: Bearer ••••••' https://api.example.com/contracts/CT-2026-0142</CodeSnippet.Code> </pre> </div> <CopyCode /> </div> </CodeSnippet.Root> ); }

ชิปในประโยค

snippet แบบ inline ใช้โค้ดเป็นปุ่มคัดลอก: ใส่ Code ไว้ใน Copy.Trigger ปุ่มจะได้ชื่อจากโค้ด ผู้ใช้จึงได้ยินสิ่งที่จะคัดลอก

import { CodeSnippet, Copy } from '@purpurabog/ui/headless'; function Chip() { const { copyText } = CodeSnippet.useCodeSnippetContext(); return ( <Copy.Root text={copyText}> <Copy.Trigger className="inline cursor-pointer rounded-sm border border-transparent bg-surface-1 px-0 text-ink hover:bg-ghost-hover focus-visible:border-focus focus-visible:outline-none data-copied:bg-layer-selected-1"> <CodeSnippet.Code className="px-2 type-code whitespace-pre">CC-4410-PROC</CodeSnippet.Code> </Copy.Trigger> <Copy.Status className="sr-only">คัดลอกแล้ว</Copy.Status> </Copy.Root> ); } export default function Example() { return ( <p className="text-sm text-ink"> ใช้ศูนย์ต้นทุน{' '} <CodeSnippet.Root> <Chip /> </CodeSnippet.Root>{' '} สำหรับวัสดุสำนักงาน </p> ); }

ใส่ style

import { CodeSnippet, Copy } from '@purpurabog/ui/headless'; const log = Array.from({ length: 20 }, (_, i) => `2026-03-14 09:${String(10 + i).padStart(2, '0')}:00 INFO approval.service step ${i + 1} done for CT-2026-0142`).join('\n'); function Parts() { const { copyText, expanded } = CodeSnippet.useCodeSnippetContext(); return ( <div className="relative max-w-3xl bg-surface-1 p-4"> <CodeSnippet.Content className="overflow-y-auto transition-[max-height] duration-150 data-expanded:pb-4"> <pre tabIndex={0} role="textbox" aria-readonly="true" aria-multiline="true" aria-label="log การอนุมัติ" className="m-0 pe-10 type-code whitespace-pre text-ink focus-visible:outline-2 focus-visible:outline-focus"> <CodeSnippet.Code>{log}</CodeSnippet.Code> </pre> </CodeSnippet.Content> <Copy.Root text={copyText}> <Copy.Trigger aria-label="คัดลอก log" className="group absolute end-2 top-2 grid size-8 cursor-pointer place-items-center bg-surface-1 text-icon-primary hover:bg-ghost-hover 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 group-data-copied:hidden"> <path d="M14 5v9H5V5h9m0-1H5a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h9a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1Z" /> <path d="M2 9H1V2a1 1 0 0 1 1-1h7v1H2Z" /> </svg> <svg viewBox="0 0 16 16" aria-hidden="true" className="hidden size-4 fill-success group-data-copied:block"> <path d="M6 10.6 2.7 7.3 2 8l4 4 8-8-.7-.7z" /> </svg> </Copy.Trigger> <Copy.Status className="sr-only">คัดลอกแล้ว</Copy.Status> </Copy.Root> <CodeSnippet.Toggle className="group absolute end-0 bottom-0 inline-flex h-8 cursor-pointer items-center gap-2 bg-surface-1 px-4 text-sm text-ink hover:bg-ghost-hover focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus"> {expanded ? 'แสดงน้อยลง' : 'แสดงเพิ่มเติม'} <svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 fill-icon-primary transition-transform group-data-expanded:rotate-180"> <path d="M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z" /> </svg> </CodeSnippet.Toggle> </div> ); } export default function Example() { return ( <CodeSnippet.Root> <Parts /> </CodeSnippet.Root> ); } /* className="content" ที่ Content, "toggle" ที่ Toggle */ .content { overflow-y: auto; transition: max-height 150ms; } .content[data-expanded] { padding-bottom: 16px; } .toggle { position: absolute; inset-inline-end: 0; inset-block-end: 0; height: 32px; padding: 0 16px; border: 0; background: #f4f4f4; } .toggle[data-expanded] svg { rotate: 180deg; }

Parts

CodeSnippet.Root

render แค่ children เก็บสถานะเปิด จำนวนแถว และข้อความของโค้ด

Prop Type ค่าเริ่มต้น
text string '' สิ่งที่คัดลอกแทนโค้ดที่แสดง
expanded boolean แสดงทุกแถว (controlled)
defaultExpanded boolean false สถานะเริ่มต้น (uncontrolled)
onExpandedChange (expanded: boolean) => void ทุกครั้งที่กด Toggle และตอนพับกลับเอง
maxCollapsedRows number 15 จำนวนแถว (16px) ที่แสดงตอนพับ 0 คือไม่จำกัด
maxExpandedRows number 0 จำนวนแถวที่แสดงตอนเปิด 0 คือไม่จำกัด
minCollapsedRows number 3 จำนวนแถวต่ำสุดตอนพับ
minExpandedRows number 16 จำนวนแถวต่ำสุดตอนเปิด snippet ที่เปิดอยู่และโค้ดสูงไม่เกินนี้จะพับกลับ

CodeSnippet.useCodeSnippetContext() คืนค่าต่าง ๆ เช่น copyText (text หรือถ้าไม่มีคือข้อความของโค้ดโดยตัดการขึ้นบรรทัดใหม่ตัวแรกออกหนึ่งครั้งแบบ <pre>), expanded, canExpand (Toggle แสดงอยู่) และ setExpanded()

CodeSnippet.Code

<code> ข้อความของมันถูกอ่านกลับหลัง render ทุกครั้ง children จึงเป็น element อะไรก็ได้ (เช่น ระบายสี syntax) และข้อความนั้นคือ copyText รับ prop ของ <code> ได้ทุกตัว

CodeSnippet.Content

<div> รอบโค้ดของ snippet หลายบรรทัด ตั้ง max-height / min-height ตามจำนวนแถว (style ของคุณรวมต่อท้าย) วัดโค้ดเมื่อขนาดเปลี่ยน และพับ snippet ที่เปิดอยู่กลับเมื่อโค้ดพอดี

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

CodeSnippet.Toggle

<button type="button"> ที่มี aria-expanded render เฉพาะตอนที่โค้ดสูงกว่าจำนวนแถวตอนพับ (canExpand) กดแล้วเปิดหรือพับ รับ prop ของ button ได้ทุกตัว ยกเว้น type ใส่ข้อความเอง

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

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

ปุ่ม ทำอะไร
Tab ไปที่กล่องโค้ดของคุณ (ที่มี tabIndex={0}) แล้วปุ่มคัดลอก แล้ว Toggle
Enter, Space ที่ Toggle: เปิดหรือพับ ที่ trigger คัดลอก: คัดลอก
ลูกศร เลื่อนกล่องโค้ดที่โฟกัสอยู่

part ตั้ง aria-expanded ที่ Toggle ส่วน part คัดลอกให้ปุ่มและ live region แบบ polite

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

  • textbox อ่านอย่างเดียวรอบโค้ด: role="textbox", aria-readonly="true" (และ aria-multiline สำหรับบล็อก), tabIndex={0} และชื่อ (aria-label)
  • ชื่อของปุ่มคัดลอกที่มีแต่ไอคอน และข้อความหลังคัดลอกใน Copy.Status
  • ข้อความ "แสดงเพิ่มเติม" / "แสดงน้อยลง" และ style ของโฟกัสที่มองเห็นได้ทุกจุด