Progress indicator
Progress indicator บอกว่าผู้ใช้อยู่ตรงไหนของงานที่มีหลายขั้นตอน ขั้นไหนเสร็จแล้ว ขั้นไหนเป็นขั้นปัจจุบัน และอะไรจะตามมา ใช้เหนือฟอร์มหลายขั้นตอนหรือ wizard
HTMLReactReact + Tailwind<ui-progress-indicator current-index="1">
<ui-progress-step label="รายละเอียดสัญญา"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
<ui-progress-step label="ตรวจทาน"></ui-progress-step>
<ui-progress-step label="ส่ง"></ui-progress-step>
</ui-progress-indicator>
<UiProgressIndicator currentIndex={1}>
<UiProgressStep label="รายละเอียดสัญญา" />
<UiProgressStep label="ผู้ลงนาม" />
<UiProgressStep label="ตรวจทาน" />
<UiProgressStep label="ส่ง" />
</UiProgressIndicator>
<ProgressIndicator currentIndex={1}>
<ProgressStep label="รายละเอียดสัญญา" />
<ProgressStep label="ผู้ลงนาม" />
<ProgressStep label="ตรวจทาน" />
<ProgressStep label="ส่ง" />
</ProgressIndicator>
ติดตั้ง
npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/progress-indicator
npx shadcn@4 add @kanok-ui/tw-progress-indicator
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/progress-indicator.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/progress-indicator/progress-indicator-skeleton.ts
# components/ui/primitives/progress-indicator/progress-indicator.ts
# components/ui/primitives/progress-indicator/progress-step.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# shared-element, shared-localize, shared-skeleton, tokens
# React + Tailwind
# 1. ติดตั้ง dependency
npm install @purpurabog/ui@^2.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/tw-progress-indicator.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/progress-indicator.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# tw-i18n, tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/progress-indicator';</script>
<ui-progress-indicator current-index="0">
<ui-progress-step label="รายละเอียดสัญญา"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
</ui-progress-indicator>
import { UiProgressIndicator, UiProgressStep } from '@purpurabog/ui/react';
<UiProgressIndicator currentIndex={0}>
<UiProgressStep label="รายละเอียดสัญญา" />
<UiProgressStep label="ผู้ลงนาม" />
</UiProgressIndicator>
import { ProgressIndicator, ProgressStep } from '@/components/ui/react-tw/progress-indicator';
<ProgressIndicator currentIndex={0}>
<ProgressStep label="รายละเอียดสัญญา" />
<ProgressStep label="ผู้ลงนาม" />
</ProgressIndicator>
สถานะของขั้นตอน
current-index (เริ่มที่ 0) กำหนดสถานะของแต่ละขั้น: ขั้นก่อนหน้าเสร็จแล้ว (เครื่องหมายถูก) ขั้นที่ตำแหน่งนั้นเป็นขั้นปัจจุบัน (วงกลมครึ่งวง) ขั้นหลังจากนั้นยังไม่ถึง (วงกลมว่าง) มี flag สองตัวที่ใส่เพิ่มที่ขั้นได้:
invalid แสดงไอคอนเตือนสีแดง แปลว่าขั้นนี้มี error ต้องแก้
disabled ทำให้ขั้นเป็นสีเทา คลิกไม่ได้ แม้ indicator จะเป็นแบบ interactive
HTMLReactReact + Tailwind<ui-progress-indicator current-index="1">
<ui-progress-step label="รายละเอียดสัญญา"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
<ui-progress-step label="ตรวจทาน" invalid></ui-progress-step>
<ui-progress-step label="ส่ง" disabled></ui-progress-step>
</ui-progress-indicator>
<UiProgressIndicator currentIndex={1}>
<UiProgressStep label="รายละเอียดสัญญา" />
<UiProgressStep label="ผู้ลงนาม" />
<UiProgressStep label="ตรวจทาน" invalid />
<UiProgressStep label="ส่ง" disabled />
</UiProgressIndicator>
<ProgressIndicator currentIndex={1}>
<ProgressStep label="รายละเอียดสัญญา" />
<ProgressStep label="ผู้ลงนาม" />
<ProgressStep label="ตรวจทาน" invalid />
<ProgressStep label="ส่ง" disabled />
</ProgressIndicator>
ข้อความรอง
secondary-label เพิ่มบรรทัดตัวเล็กใต้ label เช่น "ไม่บังคับ" วันที่ หรือจำนวน
HTMLReactReact + Tailwind<ui-progress-indicator current-index="2">
<ui-progress-step label="คำขอ" secondary-label="1 ต.ค."></ui-progress-step>
<ui-progress-step label="ฝ่ายกฎหมายตรวจ" secondary-label="3 ต.ค."></ui-progress-step>
<ui-progress-step label="ไฟล์แนบ" secondary-label="ไม่บังคับ"></ui-progress-step>
<ui-progress-step label="อนุมัติ"></ui-progress-step>
</ui-progress-indicator>
<UiProgressIndicator currentIndex={2}>
<UiProgressStep label="คำขอ" secondaryLabel="1 ต.ค." />
<UiProgressStep label="ฝ่ายกฎหมายตรวจ" secondaryLabel="3 ต.ค." />
<UiProgressStep label="ไฟล์แนบ" secondaryLabel="ไม่บังคับ" />
<UiProgressStep label="อนุมัติ" />
</UiProgressIndicator>
<ProgressIndicator currentIndex={2}>
<ProgressStep label="คำขอ" secondaryLabel="1 ต.ค." />
<ProgressStep label="ฝ่ายกฎหมายตรวจ" secondaryLabel="3 ต.ค." />
<ProgressStep label="ไฟล์แนบ" secondaryLabel="ไม่บังคับ" />
<ProgressStep label="อนุมัติ" />
</ProgressIndicator>
เสร็จไม่ตามลำดับ
complete ที่ขั้นทำให้ขั้นนั้นเสร็จแล้วไม่ว่า current-index จะเป็นเท่าไร ใช้เมื่อผู้ใช้ทำขั้นตอนไม่เรียงลำดับได้ ในตัวอย่างนี้ไฟล์แนบส่งมาแล้ว ขณะที่ผู้ใช้ยังอยู่ที่ขั้นผู้ลงนาม
HTMLReactReact + Tailwind<ui-progress-indicator current-index="1">
<ui-progress-step label="รายละเอียดสัญญา"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
<ui-progress-step label="ไฟล์แนบ" complete></ui-progress-step>
<ui-progress-step label="ส่ง"></ui-progress-step>
</ui-progress-indicator>
<UiProgressIndicator currentIndex={1}>
<UiProgressStep label="รายละเอียดสัญญา" />
<UiProgressStep label="ผู้ลงนาม" />
<UiProgressStep label="ไฟล์แนบ" complete />
<UiProgressStep label="ส่ง" />
</UiProgressIndicator>
<ProgressIndicator currentIndex={1}>
<ProgressStep label="รายละเอียดสัญญา" />
<ProgressStep label="ผู้ลงนาม" />
<ProgressStep label="ไฟล์แนบ" complete />
<ProgressStep label="ส่ง" />
</ProgressIndicator>
ให้ผู้ใช้กระโดดไปขั้นอื่น
interactive ทำให้ขั้นตอนเป็นปุ่ม คลิกที่ขั้น (ที่ไม่ใช่ขั้นปัจจุบันและไม่ได้ปิดใช้งาน) จะทำให้ขั้นนั้นเป็นขั้นปัจจุบัน และเกิด ui-change พร้อม detail.index ใน React + Tailwind onChange ได้ index ใช้เมื่อย้อนกลับไปขั้นก่อนหน้าได้อย่างปลอดภัย เช่น แก้รายละเอียดก่อนส่ง
HTMLReactReact + Tailwind<ui-progress-indicator id="progress-jump" current-index="2" interactive>
<ui-progress-step label="รายละเอียดสัญญา"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
<ui-progress-step label="ตรวจทาน"></ui-progress-step>
<ui-progress-step label="ส่ง" disabled></ui-progress-step>
</ui-progress-indicator>
<p>ขั้นปัจจุบัน: <output id="progress-jump-out">2</output></p>
<script type="module">
const steps = document.getElementById('progress-jump');
const out = document.getElementById('progress-jump-out');
steps.addEventListener('ui-change', (event) => (out.value = event.detail.index));
</script>
const [step, setStep] = useState(2);
<UiProgressIndicator currentIndex={step} interactive onUiChange={(e) => setStep(e.detail.index)}>
<UiProgressStep label="รายละเอียดสัญญา" />
<UiProgressStep label="ผู้ลงนาม" />
<UiProgressStep label="ตรวจทาน" />
<UiProgressStep label="ส่ง" disabled />
</UiProgressIndicator>
<p>ขั้นปัจจุบัน: <output>{step}</output></p>
const [step, setStep] = useState(2);
<ProgressIndicator currentIndex={step} interactive onChange={setStep}>
<ProgressStep label="รายละเอียดสัญญา" />
<ProgressStep label="ผู้ลงนาม" />
<ProgressStep label="ตรวจทาน" />
<ProgressStep label="ส่ง" disabled />
</ProgressIndicator>
<p>ขั้นปัจจุบัน: <output>{step}</output></p>
React + Tailwind รับ defaultCurrentIndex ด้วย เมื่อไม่ต้องเก็บขั้นปัจจุบันเอง
ปุ่มย้อนกลับและถัดไป
ส่วนใหญ่หน้าจะพาผู้ใช้ไปทีละขั้นเอง: ตั้ง current-index จากปุ่มย้อนกลับและถัดไปของคุณ indicator แค่แสดงตำแหน่ง ไม่เปลี่ยนเองถ้าไม่ได้เป็น interactive
ขั้นที่ 1 จาก 4: รายละเอียดสัญญา
ย้อนกลับ
ถัดไป
HTMLReactReact + Tailwind<ui-progress-indicator id="progress-wizard" current-index="0">
<ui-progress-step label="รายละเอียดสัญญา"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
<ui-progress-step label="ตรวจทาน"></ui-progress-step>
<ui-progress-step label="ส่ง"></ui-progress-step>
</ui-progress-indicator>
<p id="progress-wizard-text">ขั้นที่ 1 จาก 4: รายละเอียดสัญญา</p>
<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="progress-wizard-back" kind="secondary" disabled>ย้อนกลับ</ui-button>
<ui-button id="progress-wizard-next">ถัดไป</ui-button>
</div>
<script type="module">
const indicator = document.getElementById('progress-wizard');
const text = document.getElementById('progress-wizard-text');
const back = document.getElementById('progress-wizard-back');
const next = document.getElementById('progress-wizard-next');
const labels = [...indicator.querySelectorAll('ui-progress-step')].map((step) => step.label);
const go = (index) => {
indicator.currentIndex = index;
text.textContent = `ขั้นที่ ${index + 1} จาก ${labels.length}: ${labels[index]}`;
back.disabled = index === 0;
next.disabled = index === labels.length - 1;
};
back.addEventListener('click', () => go(indicator.currentIndex - 1));
next.addEventListener('click', () => go(indicator.currentIndex + 1));
</script>
const labels = ['รายละเอียดสัญญา', 'ผู้ลงนาม', 'ตรวจทาน', 'ส่ง'];
const [step, setStep] = useState(0);
<UiProgressIndicator currentIndex={step}>
{labels.map((label) => <UiProgressStep key={label} label={label} />)}
</UiProgressIndicator>
<p>ขั้นที่ {step + 1} จาก {labels.length}: {labels[step]}</p>
<UiButton kind="secondary" disabled={step === 0} onClick={() => setStep(step - 1)}>ย้อนกลับ</UiButton>
<UiButton disabled={step === labels.length - 1} onClick={() => setStep(step + 1)}>ถัดไป</UiButton>
const labels = ['รายละเอียดสัญญา', 'ผู้ลงนาม', 'ตรวจทาน', 'ส่ง'];
const [step, setStep] = useState(0);
<ProgressIndicator currentIndex={step}>
{labels.map((label) => <ProgressStep key={label} label={label} />)}
</ProgressIndicator>
<p>ขั้นที่ {step + 1} จาก {labels.length}: {labels[step]}</p>
<Button kind="secondary" disabled={step === 0} onClick={() => setStep(step - 1)}>ย้อนกลับ</Button>
<Button disabled={step === labels.length - 1} onClick={() => setStep(step + 1)}>ถัดไป</Button>
แนวตั้ง
vertical เรียงขั้นตอนลงมา ใช้ในคอลัมน์แคบข้างฟอร์ม หรือเมื่อ label ยาว
HTMLReactReact + Tailwind<ui-progress-indicator current-index="1" vertical>
<ui-progress-step label="ส่งคำขอแล้ว" secondary-label="1 ต.ค. 09:12"></ui-progress-step>
<ui-progress-step label="ฝ่ายกฎหมายตรวจ" secondary-label="กำลังดำเนินการ"></ui-progress-step>
<ui-progress-step label="กรรมการผู้จัดการอนุมัติ"></ui-progress-step>
<ui-progress-step label="ลงนาม"></ui-progress-step>
</ui-progress-indicator>
<UiProgressIndicator currentIndex={1} vertical>
<UiProgressStep label="ส่งคำขอแล้ว" secondaryLabel="1 ต.ค. 09:12" />
<UiProgressStep label="ฝ่ายกฎหมายตรวจ" secondaryLabel="กำลังดำเนินการ" />
<UiProgressStep label="กรรมการผู้จัดการอนุมัติ" />
<UiProgressStep label="ลงนาม" />
</UiProgressIndicator>
<ProgressIndicator currentIndex={1} vertical>
<ProgressStep label="ส่งคำขอแล้ว" secondaryLabel="1 ต.ค. 09:12" />
<ProgressStep label="ฝ่ายกฎหมายตรวจ" secondaryLabel="กำลังดำเนินการ" />
<ProgressStep label="กรรมการผู้จัดการอนุมัติ" />
<ProgressStep label="ลงนาม" />
</ProgressIndicator>
กระจายเต็มความกว้าง
ปกติแต่ละขั้นกว้าง 128px และ label ที่ยาวกว่าจะขึ้นบรรทัดใหม่ที่ 88px (Carbon ตัดด้วย ... แต่ที่นี่ไม่ซ่อนชื่อขั้นตอน) space-equally ยืดขั้นตอนให้แบ่งความกว้างทั้งหมดเท่า ๆ กัน ใช้คู่กับ vertical ไม่ได้ผล
HTMLReactReact + Tailwind<ui-progress-indicator current-index="2" space-equally>
<ui-progress-step label="รายละเอียด"></ui-progress-step>
<ui-progress-step label="ผู้ลงนาม"></ui-progress-step>
<ui-progress-step label="ตรวจทาน"></ui-progress-step>
<ui-progress-step label="ส่ง"></ui-progress-step>
</ui-progress-indicator>
<UiProgressIndicator currentIndex={2} spaceEqually>
<UiProgressStep label="รายละเอียด" />
<UiProgressStep label="ผู้ลงนาม" />
<UiProgressStep label="ตรวจทาน" />
<UiProgressStep label="ส่ง" />
</UiProgressIndicator>
<ProgressIndicator currentIndex={2} spaceEqually>
<ProgressStep label="รายละเอียด" />
<ProgressStep label="ผู้ลงนาม" />
<ProgressStep label="ตรวจทาน" />
<ProgressStep label="ส่ง" />
</ProgressIndicator>
Skeleton
ระหว่างโหลดขั้นตอน ให้แสดง ui-progress-indicator-skeleton: ขั้นว่าง steps ขั้น (ค่าเริ่มต้น 4) เรียงเป็นแถว หรือเรียงลงมาด้วย vertical skeleton ถูกซ่อนจาก screen reader จึงควรบอกว่า "กำลังโหลด" ที่ container (เช่น aria-busy)
HTMLReactReact + Tailwind<ui-progress-indicator-skeleton></ui-progress-indicator-skeleton>
<ui-progress-indicator-skeleton steps="3" vertical></ui-progress-indicator-skeleton>
import { UiProgressIndicatorSkeleton } from '@purpurabog/ui/react';
<UiProgressIndicatorSkeleton />
<UiProgressIndicatorSkeleton steps={3} vertical />
import { ProgressIndicatorSkeleton } from '@/components/ui/react-tw/progress-indicator';
<ProgressIndicatorSkeleton />
<ProgressIndicatorSkeleton steps={3} vertical />
สำหรับ web component ให้ import จาก @purpurabog/ui/progress-indicator-skeleton
คีย์บอร์ด
| ปุ่ม |
ทำอะไร |
| Tab / Shift+Tab |
เมื่อเป็น interactive: ย้ายไปมาระหว่างขั้นตอน (ข้ามขั้นที่ปิดใช้งาน) ถ้าไม่ได้เป็น interactive ขั้นตอนจะไม่รับ Tab |
| Enter, Space |
เมื่อเป็น interactive: ทำให้ขั้นที่โฟกัสอยู่เป็นขั้นปัจจุบัน (ขั้นปัจจุบันเองไม่มีผล) |
Accessibility
- indicator เป็น list และแต่ละขั้นเป็น list item เหมือน
<ul> / <li> ของ Carbon
- ขั้นปัจจุบันมี
aria-current="step" หลัง label แต่ละขั้น screen reader จะอ่านสถานะ: "เสร็จแล้ว" "ขั้นปัจจุบัน" "ยังไม่ถึง" หรือ "ไม่ถูกต้อง" (ภาษาอังกฤษเมื่อ lang="en")
- ปุ่มของขั้นที่ปิดใช้งานเป็น disabled และมี
aria-disabled
- indicator แสดงความคืบหน้า แต่ไม่ได้บอกว่างานคืออะไร ให้ใส่หัวข้อไว้ด้านบน และบอกขั้นปัจจุบันเป็นข้อความด้วย (เช่น "ขั้นที่ 2 จาก 4" ด้านบน) สำหรับคนที่มองไม่เห็นไอคอน
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Progress indicator
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-progress-indicator> · React UiProgressIndicator
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
current-index |
currentIndex |
number |
0 |
Index of the current step (0-based). |
vertical |
vertical |
boolean |
false |
|
space-equally |
spaceEqually |
boolean |
false |
Stretch the steps to share the width (horizontal only). |
interactive |
interactive |
boolean |
false |
Let the user jump between steps (fires ui-change). |
Method
| Method |
คำอธิบาย |
focus(options) |
Interactive indicators: focus the current step. |
Event
| Event |
React |
คำอธิบาย |
ui-change |
onUiChange |
detail: { index } when the user picks another step |
Slot
| Slot |
คำอธิบาย |
| (ค่าเริ่มต้น) |
<ui-progress-step> elements |
CSS part
| Part |
คำอธิบาย |
base |
The list |
<ui-progress-step> · React UiProgressStep
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
label |
label |
string |
'' |
|
secondary-label |
secondaryLabel |
string |
'' |
Smaller text under the label (Carbon secondaryLabel). |
complete |
complete |
boolean |
false |
Marks the step done regardless of current-index. |
invalid |
invalid |
boolean |
false |
|
disabled |
disabled |
boolean |
false |
|
data-state |
state |
ProgressStepState |
'incomplete' |
Set by the indicator: the resolved state. |
Property
| Property |
ชนิด |
คำอธิบาย |
clickable |
boolean |
Set by the indicator: whether steps are clickable. |
index |
number |
Set by the indicator: this step's position. |
Event
| Event |
React |
คำอธิบาย |
ui-step-click |
ไม่มี (ใช้ ref) |
|
Slot
| Slot |
คำอธิบาย |
| (ค่าเริ่มต้น) |
Nothing; use label and secondary-label |
CSS part
| Part |
คำอธิบาย |
button |
The clickable step |
icon |
The state icon |
label |
The label |
secondary-label |
The secondary label |
line |
The progress line |
CSS custom property
| Custom property |
ค่าเริ่มต้น |
คำอธิบาย |
--ui-progress-step-flex |
|
/ --ui-progress-step-min-width - A horizontal step's flex and least width (default 0 1 auto, 112px: Carbon's 128px steps; Kanok: 1 1 0, 120px) |
--ui-progress-label-max-width |
|
/ --ui-progress-label-gap - Its label's max-width and end margin (default 88px, 0) |
--ui-progress-label-white-space |
|
/ --ui-progress-label-overflow / --ui-progress-label-text-overflow - Its label on one line (default normal, visible, clip: it wraps; Kanok: one line, an ellipsis) |
<ui-progress-indicator-skeleton> · React UiProgressIndicatorSkeleton
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
steps |
steps |
number |
4 |
|
vertical |
vertical |
boolean |
false |
|
CSS part
| Part |
คำอธิบาย |
step |
Each step |
label |
Each label bar |