Loading
Spinner บอกว่าหน้าจอหรือพื้นที่หนึ่งกำลังรอข้อมูล spinner ของ Carbon ขนาด 88px ส่วน small ขนาด 16px มีรางสีเทา คลุมทั้งหน้า (overlay) หรือคลุมแค่พื้นที่เดียว (contained) ก็ได้ และรอก่อนแสดงได้ งานที่เสร็จเร็วจึงไม่มี spinner กะพริบ
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 32px; align-items: center">
<ui-loading></ui-loading>
<ui-loading small></ui-loading>
</div>
<UiLoading />
<UiLoading small />
<Loading />
<Loading small />
ติดตั้ง
npmshadcn · Web componentshadcn · React + Tailwindคัดลอกเองnpm install @purpurabog/ui
npx shadcn@4 add @kanok-ui/loading
npx shadcn@4 add @kanok-ui/tw-loading
# Web component
# 1. ติดตั้ง dependency
npm install lit@^3.3.0
# 2. คัดลอก "content" ของแต่ละไฟล์จาก
# https://cdn.jsdelivr.net/npm/@purpurabog/ui@2.3.0/registry/loading.json
# ไปไว้ที่ target ของมัน:
# components/ui/primitives/loading/loading.ts
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-spin-delay, shared-element, shared-field, shared-localize, 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-loading.json
# ไปไว้ที่ target ของมัน:
# components/ui/react-tw/loading.tsx
# 3. ทำแบบเดียวกันกับ item ที่มันใช้ (registry/<name>.json):
# core-status-indicator, tw-i18n, tw-theme, tw-utils
npm ให้ทุก component จากแพ็กเกจ ส่วน shadcn คัดลอกซอร์สเข้าแอปของคุณแทน เป็นสำเนาที่ล็อกเวอร์ชันนี้และแก้เองได้ คำสั่ง shadcn ต้องตั้ง registry @kanok-ui ใน components.json ครั้งเดียวก่อน ดู ติดตั้ง
วิธีใช้
HTMLReactReact + Tailwind<script type="module">import '@purpurabog/ui/loading';</script>
<ui-loading></ui-loading>
import { UiLoading } from '@purpurabog/ui/react';
<UiLoading />
import { Loading } from '@/components/ui/react-tw/loading';
<Loading />
Loading, inline loading, progress bar หรือ skeleton?
- Loading: ใช้ทั้งหน้าจอหรือพื้นที่หนึ่งไม่ได้จนกว่าข้อมูลจะมา
- Inline loading: สถานะเล็ก ๆ ข้างสิ่งที่กำลังบันทึกหรือส่ง แล้วจบด้วย "เสร็จแล้ว" หรือ "ไม่สำเร็จ"
- Progress bar: งานที่รู้ขนาด เช่น อัปโหลดไฟล์ไปแล้วกี่ไฟล์จากห้าไฟล์
- Skeleton: การโหลดครั้งแรกของหน้า เมื่อรู้รูปร่างของเนื้อหาที่จะมา
- ในปุ่ม ให้ใช้
loading ของปุ่มเอง ดู Button
ขนาดเล็ก
small คือ spinner 16px ที่มีรางสีเทา สำหรับที่แคบ เช่น หัวการ์ดหรือ toolbar ของตาราง
กำลังโหลดรายการรออนุมัติ…
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 8px; align-items: center">
<ui-loading small description="กำลังโหลดรายการรออนุมัติ"></ui-loading>
<span>กำลังโหลดรายการรออนุมัติ…</span>
</div>
<UiLoading small description="กำลังโหลดรายการรออนุมัติ" />
<span>กำลังโหลดรายการรออนุมัติ…</span>
<Loading small description="กำลังโหลดรายการรออนุมัติ" />
<span>กำลังโหลดรายการรออนุมัติ…</span>
คำอธิบาย
description คือชื่อของ spinner สำหรับ screen reader และจะถูกอ่านเมื่อ spinner แสดง ค่าเริ่มต้นคือ "กำลังโหลด" แปลตามภาษาของหน้า ให้บอกว่ากำลังโหลดอะไร
HTMLReactReact + Tailwind<ui-loading description="กำลังโหลดรายการสัญญา"></ui-loading>
<UiLoading description="กำลังโหลดรายการสัญญา" />
<Loading description="กำลังโหลดรายการสัญญา" />
เริ่มและหยุด
spinner ทำงานตั้งแต่แรก ตั้ง active เป็น false เพื่อหยุดและซ่อน element ยังอยู่ในหน้า พร้อมใช้ครั้งต่อไป ใน HTML เขียน active="false" ถ้าต้องการให้เริ่มแบบหยุดอยู่ ระหว่างที่แสดงจริง element จะมี attribute data-shown
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="loading-toggle" kind="secondary">หยุด</ui-button>
<ui-loading id="loading-toggle-spinner" small description="กำลังโหลดสัญญา"></ui-loading>
</div>
<p>Active: <output id="loading-toggle-out">true</output></p>
<script type="module">
const spinner = document.getElementById('loading-toggle-spinner');
const button = document.getElementById('loading-toggle');
button.addEventListener('click', () => {
spinner.active = !spinner.active;
button.textContent = spinner.active ? 'หยุด' : 'เริ่ม';
document.getElementById('loading-toggle-out').textContent = String(spinner.active);
});
</script>
const [busy, setBusy] = useState(true);
<UiButton kind="secondary" onClick={() => setBusy(!busy)}>{busy ? 'หยุด' : 'เริ่ม'}</UiButton>
<UiLoading small active={busy} description="กำลังโหลดสัญญา" />
<p>Active: <output>{String(busy)}</output></p>
const [busy, setBusy] = useState(true);
<Button kind="secondary" onClick={() => setBusy(!busy)}>{busy ? 'หยุด' : 'เริ่ม'}</Button>
<Loading small active={busy} description="กำลังโหลดสัญญา" />
<p>Active: <output>{String(busy)}</output></p>
คลุมพื้นที่เดียว
contained ห่อพื้นที่หนึ่ง เช่น การ์ดหรือตาราง แทนการคลุมทั้งหน้า ระหว่าง active overlay จะคลุมแค่กล่องนั้น และเนื้อหาข้างใต้เป็น inert และ aria-busy ส่วนอื่นของหน้ายังใช้ได้ Carbon ไม่มี loading แบบ contained ส่วนนี้เราเพิ่มเอง
รีเฟรชสรุป (2 วินาที)
สัญญา CT-2026-118
บริษัท สยามโลจิสติกส์ จำกัด · 1 พ.ย. 2569 – 31 ต.ค. 2570 · ฿1,250,000
HTMLReactReact + Tailwind<ui-button id="loading-area-run" kind="tertiary">รีเฟรชสรุป (2 วินาที)</ui-button>
<ui-loading id="loading-area" contained active="false" description="กำลังรีเฟรชสรุป">
<ui-tile>
<h4 style="margin: 0 0 8px">สัญญา CT-2026-118</h4>
<p style="margin: 0">บริษัท สยามโลจิสติกส์ จำกัด · 1 พ.ย. 2569 – 31 ต.ค. 2570 · ฿1,250,000</p>
</ui-tile>
</ui-loading>
<script type="module">
const area = document.getElementById('loading-area');
document.getElementById('loading-area-run').addEventListener('click', () => {
area.active = true;
setTimeout(() => { area.active = false; }, 2000);
});
</script>
<UiButton kind="tertiary" onClick={refresh}>รีเฟรชสรุป</UiButton>
<UiLoading contained active={refreshing} description="กำลังรีเฟรชสรุป">
<UiTile>
<h4>สัญญา CT-2026-118</h4>
<p>บริษัท สยามโลจิสติกส์ จำกัด · 1 พ.ย. 2569 – 31 ต.ค. 2570 · ฿1,250,000</p>
</UiTile>
</UiLoading>
<Button kind="tertiary" onClick={refresh}>รีเฟรชสรุป</Button>
<Loading contained active={refreshing} description="กำลังรีเฟรชสรุป">
<Tile>
<h4>สัญญา CT-2026-118</h4>
<p>บริษัท สยามโลจิสติกส์ จำกัด · 1 พ.ย. 2569 – 31 ต.ค. 2570 · ฿1,250,000</p>
</Tile>
</Loading>
refresh แทนโค้ดของคุณเองที่ตั้ง refreshing ระหว่างทำงาน
บันทึกฟอร์มในที่
ใส่ฟอร์มไว้ใน loading แบบ contained ระหว่างบันทึก โฟกัสที่อยู่ในพื้นที่จะไปรอที่ spinner แล้วกลับที่เดิม ผู้ใช้คีย์บอร์ดจึงไม่หลงตำแหน่ง
HTMLReactReact + Tailwind<ui-loading id="loading-form" contained active="false" description="กำลังบันทึกแผนก">
<form id="loading-form-form" method="dialog" style="display: grid; gap: 16px; justify-items: start; padding: 16px; border: 1px solid var(--ui-hairline)">
<ui-text-input label="แผนก" name="department" value="จัดซื้อ"></ui-text-input>
<ui-button type="submit">บันทึก</ui-button>
</form>
</ui-loading>
<p>บันทึกแล้ว: <output id="loading-form-out">ยังไม่บันทึก</output></p>
<script type="module">
const loading = document.getElementById('loading-form');
const form = document.getElementById('loading-form-form');
form.addEventListener('submit', (event) => {
event.preventDefault();
const data = new FormData(form);
loading.active = true;
setTimeout(() => {
loading.active = false;
document.getElementById('loading-form-out').textContent = `department=${data.get('department')}`;
}, 1500);
});
</script>
<UiLoading contained active={saving} description="กำลังบันทึกแผนก">
<form onSubmit={(e) => { e.preventDefault(); save(new FormData(e.currentTarget)); }}>
<UiTextInput label="แผนก" name="department" value="จัดซื้อ" />
<UiButton type="submit">บันทึก</UiButton>
</form>
</UiLoading>
<Loading contained active={saving} description="กำลังบันทึกแผนก">
<form onSubmit={(e) => { e.preventDefault(); save(new FormData(e.currentTarget)); }}>
<TextInput label="แผนก" name="department" defaultValue="จัดซื้อ" />
<Button type="submit">บันทึก</Button>
</form>
</Loading>
save แทนโค้ดของคุณเองที่ตั้ง saving ระหว่างทำงาน
รอก่อนแสดง
delay (มิลลิวินาที) แสดง spinner ก็ต่อเมื่องานยังไม่เสร็จหลังเวลานั้น min-duration (มิลลิวินาที, minDuration ใน React) ให้ spinner อยู่อย่างน้อยเท่านั้นเมื่อแสดงแล้ว จึงไม่กะพริบ ค่าเริ่มต้นของทั้งสองคือ 0 เหมือน Carbon ลองกดตรวจแบบเร็ว: งานจบก่อนครบเวลารอ จึงไม่มี spinner
ตรวจแบบเร็ว (0.2 วินาที)
ตรวจทั้งหมด (2 วินาที)
เอกสารผู้ขาย
หนังสือรับรองบริษัท ภ.พ.20 สำเนาสมุดบัญชีธนาคาร
Spinner กำลังแสดง:
HTMLReactReact + Tailwind<div style="display: flex; flex-wrap: wrap; gap: 16px; align-items: center">
<ui-button id="loading-delay-quick" kind="tertiary">ตรวจแบบเร็ว (0.2 วินาที)</ui-button>
<ui-button id="loading-delay-slow" kind="tertiary">ตรวจทั้งหมด (2 วินาที)</ui-button>
</div>
<ui-loading id="loading-delay" contained active="false" delay="300" min-duration="500" description="กำลังตรวจเอกสารผู้ขาย">
<ui-tile>
<h4 style="margin: 0 0 8px">เอกสารผู้ขาย</h4>
<p style="margin: 0">หนังสือรับรองบริษัท ภ.พ.20 สำเนาสมุดบัญชีธนาคาร</p>
</ui-tile>
</ui-loading>
<p>Spinner กำลังแสดง: <output id="loading-delay-out">ไม่</output></p>
<script type="module">
const loading = document.getElementById('loading-delay');
const out = document.getElementById('loading-delay-out');
new MutationObserver(() => {
out.textContent = loading.hasAttribute('data-shown') ? 'ใช่' : 'ไม่';
}).observe(loading, { attributes: true, attributeFilter: ['data-shown'] });
const run = (ms) => {
loading.active = true;
setTimeout(() => { loading.active = false; }, ms);
};
document.getElementById('loading-delay-quick').addEventListener('click', () => run(200));
document.getElementById('loading-delay-slow').addEventListener('click', () => run(2000));
</script>
<UiLoading contained active={checking} delay={300} minDuration={500} description="กำลังตรวจเอกสารผู้ขาย">
<UiTile>…</UiTile>
</UiLoading>
<Loading contained active={checking} delay={300} minDuration={500} description="กำลังตรวจเอกสารผู้ขาย">
<Tile>…</Tile>
</Loading>
delay และ min-duration ใช้ได้กับ loading ทุกแบบ ไม่ใช่แค่ contained
คลุมทั้งหน้า
overlay คลุมหน้าด้วยสี overlay ระหว่างที่ spinner แสดง และยึดโฟกัสไว้: Tab และการคลิกที่อื่นจะกลับมาที่ spinner เมื่อหยุด โฟกัสกลับไปที่เดิม modal dialog ที่เปิดซ้อนอยู่ด้านบนไม่ถูกยุ่ง ใช้เฉพาะตอนที่ใช้ทั้งหน้าจอไม่ได้ และตั้ง active จาก state ของคุณเองเสมอ
overlay เป็น position: fixed ในตัวอย่างนี้วางไว้ในกล่องที่มี transform จึงคลุมแค่กล่อง ในแอปของคุณจะคลุมทั้งหน้าต่าง
รายงานจัดซื้อรายไตรมาส 42 หน้า
สร้าง PDF (3 วินาที)
HTMLReactReact + Tailwind<div style="transform: translateZ(0); position: relative; height: 240px; overflow: hidden; border: 1px solid var(--ui-hairline)">
<div style="display: grid; gap: 16px; justify-items: start; padding: 16px">
<p style="margin: 0">รายงานจัดซื้อรายไตรมาส 42 หน้า</p>
<ui-button id="loading-overlay-open">สร้าง PDF (3 วินาที)</ui-button>
</div>
<ui-loading id="loading-overlay" overlay active="false" description="กำลังสร้าง PDF"></ui-loading>
</div>
<script type="module">
const overlay = document.getElementById('loading-overlay');
document.getElementById('loading-overlay-open').addEventListener('click', () => {
overlay.active = true;
setTimeout(() => { overlay.active = false; }, 3000);
});
</script>
<UiButton onClick={generate}>สร้าง PDF</UiButton>
<UiLoading overlay active={generating} description="กำลังสร้าง PDF" />
<Button onClick={generate}>สร้าง PDF</Button>
<Loading overlay active={generating} description="กำลังสร้าง PDF" />
generate แทนโค้ดของคุณเองที่ตั้ง generating ระหว่างทำงาน
คีย์บอร์ด
| ปุ่ม |
ทำอะไร |
| Tab |
เมื่อใช้ overlay: อยู่ที่ spinner ตลอดที่แสดง |
Esc ไม่ปิด overlay: overlay หายไปเมื่องานจบ
Accessibility
- spinner เป็นรูปภาพที่ใช้
description เป็นชื่อ อยู่ใน live region แบบ assertive ระหว่างแสดง screen reader จึงอ่านออกมา
- เมื่อใช้
overlay spinner เป็น modal dialog ที่ใช้ description เป็นชื่อ และรับโฟกัสไว้ เมื่อหยุด โฟกัสกลับไปที่เดิม
- เมื่อใช้
contained เนื้อหาเป็น inert และ aria-busy ระหว่างแสดง และ spinner เป็น group ที่ใช้ description เป็นชื่อ โฟกัสที่อยู่ข้างในจะรอที่ spinner แล้วกลับที่เดิม
- เขียน
description ว่ากำลังโหลดอะไร ("กำลังโหลดรายการสัญญา") ไม่ใช่แค่ "กำลังโหลด"
- เมื่อผู้ใช้ตั้ง
prefers-reduced-motion spinner จะหมุนช้าลง
ต้องการแค่พฤติกรรมโดยไม่มี style: Headless Loading
API
สร้างจากแพ็กเกจโดยตรง ใน React ส่ง property เป็น prop ชื่อเดียวกัน ส่วน event ก็เป็น prop เช่นกัน (ดูคอลัมน์ React)
<ui-loading> · React UiLoading
Attribute
| Attribute |
Property |
ชนิด |
ค่าเริ่มต้น |
คำอธิบาย |
active |
active |
boolean |
true |
On by default; active="false" stops it. |
small |
small |
boolean |
false |
|
overlay |
overlay |
boolean |
false |
Cover the page and trap focus while active. |
description |
description |
string |
'' |
Accessible description; defaults to the translation ("Loading"). |
contained |
contained |
boolean |
false |
Cover just this element's content (the slot) instead of the page; the content is inert while active (no Carbon counterpart). |
delay |
delay |
number |
0 |
Milliseconds active must last before the spinner shows (default 0, as Carbon). |
min-duration |
minDuration |
number |
0 |
Once shown, the least milliseconds it stays (default 0). |
Slot
| Slot |
คำอธิบาย |
| (ค่าเริ่มต้น) |
The area contained covers |
CSS part
| Part |
คำอธิบาย |
base |
The spinner wrapper (aria-live) |
overlay |
The page overlay (overlay), or the area's (contained) |
area |
The covered content (contained) |
spinner |
The <svg> |