121 lines
4.1 KiB
JavaScript
121 lines
4.1 KiB
JavaScript
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");
|
|
const abort = new AbortController(),
|
|
signal = abort.signal;
|
|
let disposed = false;
|
|
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, { signal });
|
|
element.addEventListener(
|
|
"pointerleave",
|
|
() => {
|
|
if (!element.contains(document.activeElement)) resume();
|
|
},
|
|
{ signal },
|
|
);
|
|
element.addEventListener("focusin", pause, { signal });
|
|
element.addEventListener(
|
|
"focusout",
|
|
(event) => {
|
|
if (!element.contains(event.relatedTarget) && !element.matches(":hover"))
|
|
resume();
|
|
},
|
|
{ signal },
|
|
);
|
|
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 });
|
|
}
|
|
},
|
|
{ signal },
|
|
);
|
|
// 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, signal },
|
|
);
|
|
function notify(message, { type = "info", title, duration } = {}) {
|
|
if (disposed) return;
|
|
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 = `<span class="toast-symbol">${iconSvg(config.icon, { size: 20 })}</span><div class="toast-content"><strong class="toast-title"></strong><span class="toast-message"></span></div><button type="button" class="toast-dismiss" data-toast-dismiss aria-label="${t("Закрыть уведомление", "Dismiss notification")}">${iconSvg("close", { size: 16 })}</button>`;
|
|
element.querySelector(".toast-title").textContent =
|
|
title || t(config.ru, config.en);
|
|
element.querySelector(".toast-message").textContent = String(message);
|
|
active = true;
|
|
remaining =
|
|
Number.isFinite(duration) && duration >= 0 ? duration : config.duration;
|
|
element.showPopover();
|
|
started = performance.now();
|
|
if (!element.matches(":hover") && !element.contains(document.activeElement))
|
|
resume();
|
|
}
|
|
notify.dismiss = hide;
|
|
notify.dispose = () => {
|
|
hide();
|
|
abort.abort();
|
|
disposed = true;
|
|
};
|
|
return notify;
|
|
}
|