MediaWiki:Gadget-bhc-accessibility.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
"use strict"; | "use strict"; | ||
// | // ========================================================= | ||
// | // Storage policy: | ||
// -- | // - localStorage first | ||
// - cookie fallback only if localStorage unavailable | |||
// ========================================================= | |||
const LS_KEY = "bhc_a11y_state_v1"; | |||
const COOKIE_PREFIX = "a11y_"; | const COOKIE_PREFIX = "a11y_"; | ||
function canUseLocalStorage() { | |||
try { | |||
const k = "__a11y_test__"; | |||
window.localStorage.setItem(k, "1"); | |||
window.localStorage.removeItem(k); | |||
return true; | |||
} catch (e) { | |||
return false; | |||
} | |||
} | |||
const HAS_LS = canUseLocalStorage(); | |||
// Cookie helpers (fallback) | |||
function setCookie(name, value, days = 365) { | function setCookie(name, value, days = 365) { | ||
const exp = new Date(); | const exp = new Date(); | ||
| Line 18: | Line 34: | ||
`expires=${exp.toUTCString()}; path=/; SameSite=Lax`; | `expires=${exp.toUTCString()}; path=/; SameSite=Lax`; | ||
} | } | ||
function getCookie(name, fallback = "") { | function getCookie(name, fallback = "") { | ||
const key = encodeURIComponent(COOKIE_PREFIX + name) + "="; | const key = encodeURIComponent(COOKIE_PREFIX + name) + "="; | ||
| Line 27: | Line 42: | ||
return fallback; | return fallback; | ||
} | } | ||
function delCookie(name) { | function delCookie(name) { | ||
document.cookie = | document.cookie = | ||
| Line 34: | Line 48: | ||
} | } | ||
// -- | // Unified read/write | ||
function saveStateToStorage(stateObj) { | |||
if (HAS_LS) { | |||
try { | |||
window.localStorage.setItem(LS_KEY, JSON.stringify(stateObj)); | |||
return; | |||
} catch (e) { | |||
// fall through to cookies | |||
} | |||
} | |||
// cookie fallback (per-key) | |||
setCookie("contrast", stateObj.contrast ? "1" : "0"); | |||
setCookie("underline", stateObj.underline ? "1" : "0"); | |||
setCookie("spacing", stateObj.spacing); | |||
setCookie("cursor", stateObj.cursor); | |||
setCookie("ruler", stateObj.ruler ? "1" : "0"); | |||
setCookie("fontPct", String(stateObj.fontPct)); | |||
} | |||
function loadStateFromStorage() { | |||
if (HAS_LS) { | |||
try { | |||
const raw = window.localStorage.getItem(LS_KEY); | |||
if (!raw) return null; | |||
const obj = JSON.parse(raw); | |||
return (obj && typeof obj === "object") ? obj : null; | |||
} catch (e) { | |||
// fall through to cookies | |||
} | |||
} | |||
// cookie fallback (legacy / no-LS) | |||
return { | |||
contrast: getCookie("contrast", "0") === "1", | |||
underline: getCookie("underline", "0") === "1", | |||
spacing: getCookie("spacing", "off"), | |||
cursor: getCookie("cursor", "off"), | |||
ruler: getCookie("ruler", "0") === "1", | |||
fontPct: parseInt(getCookie("fontPct", "100"), 10) | |||
}; | |||
} | |||
function clearStoredState() { | |||
if (HAS_LS) { | |||
try { window.localStorage.removeItem(LS_KEY); } catch (e) {} | |||
return; | |||
} | |||
["contrast","underline","spacing","cursor","ruler","fontPct"].forEach(delCookie); | |||
} | |||
// ========================================================= | |||
// State + announcements | // State + announcements | ||
// | // ========================================================= | ||
const STATE = { | const STATE = { | ||
contrast: false, | contrast: false, | ||
| Line 43: | Line 106: | ||
cursor: "off", // off | white | yellow | cursor: "off", // off | white | yellow | ||
ruler: false, | ruler: false, | ||
fontPct: 100 // 90.. | fontPct: 100 // 90..175 | ||
}; | }; | ||
// Reaches 175 cleanly (rather than “+12 forever”) | |||
const FONT_STEPS = [90, 102, 114, 126, 138, 150, 162, 175]; | |||
let liveEl = null; | let liveEl = null; | ||
| Line 58: | Line 124: | ||
function clamp(n, min, max) { return Math.max(min, Math.min(max, n)); } | function clamp(n, min, max) { return Math.max(min, Math.min(max, n)); } | ||
// -- | function nextFontPct(current, dir) { | ||
const cur = clamp(current, FONT_STEPS[0], FONT_STEPS[FONT_STEPS.length - 1]); | |||
let idx = 0; | |||
for (let i = 0; i < FONT_STEPS.length; i++) { | |||
if (FONT_STEPS[i] >= cur) { idx = i; break; } | |||
} | |||
if (dir > 0) return FONT_STEPS[Math.min(FONT_STEPS.length - 1, idx + 1)]; | |||
// dir < 0 | |||
// if we're exactly on a step, move down one; otherwise go to previous step | |||
if (FONT_STEPS[idx] === cur) return FONT_STEPS[Math.max(0, idx - 1)]; | |||
return FONT_STEPS[Math.max(0, idx - 1)]; | |||
} | |||
// ========================================================= | |||
// Apply state to DOM | // Apply state to DOM | ||
// | // ========================================================= | ||
function applyState() { | function applyState() { | ||
document.body.classList.toggle("a11y-contrast", !!STATE.contrast); | document.body.classList.toggle("a11y-contrast", !!STATE.contrast); | ||
| Line 84: | Line 163: | ||
updateUiButtons(); | updateUiButtons(); | ||
saveStateToStorage(STATE); | |||
} | } | ||
// | // ========================================================= | ||
// Ruler support | // Ruler support | ||
// | // ========================================================= | ||
function ensureRuler() { | function ensureRuler() { | ||
if (rulerEl) return; | if (rulerEl) return; | ||
| Line 124: | Line 204: | ||
e.preventDefault(); | e.preventDefault(); | ||
STATE.ruler = false; | STATE.ruler = false; | ||
applyState(); | applyState(); | ||
announce("Reading ruler off"); | announce("Reading ruler off"); | ||
| Line 145: | Line 224: | ||
} | } | ||
// | // ========================================================= | ||
// UI creation | // UI creation | ||
// | // ========================================================= | ||
function ensureUi() { | function ensureUi() { | ||
if (document.getElementById("a11y-toggle")) return; | if (document.getElementById("a11y-toggle")) return; | ||
const helpHref = (window.mw && mw.util && mw.util.getUrl) | const helpHref = (window.mw && mw.util && mw.util.getUrl) | ||
? mw.util.getUrl("Accessibility") | ? mw.util.getUrl("Accessibility") | ||
| Line 178: | Line 256: | ||
panel.hidden = true; | panel.hidden = true; | ||
// NOTE: spacing controls moved ABOVE contrast controls (your priority #1) | |||
panel.innerHTML = ` | panel.innerHTML = ` | ||
<div class="a11y-head"> | <div class="a11y-head"> | ||
| Line 188: | Line 267: | ||
<button type="button" class="a11y-btn" data-action="font-plus" aria-pressed="false" aria-label="Increase text size">A+</button> | <button type="button" class="a11y-btn" data-action="font-plus" aria-pressed="false" aria-label="Increase text size">A+</button> | ||
<button type="button" class="a11y-btn" data-action="font-reset" aria-pressed="false" aria-label="Reset text size">Reset</button> | <button type="button" class="a11y-btn" data-action="font-reset" aria-pressed="false" aria-label="Reset text size">Reset</button> | ||
</div> | </div> | ||
<div class="a11y-row" role="group" aria-label="Text spacing"> | <div class="a11y-row" role="group" aria-label="Text spacing"> | ||
<button type="button" class="a11y-btn" data-action="spacing-minus" aria-pressed="false">Less spacing</button> | <button type="button" class="a11y-btn" data-action="spacing-minus" aria-pressed="false">Less spacing</button> | ||
<button type="button" class="a11y-btn" data-action="spacing-plus" aria-pressed="false">More spacing</button> | <button type="button" class="a11y-btn" data-action="spacing-plus" aria-pressed="false">More spacing</button> | ||
<button type="button" class="a11y-btn" data-action="spacing-reset" aria-pressed="false">Spacing reset</button> | <button type="button" class="a11y-btn" data-action="spacing-reset" aria-pressed="false">Spacing reset</button> | ||
</div> | |||
<div class="a11y-row" role="group" aria-label="Contrast and links"> | |||
<button type="button" class="a11y-btn" data-action="contrast" aria-pressed="false">High contrast</button> | |||
<button type="button" class="a11y-btn" data-action="underline" aria-pressed="false">Underline links</button> | |||
</div> | </div> | ||
| Line 276: | Line 354: | ||
setPressed("spacing-minus", STATE.spacing === "minus"); | setPressed("spacing-minus", STATE.spacing === "minus"); | ||
setPressed("spacing-reset", false); | setPressed("spacing-reset", false); | ||
setPressed("font-plus", false); | setPressed("font-plus", false); | ||
setPressed("font-minus", false); | setPressed("font-minus", false); | ||
setPressed("font-reset", STATE.fontPct !== 100); | setPressed("font-reset", STATE.fontPct !== 100); | ||
setPressed("reset", false); | setPressed("reset", false); | ||
| Line 294: | Line 374: | ||
} | } | ||
// | // ========================================================= | ||
// Actions | // Actions | ||
// | // ========================================================= | ||
function handleAction(action) { | function handleAction(action) { | ||
switch (action) { | switch (action) { | ||
case "contrast": | case "contrast": | ||
STATE.contrast = !STATE.contrast; | STATE.contrast = !STATE.contrast; | ||
applyState(); | applyState(); | ||
announce(`High contrast ${STATE.contrast ? "on" : "off"}`); | announce(`High contrast ${STATE.contrast ? "on" : "off"}`); | ||
| Line 308: | Line 387: | ||
case "underline": | case "underline": | ||
STATE.underline = !STATE.underline; | STATE.underline = !STATE.underline; | ||
applyState(); | applyState(); | ||
announce(`Underline links ${STATE.underline ? "on" : "off"}`); | announce(`Underline links ${STATE.underline ? "on" : "off"}`); | ||
| Line 315: | Line 393: | ||
case "spacing-plus": | case "spacing-plus": | ||
STATE.spacing = (STATE.spacing === "plus") ? "off" : "plus"; | STATE.spacing = (STATE.spacing === "plus") ? "off" : "plus"; | ||
applyState(); | applyState(); | ||
announce(STATE.spacing === "plus" ? "Text spacing increased" : "Text spacing normal"); | announce(STATE.spacing === "plus" ? "Text spacing increased" : "Text spacing normal"); | ||
| Line 322: | Line 399: | ||
case "spacing-minus": | case "spacing-minus": | ||
STATE.spacing = (STATE.spacing === "minus") ? "off" : "minus"; | STATE.spacing = (STATE.spacing === "minus") ? "off" : "minus"; | ||
applyState(); | applyState(); | ||
announce(STATE.spacing === "minus" ? "Text spacing decreased" : "Text spacing normal"); | announce(STATE.spacing === "minus" ? "Text spacing decreased" : "Text spacing normal"); | ||
| Line 329: | Line 405: | ||
case "spacing-reset": | case "spacing-reset": | ||
STATE.spacing = "off"; | STATE.spacing = "off"; | ||
applyState(); | applyState(); | ||
announce("Text spacing reset"); | announce("Text spacing reset"); | ||
| Line 336: | Line 411: | ||
case "cursor": | case "cursor": | ||
STATE.cursor = (STATE.cursor === "off") ? "white" : (STATE.cursor === "white") ? "yellow" : "off"; | STATE.cursor = (STATE.cursor === "off") ? "white" : (STATE.cursor === "white") ? "yellow" : "off"; | ||
applyState(); | applyState(); | ||
announce( | announce( | ||
| Line 347: | Line 421: | ||
case "ruler": | case "ruler": | ||
STATE.ruler = !STATE.ruler; | STATE.ruler = !STATE.ruler; | ||
if (STATE.ruler && !rulerY) rulerY = Math.floor(window.innerHeight / 2); | if (STATE.ruler && !rulerY) rulerY = Math.floor(window.innerHeight / 2); | ||
applyState(); | applyState(); | ||
| Line 354: | Line 427: | ||
case "font-plus": | case "font-plus": | ||
STATE.fontPct = | STATE.fontPct = nextFontPct(STATE.fontPct, +1); | ||
applyState(); | applyState(); | ||
announce( | announce(`Text size ${STATE.fontPct}%`); | ||
return; | return; | ||
case "font-minus": | case "font-minus": | ||
STATE.fontPct = | STATE.fontPct = nextFontPct(STATE.fontPct, -1); | ||
applyState(); | applyState(); | ||
announce( | announce(`Text size ${STATE.fontPct}%`); | ||
return; | return; | ||
case "font-reset": | case "font-reset": | ||
STATE.fontPct = 100; | STATE.fontPct = 100; | ||
applyState(); | applyState(); | ||
announce("Text size reset"); | announce("Text size reset"); | ||
| Line 375: | Line 445: | ||
case "reset": | case "reset": | ||
clearStoredState(); | |||
STATE.contrast = false; | STATE.contrast = false; | ||
STATE.underline = false; | STATE.underline = false; | ||
| Line 389: | Line 459: | ||
} | } | ||
// | // ========================================================= | ||
// Load saved settings | // Load saved settings | ||
// | // ========================================================= | ||
function loadState() { | function loadState() { | ||
STATE.contrast = | const saved = loadStateFromStorage(); | ||
STATE.underline = | if (!saved) return; | ||
STATE.contrast = !!saved.contrast; | |||
STATE.underline = !!saved.underline; | |||
const spacing = | const spacing = saved.spacing; | ||
STATE.spacing = (spacing === "plus" || spacing === "minus") ? spacing : "off"; | STATE.spacing = (spacing === "plus" || spacing === "minus") ? spacing : "off"; | ||
const cursor = | const cursor = saved.cursor; | ||
STATE.cursor = (cursor === "white" || cursor === "yellow") ? cursor : "off"; | STATE.cursor = (cursor === "white" || cursor === "yellow") ? cursor : "off"; | ||
STATE.ruler = | STATE.ruler = !!saved.ruler; | ||
const fp = parseInt( | const fp = parseInt(saved.fontPct, 10); | ||
STATE.fontPct = | STATE.fontPct = isNaN(fp) ? 100 : clamp(fp, FONT_STEPS[0], FONT_STEPS[FONT_STEPS.length - 1]); | ||
} | } | ||
// | // ========================================================= | ||
// Init | // Init | ||
// | // ========================================================= | ||
function init() { | function init() { | ||
ensureUi(); | ensureUi(); | ||
Revision as of 18:47, 27 January 2026
/*
⚠️ Do not reorder rules below this line — contrast overrides depend on cascade order.
*/
(() => {
"use strict";
// =========================================================
// Storage policy:
// - localStorage first
// - cookie fallback only if localStorage unavailable
// =========================================================
const LS_KEY = "bhc_a11y_state_v1";
const COOKIE_PREFIX = "a11y_";
function canUseLocalStorage() {
try {
const k = "__a11y_test__";
window.localStorage.setItem(k, "1");
window.localStorage.removeItem(k);
return true;
} catch (e) {
return false;
}
}
const HAS_LS = canUseLocalStorage();
// Cookie helpers (fallback)
function setCookie(name, value, days = 365) {
const exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie =
`${encodeURIComponent(COOKIE_PREFIX + name)}=${encodeURIComponent(String(value))}; ` +
`expires=${exp.toUTCString()}; path=/; SameSite=Lax`;
}
function getCookie(name, fallback = "") {
const key = encodeURIComponent(COOKIE_PREFIX + name) + "=";
const parts = document.cookie.split(";").map(s => s.trim());
for (const p of parts) {
if (p.startsWith(key)) return decodeURIComponent(p.substring(key.length));
}
return fallback;
}
function delCookie(name) {
document.cookie =
`${encodeURIComponent(COOKIE_PREFIX + name)}=; ` +
`expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; SameSite=Lax`;
}
// Unified read/write
function saveStateToStorage(stateObj) {
if (HAS_LS) {
try {
window.localStorage.setItem(LS_KEY, JSON.stringify(stateObj));
return;
} catch (e) {
// fall through to cookies
}
}
// cookie fallback (per-key)
setCookie("contrast", stateObj.contrast ? "1" : "0");
setCookie("underline", stateObj.underline ? "1" : "0");
setCookie("spacing", stateObj.spacing);
setCookie("cursor", stateObj.cursor);
setCookie("ruler", stateObj.ruler ? "1" : "0");
setCookie("fontPct", String(stateObj.fontPct));
}
function loadStateFromStorage() {
if (HAS_LS) {
try {
const raw = window.localStorage.getItem(LS_KEY);
if (!raw) return null;
const obj = JSON.parse(raw);
return (obj && typeof obj === "object") ? obj : null;
} catch (e) {
// fall through to cookies
}
}
// cookie fallback (legacy / no-LS)
return {
contrast: getCookie("contrast", "0") === "1",
underline: getCookie("underline", "0") === "1",
spacing: getCookie("spacing", "off"),
cursor: getCookie("cursor", "off"),
ruler: getCookie("ruler", "0") === "1",
fontPct: parseInt(getCookie("fontPct", "100"), 10)
};
}
function clearStoredState() {
if (HAS_LS) {
try { window.localStorage.removeItem(LS_KEY); } catch (e) {}
return;
}
["contrast","underline","spacing","cursor","ruler","fontPct"].forEach(delCookie);
}
// =========================================================
// State + announcements
// =========================================================
const STATE = {
contrast: false,
underline: false,
spacing: "off", // off | plus | minus
cursor: "off", // off | white | yellow
ruler: false,
fontPct: 100 // 90..175
};
// Reaches 175 cleanly (rather than “+12 forever”)
const FONT_STEPS = [90, 102, 114, 126, 138, 150, 162, 175];
let liveEl = null;
let rulerEl = null;
let rulerY = 0;
function announce(msg) {
if (!liveEl) return;
liveEl.textContent = "";
setTimeout(() => { liveEl.textContent = msg; }, 10);
}
function clamp(n, min, max) { return Math.max(min, Math.min(max, n)); }
function nextFontPct(current, dir) {
const cur = clamp(current, FONT_STEPS[0], FONT_STEPS[FONT_STEPS.length - 1]);
let idx = 0;
for (let i = 0; i < FONT_STEPS.length; i++) {
if (FONT_STEPS[i] >= cur) { idx = i; break; }
}
if (dir > 0) return FONT_STEPS[Math.min(FONT_STEPS.length - 1, idx + 1)];
// dir < 0
// if we're exactly on a step, move down one; otherwise go to previous step
if (FONT_STEPS[idx] === cur) return FONT_STEPS[Math.max(0, idx - 1)];
return FONT_STEPS[Math.max(0, idx - 1)];
}
// =========================================================
// Apply state to DOM
// =========================================================
function applyState() {
document.body.classList.toggle("a11y-contrast", !!STATE.contrast);
document.body.classList.toggle("a11y-underline-links", !!STATE.underline);
document.body.classList.toggle("a11y-spacing-plus", STATE.spacing === "plus");
document.body.classList.toggle("a11y-spacing-minus", STATE.spacing === "minus");
document.body.classList.toggle("a11y-cursor-white", STATE.cursor === "white");
document.body.classList.toggle("a11y-cursor-yellow", STATE.cursor === "yellow");
document.documentElement.style.fontSize = `${STATE.fontPct}%`;
ensureRuler();
if (STATE.ruler) {
rulerEl.hidden = false;
setRulerY(rulerY || Math.floor(window.innerHeight / 2));
enableRulerListeners();
} else {
if (rulerEl) rulerEl.hidden = true;
disableRulerListeners();
}
updateUiButtons();
saveStateToStorage(STATE);
}
// =========================================================
// Ruler support
// =========================================================
function ensureRuler() {
if (rulerEl) return;
rulerEl = document.getElementById("a11y-ruler");
if (!rulerEl) {
rulerEl = document.createElement("div");
rulerEl.id = "a11y-ruler";
rulerEl.hidden = true;
document.body.appendChild(rulerEl);
}
}
function setRulerY(y) {
rulerY = clamp(y, 0, Math.max(0, window.innerHeight - 1));
if (rulerEl) rulerEl.style.transform = `translateY(${rulerY}px)`;
}
function onMouseMove(e) {
if (!STATE.ruler) return;
setRulerY(e.clientY - 11);
}
function onKeyDown(e) {
if (!STATE.ruler) return;
if (e.key === "ArrowDown") {
e.preventDefault();
setRulerY(rulerY + 10);
announce("Reading ruler moved down");
} else if (e.key === "ArrowUp") {
e.preventDefault();
setRulerY(rulerY - 10);
announce("Reading ruler moved up");
} else if (e.key === "Escape") {
e.preventDefault();
STATE.ruler = false;
applyState();
announce("Reading ruler off");
}
}
let rulerListenersOn = false;
function enableRulerListeners() {
if (rulerListenersOn) return;
rulerListenersOn = true;
window.addEventListener("mousemove", onMouseMove, { passive: true });
window.addEventListener("keydown", onKeyDown);
window.addEventListener("resize", () => setRulerY(rulerY), { passive: true });
}
function disableRulerListeners() {
if (!rulerListenersOn) return;
rulerListenersOn = false;
window.removeEventListener("mousemove", onMouseMove);
window.removeEventListener("keydown", onKeyDown);
}
// =========================================================
// UI creation
// =========================================================
function ensureUi() {
if (document.getElementById("a11y-toggle")) return;
const helpHref = (window.mw && mw.util && mw.util.getUrl)
? mw.util.getUrl("Accessibility")
: "/index.php/Accessibility";
const toggle = document.createElement("button");
toggle.type = "button";
toggle.id = "a11y-toggle";
toggle.className = "a11y-toggle";
toggle.setAttribute("aria-label", "Accessibility options");
toggle.setAttribute("aria-haspopup", "dialog");
toggle.setAttribute("aria-expanded", "false");
toggle.innerHTML = `
<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
<path d="M12 2a2 2 0 1 0 .001 4.001A2 2 0 0 0 12 2zm9 7h-6.5c-.7 0-1.3.4-1.6 1L12 12l-.9-2c-.3-.6-.9-1-1.6-1H3a1 1 0 0 0 0 2h5.3l1.3 2.9-1.7 6.2a1 1 0 0 0 1.9.6l1.2-4.3 1.2 4.3a1 1 0 0 0 1.9-.6l-1.7-6.2 1.3-2.9H21a1 1 0 0 0 0-2z"/>
</svg>
`;
document.body.appendChild(toggle);
const panel = document.createElement("div");
panel.id = "a11y-panel";
panel.className = "a11y-panel";
panel.setAttribute("role", "dialog");
panel.setAttribute("aria-label", "Accessibility options");
panel.hidden = true;
// NOTE: spacing controls moved ABOVE contrast controls (your priority #1)
panel.innerHTML = `
<div class="a11y-head">
<h2>Accessibility</h2>
<a class="a11y-help" href="${helpHref}" aria-label="Accessibility help">?</a>
</div>
<div class="a11y-row" role="group" aria-label="Text size">
<button type="button" class="a11y-btn" data-action="font-minus" aria-pressed="false" aria-label="Decrease text size">A−</button>
<button type="button" class="a11y-btn" data-action="font-plus" aria-pressed="false" aria-label="Increase text size">A+</button>
<button type="button" class="a11y-btn" data-action="font-reset" aria-pressed="false" aria-label="Reset text size">Reset</button>
</div>
<div class="a11y-row" role="group" aria-label="Text spacing">
<button type="button" class="a11y-btn" data-action="spacing-minus" aria-pressed="false">Less spacing</button>
<button type="button" class="a11y-btn" data-action="spacing-plus" aria-pressed="false">More spacing</button>
<button type="button" class="a11y-btn" data-action="spacing-reset" aria-pressed="false">Spacing reset</button>
</div>
<div class="a11y-row" role="group" aria-label="Contrast and links">
<button type="button" class="a11y-btn" data-action="contrast" aria-pressed="false">High contrast</button>
<button type="button" class="a11y-btn" data-action="underline" aria-pressed="false">Underline links</button>
</div>
<div class="a11y-row" role="group" aria-label="Cursor and reading aid">
<button type="button" class="a11y-btn" data-action="cursor" aria-pressed="false" aria-label="Large cursor setting">Large cursor: Off</button>
<button type="button" class="a11y-btn" data-action="ruler" aria-pressed="false" aria-label="Toggle reading ruler">Reading ruler</button>
</div>
<div class="a11y-row" role="group" aria-label="Reset all">
<button type="button" class="a11y-btn" data-action="reset" aria-pressed="false">Reset all</button>
</div>
<div class="a11y-hint">
Settings are saved on this device. Large cursor cycles <strong>Off → White → Yellow → Off</strong>.
When Reading ruler is on, use <strong>↑/↓</strong> to move it and <strong>Esc</strong> to turn it off.
</div>
<div id="a11y-live" class="a11y-sr" aria-live="polite" aria-atomic="true"></div>
`;
document.body.appendChild(panel);
liveEl = panel.querySelector("#a11y-live");
function openPanel() {
panel.hidden = false;
toggle.setAttribute("aria-expanded", "true");
const firstBtn = panel.querySelector("button.a11y-btn");
if (firstBtn) firstBtn.focus();
}
function closePanel() {
panel.hidden = true;
toggle.setAttribute("aria-expanded", "false");
toggle.focus();
}
toggle.addEventListener("click", () => {
if (panel.hidden) openPanel(); else closePanel();
});
document.addEventListener("click", (e) => {
if (panel.hidden) return;
if (panel.contains(e.target) || toggle.contains(e.target)) return;
closePanel();
});
document.addEventListener("keydown", (e) => {
if (panel.hidden) return;
if (e.key === "Escape") {
e.preventDefault();
closePanel();
}
});
panel.addEventListener("click", (e) => {
const btn = e.target.closest("button[data-action]");
if (!btn) return;
const action = btn.getAttribute("data-action") || "";
handleAction(action);
});
}
function updateUiButtons() {
const panel = document.getElementById("a11y-panel");
if (!panel) return;
const setPressed = (action, pressed) => {
const b = panel.querySelector(`button[data-action="${action}"]`);
if (b) b.setAttribute("aria-pressed", pressed ? "true" : "false");
};
setPressed("contrast", STATE.contrast);
setPressed("underline", STATE.underline);
setPressed("ruler", STATE.ruler);
setPressed("cursor", STATE.cursor !== "off");
setPressed("spacing-plus", STATE.spacing === "plus");
setPressed("spacing-minus", STATE.spacing === "minus");
setPressed("spacing-reset", false);
setPressed("font-plus", false);
setPressed("font-minus", false);
setPressed("font-reset", STATE.fontPct !== 100);
setPressed("reset", false);
const cursorBtn = panel.querySelector('button[data-action="cursor"]');
if (cursorBtn) {
const label = (STATE.cursor === "off") ? "Off" : (STATE.cursor === "white") ? "White" : "Yellow";
cursorBtn.textContent = `Large cursor: ${label}`;
cursorBtn.setAttribute(
"aria-label",
STATE.cursor === "off"
? "Large cursor off. Press to turn on"
: `Large cursor ${label}. Press to change colour or turn off`
);
}
}
// =========================================================
// Actions
// =========================================================
function handleAction(action) {
switch (action) {
case "contrast":
STATE.contrast = !STATE.contrast;
applyState();
announce(`High contrast ${STATE.contrast ? "on" : "off"}`);
return;
case "underline":
STATE.underline = !STATE.underline;
applyState();
announce(`Underline links ${STATE.underline ? "on" : "off"}`);
return;
case "spacing-plus":
STATE.spacing = (STATE.spacing === "plus") ? "off" : "plus";
applyState();
announce(STATE.spacing === "plus" ? "Text spacing increased" : "Text spacing normal");
return;
case "spacing-minus":
STATE.spacing = (STATE.spacing === "minus") ? "off" : "minus";
applyState();
announce(STATE.spacing === "minus" ? "Text spacing decreased" : "Text spacing normal");
return;
case "spacing-reset":
STATE.spacing = "off";
applyState();
announce("Text spacing reset");
return;
case "cursor":
STATE.cursor = (STATE.cursor === "off") ? "white" : (STATE.cursor === "white") ? "yellow" : "off";
applyState();
announce(
STATE.cursor === "off" ? "Large cursor off" :
STATE.cursor === "white" ? "Large cursor white" :
"Large cursor yellow"
);
return;
case "ruler":
STATE.ruler = !STATE.ruler;
if (STATE.ruler && !rulerY) rulerY = Math.floor(window.innerHeight / 2);
applyState();
announce(`Reading ruler ${STATE.ruler ? "on" : "off"}`);
return;
case "font-plus":
STATE.fontPct = nextFontPct(STATE.fontPct, +1);
applyState();
announce(`Text size ${STATE.fontPct}%`);
return;
case "font-minus":
STATE.fontPct = nextFontPct(STATE.fontPct, -1);
applyState();
announce(`Text size ${STATE.fontPct}%`);
return;
case "font-reset":
STATE.fontPct = 100;
applyState();
announce("Text size reset");
return;
case "reset":
clearStoredState();
STATE.contrast = false;
STATE.underline = false;
STATE.spacing = "off";
STATE.cursor = "off";
STATE.ruler = false;
STATE.fontPct = 100;
rulerY = 0;
applyState();
announce("Accessibility settings reset");
return;
}
}
// =========================================================
// Load saved settings
// =========================================================
function loadState() {
const saved = loadStateFromStorage();
if (!saved) return;
STATE.contrast = !!saved.contrast;
STATE.underline = !!saved.underline;
const spacing = saved.spacing;
STATE.spacing = (spacing === "plus" || spacing === "minus") ? spacing : "off";
const cursor = saved.cursor;
STATE.cursor = (cursor === "white" || cursor === "yellow") ? cursor : "off";
STATE.ruler = !!saved.ruler;
const fp = parseInt(saved.fontPct, 10);
STATE.fontPct = isNaN(fp) ? 100 : clamp(fp, FONT_STEPS[0], FONT_STEPS[FONT_STEPS.length - 1]);
}
// =========================================================
// Init
// =========================================================
function init() {
ensureUi();
loadState();
applyState();
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();
