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

Button

ปุ่มใช้สั่งให้ทำงานบางอย่าง เช่น บันทึก ส่งฟอร์ม หรือเปิด dialog ถ้าจะไปหน้าอื่น ให้ใส่ href แล้วปุ่มจะกลายเป็นลิงก์จริง

ทุกตัวอย่างด้านล่างมี 3 แบบ: web component (HTML ใช้ได้ทุก framework), wrapper ของ React (@purpurabog/ui/react) และไฟล์ React + Tailwind ที่ copy ด้วย shadcn เลือกแท็บครั้งเดียว ทั้งเว็บจะเปิดแท็บนั้นตาม

ติดตั้ง

npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui npx shadcn@4 add @kanok-ui/button npx shadcn@4 add @kanok-ui/tw-button # Web component # 1. ติดตั้ง dependency npm install lit@^3.3.0 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/button.json # ไปไว้ที่ target ของมัน: # components/ui/primitives/button/button-skeleton.ts # components/ui/primitives/button/button.styles.ts # components/ui/primitives/button/button.ts # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # core-button, shared-element, shared-field, shared-form-control, shared-localize, shared-navigate, shared-skeleton, shared-slots, tokens # React + Tailwind # 1. ติดตั้ง dependency npm install class-variance-authority@0.7.1 # 2. คัดลอก "content" ของแต่ละไฟล์จาก # https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-button.json # ไปไว้ที่ target ของมัน: # components/ui/react-tw/button.tsx # 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json): # core-button, tw-i18n, tw-theme, tw-utils

npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง

วิธีใช้

HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/button';</script> <ui-button kind="primary" type="submit">บันทึก</ui-button> // npm i @purpurabog/ui lit @lit/react import { UiButton } from '@purpurabog/ui/react'; <UiButton kind="primary" type="submit">บันทึก</UiButton> import { Button } from '@/components/ui/react-tw/button'; <Button kind="primary" type="submit">บันทึก</Button>

เลือกชนิดปุ่ม

บันทึก ยกเลิก ส่งออก ดูเพิ่มเติม ลบ
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button>บันทึก</ui-button> <ui-button kind="secondary">ยกเลิก</ui-button> <ui-button kind="tertiary">ส่งออก</ui-button> <ui-button kind="ghost">ดูเพิ่มเติม</ui-button> <ui-button kind="danger">ลบ</ui-button> </div> <UiButton>บันทึก</UiButton> <UiButton kind="secondary">ยกเลิก</UiButton> <UiButton kind="tertiary">ส่งออก</UiButton> <UiButton kind="ghost">ดูเพิ่มเติม</UiButton> <UiButton kind="danger">ลบ</UiButton> <Button>บันทึก</Button> <Button kind="secondary">ยกเลิก</Button> <Button kind="tertiary">ส่งออก</Button> <Button kind="ghost">ดูเพิ่มเติม</Button> <Button kind="danger">ลบ</Button>
kind ใช้กับ
primary การกระทำหลักหนึ่งเดียวของหน้าหรือ dialog
secondary การกระทำข้างปุ่มหลัก (ยกเลิก, ย้อนกลับ)
tertiary การกระทำรองที่ยังต้องเห็นขอบปุ่ม
ghost การกระทำเบา ๆ ในแถบเครื่องมือ ตาราง หรือข้างเนื้อหา
danger, danger-tertiary, danger-ghost การลบหรือการกระทำที่ย้อนกลับไม่ได้

ในแต่ละส่วนของหน้าควรมีปุ่ม primary ปุ่มเดียว kind เป็นชื่อของ Carbon ส่วน web component รับ variant แทนกันได้

ปุ่มอันตราย

ใช้ปุ่มชนิด danger กับการลบหรือการกระทำที่ย้อนกลับไม่ได้: danger เป็นปุ่มหลักของ dialog ยืนยัน ส่วน danger-tertiary และ danger-ghost ใช้ข้างเนื้อหาอื่น ใส่ danger description ด้วย screen reader จะอ่านต่อจากชื่อปุ่ม คำเตือนจึงไม่ได้อยู่แค่ในสีแดง

