feat: architecture multi-thèmes (6 designs) et nouveau logo SVG
This commit is contained in:
1 parent
685599e706
commit
e180a97113
20 files changed
+3177
-118
No files matched your search
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* theme-switcher.js — Interface de contrôle et d'exploration des thèmes
|
||||
* =============================================================================
|
||||
* Génère et pilote :
|
||||
* 1. Un dock flottant discret mais immédiatement repérable affichant le nom
|
||||
* du thème actif + deux actions directes (« Surprise » et « Explorer »).
|
||||
* 2. Une modale <dialog> accessible présentant tous les thèmes enregistrés
|
||||
* dans themes.js (palette, description, tags, bascule instantanée).
|
||||
*
|
||||
* Injection en amélioration progressive : sans JavaScript, aucun contrôle
|
||||
* inerte n'apparaît dans la page.
|
||||
*/
|
||||
|
||||
import { THEMES, getThemeById } from "../themes.js";
|
||||
import {
|
||||
applyTheme,
|
||||
getActiveThemeId,
|
||||
onThemeChange,
|
||||
pickSurpriseTheme,
|
||||
} from "./theme-manager.js";
|
||||
|
||||
/**
|
||||
* Crée la structure DOM du dock flottant et de la modale <dialog>.
|
||||
* @returns {{ dock: HTMLElement, modal: HTMLDialogElement }}
|
||||
*/
|
||||
function buildSwitcherUI() {
|
||||
const dock = document.createElement("aside");
|
||||
dock.className = "theme-dock screen-only";
|
||||
dock.setAttribute("aria-label", "Sélecteur de design");
|
||||
dock.innerHTML = `
|
||||
<span class="theme-dock__status">
|
||||
<span class="theme-dock__dot" aria-hidden="true"></span>
|
||||
<span class="theme-dock__label">Design :</span>
|
||||
<span class="theme-dock__name" data-theme-current-name></span>
|
||||
</span>
|
||||
<div class="theme-dock__actions">
|
||||
<button type="button" class="theme-dock__btn theme-dock__btn--primary" data-theme-surprise>
|
||||
<svg class="theme-dock__icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
||||
<path d="M16 3h5v5M4 20 21 3M21 16v5h-5M15 15l6 6M4 4l5 5"/>
|
||||
</svg>
|
||||
Surprise
|
||||
</button>
|
||||
<button type="button" class="theme-dock__btn" data-theme-explore aria-haspopup="dialog">
|
||||
<svg class="theme-dock__icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false">
|
||||
<rect x="3" y="3" width="7" height="7" rx="1"/>
|
||||
<rect x="14" y="3" width="7" height="7" rx="1"/>
|
||||
<rect x="3" y="14" width="7" height="7" rx="1"/>
|
||||
<rect x="14" y="14" width="7" height="7" rx="1"/>
|
||||
</svg>
|
||||
Explorer
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const modal = document.createElement("dialog");
|
||||
modal.className = "theme-modal screen-only";
|
||||
modal.setAttribute("aria-labelledby", "theme-modal-title");
|
||||
modal.innerHTML = `
|
||||
<header class="theme-modal__header">
|
||||
<div>
|
||||
<h2 class="theme-modal__title" id="theme-modal-title">Explorer les designs</h2>
|
||||
<p class="theme-modal__subtitle">
|
||||
Même contenu, plusieurs directions artistiques. L'impression PDF reste toujours sobre.
|
||||
</p>
|
||||
</div>
|
||||
<button type="button" class="theme-modal__close" data-theme-close aria-label="Fermer la fenêtre des designs">
|
||||
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" aria-hidden="true" focusable="false">
|
||||
<path d="M18 6 6 18M6 6l12 12"/>
|
||||
</svg>
|
||||
</button>
|
||||
</header>
|
||||
<div class="theme-modal__body">
|
||||
<div class="theme-modal__grid" data-theme-grid></div>
|
||||
</div>
|
||||
<footer class="theme-modal__footer">
|
||||
<span>Le lien mis à jour dans la barre d'adresse permet de partager un design précis.</span>
|
||||
<button type="button" class="theme-modal__surprise" data-theme-surprise>
|
||||
Tirage aléatoire
|
||||
</button>
|
||||
</footer>
|
||||
`;
|
||||
|
||||
const grid = modal.querySelector("[data-theme-grid]");
|
||||
for (const theme of THEMES) {
|
||||
const card = document.createElement("article");
|
||||
card.className = "theme-card";
|
||||
card.dataset.themeCard = theme.id;
|
||||
|
||||
const swatchesHtml = theme.palette
|
||||
.map(
|
||||
(color) =>
|
||||
`<span class="theme-card__swatch" style="background-color:${color}" aria-hidden="true"></span>`
|
||||
)
|
||||
.join("");
|
||||
|
||||
const tagsHtml = theme.tags.map((tag) => `<li>${tag}</li>`).join("");
|
||||
|
||||
card.innerHTML = `
|
||||
<div class="theme-card__swatches">${swatchesHtml}</div>
|
||||
<div class="theme-card__head">
|
||||
<h3 class="theme-card__name">${theme.name}</h3>
|
||||
<span class="theme-card__active" hidden>Actif</span>
|
||||
</div>
|
||||
<p class="theme-card__subtitle">${theme.subtitle}</p>
|
||||
<p class="theme-card__desc">${theme.description}</p>
|
||||
<ul class="theme-card__tags" role="list">${tagsHtml}</ul>
|
||||
<button type="button" class="theme-card__btn" data-theme-select="${theme.id}">
|
||||
Appliquer ce design
|
||||
</button>
|
||||
`;
|
||||
|
||||
grid.appendChild(card);
|
||||
}
|
||||
|
||||
document.body.append(dock, modal);
|
||||
return { dock, modal };
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise le contrôleur de thèmes.
|
||||
*
|
||||
* @param {object} [options]
|
||||
* @param {HTMLElement | null} [options.status] - Région aria-live pour annonces vocales.
|
||||
* @returns {void}
|
||||
*/
|
||||
export function initThemeSwitcher({ status } = {}) {
|
||||
const { dock, modal } = buildSwitcherUI();
|
||||
const currentNameEl = dock.querySelector("[data-theme-current-name]");
|
||||
const exploreBtn = dock.querySelector("[data-theme-explore]");
|
||||
const closeBtn = modal.querySelector("[data-theme-close]");
|
||||
const cards = modal.querySelectorAll("[data-theme-card]");
|
||||
|
||||
/**
|
||||
* Synchronise l'interface avec le thème actif.
|
||||
* @param {import("../themes.js").ThemeDefinition} theme
|
||||
*/
|
||||
const syncUI = (theme) => {
|
||||
if (currentNameEl) currentNameEl.textContent = theme.name;
|
||||
|
||||
cards.forEach((card) => {
|
||||
const isActive = card.dataset.themeCard === theme.id;
|
||||
card.dataset.active = String(isActive);
|
||||
|
||||
const badge = card.querySelector(".theme-card__active");
|
||||
const btn = card.querySelector("[data-theme-select]");
|
||||
|
||||
if (badge) badge.hidden = !isActive;
|
||||
if (btn) {
|
||||
btn.textContent = isActive ? "Design actif" : "Appliquer ce design";
|
||||
btn.setAttribute("aria-pressed", String(isActive));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Annonce un changement de thème aux lecteurs d'écran.
|
||||
* @param {string} message
|
||||
*/
|
||||
const announce = (message) => {
|
||||
if (!status) return;
|
||||
status.textContent = "";
|
||||
requestAnimationFrame(() => {
|
||||
status.textContent = message;
|
||||
});
|
||||
};
|
||||
|
||||
// Actions « Surprise » (dock + pied de modale)
|
||||
const handleSurprise = () => {
|
||||
const next = pickSurpriseTheme();
|
||||
announce(`Design activé : ${next.name} (${next.subtitle}).`);
|
||||
};
|
||||
|
||||
dock.querySelector("[data-theme-surprise]")?.addEventListener("click", handleSurprise);
|
||||
modal.querySelector("[data-theme-surprise]")?.addEventListener("click", handleSurprise);
|
||||
|
||||
// Ouvrir la modale
|
||||
exploreBtn?.addEventListener("click", () => {
|
||||
if (typeof modal.showModal === "function") {
|
||||
modal.showModal();
|
||||
} else {
|
||||
modal.setAttribute("open", "");
|
||||
}
|
||||
});
|
||||
|
||||
// Fermer la modale
|
||||
const closeModal = () => {
|
||||
if (typeof modal.close === "function") modal.close();
|
||||
else modal.removeAttribute("open");
|
||||
exploreBtn?.focus();
|
||||
};
|
||||
|
||||
closeBtn?.addEventListener("click", closeModal);
|
||||
|
||||
// Fermeture au clic sur l'arrière-plan (backdrop)
|
||||
modal.addEventListener("click", (event) => {
|
||||
if (event.target === modal) closeModal();
|
||||
});
|
||||
|
||||
// Sélection directe d'un thème dans la modale
|
||||
modal.addEventListener("click", (event) => {
|
||||
const selectBtn = event.target.closest("[data-theme-select]");
|
||||
if (!selectBtn) return;
|
||||
|
||||
const themeId = selectBtn.dataset.themeSelect;
|
||||
const applied = applyTheme(themeId, { persist: true, updateUrl: true, animate: true });
|
||||
announce(`Design activé : ${applied.name} (${applied.subtitle}).`);
|
||||
});
|
||||
|
||||
// État initial + abonnement aux futurs changements
|
||||
const activeTheme = getThemeById(getActiveThemeId()) ?? THEMES[0];
|
||||
syncUI(activeTheme);
|
||||
onThemeChange(syncUI);
|
||||
}
|
||||
Reference in new issue
Block a user