Repository Public
1// Composants d'interface : dialogues modaux (élément <dialog> natif : focus piégé, Échap), confirmations,2// notifications, contrôle segmenté, bascule de thème.34import { h, icon } from "./dom.js";56/** Ouvre un dialogue modal. `render(close)` retourne { body, foot } ; la promesse se résout à la fermeture. */7export function openDialog({ title, wide = false, render, dismissable = true, onClose }) {8 return new Promise((resolve) => {9 let result;10 const dlg = h("dialog", { class: `dialog${wide ? " wide" : ""}`, "aria-labelledby": "dlg-title" });11 const close = (value) => { result = value; dlg.close(); };12 const parts = render(close, dlg);13 dlg.append(14 h("div", { class: "dialog-head" },15 h("h2", { id: "dlg-title" }, title),16 dismissable ? h("button", { class: "btn btn-ghost btn-sm btn-icon", type: "button", "aria-label": "Fermer", onclick: () => close(undefined) }, icon("x")) : null),17 h("div", { class: "dialog-body" }, parts.body));18 if (parts.foot) dlg.append(h("div", { class: "dialog-foot" }, parts.foot));19 dlg.addEventListener("cancel", (e) => { if (!dismissable) e.preventDefault(); });20 dlg.addEventListener("click", (e) => { if (e.target === dlg && dismissable && !dlg.dataset.sticky) close(undefined); });21 dlg.addEventListener("close", () => {22 try { if (onClose) onClose(); } finally { dlg.remove(); resolve(result); }23 });24 document.body.append(dlg);25 dlg.showModal();26 });27}2829/** Confirmation explicite ; résout true si confirmé. */30export function confirmDialog({ title, message, confirmLabel = "Confirmer", danger = false, details = null }) {31 return openDialog({32 title,33 render: (close) => ({34 body: [h("p", null, message), details],35 foot: [36 h("button", { class: "btn btn-ghost", type: "button", onclick: () => close(false) }, "Annuler"),37 h("button", { class: `btn ${danger ? "btn-danger" : "btn-primary"}`, type: "button", onclick: () => close(true) }, confirmLabel),38 ],39 }),40 }).then((v) => v === true);41}4243export function toast(message, kind = "ok") {44 const host = document.getElementById("toasts");45 if (!host) return;46 const el = h("div", { class: `toast${kind === "ok" ? "" : ` is-${kind}`}`, role: kind === "danger" ? "alert" : "status" },47 icon(kind === "danger" ? "alert" : kind === "info" ? "info" : "check"), h("span", null, message));48 host.append(el);49 setTimeout(() => el.remove(), kind === "danger" ? 8000 : 4500);50}5152export function note(kind, ...content) {53 const icons = { warn: "alert", danger: "alert", ok: "check", info: "info" };54 return h("div", { class: `note${kind === "info" ? "" : ` is-${kind}`}`, role: kind === "danger" ? "alert" : null },55 icon(icons[kind] || "info"), h("div", null, ...content));56}5758export function empty(title, text, iconName = "info") {59 return h("div", { class: "empty" }, icon(iconName), h("b", null, title), text ? h("span", null, text) : null);60}6162/** Contrôle segmenté : options = [{ value, label, title }] ; retourne l'élément (role="group"). */63export function segmented(label, options, value, onChange) {64 const el = h("div", { class: "seg", role: "group", "aria-label": label });65 for (const o of options) {66 el.append(h("button", {67 type: "button", "aria-pressed": String(o.value === value), title: o.title || null,68 onclick: (e) => {69 for (const b of el.children) b.setAttribute("aria-pressed", String(b === e.currentTarget));70 onChange(o.value);71 },72 }, o.label));73 }74 return el;75}7677export function spinner() {78 return h("span", { class: "spinner", role: "status", "aria-label": "Chargement" });79}8081/** Met un bouton en attente pendant une action asynchrone. */82export async function busy(button, fn) {83 if (button.disabled) return undefined;84 const saved = [...button.childNodes];85 button.disabled = true;86 button.setAttribute("aria-busy", "true");87 button.prepend(h("span", { class: "spinner", "aria-hidden": "true" }));88 try {89 return await fn();90 } finally {91 button.replaceChildren(...saved);92 button.disabled = false;93 button.removeAttribute("aria-busy");94 }95}9697// --- thème -----------------------------------------------------------------------------------------98export function currentTheme() {99 const set = document.documentElement.getAttribute("data-theme");100 if (set === "light" || set === "dark") return set;101 return window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";102}103104export function toggleTheme() {105 const next = currentTheme() === "dark" ? "light" : "dark";106 document.documentElement.setAttribute("data-theme", next);107 try { localStorage.setItem("theme", next); } catch { /* stockage indisponible */ }108 syncThemeButtons();109}110111export function syncThemeButtons() {112 const dark = currentTheme() === "dark";113 for (const btn of document.querySelectorAll("[data-theme-toggle]")) {114 const label = dark ? "Passer au thème clair" : "Passer au thème sombre";115 btn.setAttribute("aria-label", label);116 btn.title = label;117 const ic = btn.querySelector(".icon");118 if (ic) ic.replaceWith(icon(dark ? "sun" : "moon"));119 const text = btn.querySelector("[data-theme-label]");120 if (text) text.textContent = dark ? "Thème clair" : "Thème sombre";121 }122}123124export function bindThemeButtons(root = document) {125 for (const btn of root.querySelectorAll("[data-theme-toggle]")) {126 if (btn.dataset.bound) continue;127 btn.dataset.bound = "1";128 btn.addEventListener("click", toggleTheme);129 }130 syncThemeButtons();131}132