ลบสัญญา ยกเลิกสัญญาเช่า นำผู้ลงนามออก
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button kind="danger" danger-description="ลบสัญญาถาวร">ลบสัญญา</ui-button> <ui-button kind="danger-tertiary" danger-description="ย้อนกลับไม่ได้">ยกเลิกสัญญาเช่า</ui-button> <ui-button kind="danger-ghost" danger-description="ย้อนกลับไม่ได้">นำผู้ลงนามออก</ui-button> </div> <UiButton kind="danger" dangerDescription="ลบสัญญาถาวร">ลบสัญญา</UiButton> <UiButton kind="danger-tertiary" dangerDescription="ย้อนกลับไม่ได้">ยกเลิกสัญญาเช่า</UiButton> <UiButton kind="danger-ghost" dangerDescription="ย้อนกลับไม่ได้">นำผู้ลงนามออก</UiButton> <Button kind="danger" dangerDescription="ลบสัญญาถาวร">ลบสัญญา</Button> <Button kind="danger-tertiary" dangerDescription="ย้อนกลับไม่ได้">ยกเลิกสัญญาเช่า</Button> <Button kind="danger-ghost" dangerDescription="ย้อนกลับไม่ได้">นำผู้ลงนามออก</Button>

ขนาด

size: sm 32px, md 40px, lg 48px (ค่าเริ่มต้น), xl 64px, 2xl 80px ในฟอร์มให้ใช้ขนาดเท่าช่องกรอก (md สำหรับช่อง 40px) ขนาด xl และ 2xl ข้อความอยู่ชิดด้านบนแบบ Carbon

เล็ก กลาง ใหญ่ ใหญ่พิเศษ ใหญ่ที่สุด
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button size="sm">เล็ก</ui-button> <ui-button size="md">กลาง</ui-button> <ui-button>ใหญ่</ui-button> <ui-button size="xl">ใหญ่พิเศษ</ui-button> <ui-button size="2xl">ใหญ่ที่สุด</ui-button> </div> <UiButton size="sm">เล็ก</UiButton> <UiButton size="md">กลาง</UiButton> <UiButton>ใหญ่</UiButton> <UiButton size="xl">ใหญ่พิเศษ</UiButton> <UiButton size="2xl">ใหญ่ที่สุด</UiButton> <Button size="sm">เล็ก</Button> <Button size="md">กลาง</Button> <Button>ใหญ่</Button> <Button size="xl">ใหญ่พิเศษ</Button> <Button size="2xl">ใหญ่ที่สุด</Button>

เต็มความกว้าง

full-width ยืดปุ่มให้กว้างเท่ากล่องที่ครอบอยู่ ใช้กับปุ่มเดียวท้ายฟอร์มแคบ ๆ หรือบนจอมือถือ

ส่งขออนุมัติ
HTMLReactReact + Tailwind<ui-button full-width>ส่งขออนุมัติ</ui-button> <UiButton fullWidth>ส่งขออนุมัติ</UiButton> <Button fullWidth>ส่งขออนุมัติ</Button>

ชุดปุ่ม

<ui-button-set> วางปุ่มเรียงกัน แต่ละปุ่มกว้างไม่เกิน 196px และมีเส้นบาง ๆ คั่น เป็นท้ายฟอร์มหรือท้าย side panel แบบ Carbon ให้วางปุ่ม secondary ไว้ก่อน stacked วางปุ่มละแถว ใช้กับ panel แคบ ๆ

