ui
ธีม รูปแบบ CarbonKanok ภาษา English
ภาพรวมติดตั้งการย้ายไป 1.0ธีมและ layerดีไซน์ Kanokปรับแต่งหน้าตาไอคอนฟอร์มและการตรวจค่าใช้กับ frameworkใช้กับ AIButtonText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderModalSide panelTearsheetNotificationToastTooltipDefinition tooltipPopoverToggletipMenuData tableStructured listPaginationContained listTree viewTabsBreadcrumbLinkPagination navContent switcherAccordionProgress indicatorUI shellTagTileCardProduct cardCode snippetCopy buttonUser avatarTruncated textListLoadingInline loadingProgress barSkeletonBadge indicatorStatus indicatorGridHeadingLayerAction setPage headerBig numberEdit in placeแนะนำText inputText areaNumber inputSearchSelectDropdownComboboxMulti-selectCheckboxRadio buttonToggleDate pickerTime pickerSliderFile uploaderTabsPagination navContent switcherTooltipDefinition tooltipPopoverMenuAccordionProgress indicatorUI shellModalSide panelTearsheetNotificationToastData tableStructured listPaginationTree viewCode snippetCopy buttonTruncated textLoadingTagCardProduct cardEdit in place

Headless Loading

Loading คือพฤติกรรมของ Loading แบบมี style โดยไม่มี class เลย Loading.Root ตัดสินว่า spinner แสดงเมื่อไร: ระหว่าง active หลัง delay มิลลิวินาที และเมื่อแสดงแล้วอยู่อย่างน้อย minDuration มิลลิวินาที งานที่เสร็จเร็วจึงไม่มี spinner กะพริบ Spinner ทำให้กราฟิกของคุณเป็น live region แบบ assertive ระหว่างแสดง Indicator ยึดโฟกัสไว้: เป็น modal dialog คลุมหน้าเมื่อใช้ modal หรือเป็นที่รอโฟกัสของ Area ที่กำลังโหลด แล้วโฟกัสกลับที่เดิมเมื่อจบ

คุณเป็นเจ้าของกราฟิก spinner, overlay, การซ่อน part ตอนที่ไม่แสดง และ style ทั้งหมด ทุก part มี data-shown ระหว่างที่ spinner แสดง

ติดตั้ง

ส่วน headless มากับแพ็กเกจเลย (ไม่มี item ของ shadcn; item ของ React + Tailwind สร้างบนมัน):

npm install @purpurabog/ui import { Loading } from '@purpurabog/ui/headless';

วิธีใช้

import { Loading } from '@purpurabog/ui/headless'; export default function Example() { return ( <Loading.Root> <Loading.Spinner className="size-[88px] animate-spin [animation-duration:690ms] motion-reduce:[animation-duration:2s]"> <svg viewBox="0 0 100 100" role="img" aria-label="กำลังโหลดสัญญา" className="block size-full fill-transparent"> <circle cx="50" cy="50" r="44" className="stroke-interactive [stroke-dasharray:276.46_276.46] [stroke-dashoffset:52.53] [stroke-width:10]" /> </svg> </Loading.Spinner> </Loading.Root> ); }

โครงสร้าง

<Loading.Root> <Loading.Area /> <Loading.Indicator> <Loading.Spinner /> </Loading.Indicator> </Loading.Root>

Area และ Indicator ไม่บังคับ spinner ธรรมดาใช้แค่ Root กับ Spinner

เริ่ม หยุด และรอ

active เริ่มและหยุด (เปิดตั้งแต่แรก) delay รอก่อนแสดง และ minDuration ให้อยู่ต่อเมื่อแสดงแล้ว useLoadingContext().shown บอก component ใดก็ได้ใน Root ว่าตอนนี้แสดงอยู่หรือไม่ part ไม่ซ่อนตัวเอง: ให้ซ่อน spinner ตอนที่ไม่มี data-shown ลองกดตรวจแบบเร็ว: งานจบก่อนครบ 300 มิลลิวินาที จึงไม่มีอะไรแสดง

import { useState } from 'react'; import { Loading } from '@purpurabog/ui/headless'; const button = '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'; function Shown() { const { shown } = Loading.useLoadingContext(); return <p className="text-xs text-ink-muted">แสดงอยู่: {String(shown)}</p>; } export default function Example() { const [active, setActive] = useState(false); const run = (ms: number) => { setActive(true); setTimeout(() => setActive(false), ms); }; return ( <div className="grid justify-items-start gap-3"> <div className="flex flex-wrap gap-4"> <button type="button" className={button} onClick={() => run(200)}>ตรวจแบบเร็ว (0.2 วินาที)</button> <button type="button" className={button} onClick={() => run(2000)}>ตรวจทั้งหมด (2 วินาที)</button> </div> <Loading.Root active={active} delay={300} minDuration={500}> <div className="flex h-6 items-center gap-2"> <Loading.Spinner className="hidden size-4 animate-spin [animation-duration:690ms] data-shown:block"> <svg viewBox="0 0 100 100" role="img" aria-label="กำลังตรวจเอกสารผู้ขาย" className="block size-full fill-transparent"> <circle cx="50" cy="50" r="42" className="stroke-layer-accent [stroke-width:16]" /> <circle cx="50" cy="50" r="42" className="stroke-interactive [stroke-dasharray:276.46_276.46] [stroke-dashoffset:143.76] [stroke-width:16]" /> </svg> </Loading.Spinner> <Shown /> </div> </Loading.Root> </div> ); }

