Headless Progress indicator
part ของ ProgressIndicator ให้พฤติกรรมเดียวกับ Progress indicator แบบมี style แต่ไม่มี style เลย ได้แก่ รายการขั้นตอนแบบมีลำดับ ซึ่งสถานะ (เสร็จแล้ว ขั้นปัจจุบัน ยังไม่ถึง) ตาม index ปัจจุบัน aria-current="step" ที่ขั้นปัจจุบัน และเมื่อเป็นแบบ interactive จะมีปุ่มที่กระโดดไปยังขั้นนั้นได้
ส่วนที่คุณทำเองคือ markup ในแต่ละขั้น (ไอคอน label บรรทัดที่สอง) คำที่ screen reader อ่านสำหรับแต่ละสถานะ และ style ทั้งหมด part ต่าง ๆ บอกสถานะผ่าน attribute data-* ให้คุณใช้ใส่ style
ติดตั้ง
ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):
npm install @purpurabog/ui
import { ProgressIndicator } from '@purpurabog/ui/headless';
วิธีใช้
import { ProgressIndicator } from '@purpurabog/ui/headless';
const steps = ['รายละเอียดสัญญา', 'ผู้ลงนาม', 'ตรวจทาน', 'ส่ง'];
function StepIcon() {
const { state } = ProgressIndicator.useStepContext();
return (
<svg viewBox="0 0 16 16" aria-hidden="true" className="size-4 shrink-0 fill-interactive group-data-incomplete:fill-ink">
{state === 'complete' ? <path d="M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 13a6 6 0 1 1 0-12 6 6 0 0 1 0 12Zm-1-2.6-2.7-2.7.7-.7L7 10l4-4 .7.7z" /> : null}
{state === 'current' ? <path d="M8 1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Zm0 13a6 6 0 0 1 0-12z" /> : null}
{state === 'incomplete' ? <path d="M8 2a6 6 0 1 1 0 12A6 6 0 0 1 8 2m0-1a7 7 0 1 0 0 14A7 7 0 0 0 8 1Z" /> : null}
</svg>
);
}
const words = { complete: 'เสร็จแล้ว', current: 'ขั้นปัจจุบัน', incomplete: 'ยังไม่ถึง' };
function StateWords() {
const { state } = ProgressIndicator.useStepContext();
return <span className="sr-only">{words[state]}</span>;
}
export default function Example() {
return (
<ProgressIndicator.Root currentIndex={1} className="flex">
{steps.map((label) => (
<ProgressIndicator.Step key={label} className="w-32 border-t-2 border-interactive data-incomplete:border-hairline">
<ProgressIndicator.StepButton className="group flex w-full gap-2 pt-2 pe-4 text-start text-sm text-ink">
<StepIcon />
{label}
<StateWords />
</ProgressIndicator.StepButton>
</ProgressIndicator.Step>
))}
</ProgressIndicator.Root>
);
}
ลูกโดยตรงของ Root แต่ละตัวคือหนึ่งขั้น นับตามตำแหน่ง ตัวแรกคือ index 0 useStepContext() ให้ state แก่ component ที่อยู่ในขั้น ไอคอนและคำสำหรับ screen reader จึงตามสถานะได้
โครงสร้าง
<ProgressIndicator.Root currentIndex={0}>
<ProgressIndicator.Step>
<ProgressIndicator.StepButton />
</ProgressIndicator.Step>
</ProgressIndicator.Root>
ขั้นที่ไม่ถูกต้อง ปิดใช้งาน และเสร็จแล้ว
flag สามตัวที่ Step เพิ่มจากสถานะที่ได้จาก index:
complete ทำให้ขั้นนั้นเสร็จแล้วไม่ว่า index ปัจจุบันจะเป็นเท่าไร (data-complete)
invalid ตั้ง data-invalid ให้คุณแสดงคำเตือนตาม style ของคุณ
disabled ทำให้ปุ่มของขั้นเป็น disabled (data-disabled) คลิกหรือโฟกัสไม่ได้
import { ProgressIndicator } from '@purpurabog/ui/headless';
const steps = [
{ label: 'รายละเอียดสัญญา' },
{ label: 'ผู้ลงนาม' },
{ label: 'ไฟล์แนบ', complete: true },
{ label: 'ตรวจทาน', invalid: true },
{ label: 'ส่ง', disabled: true },
];
const words = { complete: 'เสร็จแล้ว', current: 'ขั้นปัจจุบัน', incomplete: 'ยังไม่ถึง' };
function StepWords() {
const { state, invalid } = ProgressIndicator.useStepContext();
return <span className="sr-only">{invalid ? 'ไม่ถูกต้อง' : words[state]}</span>;
}
export default function Example() {
return (
<ProgressIndicator.Root currentIndex={1} className="flex flex-wrap">
{steps.map(({ label, ...flags }) => (
<ProgressIndicator.Step key={label} {...flags} className="w-32 border-t-2 border-interactive data-incomplete:border-hairline">
<ProgressIndicator.StepButton className="flex w-full items-center gap-2 pt-2 pe-4 text-start text-sm text-ink data-current:font-semibold data-invalid:text-error-text data-disabled:text-ink-disabled">
<span aria-hidden="true" className="flex size-4 shrink-0 items-center justify-center rounded-full border border-current text-[10px]">
{flags.complete ? '✓' : null}
</span>
{label}
<StepWords />
</ProgressIndicator.StepButton>
</ProgressIndicator.Step>
))}
</ProgressIndicator.Root>
);
}
ให้ผู้ใช้กระโดดไปขั้นอื่น
interactive ทำให้ปุ่มอยู่ในลำดับ Tab คลิกที่ขั้นที่ไม่ใช่ขั้นปัจจุบันและไม่ได้ปิดใช้งาน จะทำให้ขั้นนั้นเป็นขั้นปัจจุบัน และเรียก onChange พร้อม index (data-clickable บอกว่าขั้นไหนคลิกได้) เก็บ index เองด้วย currentIndex หรือให้ defaultCurrentIndex แล้วให้ Root เก็บเอง
import { useState } from 'react';
import { ProgressIndicator } from '@purpurabog/ui/headless';
const steps = ['รายละเอียดสัญญา', 'ผู้ลงนาม', 'ตรวจทาน', 'ส่ง'];
export default function Example() {
const [step, setStep] = useState(2);
return (
<div className="grid gap-4">
<ProgressIndicator.Root interactive currentIndex={step} onChange={setStep} className="flex">
{steps.map((label) => (
<ProgressIndicator.Step key={label} className="w-32 border-t-2 border-interactive data-incomplete:border-hairline">
<ProgressIndicator.StepButton className="flex w-full pt-2 pe-4 text-start text-sm text-ink focus-visible:outline-2 focus-visible:outline-focus data-clickable:cursor-pointer data-clickable:text-link data-clickable:hover:underline data-current:font-semibold">
{label}
</ProgressIndicator.StepButton>
</ProgressIndicator.Step>
))}
</ProgressIndicator.Root>
<p className="text-xs text-ink-muted">ขั้นปัจจุบัน: {step}</p>
</div>
);
}
ปุ่มย้อนกลับและถัดไป
ถ้าไม่มี interactive ขั้นตอนจะไม่ตอบสนองต่อการคลิกและไม่อยู่ในลำดับ Tab ปุ่มของคุณเองจะพาไปทีละขั้นโดยเปลี่ยน currentIndex
import { useState } from 'react';
import { ProgressIndicator } from '@purpurabog/ui/headless';
const steps = ['รายละเอียดสัญญา', 'ผู้ลงนาม', 'ตรวจทาน', 'ส่ง'];
export default function Example() {
const [step, setStep] = useState(0);
return (
<div className="grid gap-4">
<ProgressIndicator.Root currentIndex={step} className="flex">
{steps.map((label) => (
<ProgressIndicator.Step key={label} className="w-32 border-t-2 border-interactive data-incomplete:border-hairline">
<ProgressIndicator.StepButton className="flex w-full pt-2 pe-4 text-start text-sm text-ink-muted data-complete:text-ink data-current:font-semibold data-current:text-ink">
{label}
</ProgressIndicator.StepButton>
</ProgressIndicator.Step>
))}
</ProgressIndicator.Root>
<p className="text-sm text-ink">ขั้นที่ {step + 1} จาก {steps.length}: {steps[step]}</p>
<div className="flex gap-px">
<button type="button" disabled={step === 0} onClick={() => setStep(step - 1)} className="h-10 bg-secondary px-4 text-sm text-on-primary hover:bg-secondary-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus disabled:bg-button-disabled disabled:text-on-color-disabled">ย้อนกลับ</button>
<button type="button" disabled={step === steps.length - 1} onClick={() => setStep(step + 1)} className="h-10 bg-interactive px-4 text-sm text-on-primary hover:bg-blue-hover focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-focus disabled:bg-button-disabled disabled:text-on-color-disabled">ถัดไป</button>
</div>
</div>
);
}
แนวตั้ง
vertical ตั้ง data-orientation="vertical" ที่ root ทุกขั้น และทุกปุ่ม (ไม่อย่างนั้นเป็น horizontal) layout เป็นของคุณ ในตัวอย่างนี้เส้นวิ่งลงมาตามขอบด้านหน้า
import { ProgressIndicator } from '@purpurabog/ui/headless';
const steps = [
{ label: 'ส่งคำขอแล้ว', note: '1 ต.ค. 09:12' },
{ label: 'ฝ่ายกฎหมายตรวจ', note: 'กำลังดำเนินการ' },
{ label: 'กรรมการผู้จัดการอนุมัติ' },
{ label: 'ลงนาม' },
];
export default function Example() {
return (
<ProgressIndicator.Root vertical currentIndex={1} className="flex data-[orientation=horizontal]:flex-row data-[orientation=vertical]:flex-col">
{steps.map((s) => (
<ProgressIndicator.Step key={s.label} className="min-h-14 border-s-2 border-interactive data-incomplete:border-hairline">
<ProgressIndicator.StepButton className="flex w-full flex-col ps-4 pt-1 text-start text-sm text-ink data-current:font-semibold">
{s.label}
{s.note ? <span className="text-xs font-normal text-ink-muted">{s.note}</span> : null}
</ProgressIndicator.StepButton>
</ProgressIndicator.Step>
))}
</ProgressIndicator.Root>
);
}
ใส่ style
import { ProgressIndicator } from '@purpurabog/ui/headless';
const steps = ['รายละเอียด', 'ผู้ลงนาม', 'ตรวจทาน', 'ส่ง'];
export default function Example() {
return (
<ProgressIndicator.Root interactive defaultCurrentIndex={1} className="flex">
{steps.map((label, i) => (
<ProgressIndicator.Step key={label} invalid={i === 2} className="flex-1 border-t-4 border-interactive data-incomplete:border-hairline data-invalid:border-error">
<ProgressIndicator.StepButton className="w-full pt-2 pe-4 text-start text-sm text-ink-muted focus-visible:outline-2 focus-visible:outline-focus data-complete:text-ink data-current:font-semibold data-current:text-ink data-invalid:text-error-text data-clickable:cursor-pointer data-clickable:hover:text-link-hover">
{i + 1}. {label}
</ProgressIndicator.StepButton>
</ProgressIndicator.Step>
))}
</ProgressIndicator.Root>
);
}
/* className="step" ที่ Step, "step-button" ที่ StepButton */
.step { border-top: 4px solid #0f62fe; }
.step[data-incomplete] { border-top-color: #e0e0e0; }
.step[data-invalid] { border-top-color: #da1e28; }
.step-button[data-current] { font-weight: 600; }
.step-button[data-clickable] { cursor: pointer; }
.step-button[data-disabled] { color: #c6c6c6; }
Parts
ProgressIndicator.Root
<ol> ลูกโดยตรงแต่ละตัวคือหนึ่งขั้นตามตำแหน่ง component ของคุณเองที่ render Step จึงใช้เป็นลูกได้
| Prop |
Type |
Default |
|
currentIndex |
number |
|
index ของขั้นปัจจุบัน เริ่มที่ 0 (controlled) |
defaultCurrentIndex |
number |
0 |
index เริ่มต้น (uncontrolled) |
onChange |
(index: number) => void |
|
หลังผู้ใช้กระโดดไปขั้นอื่น |
interactive |
boolean |
false |
ปุ่มของขั้นรับ Tab และกระโดดเมื่อคลิก |
vertical |
boolean |
false |
ตั้ง data-orientation="vertical" |
| Attribute |
เมื่อ |
data-orientation |
horizontal หรือ vertical |
ProgressIndicator.useProgressIndicatorContext() คืน { currentIndex, vertical, interactive, pick } ส่วน ProgressIndicator.stepState(index, currentIndex, complete?) คืนสถานะที่ขั้นตำแหน่ง index จะได้
ProgressIndicator.Step
<li>: หนึ่งขั้น
| Prop |
Type |
Default |
|
complete |
boolean |
false |
เสร็จแล้วไม่ว่า index ปัจจุบันจะเป็นเท่าไร |
invalid |
boolean |
false |
ตั้ง data-invalid |
disabled |
boolean |
false |
ปุ่มของขั้นเป็น disabled |
| Attribute |
เมื่อ |
data-complete / data-current / data-incomplete |
มีหนึ่งตัว: สถานะของขั้น |
data-invalid |
ตั้ง invalid |
data-disabled |
ตั้ง disabled |
data-clickable |
คลิกที่ปุ่มจะกระโดดมาที่นี่: interactive ไม่ได้ปิดใช้งาน และไม่ใช่ขั้นปัจจุบัน |
data-orientation |
horizontal หรือ vertical |
ProgressIndicator.useStepContext() คืน { index, state, invalid, disabled, vertical, interactive, clickable, pick } ให้ component ที่อยู่ในขั้น
<button type="button"> ของขั้นปัจจุบันมี aria-current="step" ปุ่มอยู่นอกลำดับ Tab (tabindex="-1") ถ้า root ไม่ได้เป็น interactive และเป็น disabled (พร้อม aria-disabled) เมื่อขั้นปิดใช้งาน รับ prop ของปุ่มทุกตัว ยกเว้น type และมี data attribute เหมือน Step ของตัวเอง
คีย์บอร์ดและ accessibility
| ปุ่ม |
ทำอะไร |
| Tab / Shift+Tab |
เมื่อเป็น interactive: ย้ายไปมาระหว่างปุ่มของขั้น (ข้ามปุ่มที่ปิดใช้งาน) ถ้าไม่ได้เป็น interactive ปุ่มจะไม่รับ Tab |
| Enter, Space |
เมื่อเป็น interactive: กระโดดไปขั้นที่โฟกัสอยู่ (ขั้นปัจจุบันไม่มีผล) |
part ตั้ง aria-current="step" disabled และ aria-disabled และลำดับ Tab รายการเป็น <ol> ที่มี <li>
ส่วนที่คุณเพิ่มเอง:
- ข้อความ label ใน
StepButton แต่ละอัน
- สถานะเป็นคำสำหรับ screen reader ("เสร็จแล้ว" "ไม่ถูกต้อง" …) เหมือนข้อความ
sr-only ในตัวอย่างแรก ไอคอนอย่างเดียวไม่ได้บอกอะไร
- หัวข้อที่บอกว่างานคืออะไร และบอกขั้นปัจจุบันเป็นข้อความถ้าขั้นตอนมีขนาดเล็ก