/** * ปุ่มลบ/ล้างที่หายไปพร้อมสิ่งที่มันลบ — ปุ่มที่ถือโฟกัสถูกถอดจากหน้า เบราว์เซอร์ย้ายโฟกัสไป body * ผู้ใช้คีย์บอร์ด/โปรแกรมอ่านจอหลงตำแหน่ง (กด Tab แล้วเริ่มจากบนสุดของหน้า) * ปุ่มลบรายการใส่ `data-remove-item` · ปุ่มเพิ่มรายการใส่ `data-add-item` (เป้าหมายเมื่อไม่เหลือรายการ) */ const REMOVE_ITEM = "[data-remove-item]"; /** แอปที่ลบหลังรอ server (await) ปุ่มหายช้ากว่าเฟรมถัดไป — เฝ้ารอได้นานสุดเท่านี้ */ const WAIT_REMOVAL_MS = 10_000; /** * เรียก `then` เมื่อ `button` ถูกถอดจากหน้า (เช็กเฟรมถัดไป แล้วเฝ้า DOM ต่ออีกถึง 10 วินาที) — ยังไม่ถูกถอด = ไม่เรียก * ระหว่างรอผู้ใช้ย้ายโฟกัสไปที่อื่นเองแล้ว (activeElement ไม่ใช่ body) = ไม่แย่งโฟกัส */ function whenRemoved(button: HTMLElement, then: () => void): void { let done = false; let observer: MutationObserver | undefined; let timer: ReturnType | undefined; const stop = () => { done = true; observer?.disconnect(); if (timer) clearTimeout(timer); }; const check = () => { if (done || button.isConnected) return; stop(); const active = document.activeElement; if (active && active !== document.body && active !== document.documentElement) return; then(); }; requestAnimationFrame(() => { check(); if (done || typeof MutationObserver === "undefined") return; observer = new MutationObserver(check); observer.observe(document.body, { childList: true, subtree: true }); timer = setTimeout(stop, WAIT_REMOVAL_MS); }); } /** * เรียกใน onClick ก่อนลบจริง (ก่อน `await` ด้วย) — เมื่อปุ่มหายไป ย้ายโฟกัสไปปุ่มลบของรายการถัดไป (รายการสุดท้าย → ก่อนหน้า) * ไม่เหลือรายการ → `fallback` · ปุ่มยังอยู่ (key เป็น index แล้วแถวถัดไปเลื่อนมาแทน, แอปไม่ได้ลบ) = ไม่ยุ่ง */ export function focusAfterRemove( button: HTMLElement, container: Element | null | undefined, fallback?: () => HTMLElement | null | undefined, ): void { const index = container ? [...container.querySelectorAll(REMOVE_ITEM)].indexOf(button) : -1; whenRemoved(button, () => { const items = container?.isConnected ? [...container.querySelectorAll(REMOVE_ITEM)] : []; (items[Math.min(Math.max(index, 0), items.length - 1)] ?? fallback?.())?.focus(); }); } const FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]):not([type="hidden"]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])'; function canTakeFocus(el: HTMLElement): boolean { return ( el.isConnected && !el.closest('[inert],[aria-hidden="true"]') && (el.checkVisibility?.() ?? true) ); } /** * ปุ่มปิดที่หายไปพร้อมแถบของมัน (Banner, Notice, SelectionBar) ซึ่งไม่มีรายการข้างเคียงให้ย้ายไป — * เรียกใน onClick ก่อนปิด (ก่อน `await` ด้วย): เมื่อปุ่มหายไป โฟกัสไปตัวถัดไปตามลำดับ Tab (ไม่มี → ตัวก่อนหน้า) เหมือนกด Tab ต่อจากจุดเดิม * ปุ่มยังอยู่ (แอปไม่ได้ปิดจริง) = ไม่ยุ่ง */ export function focusAfterDismiss(button: HTMLElement): void { const all = [...document.querySelectorAll(FOCUSABLE)]; const index = all.indexOf(button); const order = [...all.slice(index + 1), ...all.slice(0, Math.max(index, 0)).reverse()]; whenRemoved(button, () => { order.find(canTakeFocus)?.focus(); }); } /** ปุ่ม X ในช่องกรอก (`InputGroup`) หายไปเมื่อช่องว่าง — โฟกัสกลับช่องกรอกนั้น พิมพ์ใหม่ได้ทันที */ export function focusFieldOf(button: HTMLElement): void { button .closest('[data-slot="input-group"]') ?.querySelector("input, textarea") ?.focus(); }