import { iconSvg } from "../icons/icons.js"; const types = { success: { icon: "check", ru: "Готово", en: "Success", duration: 4000 }, info: { icon: "info", ru: "Информация", en: "Information", duration: 5000 }, warning: { icon: "warning", ru: "Внимание", en: "Warning", duration: 8000 }, error: { icon: "error", ru: "Ошибка", en: "Error", duration: 0 }, }; export function createNotifier(element, { t = (ru) => ru } = {}) { element.classList.add("anidev-toast"); let timer, remaining = 0, started = 0, active = false, invoker = null; function hide() { clearTimeout(timer); active = false; if (element.isConnected && element.matches(":popover-open")) element.hidePopover(); } function resume() { if (!active || !remaining) return; clearTimeout(timer); started = performance.now(); timer = setTimeout(hide, remaining); } function pause() { if (!active || !remaining) return; clearTimeout(timer); remaining = Math.max(1, remaining - (performance.now() - started)); } element.addEventListener("pointerenter", pause); element.addEventListener("pointerleave", () => { if (!element.contains(document.activeElement)) resume(); }); element.addEventListener("focusin", pause); element.addEventListener("focusout", (event) => { if (!element.contains(event.relatedTarget) && !element.matches(":hover")) resume(); }); element.addEventListener("click", (event) => { if (event.target.closest("[data-toast-dismiss]")) { const restore = element.contains(document.activeElement); hide(); if ( restore && invoker?.isConnected && !invoker.closest("[inert]") && invoker.getClientRects().length ) invoker.focus({ preventScroll: true }); } }); // A modal makes outside elements inert: put the toast inside it, then use the top layer. document.addEventListener( "close", (event) => { if ( event.target instanceof HTMLDialogElement && event.target.contains(element) ) { hide(); document.body.append(element); } }, { capture: true }, ); function notify(message, { type = "info", title, duration } = {}) { type = Object.hasOwn(types, type) ? type : "info"; const config = types[type]; if (!element.contains(document.activeElement)) invoker = document.activeElement; hide(); const modal = [...document.querySelectorAll("dialog[open]")].at(-1); if (element.parentElement !== (modal || document.body)) (modal || document.body).append(element); element.popover = "manual"; element.dataset.type = type; element.setAttribute("role", type === "error" ? "alert" : "status"); element.setAttribute( "aria-live", type === "error" ? "assertive" : "polite", ); element.setAttribute("aria-atomic", "true"); element.innerHTML = `${iconSvg(config.icon, { size: 20 })}