คลุมพื้นที่เดียว

ห่อเนื้อหาด้วย Area แล้ววาง Indicator ทับไว้ ระหว่างแสดง พื้นที่เป็น inert และ aria-busy ส่วน indicator เป็น group ที่ใช้ aria-label เป็นชื่อ โฟกัสที่อยู่ในพื้นที่จะรอที่ indicator แล้วกลับที่เดิม: กดรีเฟรชด้วยคีย์บอร์ด แล้วโฟกัสจะกลับมาที่ปุ่ม

import { useState } from 'react'; import { Loading } from '@purpurabog/ui/headless'; export default function Example() { const [busy, setBusy] = useState(false); const refresh = () => { setBusy(true); setTimeout(() => setBusy(false), 2000); }; return ( <Loading.Root active={busy}> <div className="relative max-w-md"> <Loading.Area className="grid justify-items-start gap-3 border border-hairline bg-layer-1 p-4 text-ink"> <h4 className="text-sm font-semibold">สัญญา CT-2026-118</h4> <p className="text-sm">บริษัท สยามโลจิสติกส์ จำกัด · ฿1,250,000</p> <button type="button" onClick={refresh} 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">รีเฟรช (2 วินาที)</button> </Loading.Area> <Loading.Indicator aria-label="กำลังรีเฟรชสัญญา" className="absolute inset-0 z-1 hidden items-center justify-center bg-overlay outline-none data-shown:flex"> <Loading.Spinner className="size-[88px] animate-spin [animation-duration:690ms]"> <svg viewBox="0 0 100 100" role="img" aria-label="กำลังรีเฟรชสัญญา" className="block size-full fill-transparent"> <circle cx="50" cy="50" r="44" className="stroke-interactive [stroke-dasharray:276.46_276.46] [stroke-dashoffset:52.53] [stroke-width:10]" /> </svg> </Loading.Spinner> </Loading.Indicator> </div> </Loading.Root> ); }

คลุมทั้งหน้า

เมื่อใช้ modal Indicator จะเป็น modal dialog ระหว่างแสดง รับโฟกัสไว้ และ Tab หรือโฟกัสที่ย้ายไปที่อื่นจะกลับมาที่มัน modal dialog ที่เปิดซ้อนอยู่ด้านบนไม่ถูกยุ่ง เมื่อหยุด โฟกัสกลับไปที่เดิม มันไม่ใช่ <dialog> จึงปิดด้วย Esc ไม่ได้

overlay เป็น position: fixed ในตัวอย่างนี้วางไว้ในกล่องที่มี transform จึงคลุมแค่กล่อง

import { useState } from 'react'; import { Loading } from '@purpurabog/ui/headless'; export default function Example() { const [busy, setBusy] = useState(false); const generate = () => { setBusy(true); setTimeout(() => setBusy(false), 3000); }; return ( <div className="relative h-60 overflow-hidden border border-hairline bg-canvas text-ink [transform:translateZ(0)]"> <div className="grid justify-items-start gap-4 p-4"> <p className="text-sm">รายงานจัดซื้อรายไตรมาส 42 หน้า</p> <button type="button" onClick={generate} 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">สร้าง PDF (3 วินาที)</button> </div> <Loading.Root active={busy} modal> <Loading.Indicator aria-label="กำลังสร้าง PDF" className="fixed inset-0 z-10 hidden items-center justify-center bg-overlay outline-none data-shown:flex"> <Loading.Spinner className="size-[88px] animate-spin [animation-duration:690ms]"> <svg viewBox="0 0 100 100" role="img" aria-label="กำลังสร้าง PDF" className="block size-full fill-transparent"> <circle cx="50" cy="50" r="44" className="stroke-interactive [stroke-dasharray:276.46_276.46] [stroke-dashoffset:52.53] [stroke-width:10]" /> </svg> </Loading.Spinner> </Loading.Indicator> </Loading.Root> </div> ); }

แต่ง style

ทุก part มี data-shown ระหว่างที่ spinner แสดง ซ่อน indicator และ spinner ตอนที่ไม่มี และทำให้พื้นที่จางหรือเบลอตอนที่มี

import { useState } from 'react'; import { Loading } from '@purpurabog/ui/headless'; export default function Example() { const [busy, setBusy] = useState(false); return ( <div className="grid justify-items-start gap-3"> <button type="button" onClick={() => setBusy(!busy)} 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"> {busy ? 'หยุด' : 'เริ่ม'} </button> <Loading.Root active={busy} delay={200}> <div className="relative w-full max-w-md"> <Loading.Area className="border border-hairline bg-layer-1 p-4 text-sm text-ink transition-opacity data-shown:opacity-40"> เอกสารผู้ขาย: หนังสือรับรองบริษัท ภ.พ.20 สำเนาสมุดบัญชีธนาคาร </Loading.Area> <Loading.Indicator aria-label="กำลังตรวจเอกสาร" className="absolute inset-0 hidden items-center justify-center outline-none data-shown:flex"> <Loading.Spinner className="size-8 animate-spin [animation-duration:690ms] motion-reduce:[animation-duration:2s]"> <svg viewBox="0 0 100 100" role="img" aria-label="กำลังตรวจเอกสาร" className="block size-full fill-transparent"> <circle cx="50" cy="50" r="42" className="stroke-layer-accent [stroke-width:12]" /> <circle cx="50" cy="50" r="42" className="stroke-interactive [stroke-dasharray:276.46_276.46] [stroke-dashoffset:143.76] [stroke-width:12]" /> </svg> </Loading.Spinner> </Loading.Indicator> </div> </Loading.Root> </div> ); } /* className="area" ที่ Area, "indicator" ที่ Indicator, "spinner" ที่ Spinner */ .area[data-shown] { opacity: 0.4; } .indicator { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; } .indicator[data-shown] { display: flex; } .spinner { width: 32px; height: 32px; animation: spin 690ms linear infinite; } @keyframes spin { to { transform: rotate(360deg); } }

Parts

Loading.Root

render แค่ children เก็บสถานะว่า spinner แสดงหรือไม่ และย้ายโฟกัสระหว่างแสดง

Prop Type ค่าเริ่มต้น
active boolean true เริ่มและหยุด false คือซ่อน
delay number 0 active ต้องค้างนานกี่มิลลิวินาทีก่อนแสดง
minDuration number 0 เมื่อแสดงแล้ว อยู่อย่างน้อยกี่มิลลิวินาที
modal boolean false overlay คลุมหน้า: Indicator เป็น modal dialog ที่ยึดโฟกัสระหว่างแสดง

Loading.useLoadingContext() คืนค่า shown และ modal (รวมถึง ref ของ area และ indicator) ให้ component ใดก็ได้ใน Root

Loading.Area

<div> ที่ห่อเนื้อหาซึ่ง loading แบบ contained คลุมอยู่ ระหว่างแสดงจะเป็น inert และมี aria-busy="true"

Attribute เมื่อ
data-shown spinner แสดงอยู่

Loading.Indicator

<div> ที่ยึดโฟกัสระหว่างแสดง รับ prop ของ div ทุกตัวยกเว้น role และ tabIndex ตั้งชื่อด้วย aria-label ระหว่างแสดงจะมี tabIndex={-1}, role="dialog" กับ aria-modal="true" เมื่อ Root เป็น modal หรือ role="group" ในกรณีอื่น และ aria-label ของมัน ตอนซ่อนไม่มีทั้ง role และชื่อ

Attribute เมื่อ
data-shown spinner แสดงอยู่

Loading.Spinner

<div> ที่ห่อกราฟิก spinner ของคุณ มี aria-atomic="true" ระหว่างแสดงเป็น live region แบบ assertive (aria-live="assertive") ชื่อของกราฟิกจึงถูกอ่าน ตอนซ่อนเป็น aria-live="off"

Attribute เมื่อ
data-shown spinner แสดงอยู่

คีย์บอร์ดและ accessibility

ปุ่ม ทำอะไร
Tab เมื่อใช้ modal: อยู่ที่ indicator ตลอดที่แสดง

part จัดการให้:

  • ย้ายโฟกัสไปที่ Indicator เมื่อ loading แบบ modal แสดง หรือเมื่อโฟกัสอยู่ใน Area แล้วคืนโฟกัสเมื่อจบ
  • ตั้ง role, aria-modal, aria-label และ tabIndex ของ Indicator ระหว่างแสดง
  • ทำให้ Area เป็น inert และ aria-busy
  • ทำให้ Spinner เป็น live region แบบ assertive ระหว่างแสดง

คุณต้องเพิ่มเอง:

  • กราฟิก spinner ที่มีชื่อ (role="img" และ aria-label ที่ <svg>) และ aria-label ของ Indicator บอกว่ากำลังโหลดอะไร
  • การซ่อน part ตอนที่ไม่มี data-shown และสี overlay
  • spinner ที่หมุนช้าลงหรือหยุดเมื่อผู้ใช้ตั้ง prefers-reduced-motion