ยกเลิก บันทึก
อนุมัติ ขอให้แก้ไข
HTMLReactReact + Tailwind<ui-button-set> <ui-button kind="secondary">ยกเลิก</ui-button> <ui-button>บันทึก</ui-button> </ui-button-set> <div style="max-width: 196px"> <ui-button-set stacked> <ui-button>อนุมัติ</ui-button> <ui-button kind="secondary">ขอให้แก้ไข</ui-button> </ui-button-set> </div> import { UiButton, UiButtonSet } from '@purpurabog/ui/react'; <UiButtonSet> <UiButton kind="secondary">ยกเลิก</UiButton> <UiButton>บันทึก</UiButton> </UiButtonSet> <UiButtonSet stacked> <UiButton>อนุมัติ</UiButton> <UiButton kind="secondary">ขอให้แก้ไข</UiButton> </UiButtonSet> // npx shadcn@4 add @kanok-ui/tw-button-set import { ButtonSet } from '@/components/ui/react-tw/button-set'; <ButtonSet> <Button kind="secondary">ยกเลิก</Button> <Button>บันทึก</Button> </ButtonSet> <ButtonSet stacked> <Button>อนุมัติ</Button> <Button kind="secondary">ขอให้แก้ไข</Button> </ButtonSet>

ไอคอน

web component ใส่ไอคอนใน slot prefix หรือ suffix ส่วนปุ่มแบบ React + Tailwind ใส่ไอคอนได้เฉพาะท้ายปุ่มผ่าน prop icon Carbon วางไอคอนไว้ท้ายปุ่ม และปุ่ม ghost ที่พาไปดูรายละเอียดมักจบด้วยลูกศร ถ้าปุ่มมีแต่ไอคอน ดู ปุ่มไอคอนอย่างเดียว

เพิ่ม รายละเอียด บันทึกร่าง
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button kind="primary">เพิ่ม <svg slot="suffix" viewBox="0 0 16 16"><path d="M8.5 7.5V3h-1v4.5H3v1h4.5V13h1V8.5H13v-1z"/></svg> </ui-button> <ui-button kind="ghost">รายละเอียด <svg slot="suffix" viewBox="0 0 16 16"><path d="m11 8-5 5-.7-.7L9.6 8 5.3 3.7 6 3z"/></svg> </ui-button> <ui-button kind="tertiary"> <svg slot="prefix" viewBox="0 0 16 16"><path d="M13 2H3a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V5zm-7 1h4v2H6zm7 10H3V3h2v3h6V3.4l2 2z"/></svg> บันทึกร่าง </ui-button> </div> import { Add as AddIcon, ChevronRight as ChevronRightIcon, Save as SaveIcon } from '@carbon/icons-react'; <UiButton kind="primary"> เพิ่ม <AddIcon slot="suffix" /> </UiButton> <UiButton kind="ghost"> รายละเอียด <ChevronRightIcon slot="suffix" /> </UiButton> <UiButton kind="tertiary"> <SaveIcon slot="prefix" /> บันทึกร่าง </UiButton> import { Add as AddIcon, ChevronRight as ChevronRightIcon, Save as SaveIcon } from '@carbon/icons-react'; <Button kind="primary" icon={<AddIcon />}>เพิ่ม</Button> <Button kind="ghost" icon={<ChevronRightIcon />}>รายละเอียด</Button> {/* ไม่มีไอคอนหน้าปุ่ม: ไอคอนไปอยู่ท้ายปุ่ม */} <Button kind="tertiary" icon={<SaveIcon />}>บันทึกร่าง</Button>

ปุ่มไอคอนอย่างเดียว

ปุ่มที่มีแต่ไอคอนให้ใช้ <ui-icon-button> (IconButton ของ Carbon) label เป็นทั้งชื่อปุ่มและข้อความ tooltip ผู้ใช้จึงเห็นความหมายเมื่อชี้เมาส์หรือโฟกัส ค่าเริ่มต้นเป็นชนิด ghost และ tooltip-placement ย้ายตำแหน่ง tooltip (ค่าเริ่มต้นอยู่ด้านล่าง) <ui-button icon-only label="…"> ก็ทำปุ่มสี่เหลี่ยมได้ แต่ไม่มี tooltip

HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-icon-button label="แก้ไขสัญญา"> <svg viewBox="0 0 16 16"><path d="m13.7 4.3-2-2a1 1 0 0 0-1.4 0L2 10.6V14h3.4l8.3-8.3a1 1 0 0 0 0-1.4ZM5 13H3v-2l6-6 2 2Zm7-7-2-2 1-1 2 2Z"/></svg> </ui-icon-button> <ui-icon-button label="เพิ่มผู้ลงนาม" kind="primary"> <svg viewBox="0 0 16 16"><path d="M8.5 7.5V3h-1v4.5H3v1h4.5V13h1V8.5H13v-1z"/></svg> </ui-icon-button> <ui-icon-button label="แก้ไขเงื่อนไข" kind="tertiary" tooltip-placement="right"> <svg viewBox="0 0 16 16"><path d="m13.7 4.3-2-2a1 1 0 0 0-1.4 0L2 10.6V14h3.4l8.3-8.3a1 1 0 0 0 0-1.4ZM5 13H3v-2l6-6 2 2Zm7-7-2-2 1-1 2 2Z"/></svg> </ui-icon-button> </div> import { UiIconButton } from '@purpurabog/ui/react'; import { Add as AddIcon, Edit as EditIcon } from '@carbon/icons-react'; <UiIconButton label="แก้ไขสัญญา"><EditIcon /></UiIconButton> <UiIconButton label="เพิ่มผู้ลงนาม" kind="primary"><AddIcon /></UiIconButton> <UiIconButton label="แก้ไขเงื่อนไข" kind="tertiary" tooltipPlacement="right"><EditIcon /></UiIconButton> import { Add as AddIcon, Edit as EditIcon } from '@carbon/icons-react'; // npx shadcn@4 add @kanok-ui/tw-icon-button import { IconButton } from '@/components/ui/react-tw/icon-button'; <IconButton label="แก้ไขสัญญา"><EditIcon /></IconButton> <IconButton label="เพิ่มผู้ลงนาม" kind="primary"><AddIcon /></IconButton> <IconButton label="แก้ไขเงื่อนไข" kind="tertiary" tooltipPlacement="right"><EditIcon /></IconButton>

ตัวเลขแจ้งเตือน

badge-count ใส่ badge indicator แบบ Carbon ไว้ที่มุมของปุ่มไอคอนชนิด ghost: ตัวเลขแสดงจำนวน ส่วน 0 แสดงเป็นจุด screen reader อ่านจำนวนเป็นคำอธิบายของปุ่ม ใช้กับงานที่รอผู้ใช้อยู่ เช่น รายการรออนุมัติ

HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-icon-button label="รออนุมัติ" badge-count="4"> <svg viewBox="0 0 32 32"><path d="M28.7 19.3 26 16.6V13a10 10 0 0 0-9-10v-2h-2v2a10 10 0 0 0-9 10v3.6l-2.7 2.7A1 1 0 0 0 3 20v3a1 1 0 0 0 1 1h7v.8a5 5 0 0 0 10 0V24h7a1 1 0 0 0 1-1v-3a1 1 0 0 0-.3-.7ZM19 24.8a3 3 0 0 1-6 0V24h6ZM27 22H5v-1.6l2.7-2.7A1 1 0 0 0 8 17v-4a8 8 0 0 1 16 0v4a1 1 0 0 0 .3.7l2.7 2.7Z"/></svg> </ui-icon-button> <ui-icon-button label="การแจ้งเตือน มีรายการใหม่" badge-count="0"> <svg viewBox="0 0 32 32"><path d="M28.7 19.3 26 16.6V13a10 10 0 0 0-9-10v-2h-2v2a10 10 0 0 0-9 10v3.6l-2.7 2.7A1 1 0 0 0 3 20v3a1 1 0 0 0 1 1h7v.8a5 5 0 0 0 10 0V24h7a1 1 0 0 0 1-1v-3a1 1 0 0 0-.3-.7ZM19 24.8a3 3 0 0 1-6 0V24h6ZM27 22H5v-1.6l2.7-2.7A1 1 0 0 0 8 17v-4a8 8 0 0 1 16 0v4a1 1 0 0 0 .3.7l2.7 2.7Z"/></svg> </ui-icon-button> </div> import { Notification as NotificationIcon } from '@carbon/icons-react'; <UiIconButton label="รออนุมัติ" badgeCount={4}><NotificationIcon /></UiIconButton> <UiIconButton label="การแจ้งเตือน มีรายการใหม่" badgeCount={0}><NotificationIcon /></UiIconButton> import { Notification as NotificationIcon } from '@carbon/icons-react'; <IconButton label="รออนุมัติ" badgeCount={4}><NotificationIcon /></IconButton> <IconButton label="การแจ้งเตือน มีรายการใหม่" badgeCount={0}><NotificationIcon /></IconButton>

