| 1 | const DEFAULT_DURATION_MS = 3500; |
| 2 | const LEAVE_MS = 280; |
| 3 | |
| 4 | /** |
| 5 | * Temporary top-of-screen toast messages (play HUD). |
| 6 | */ |
| 7 | export function initToasts () |
| 8 | { |
| 9 | const root = document.getElementById("toast-stack"); |
| 10 | if (!root) |
| 11 | { |
| 12 | return { |
| 13 | show () |
| 14 | {} |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | function show (message, options = {}) |
| 19 | { |
| 20 | const text = String(message ?? "").trim(); |
| 21 | if (!text) |
| 22 | { return; } |
| 23 | const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS); |
| 24 | const el = document.createElement("div"); |
| 25 | el.className = "toast"; |
| 26 | el.setAttribute("role", "status"); |
| 27 | el.textContent = text; |
| 28 | root.appendChild(el); |
| 29 | requestAnimationFrame(() => |
| 30 | { |
| 31 | el.classList.add("toast-visible"); |
| 32 | }); |
| 33 | window.setTimeout(() => |
| 34 | { |
| 35 | el.classList.remove("toast-visible"); |
| 36 | el.classList.add("toast-leaving"); |
| 37 | window.setTimeout(() => el.remove(), LEAVE_MS); |
| 38 | }, durationMs); |
| 39 | } |
| 40 | |
| 41 | return { show }; |
| 42 | } |