ปุ่มไอคอน
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" />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | Eye | LucideIcon | — |
| label* จำเป็น | "ดูรายละเอียด PR-2570-0001" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
ขนาด
ขนาดปกติ 44 พิกเซล เหมาะกับการแตะ size sm คือ 36 พิกเซล สำหรับแถวที่แน่น
<IconButton icon={Pencil} label="แก้ไข PR-2570-0002" />
<IconButton icon={Pencil} label="แก้ไข PR-2570-0002" size="sm" />| Prop | ค่า 1 | ค่า 2 | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|---|
| icon* จำเป็น | Pencil | Pencil | LucideIcon | — |
| label* จำเป็น | "แก้ไข PR-2570-0002" | "แก้ไข PR-2570-0002" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| size | — | "sm" | "md" | "sm" ค่าที่ใช้ได้: "md""sm" | sm is 36 pixels, for a dense row. md is 44 pixels, the touch size. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — | |
มีเส้นขอบ
outline เพิ่มเส้นขอบ เช่น เมื่อวางข้างช่องกรอก ให้ดูเป็นปุ่มคู่กับช่องนั้น ปุ่มคัดลอกใช้ CopyButton ซึ่งเปลี่ยนเป็นเครื่องหมายถูกเมื่อคัดลอกแล้ว
<IconButton icon={Printer} label="พิมพ์ PR-2570-0001" outline />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | Printer | LucideIcon | — |
| label* จำเป็น | "พิมพ์ PR-2570-0001" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| outline | true | boolean | Adds a border, such as beside an input. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
งานที่ลบ
tone danger ใช้กับปุ่มที่ลบ และต้องถามยืนยันใน Dialog ก่อนลบจริง
<IconButton icon={Trash} label="ลบ PR-2570-0004" tone="danger" />| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | Trash | LucideIcon | — |
| label* จำเป็น | "ลบ PR-2570-0004" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| tone | "danger" | "plain" | "danger" ค่าที่ใช้ได้: "plain""danger" | danger for an action that deletes. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
ปุ่มเปิดปิด
หน้าจอเก็บสถานะไว้ใน state แล้วส่ง pressed ปุ่มจะบอกโปรแกรมอ่านหน้าจอว่าเปิดหรือปิดอยู่ label คงเดิมไม่ว่าจะเปิดหรือปิด ลองกด
const [pressed, setPressed] = useState(false);
<IconButton
icon={pressed ? BellRing : Bell}
label="ติดตาม PR-2570-0001"
pressed={pressed}
onClick={() => setPressed(!pressed)}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | pressed ? BellRing : Bell | LucideIcon | — |
| label* จำเป็น | "ติดตาม PR-2570-0001" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| pressed | pressed | boolean | For a toggle, whether it's on. |
| onClick | () => setPressed(!pressed) | (e: MouseEvent<HTMLElement>) => void | — |
ลิงก์
ส่ง href เมื่อปุ่มพาไปหน้าอื่น เช่น headerAction ของ Drawer ที่เปิดหน้าเต็มของรายการ
<IconButton
icon={ExternalLink}
label="เปิด PR-2570-0001 หน้าเต็ม"
href="#pr-2570-0001"
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| icon* จำเป็น | ExternalLink | LucideIcon | — |
| label* จำเป็น | "เปิด PR-2570-0001 หน้าเต็ม" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| href | "#pr-2570-0001" | string | Makes it a link to this URL. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — |
พร้อมตัวเลข
count แสดงตัวเลขสีแดงที่มุม เช่น รายการที่ยังไม่อ่าน เกิน 99 แสดงเป็น 99+ ตัวเลขเป็นส่วนหนึ่งของชื่อที่โปรแกรมอ่านหน้าจออ่าน
<IconButton icon={Bell} label="การแจ้งเตือน" count={8} />
<IconButton icon={Bell} label="การแจ้งเตือน" count={120} />| Prop | ค่า 1 | ค่า 2 | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|---|
| icon* จำเป็น | Bell | Bell | LucideIcon | — |
| label* จำเป็น | "การแจ้งเตือน" | "การแจ้งเตือน" | string | The accessible name, already in the screen language. Screen readers read it, and it shows on hover. |
| count | 8 | 120 | number | A red number on its corner, such as unread items. Over 99 shows 99+. Leave it out, or pass 0, for none. |
| onClick | ไม่ได้ส่งในตัวอย่างนี้ | (e: MouseEvent<HTMLElement>) => void | — | |
props ทั้งหมด
ตัวอย่างด้านบนใช้ 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. |
| outline | boolean | false | Adds a border, such as beside an input. |
| disabled | boolean | false | — |
| pressed | boolean | — | For a toggle, whether it's on. |
| count | number | — | A red number on its corner, such as unread items. Over 99 shows 99+. Leave it out, or pass 0, for none. |
| href | string | — | 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