ปุ่มกดค้าง

pressed ทำให้ปุ่มเปิดหรือปิดค้างไว้ เช่น ปักหมุดสัญญาไว้ที่หน้าแดชบอร์ด screen reader จะอ่านว่ากดอยู่หรือไม่ได้กด ให้สลับค่าใน handler ของการคลิก ปุ่มปกติไม่ต้องตั้งค่านี้ ใช้ได้ทั้ง <ui-button> และ <ui-icon-button>

ปักหมุด: ใช่

HTMLReactReact + Tailwind<ui-icon-button id="button-pin" label="ปักหมุดที่แดชบอร์ด" pressed> <svg viewBox="0 0 16 16"><path d="M10 1 9 2l1 1-3 3-3-.5L3.5 6 6 8.5 2 12.5V14h1.5L7.5 10l2.5 2.5.5-.5-.5-3 3-3 1 1 1-1z"/></svg> </ui-icon-button> <p>ปักหมุด: <output id="button-pin-out">ใช่</output></p> <script type="module"> const pin = document.getElementById('button-pin'); pin.addEventListener('click', () => { pin.pressed = !pin.pressed; document.getElementById('button-pin-out').textContent = pin.pressed ? 'ใช่' : 'ไม่'; }); </script> import { Pin as PinIcon } from '@carbon/icons-react'; <UiIconButton label="ปักหมุดที่แดชบอร์ด" pressed={pinned} onClick={() => setPinned(!pinned)}> <PinIcon /> </UiIconButton> import { Pin as PinIcon } from '@carbon/icons-react'; <IconButton label="ปักหมุดที่แดชบอร์ด" aria-pressed={pinned} onClick={() => setPinned(!pinned)}> <PinIcon /> </IconButton>

กำลังโหลด

loading แสดง spinner เล็กแบบ Carbon ไม่รับการคลิก และโฟกัสยังอยู่ที่ปุ่ม คนที่ใช้คีย์บอร์ดจึงไม่หลงตำแหน่ง ข้อความระหว่างโหลดแสดงแทนข้อความปุ่ม ส่วน loading label คือข้อความที่ screen reader อ่าน

loading-position ย้าย spinner: end (ค่าเริ่มต้น) อยู่ตรงที่ไอคอนท้ายปุ่ม start อยู่แทนที่ข้อความ และ center อยู่กลางปุ่ม

บันทึก ส่ง ส่งออก
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button loading loading-text="กำลังบันทึก…">บันทึก</ui-button> <ui-button kind="secondary" loading loading-position="start" loading-text="กำลังส่ง…">ส่ง</ui-button> <ui-button kind="tertiary" loading loading-position="center">ส่งออก</ui-button> </div> <UiButton loading={saving} loadingText="กำลังบันทึก…">บันทึก</UiButton> <UiButton kind="secondary" loading={sending} loadingPosition="start" loadingText="กำลังส่ง…">ส่ง</UiButton> <UiButton kind="tertiary" loading={exporting} loadingPosition="center">ส่งออก</UiButton> <Button loading={saving} loadingText="กำลังบันทึก…">บันทึก</Button> <Button kind="secondary" loading={sending} loadingPosition="start" loadingText="กำลังส่ง…">ส่ง</Button> <Button kind="tertiary" loading={exporting} loadingPosition="center">ส่งออก</Button>

