mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Redesign navigation: slide-in drawers for levels and settings, level header and deep links
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
// Panneau coulissant (tiroir) accessible : dialogue modal avec focus piégé, Échap, clic sur le voile,
|
||||
// retour du focus au déclencheur, reste de la page rendu inerte pendant l'ouverture.
|
||||
// Émet 'open' et 'close'.
|
||||
|
||||
import { el, uniqueId } from './dom.js';
|
||||
|
||||
const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
||||
|
||||
export class Drawer extends EventTarget {
|
||||
#side;
|
||||
#panel;
|
||||
#overlay;
|
||||
#titleElement;
|
||||
#closeButton;
|
||||
#trigger;
|
||||
#inertTargets;
|
||||
#isOpen = false;
|
||||
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {'left'|'right'} options.side
|
||||
* @param {string} options.title Titre (h2) du tiroir.
|
||||
* @param {string} options.closeLabel Libellé accessible du bouton de fermeture.
|
||||
* @param {HTMLElement} options.content Contenu du tiroir.
|
||||
* @param {HTMLElement} options.trigger Bouton qui ouvre le tiroir (reçoit aria-expanded).
|
||||
* @param {HTMLElement[]} [options.inertTargets] Zones rendues inertes pendant l'ouverture.
|
||||
*/
|
||||
constructor({ side, title, closeLabel, content, trigger, inertTargets = [] }) {
|
||||
super();
|
||||
this.#side = side;
|
||||
this.#trigger = trigger;
|
||||
this.#inertTargets = inertTargets;
|
||||
const titleId = uniqueId('drawer-title');
|
||||
const panelId = uniqueId('drawer');
|
||||
|
||||
this.#titleElement = el('h2', { id: titleId, class: 'drawer-title', text: title });
|
||||
this.#closeButton = el('button', { type: 'button', class: 'btn drawer-close', 'aria-label': closeLabel, onclick: () => this.close() }, el('span', { 'aria-hidden': 'true', text: '✕' }));
|
||||
this.#panel = el(
|
||||
'aside',
|
||||
{ id: panelId, class: `drawer drawer-${side}`, role: 'dialog', 'aria-modal': 'true', 'aria-labelledby': titleId, onkeydown: (event) => this.#onKeyDown(event) },
|
||||
el('div', { class: 'drawer-header' }, this.#titleElement, this.#closeButton),
|
||||
el('div', { class: 'drawer-body' }, content),
|
||||
);
|
||||
this.#overlay = el('div', { class: 'drawer-overlay', onclick: () => this.close() });
|
||||
|
||||
trigger.setAttribute('aria-haspopup', 'dialog');
|
||||
trigger.setAttribute('aria-controls', panelId);
|
||||
trigger.setAttribute('aria-expanded', 'false');
|
||||
trigger.addEventListener('click', () => this.toggle());
|
||||
}
|
||||
|
||||
/** Éléments à insérer dans le document (voile puis panneau). */
|
||||
get elements() {
|
||||
return [this.#overlay, this.#panel];
|
||||
}
|
||||
|
||||
get isOpen() {
|
||||
return this.#isOpen;
|
||||
}
|
||||
|
||||
setTitle(title) {
|
||||
this.#titleElement.textContent = title;
|
||||
}
|
||||
|
||||
setCloseLabel(label) {
|
||||
this.#closeButton.setAttribute('aria-label', label);
|
||||
}
|
||||
|
||||
open() {
|
||||
if (this.#isOpen) {
|
||||
return;
|
||||
}
|
||||
this.#isOpen = true;
|
||||
this.#panel.classList.add('is-open');
|
||||
this.#overlay.classList.add('is-open');
|
||||
this.#trigger.setAttribute('aria-expanded', 'true');
|
||||
this.#inertTargets.forEach((target) => target.setAttribute('inert', ''));
|
||||
this.#closeButton.focus();
|
||||
this.dispatchEvent(new CustomEvent('open'));
|
||||
}
|
||||
|
||||
close({ restoreFocus = true } = {}) {
|
||||
if (!this.#isOpen) {
|
||||
return;
|
||||
}
|
||||
this.#isOpen = false;
|
||||
this.#panel.classList.remove('is-open');
|
||||
this.#overlay.classList.remove('is-open');
|
||||
this.#trigger.setAttribute('aria-expanded', 'false');
|
||||
this.#inertTargets.forEach((target) => target.removeAttribute('inert'));
|
||||
if (restoreFocus) {
|
||||
this.#trigger.focus();
|
||||
}
|
||||
this.dispatchEvent(new CustomEvent('close'));
|
||||
}
|
||||
|
||||
toggle() {
|
||||
if (this.#isOpen) {
|
||||
this.close();
|
||||
} else {
|
||||
this.open();
|
||||
}
|
||||
}
|
||||
|
||||
#onKeyDown(event) {
|
||||
if (event.key === 'Escape') {
|
||||
event.stopPropagation();
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
if (event.key !== 'Tab') {
|
||||
return;
|
||||
}
|
||||
// Piège à focus : Tab et Maj+Tab restent dans le tiroir.
|
||||
const focusable = [...this.#panel.querySelectorAll(FOCUSABLE)].filter((element) => !element.hidden);
|
||||
if (focusable.length === 0) {
|
||||
return;
|
||||
}
|
||||
const first = focusable[0];
|
||||
const last = focusable[focusable.length - 1];
|
||||
if (event.shiftKey && document.activeElement === first) {
|
||||
event.preventDefault();
|
||||
last.focus();
|
||||
} else if (!event.shiftKey && document.activeElement === last) {
|
||||
event.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,8 +41,11 @@ export class ProgressMap extends EventTarget {
|
||||
);
|
||||
return el('li', {}, button);
|
||||
});
|
||||
const doneCount = levels.filter(({ id }) => id in progress.completedLevels).length;
|
||||
const summary = this.#i18n.t('progress.levels_done', { done: doneCount, total: levels.length });
|
||||
this.#root.replaceChildren(
|
||||
el('h2', { text: this.#i18n.t('progress.title') }),
|
||||
el('p', { class: 'progress-count', id: 'progress-count', text: summary }),
|
||||
el('progress', { class: 'progress-bar', max: levels.length, value: doneCount, 'aria-labelledby': 'progress-count' }),
|
||||
el('p', { class: 'progress-summary', text: `${this.#i18n.t('progress.xp', { xp: progress.xp })} · ${this.#i18n.t('progress.streak', { days: progress.streak.current })}` }),
|
||||
el('ol', { class: 'level-list' }, ...items),
|
||||
);
|
||||
|
||||
@@ -23,7 +23,6 @@ export class SettingsPanel extends EventTarget {
|
||||
render(settings) {
|
||||
const t = (key) => this.#i18n.t(key);
|
||||
this.#root.replaceChildren(
|
||||
el('h2', { text: t('settings.title') }),
|
||||
this.#field('settings.language', 'select', {
|
||||
value: settings.lang,
|
||||
options: this.#i18n.languages.map((lang) => [lang, lang.toUpperCase()]),
|
||||
@@ -44,6 +43,11 @@ export class SettingsPanel extends EventTarget {
|
||||
);
|
||||
}
|
||||
|
||||
/** Redonne le focus à un réglage après un nouveau rendu (ex. changement de langue). */
|
||||
focusSetting(key) {
|
||||
this.#root.querySelector(`[data-setting="${key}"]`)?.focus();
|
||||
}
|
||||
|
||||
#testButton(labelKey, kind) {
|
||||
const button = el('button', {
|
||||
type: 'button',
|
||||
@@ -69,6 +73,7 @@ export class SettingsPanel extends EventTarget {
|
||||
} else {
|
||||
control = el('input', { id, type: 'range', value, ...range, onchange: (event) => emit(number ? Number(event.target.value) : event.target.value) });
|
||||
}
|
||||
control.dataset.setting = key;
|
||||
return el('div', { class: 'setting' }, el('label', { for: id, text: this.#i18n.t(labelKey) }), control);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user