ข้ามไปยังเนื้อหา

ปุ่มไอคอน

IconButton

A button that shows only an icon, such as Close or View details. label is its accessible name, so say what it does, such as ดูรายละเอียด PR-2570-0001. Use Button when there's room for text.

ใช้เมื่อไม่มีที่สำหรับข้อความ เช่น ปุ่มในแถวของตาราง หรือปุ่มข้างช่องกรอก ไอคอนต้องเป็นที่รู้จัก และ label บอกว่าปุ่มทำอะไรกับรายการไหน ถ้ามีที่พอ ใช้ Button ที่มีข้อความ

import { IconButton } from "@bsru/ui";

ตัวอย่าง

label คือชื่อของปุ่ม

โปรแกรมอ่านหน้าจออ่าน label และแสดงเมื่อชี้ ระบุรายการด้วย เพราะในตารางมีปุ่มแบบเดียวกันหลายแถว

<IconButton icon={Eye} label="ดูรายละเอียด PR-2570-0001" />

ขนาด

ขนาดปกติ 44 พิกเซล เหมาะกับการแตะ size sm คือ 36 พิกเซล สำหรับแถวที่แน่น

<IconButton icon={Pencil} label="แก้ไข PR-2570-0002" />
<IconButton icon={Pencil} label="แก้ไข PR-2570-0002" size="sm" />

มีเส้นขอบ

outline เพิ่มเส้นขอบ เช่น เมื่อวางข้างช่องกรอก ให้ดูเป็นปุ่มคู่กับช่องนั้น ปุ่มคัดลอกใช้ CopyButton ซึ่งเปลี่ยนเป็นเครื่องหมายถูกเมื่อคัดลอกแล้ว

<IconButton icon={Printer} label="พิมพ์ PR-2570-0001" outline />

งานที่ลบ

tone danger ใช้กับปุ่มที่ลบ และต้องถามยืนยันใน Dialog ก่อนลบจริง

<IconButton icon={Trash} label="ลบ PR-2570-0004" tone="danger" />

ปุ่มเปิดปิด

หน้าจอเก็บสถานะไว้ใน state แล้วส่ง pressed ปุ่มจะบอกโปรแกรมอ่านหน้าจอว่าเปิดหรือปิดอยู่ label คงเดิมไม่ว่าจะเปิดหรือปิด ลองกด

const [pressed, setPressed] = useState(false);

<IconButton
  icon={pressed ? BellRing : Bell}
  label="ติดตาม PR-2570-0001"
  pressed={pressed}
  onClick={() => setPressed(!pressed)}
/>

ลิงก์

ส่ง href เมื่อปุ่มพาไปหน้าอื่น เช่น headerAction ของ Drawer ที่เปิดหน้าเต็มของรายการ

<IconButton
  icon={ExternalLink}
  label="เปิด PR-2570-0001 หน้าเต็ม"
  href="#pr-2570-0001"
/>

พร้อมตัวเลข

count แสดงตัวเลขสีแดงที่มุม เช่น รายการที่ยังไม่อ่าน เกิน 99 แสดงเป็น 99+ ตัวเลขเป็นส่วนหนึ่งของชื่อที่โปรแกรมอ่านหน้าจออ่าน

<IconButton icon={Bell} label="การแจ้งเตือน" count={8} />
<IconButton icon={Bell} label="การแจ้งเตือน" count={120} />

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

IconButton props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
icon* จำเป็นLucideIcon——
label* จำเป็นstring—The accessible name, already in the screen language. Screen readers read it, and it shows on hover.
tone"plain" | "danger""plain"danger for an action that deletes.
size"md" | "sm""md"sm is 36 pixels, for a dense row. md is 44 pixels, the touch size.
outlinebooleanfalseAdds a border, such as beside an input.
disabledbooleanfalse—
pressedboolean—For a toggle, whether it's on.
countnumber—A red number on its corner, such as unread items. Over 99 shows 99+. Leave it out, or pass 0, for none.
hrefstring—Makes it a link to this URL.
onClick(e: MouseEvent<HTMLElement>) => void——

ข้อควรปฏิบัติ

  • label จำเป็นเสมอ และส่งมาในภาษาของหน้าจออยู่แล้ว บอกการกระทำและรายการ เช่น ลบ PR-2570-0004
  • ใช้ไอคอนที่คนรู้จักเท่านั้น เช่น ปิด แก้ไข ลบ ถ้าความหมายไม่ชัด ใช้ Button ที่มีข้อความ
  • ปุ่มเปิดปิดใช้ pressed และคง label เดิม ไม่เปลี่ยนคำตามสถานะ
  • แป้นพิมพ์: Tab ไปที่ปุ่ม Enter หรือ Space กด

ซอร์สโค้ด: ui/src/button.tsx