Skeleton

ระหว่างที่ข้อมูลของหน้ากำลังโหลด <ui-button-skeleton> จองที่ของปุ่มไว้ เป็นแถบกว้าง 150px สูงตาม size ของปุ่ม screen reader ข้ามแถบนี้ จึงต้องบอกว่า "กำลังโหลด" ที่บริเวณรอบ ๆ แทน (เช่น aria-busy="true")

HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button-skeleton></ui-button-skeleton> <ui-button-skeleton size="md"></ui-button-skeleton> </div> import { UiButtonSkeleton } from '@purpurabog/ui/react'; <UiButtonSkeleton /> <UiButtonSkeleton size="md" /> import { ButtonSkeleton } from '@/components/ui/react-tw/button'; <ButtonSkeleton /> <ButtonSkeleton size="md" />

ปิดใช้งาน หรือปิดใช้งานแต่บอกเหตุผล

disabled เอาปุ่มออกจากลำดับ Tab แบบ Carbon ถ้าผู้ใช้ต้องรู้ว่าทำไมกดไม่ได้ ให้ใช้ disabled-focusable แทน ปุ่มจะดูเหมือนปิดใช้งานและไม่รับการคลิก แต่ยังกด Tab ไปถึงและอ่าน tooltip ได้

อนุมัติ อนุมัติ
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center"> <ui-button disabled>อนุมัติ</ui-button> <ui-tooltip text="เฉพาะผู้อนุมัติเท่านั้นที่อนุมัติสัญญาได้"> <ui-button disabled-focusable>อนุมัติ</ui-button> </ui-tooltip> </div> <UiButton disabled>อนุมัติ</UiButton> <UiTooltip text="เฉพาะผู้อนุมัติเท่านั้นที่อนุมัติสัญญาได้"> <UiButton disabledFocusable>อนุมัติ</UiButton> </UiTooltip> <Button disabled>อนุมัติ</Button> <Tooltip text="เฉพาะผู้อนุมัติเท่านั้นที่อนุมัติสัญญาได้"> <Button disabledFocusable>อนุมัติ</Button> </Tooltip>

ลิงก์ที่หน้าตาเป็นปุ่ม

ถ้าใส่ href ปุ่มจะสร้าง <a> และ screen reader จะอ่านว่าเป็นลิงก์ ฝั่ง web component การคลิกปกติจะยิง event ui-navigate ที่ยกเลิกได้ ให้ router ของ SPA จัดการต่อ (ดู ใช้กับ framework) ในตัวอย่างนี้ event ถูกยกเลิกและแสดง href แทน

สร้างสัญญา

router ได้รับ:

HTMLReactReact + Tailwind<ui-button id="button-new-contract" kind="tertiary" href="/contracts/new">สร้างสัญญา</ui-button> <p>router ได้รับ: <output id="button-new-contract-out"></output></p> <script type="module"> document.getElementById('button-new-contract').addEventListener('ui-navigate', (event) => { event.preventDefault(); document.getElementById('button-new-contract-out').textContent = event.detail.href; }); </script> <UiButton kind="tertiary" href="/contracts/new" onUiNavigate={(e) => { e.preventDefault(); navigate(e.detail.href); }}> สร้างสัญญา </UiButton> <Button kind="tertiary" href="/contracts/new">สร้างสัญญา</Button>

ส่งฟอร์ม

ใน <form> ปุ่ม type="submit" ส่งฟอร์ม และ type="reset" ล้างฟอร์ม เหมือนปุ่ม native ค่าเริ่มต้นของ type คือ button ปุ่มอื่นจึงไม่ส่งฟอร์มโดยไม่ตั้งใจ ตั้ง loading ระหว่างรอ request ผู้ใช้จะได้ไม่ส่งซ้ำ

ล้างค่า บันทึก

ส่งค่า:

