/ src / toast.js
toast.js
 1  import { toast } from "./dom.js";
 2  import { TOAST_DISPLAY_MS, TOAST_HIDE_MS } from "./constants.js";
 3  
 4  let toastTimer = null;
 5  let toastHideTimer = null;
 6  
 7  export function showToast(message) {
 8    if (!toast) return;
 9    if (toastTimer) {
10      clearTimeout(toastTimer);
11      toastTimer = null;
12    }
13    if (toastHideTimer) {
14      clearTimeout(toastHideTimer);
15      toastHideTimer = null;
16    }
17    toast.textContent = message;
18    toast.classList.remove("visible");
19    toast.classList.remove("hidden");
20    void toast.offsetHeight;
21    requestAnimationFrame(() => {
22      requestAnimationFrame(() => {
23        toast.classList.add("visible");
24      });
25    });
26    toastTimer = setTimeout(() => {
27      toast.classList.remove("visible");
28      toastHideTimer = setTimeout(() => {
29        toast.classList.add("hidden");
30        toastHideTimer = null;
31      }, TOAST_HIDE_MS);
32      toastTimer = null;
33    }, TOAST_DISPLAY_MS);
34  }