// Retours à l'utilisateur : message bref (toast) et demande de confirmation. import { h } from "./dom.js"; /** @type {HTMLElement|null} */ let toastNode = null; /** @type {ReturnType|null} */ let toastTimer = null; /** * Message bref en bas de l'écran, lu par les lecteurs d'écran. * @param {string} message */ export function toast(message) { if (!toastNode) { toastNode = h("div", { class: "toast", role: "status", "aria-live": "polite" }); document.body.append(toastNode); } toastNode.textContent = message; toastNode.classList.add("toast--on"); if (toastTimer) clearTimeout(toastTimer); toastTimer = setTimeout(() => toastNode?.classList.remove("toast--on"), 3500); } /** * Demande de confirmation dans une boîte de dialogue native (). * @param {{title: string, lines?: string[], confirm: string, cancel?: string, danger?: boolean}} options * @returns {Promise} */ export function confirmDialog({ title, lines = [], confirm, cancel = "Annuler", danger = false }) { return new Promise((resolve) => { const yes = h("button", { type: "button", class: `btn btn--block ${danger ? "btn--danger" : "btn--primary"}` }, confirm); const no = h("button", { type: "button", class: "btn btn--block btn--ghost" }, cancel); const dialog = h("dialog", { class: "dialog", "aria-labelledby": "dialog-title" }, h("h2", { class: "dialog__title", id: "dialog-title" }, title), lines.map((l) => h("p", { class: "dialog__text" }, l)), h("div", { class: "dialog__actions" }, yes, no)); /** @param {boolean} answer */ const done = (answer) => { dialog.close(); dialog.remove(); resolve(answer); }; yes.addEventListener("click", () => done(true)); no.addEventListener("click", () => done(false)); dialog.addEventListener("cancel", (e) => { e.preventDefault(); done(false); }); document.body.append(dialog); dialog.showModal(); no.focus(); }); }