HTMLReactReact + Tailwind<form id="button-form" method="dialog" style="display: grid; gap: 16px"> <ui-text-input label="แผนก" name="department" value="ฝ่ายกฎหมาย" required></ui-text-input> <ui-button-set> <ui-button kind="secondary" type="reset">ล้างค่า</ui-button> <ui-button id="button-form-submit" type="submit" loading-text="กำลังบันทึก…">บันทึก</ui-button> </ui-button-set> </form> <p>ส่งค่า: <output id="button-form-out"></output></p> <script type="module"> const form = document.getElementById('button-form'); const submit = document.getElementById('button-form-submit'); form.addEventListener('submit', (event) => { event.preventDefault(); submit.loading = true; setTimeout(() => { submit.loading = false; document.getElementById('button-form-out').textContent = [...new FormData(form)].map(([key, value]) => `${key}=${value}`).join(', '); }, 1500); }); </script> <form onSubmit={async (e) => { e.preventDefault(); setSaving(true); await save(new FormData(e.currentTarget)); setSaving(false); }}> <UiTextInput label="แผนก" name="department" value="ฝ่ายกฎหมาย" required /> <UiButtonSet> <UiButton kind="secondary" type="reset">ล้างค่า</UiButton> <UiButton type="submit" loading={saving} loadingText="กำลังบันทึก…">บันทึก</UiButton> </UiButtonSet> </form> <form onSubmit={async (e) => { e.preventDefault(); setSaving(true); await save(new FormData(e.currentTarget)); setSaving(false); }}> <TextInput label="แผนก" name="department" defaultValue="ฝ่ายกฎหมาย" required /> <ButtonSet> <Button kind="secondary" type="reset">ล้างค่า</Button> <Button type="submit" loading={saving} loadingText="กำลังบันทึก…">บันทึก</Button> </ButtonSet> </form>

Accessibility

  • ข้างในเป็น <button> จริง กด Enter หรือ Space ได้ และ type="submit" / type="reset" ทำงานใน <form>
  • ปุ่มที่มีแต่ไอคอนต้องมีชื่อ: web component ใช้ label (aria-label ที่ตัว element ไปไม่ถึงปุ่มข้างใน) ปุ่ม Tailwind ใช้ aria-label
  • pressed ทำให้เป็นปุ่มกดค้าง (aria-pressed) ส่วน haspopup กับ expanded ใช้กับปุ่มที่เปิดเมนูหรือ dialog
  • ปุ่มชนิด danger ใส่ danger description ได้ ข้อความนี้จะถูกอ่านต่อจากชื่อปุ่ม (เช่น "ลบสัญญาถาวร")

API

สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)

<ui-button> · React UiButton

Attribute

Attribute Property ชนิด ค่าเริ่มต้น คำอธิบาย
variant Same as kind, kept for teams used to variant (shadcn, MUI); kind is Carbon's name and the one in the docs
kind kind ButtonKind 'primary' Carbon kind: primary, secondary, tertiary, ghost, danger, danger-tertiary, danger-ghost.
size size ButtonSize 'lg'
disabled disabled boolean false
disabled-focusable disabledFocusable boolean false Disabled, but still focusable: aria-disabled, ignores clicks — so a tooltip can explain why.
type type 'button' | 'submit' | 'reset' 'button'
full-width fullWidth boolean false Stretch to the container width.
icon-only iconOnly boolean false Square button (as wide as it is tall, no padding) with its content centred: put the icon in the prefix slot and leave the label empty — nothing is hidden for you. Set label for its name; it shows no tooltip (use <ui-icon-button> for one).
label label string '' Accessible name; required when the button shows only an icon.
description description string '' Accessible description (set by ui-tooltip).
danger-description dangerDescription string '' Carbon dangerDescription: read after the name of a danger kind (danger, danger-tertiary, danger-ghost), e.g. "Deletes the contract for good". Empty by default, as Carbon v11 (React ButtonBase, WC cds-button).
href href string '' Render as a link.
target target '_self' | '_blank' '_self'
loading loading boolean false Busy: looks disabled with a spinner, ignores clicks, keeps focus, announces loading-label.
loading-label loadingLabel string '' What loading announces (default: loading-text, else the translation's "Loading").
loading-position loadingPosition ButtonLoadingPosition 'end' Where the spinner goes (MUI loadingPosition): end (Carbon ActionSet), start, center.
loading-text loadingText string '' Shown in the label's place while loading, e.g. "Saving…" (Carbon Modal loadingDescription).
pressed pressed boolean | undefined Toggle-button state; leave unset for a normal button.
haspopup haspopup string '' The button opens a popup: rendered as aria-haspopup (e.g. menu, listbox, dialog).
expanded expanded boolean | undefined Whether the popup / disclosed content is open (aria-expanded); left unset, only a haspopup button reports it.

Property

Property ชนิด คำอธิบาย
variant ButtonKind Same as kind (permanent alias for teams used to variant); a variant attribute works too.
form (อ่านอย่างเดียว) The <form> the button submits or resets, as a native button's form.

Event

Event React คำอธิบาย
ui-navigate ไม่มี (ใช้ ref) detail: { href } (cancelable) on a plain click of the link; cancel it to route client-side (README: SPA router)

Slot

Slot คำอธิบาย
(ค่าเริ่มต้น) Label
prefix Leading icon
suffix Trailing icon (e.g. the ghost-button chevron)

CSS part

Part คำอธิบาย
base The native button or link
prefix The leading icon slot
suffix The trailing icon slot
loading The spinner's place: at the end, the start or the centre (loading only; not when it takes the label's place)

CSS custom property

Custom property ค่าเริ่มต้น คำอธิบาย
--ui-button-align align-items of the content (ui-modal sets flex-start for its 64px footer buttons)
--ui-button-padding-top Top padding of the content (ui-modal footer: 14px)
--ui-button-padding-start Start padding (default 15px; ui-table-batch-actions: 16px)
--ui-button-padding-end End padding (default 63px, Carbon's room for an icon; ui-table-batch-actions: 16px)
--ui-button-height-lg Height of size="lg", the default (default: --ui-field-height-lg, 48px; Kanok: 32px)
--ui-button-radius Corner radius (default 0; Kanok: 6px)
--ui-button-justify justify-content of label and icon (default space-between, Carbon's label-start / icon-end; Kanok: center)
--ui-button-gap Gap between label and icon (default 32px; Kanok: 8px)
--ui-button-ghost-justify justify-content of the ghost kinds (default flex-start)
--ui-button-ghost-padding-end End padding of the ghost kinds (default 15px)
--ui-button-secondary Fill of kind="secondary" (default --ui-secondary); also --ui-button-secondary-hover, --ui-button-secondary-active
--ui-on-button-secondary Text of kind="secondary" (default --ui-on-primary)
--ui-button-secondary-border Border colour of kind="secondary" (default transparent)
--ui-button-height-xl Height of size="xl" (default 64px; Kanok: 40px); also --ui-button-height-2xl (80px; Kanok: 48px)
--ui-button-padding-xl Start and end padding of size="xl" (default: the button's own); also --ui-button-padding-2xl
--ui-button-tall-align align-items of xl / 2xl content (default flex-start, Carbon's label at the top; Kanok: center)
--ui-button-tall-padding-top Top padding of xl / 2xl content (default 14px; Kanok: 0px); also --ui-button-tall-icon-offset (1px)
--ui-focus-offset outline-offset of the focus ring (default -2px, inside; Kanok: 2px, outside)
--ui-focus-inset-width Width of the inner focus-inset line (default 2px; Kanok: 0)
--ui-button-tertiary-hover / --ui-button-tertiary-active / --ui-button-tertiary-focus - Tertiary fills (default primary, its hover and active; Kanok: the highlight tint)
--ui-button-tertiary-state-ink / --ui-button-tertiary-active-border - Tertiary text on those fills and the pressed border (default text-on-color, the button's border)