MediaWiki:Gadget-bhc-accessibility.css: Difference between revisions

Undo revision 1526 by Steve (talk)
Tag: Undo
No edit summary
Line 3: Line 3:
*/
*/


/* =========================================================
(() => {
  BHC Accessibility Gadget – MediaWiki / Timeless
  "use strict";
  ========================================================= */


/* ---------------------------------------------------------
  // -------------------------------------------------------------------
  Variables
  // Cookie helpers
  --------------------------------------------------------- */
  // -------------------------------------------------------------------
:root{
  const COOKIE_PREFIX = "a11y_";
  --a11y-bg: #111827;
  --a11y-border: #374151;
  --a11y-fg: #ffffff;
  --a11y-focus: #FFBF47;
  --a11y-link-underline: #36C; /* MediaWiki link blue */
  --a11y-menu-width: 340px;
}


/* Timeless: stop skin CSS shrinking the accessibility icon */
  function setCookie(name, value, days = 365) {
body.skin-timeless .a11y-toggle{
    const exp = new Date();
  font-size: 16px !important;
    exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
  line-height: 1 !important;
    document.cookie =
}
      `${encodeURIComponent(COOKIE_PREFIX + name)}=${encodeURIComponent(String(value))}; ` +
body.skin-timeless .a11y-toggle svg{
      `expires=${exp.toUTCString()}; path=/; SameSite=Lax`;
  display: block !important;
   }
  width: 36px !important;
  height: 36px !important;
  min-width: 36px !important;
  min-height: 36px !important;
  max-width: none !important;
   max-height: none !important;


   /* If the glyph still looks small, keep this: */
   function getCookie(name, fallback = "") {
  transform: scale(1.15) !important;
    const key = encodeURIComponent(COOKIE_PREFIX + name) + "=";
  transform-origin: 50% 50% !important;
    const parts = document.cookie.split(";").map(s => s.trim());
}
    for (const p of parts) {
/* Make sure fill isn’t being altered by skin rules */
      if (p.startsWith(key)) return decodeURIComponent(p.substring(key.length));
body.skin-timeless .a11y-toggle svg path{
    }
  fill: currentColor !important;
    return fallback;
}
  }


/* ---------------------------------------------------------
  function delCookie(name) {
  Skip links (appear when focused)
    document.cookie =
  --------------------------------------------------------- */
      `${encodeURIComponent(COOKIE_PREFIX + name)}=; ` +
.skip-link{
      `expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; SameSite=Lax`;
  position: absolute;
   }
  left: 12px;
  top: 12px;
   z-index: 11000;


   padding: 10px 14px;
   // -------------------------------------------------------------------
   border-radius: 8px;
  // State + announcements
   // -------------------------------------------------------------------
  const STATE = {
    contrast: false,
    underline: false,
    spacing: "off",          // off | plus | minus
    cursor: "off",          // off | white | yellow
    ruler: false,
    fontPct: 100            // 90..140 in steps of 10
  };


   background: #111827;
   let liveEl = null;
   color: #fff;
   let rulerEl = null;
   text-decoration: none;
   let rulerY = 0;


   transform: translateY(-150%);
   function announce(msg) {
  transition: transform 0.15s ease;
    if (!liveEl) return;
  border: 2px solid var(--a11y-focus);
    liveEl.textContent = "";
}
    setTimeout(() => { liveEl.textContent = msg; }, 10);
.skip-link:focus,
   }
.skip-link:focus-visible{
  transform: translateY(0);
   outline: none;
}
body.a11y-contrast .skip-link{
  background: #000 !important;
  color: #FFD400 !important;
  border-color: #FFD400 !important;
}


/* ---------------------------------------------------------
  function clamp(n, min, max) { return Math.max(min, Math.min(max, n)); }
  Floating accessibility toggle
  --------------------------------------------------------- */
.a11y-toggle{
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 10001;


   width: 56px;
   // -------------------------------------------------------------------
   height: 56px;
  // Apply state to DOM
  border-radius: 50%;
  // -------------------------------------------------------------------
   function applyState() {
    document.body.classList.toggle("a11y-contrast", !!STATE.contrast);
    document.body.classList.toggle("a11y-underline-links", !!STATE.underline);


  background: #005FCC;
    document.body.classList.toggle("a11y-spacing-plus", STATE.spacing === "plus");
  border: 2px solid #ffffff;
    document.body.classList.toggle("a11y-spacing-minus", STATE.spacing === "minus");
  color: #ffffff;


  display: inline-flex;
    document.body.classList.toggle("a11y-cursor-white", STATE.cursor === "white");
  align-items: center;
    document.body.classList.toggle("a11y-cursor-yellow", STATE.cursor === "yellow");
  justify-content: center;


  box-shadow: 0 10px 24px rgba(0,0,0,0.25);
    document.documentElement.style.fontSize = `${STATE.fontPct}%`;
  cursor: pointer;
}
.a11y-toggle:hover{ background:#0047A6; }
.a11y-toggle:focus-visible{
  outline: 3px solid var(--a11y-focus);
  outline-offset: 3px;
}
.a11y-toggle svg{
  width: 44px;
  height: 44px;
  fill: currentColor;
}


/* ---------------------------------------------------------
    ensureRuler();
  Panel
    if (STATE.ruler) {
  --------------------------------------------------------- */
      rulerEl.hidden = false;
.a11y-panel{
      setRulerY(rulerY || Math.floor(window.innerHeight / 2));
  position: fixed;
      enableRulerListeners();
  right: 14px;
    } else {
  bottom: 76px;
      if (rulerEl) rulerEl.hidden = true;
  z-index: 10000;
      disableRulerListeners();
    }


  width: var(--a11y-menu-width);
    updateUiButtons();
   max-width: calc(100vw - 28px);
   }


   background: var(--a11y-bg);
   // -------------------------------------------------------------------
   border: 1px solid var(--a11y-border);
   // Ruler support
  color: var(--a11y-fg);
  // -------------------------------------------------------------------
  border-radius: 16px;
  function ensureRuler() {
   padding: 12px;
    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);
    }
   }


   box-shadow: 0 14px 40px rgba(0,0,0,0.28);
   function setRulerY(y) {
}
    rulerY = clamp(y, 0, Math.max(0, window.innerHeight - 1));
.a11y-panel[hidden]{ display:none !important; }
    if (rulerEl) rulerEl.style.transform = `translateY(${rulerY}px)`;
  }


/* Header */
   function onMouseMove(e) {
.a11y-head{
    if (!STATE.ruler) return;
   display:flex;
    setRulerY(e.clientY - 11);
  align-items:center;
   }
  justify-content: space-between;
  margin-bottom: 10px;
}
.a11y-head h2{
  font-size: 16px;
  margin: 0;
}
.a11y-help{
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #fff;
  color: #111827;
  font-weight: 900;
  text-decoration: none;
  display:flex;
  align-items:center;
  justify-content:center;
}
.a11y-help:focus-visible{
  outline: 3px solid var(--a11y-focus);
   outline-offset: 2px;
}


/* Buttons */
   function onKeyDown(e) {
.a11y-row{
    if (!STATE.ruler) return;
   display:flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.a11y-btn{
  border: 1px solid #D1D5DB;
  background:#fff;
  color:#111827;
  padding: 8px 10px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.a11y-btn[aria-pressed="true"]{
  background:#111827;
  color:#fff;
}
.a11y-btn:focus-visible{
  outline: 3px solid var(--a11y-focus);
  outline-offset: 2px;
}


.a11y-hint{
    if (e.key === "ArrowDown") {
  font-size: 13px;
      e.preventDefault();
  color: var(--a11y-fg);
      setRulerY(rulerY + 10);
  line-height: 1.4;
      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;
      setCookie("ruler", "0");
      applyState();
      announce("Reading ruler off");
    }
  }


/* Screen reader only */
  let rulerListenersOn = false;
.a11y-sr{
  function enableRulerListeners() {
  position:absolute;
    if (rulerListenersOn) return;
   width:1px;
    rulerListenersOn = true;
   height:1px;
    window.addEventListener("mousemove", onMouseMove, { passive: true });
  overflow:hidden;
    window.addEventListener("keydown", onKeyDown);
  clip:rect(0,0,0,0);
    window.addEventListener("resize", () => setRulerY(rulerY), { passive: true });
}
   }
   function disableRulerListeners() {
    if (!rulerListenersOn) return;
    rulerListenersOn = false;
    window.removeEventListener("mousemove", onMouseMove);
    window.removeEventListener("keydown", onKeyDown);
  }


/* =========================================================
  // -------------------------------------------------------------------
  Feature classes
  // UI creation
  ========================================================= */
  // -------------------------------------------------------------------
  function ensureUi() {
    if (document.getElementById("a11y-toggle")) return;


/* ------------------------------
    // Help link goes to the wiki page (you said: index.php/Accessibility)
  High contrast (black/yellow/blue)
    const helpHref = (window.mw && mw.util && mw.util.getUrl)
  ------------------------------ */
      ? mw.util.getUrl("Accessibility")
body.a11y-contrast{
      : "/index.php/Accessibility";
  background:#000 !important;
  color:#FFD400 !important;


  --a11y-bg: #000;
    const toggle = document.createElement("button");
  --a11y-border: rgba(255,212,0,.7);
    toggle.type = "button";
  --a11y-fg: #FFD400;
    toggle.id = "a11y-toggle";
  --a11y-focus: #FFD400;
    toggle.className = "a11y-toggle";
  --a11y-link-underline: #36C;
    toggle.setAttribute("aria-label", "Accessibility options");
}
    toggle.setAttribute("aria-haspopup", "dialog");
    toggle.setAttribute("aria-expanded", "false");


body.a11y-contrast a,
    toggle.innerHTML = `
body.a11y-contrast a:visited{
      <svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
  color:#FFD400 !important;
        <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);


/* Headings */
    const panel = document.createElement("div");
body.a11y-contrast h1,
    panel.id = "a11y-panel";
body.a11y-contrast h2,
    panel.className = "a11y-panel";
body.a11y-contrast h3,
    panel.setAttribute("role", "dialog");
body.a11y-contrast h4,
    panel.setAttribute("aria-label", "Accessibility options");
body.a11y-contrast h5,
    panel.hidden = true;
body.a11y-contrast h6{
  color:#FFD400 !important;
  border-color:#FFD400 !important;
}


/* Main containers (Timeless needs explicit coverage) */
    panel.innerHTML = `
body.a11y-contrast #mw-wrapper,
      <div class="a11y-head">
body.a11y-contrast #mw-content-container,
        <h2>Accessibility</h2>
body.a11y-contrast #mw-content,
        <a class="a11y-help" href="${helpHref}" aria-label="Accessibility help">?</a>
body.a11y-contrast .mw-body,
      </div>
body.a11y-contrast .mw-parser-output{
  background:#000 !important;
  color:#FFD400 !important;
}


/* Sidebars / tools */
      <div class="a11y-row" role="group" aria-label="Text size">
body.a11y-contrast #site-navigation,
        <button type="button" class="a11y-btn" data-action="font-minus" aria-pressed="false" aria-label="Decrease text size">A−</button>
body.a11y-contrast #site-tools,
        <button type="button" class="a11y-btn" data-action="font-plus" aria-pressed="false" aria-label="Increase text size">A+</button>
body.a11y-contrast #page-tools,
        <button type="button" class="a11y-btn" data-action="font-reset" aria-pressed="false" aria-label="Reset text size">Reset</button>
body.a11y-contrast .mw-portlet,
      </div>
body.a11y-contrast .mw-portlet-body{
  background:#000 !important;
  color:#FFD400 !important;
  border-color: rgba(255,212,0,.7) !important;
}


/* Ensure links inside toolboxes */
      <div class="a11y-row" role="group" aria-label="Contrast and links">
body.a11y-contrast #site-navigation a,
        <button type="button" class="a11y-btn" data-action="contrast" aria-pressed="false">High contrast</button>
body.a11y-contrast #site-tools a,
        <button type="button" class="a11y-btn" data-action="underline" aria-pressed="false">Underline links</button>
body.a11y-contrast #page-tools a{
      </div>
  color:#FFD400 !important;
}


/* A11y UI in contrast */
      <div class="a11y-row" role="group" aria-label="Text spacing">
body.a11y-contrast .a11y-panel,
        <button type="button" class="a11y-btn" data-action="spacing-minus" aria-pressed="false">Less spacing</button>
body.a11y-contrast .a11y-toggle{
        <button type="button" class="a11y-btn" data-action="spacing-plus" aria-pressed="false">More spacing</button>
  background:#000 !important;
        <button type="button" class="a11y-btn" data-action="spacing-reset" aria-pressed="false">Spacing reset</button>
}
      </div>
body.a11y-contrast .a11y-btn{
  background:#000 !important;
  color:#FFD400 !important;
  border:2px solid rgba(255,212,0,.85) !important;
}


/* ------------------------------
      <div class="a11y-row" role="group" aria-label="Cursor and reading aid">
  Underline links
        <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>
/* Underline links – MediaWiki-style */
      </div>
body.a11y-underline-links a{
  text-decoration: none !important;
  border-bottom: 2px solid var(--a11y-link-underline);
  padding-bottom: 0.05em;
}
/* In contrast mode you currently want blue (#36c) */
body.a11y-underline-links.a11y-contrast a{
  border-bottom-color: #36C !important;
}


/* ------------------------------
      <div class="a11y-row" role="group" aria-label="Reset all">
  Text spacing
        <button type="button" class="a11y-btn" data-action="reset" aria-pressed="false">Reset all</button>
  ------------------------------ */
      </div>
body.a11y-spacing-plus{
  line-height: 1.8 !important;
  letter-spacing: 0.06em !important;
  word-spacing: 0.16em !important;
}
body.a11y-spacing-minus{
  line-height: 1.45 !important;
  letter-spacing: 0.01em !important;
  word-spacing: 0.02em !important;
}


/* ------------------------------
      <div class="a11y-hint">
  Large cursors (wiki-hosted)
        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.
body.a11y-cursor-white,
      </div>
body.a11y-cursor-white *{
  cursor: url("/index.php/Special:FilePath/large-arrow-white-64.png") 8 8, auto !important;
}
body.a11y-cursor-yellow,
body.a11y-cursor-yellow *{
  cursor: url("/index.php/Special:FilePath/large-arrow-yellow-64.png") 8 8, auto !important;
}


/* ------------------------------
      <div id="a11y-live" class="a11y-sr" aria-live="polite" aria-atomic="true"></div>
  Reading ruler
    `;
  ------------------------------ */
    document.body.appendChild(panel);
#a11y-ruler{
  position: fixed;
  left: 0;
  top: 0;
  height: 22px;
  width: 100vw;
  z-index: 9999;
  pointer-events: none;


  background: linear-gradient(
    liveEl = panel.querySelector("#a11y-live");
    to bottom,
    #FFD400 0%,
    #FFD400 40%,
    #000000 40%,
    #000000 60%,
    #FFD400 60%,
    #FFD400 100%
  );
}
#a11y-ruler[hidden]{ display:none !important; }


/* =========================================================
    function openPanel() {
  MediaWiki: extra HIGH CONTRAST coverage (Timeless + Vector)
      panel.hidden = false;
  Add at END of Gadget CSS so it wins.
      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();
    }


/* TOC (Timeless/Vector both use variants) */
    toggle.addEventListener("click", () => {
body.a11y-contrast #toc,
      if (panel.hidden) openPanel(); else closePanel();
body.a11y-contrast .toc,
    });
body.a11y-contrast #toc * ,
body.a11y-contrast .toc *{
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255, 212, 0, 0.75) !important;
}


/* Category links area */
    document.addEventListener("click", (e) => {
body.a11y-contrast .mw-normal-catlinks,
      if (panel.hidden) return;
body.a11y-contrast .mw-normal-catlinks *,
      if (panel.contains(e.target) || toggle.contains(e.target)) return;
body.a11y-contrast .catlinks,
      closePanel();
body.a11y-contrast .catlinks *{
    });
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255, 212, 0, 0.75) !important;
}


/* Wikitables (and common table elements that retain dark text) */
    document.addEventListener("keydown", (e) => {
body.a11y-contrast table,
      if (panel.hidden) return;
body.a11y-contrast table *,
      if (e.key === "Escape") {
body.a11y-contrast .wikitable,
        e.preventDefault();
body.a11y-contrast .wikitable *,
        closePanel();
body.a11y-contrast .mw-datatable,
      }
body.a11y-contrast .mw-datatable *{
    });
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255, 212, 0, 0.75) !important;
}


/* Table headers sometimes have their own background */
    panel.addEventListener("click", (e) => {
body.a11y-contrast .wikitable th,
      const btn = e.target.closest("button[data-action]");
body.a11y-contrast table th{
      if (!btn) return;
  background: #000 !important;
      const action = btn.getAttribute("data-action") || "";
  color: #FFD400 !important;
      handleAction(action);
}
    });
  }


/* Figures/captions (centre aligned or otherwise) */
  function updateUiButtons() {
body.a11y-contrast figure,
    const panel = document.getElementById("a11y-panel");
body.a11y-contrast figure *,
    if (!panel) return;
body.a11y-contrast .mw-halign-center,
body.a11y-contrast .mw-halign-centre,
body.a11y-contrast .mw-halign-center *,
body.a11y-contrast .mw-halign-centre *{
  background: #000 !important;
  color: #FFD400 !important;
}


/* Thumb frames / image captions often keep grey/black text */
    const setPressed = (action, pressed) => {
body.a11y-contrast .thumb,
      const b = panel.querySelector(`button[data-action="${action}"]`);
body.a11y-contrast .thumb *,
      if (b) b.setAttribute("aria-pressed", pressed ? "true" : "false");
body.a11y-contrast .thumbinner,
    };
body.a11y-contrast .thumbinner *,
body.a11y-contrast .thumbcaption,
body.a11y-contrast .thumbcaption *{
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255, 212, 0, 0.75) !important;
}


/* “Code-like” blocks sometimes use dark text */
    setPressed("contrast", STATE.contrast);
body.a11y-contrast pre,
    setPressed("underline", STATE.underline);
body.a11y-contrast pre *,
    setPressed("ruler", STATE.ruler);
body.a11y-contrast code,
    setPressed("cursor", STATE.cursor !== "off");
body.a11y-contrast code *{
    setPressed("spacing-plus", STATE.spacing === "plus");
  background: #000 !important;
    setPressed("spacing-minus", STATE.spacing === "minus");
  color: #FFD400 !important;
    setPressed("spacing-reset", false);
  border-color: rgba(255, 212, 0, 0.75) !important;
    setPressed("font-plus", false);
}
    setPressed("font-minus", false);
    setPressed("font-reset", STATE.fontPct !== 100);
    setPressed("reset", false);


/* Safety: ensure visited links don’t revert */
    const cursorBtn = panel.querySelector('button[data-action="cursor"]');
body.a11y-contrast a:visited,
    if (cursorBtn) {
body.a11y-contrast .mw-parser-output a:visited{
      const label = (STATE.cursor === "off") ? "Off" : (STATE.cursor === "white") ? "White" : "Yellow";
  color: #FFD400 !important;
      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`
      );
    }
  }


/* =========================================================
  // -------------------------------------------------------------------
  HIGH CONTRAST: Footer (Vector + Timeless)
   // Actions
  ========================================================= */
   // -------------------------------------------------------------------
body.a11y-contrast #mw-footer,
  function handleAction(action) {
body.a11y-contrast #mw-footer-container{
    switch (action) {
   background: #000 !important;
      case "contrast":
   color: #FFD400 !important;
        STATE.contrast = !STATE.contrast;
  background-image: none !important;
        setCookie("contrast", STATE.contrast ? "1" : "0");
  border-top: 10px solid #FFD400 !important;
        applyState();
}
        announce(`High contrast ${STATE.contrast ? "on" : "off"}`);
body.a11y-contrast #footer-list,
        return;
body.a11y-contrast #footer-list *,
body.a11y-contrast #footer-icons,
body.a11y-contrast #footer-icons *{
  background: #000 !important;
  color: #FFD400 !important;
}
body.a11y-contrast #mw-footer a,
body.a11y-contrast #mw-footer a:visited{
  color: #FFD400 !important;
  text-decoration-color: #FFD400 !important;
}
body.a11y-contrast #mw-footer .mw-portlet,
body.a11y-contrast #mw-footer .mw-portlet-body{
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255, 212, 0, 0.75) !important;
}
body.a11y-contrast #mw-footer hr{
  border-color: #FFD400 !important;
}


/* =========================================================
      case "underline":
  Underline links: ensure TOC wins over table border-color rules
        STATE.underline = !STATE.underline;
  ========================================================= */
        setCookie("underline", STATE.underline ? "1" : "0");
body.a11y-underline-links #toc a,
        applyState();
body.a11y-underline-links .toc a{
        announce(`Underline links ${STATE.underline ? "on" : "off"}`);
  border-bottom-color: var(--a11y-link-underline) !important;
        return;
}
body.a11y-underline-links.a11y-contrast #toc a,
body.a11y-underline-links.a11y-contrast .toc a{
  border-bottom-color: #36C !important;
}


/* =========================================================
      case "spacing-plus":
  HIGH CONTRAST: Header coverage (Timeless + Vector)
        STATE.spacing = (STATE.spacing === "plus") ? "off" : "plus";
  (title/strapline, search, user tools)
        setCookie("spacing", STATE.spacing);
  ========================================================= */
        applyState();
        announce(STATE.spacing === "plus" ? "Text spacing increased" : "Text spacing normal");
        return;


/* Timeless header containers */
      case "spacing-minus":
body.a11y-contrast #mw-header-container,
        STATE.spacing = (STATE.spacing === "minus") ? "off" : "minus";
body.a11y-contrast #mw-header,
        setCookie("spacing", STATE.spacing);
body.a11y-contrast #mw-header-container .ts-inner,
        applyState();
body.a11y-contrast #mw-header-container .mw-body,
        announce(STATE.spacing === "minus" ? "Text spacing decreased" : "Text spacing normal");
body.a11y-contrast #mw-header-container .mw-portlet,
        return;
body.a11y-contrast #mw-header-container .mw-portlet-body{
  background: #000 !important;
  color: #FFD400 !important;
  background-image: none !important;
  border-color: #FFD400 !important;
}


/* Title + strapline: force yellow (Timeless rule was burgundy) */
      case "spacing-reset":
body.a11y-contrast.body.skin-timeless #p-logo-text #p-banner,
        STATE.spacing = "off";
body.a11y-contrast.body.skin-timeless #p-banner.mw-wiki-title,
        setCookie("spacing", "off");
body.a11y-contrast.body.skin-timeless #p-logo-text .mw-wiki-title{
        applyState();
  color: #FFD400 !important;
        announce("Text spacing reset");
}
        return;


body.a11y-contrast #bhc-strapline,
      case "cursor":
body.a11y-contrast .bhc-strapline{
        STATE.cursor = (STATE.cursor === "off") ? "white" : (STATE.cursor === "white") ? "yellow" : "off";
  color: #FFD400 !important;
        setCookie("cursor", STATE.cursor);
}
        applyState();
        announce(
          STATE.cursor === "off" ? "Large cursor off" :
          STATE.cursor === "white" ? "Large cursor white" :
          "Large cursor yellow"
        );
        return;


/* Search box (input + button) */
      case "ruler":
body.a11y-contrast #p-search,
        STATE.ruler = !STATE.ruler;
body.a11y-contrast #simpleSearch{
        setCookie("ruler", STATE.ruler ? "1" : "0");
  background: #000 !important;
        if (STATE.ruler && !rulerY) rulerY = Math.floor(window.innerHeight / 2);
  color: #FFD400 !important;
        applyState();
}
        announce(`Reading ruler ${STATE.ruler ? "on" : "off"}`);
        return;


body.a11y-contrast #searchInput,
      case "font-plus":
body.a11y-contrast input[type="search"],
        STATE.fontPct = clamp(STATE.fontPct + 10, 90, 140);
body.a11y-contrast #p-search input[type="search"]{
        setCookie("fontPct", String(STATE.fontPct));
  background: #000 !important;
        applyState();
  color: #FFD400 !important;
        announce("Text size increased");
  caret-color: #FFD400 !important;
        return;
  border: 2px solid #FFD400 !important;
  box-shadow: none !important;
}
body.a11y-contrast #searchInput::placeholder{
  color: rgba(255,212,0,0.75) !important;
}
body.a11y-contrast input#mw-searchButton,
body.a11y-contrast input#searchButton,
body.a11y-contrast #mw-searchButton{
  background-color: #000 !important;
  border: 2px solid rgba(255,212,0,.85) !important;
  filter: none !important;
}


/* Search suggestions dropdown (DOM-created) */
      case "font-minus":
body.a11y-contrast .suggestions,
        STATE.fontPct = clamp(STATE.fontPct - 10, 90, 140);
body.a11y-contrast .suggestions-results,
        setCookie("fontPct", String(STATE.fontPct));
body.a11y-contrast .suggestions-special,
        applyState();
body.a11y-contrast .suggestions a,
        announce("Text size decreased");
body.a11y-contrast .suggestions-result,
        return;
body.a11y-contrast .mw-searchSuggest,
body.a11y-contrast .mw-searchSuggest *{
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255,212,0,.85) !important;
}
body.a11y-contrast .suggestions a:visited{
  color: #FFD400 !important;
}
body.a11y-contrast .suggestions-result-current,
body.a11y-contrast .suggestions a:hover{
  background: #111 !important;
}


/* Vector search tabs area */
      case "font-reset":
body.a11y-contrast #mw-search-profile-tabs,
        STATE.fontPct = 100;
body.a11y-contrast #mw-search-profile-tabs *{
        setCookie("fontPct", "100");
  background: #000 !important;
        applyState();
  color: #FFD400 !important;
        announce("Text size reset");
  border-color: rgba(255,212,0,.85) !important;
        return;
}


/* User tools dropdown text */
      case "reset":
body.a11y-contrast #user-tools,
        ["contrast","underline","spacing","cursor","ruler","fontPct"].forEach(delCookie);
body.a11y-contrast #user-tools *,
        STATE.contrast = false;
body.a11y-contrast #personal,
        STATE.underline = false;
body.a11y-contrast #personal *{
        STATE.spacing = "off";
  background: #000 !important;
        STATE.cursor = "off";
  color: #FFD400 !important;
        STATE.ruler = false;
  border-color: rgba(255,212,0,.85) !important;
        STATE.fontPct = 100;
}
        rulerY = 0;
body.a11y-contrast #personal .dropdown,
        applyState();
body.a11y-contrast #personal-inner.dropdown,
        announce("Accessibility settings reset");
body.a11y-contrast #personal .mw-portlet-body{
        return;
  background: #000 !important;
    }
  color: #FFD400 !important;
  }
  border: 2px solid rgba(255,212,0,.85) !important;
}
body.a11y-contrast #personal .mw-portlet-body,
body.a11y-contrast #personal .mw-portlet-body *,
body.a11y-contrast #personal-inner.dropdown,
body.a11y-contrast #personal-inner.dropdown *{
  background: #000 !important;
  color: #FFD400 !important;
  border-color: rgba(255,212,0,0.85) !important;
}


/* Vector header safety */
  // -------------------------------------------------------------------
body.a11y-contrast #mw-head,
  // Load saved settings
body.a11y-contrast #mw-head * ,
  // -------------------------------------------------------------------
body.a11y-contrast #mw-head-base,
  function loadState() {
body.a11y-contrast #mw-head-base * ,
    STATE.contrast = getCookie("contrast", "0") === "1";
body.a11y-contrast #mw-head .vector-menu,
    STATE.underline = getCookie("underline", "0") === "1";
body.a11y-contrast #mw-head .vector-menu *{
 
  background: #000 !important;
    const spacing = getCookie("spacing", "off");
  color: #FFD400 !important;
    STATE.spacing = (spacing === "plus" || spacing === "minus") ? spacing : "off";
  border-color: rgba(255,212,0,.85) !important;
 
}
    const cursor = getCookie("cursor", "off");
body.a11y-contrast #mw-header a,
    STATE.cursor = (cursor === "white" || cursor === "yellow") ? cursor : "off";
body.a11y-contrast #mw-header a:visited,
 
body.a11y-contrast #mw-head a,
    STATE.ruler = getCookie("ruler", "0") === "1";
body.a11y-contrast #mw-head a:visited{
 
   color: #FFD400 !important;
    const fp = parseInt(getCookie("fontPct", "100"), 10);
}
    STATE.fontPct = clamp(isNaN(fp) ? 100 : fp, 90, 140);
  }
 
  // -------------------------------------------------------------------
  // Init
  // -------------------------------------------------------------------
  function init() {
    ensureUi();
    loadState();
    applyState();
  }
 
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
   } else {
    init();
  }
})();

Revision as of 13:13, 27 January 2026

/*
⚠️ Do not reorder rules below this line — contrast overrides depend on cascade order.
*/

(() => {
  "use strict";

  // -------------------------------------------------------------------
  // Cookie helpers
  // -------------------------------------------------------------------
  const COOKIE_PREFIX = "a11y_";

  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`;
  }

  // -------------------------------------------------------------------
  // State + announcements
  // -------------------------------------------------------------------
  const STATE = {
    contrast: false,
    underline: false,
    spacing: "off",          // off | plus | minus
    cursor: "off",           // off | white | yellow
    ruler: false,
    fontPct: 100             // 90..140 in steps of 10
  };

  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)); }

  // -------------------------------------------------------------------
  // 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();
  }

  // -------------------------------------------------------------------
  // 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;
      setCookie("ruler", "0");
      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;

    // Help link goes to the wiki page (you said: index.php/Accessibility)
    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;

    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="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="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="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;
        setCookie("contrast", STATE.contrast ? "1" : "0");
        applyState();
        announce(`High contrast ${STATE.contrast ? "on" : "off"}`);
        return;

      case "underline":
        STATE.underline = !STATE.underline;
        setCookie("underline", STATE.underline ? "1" : "0");
        applyState();
        announce(`Underline links ${STATE.underline ? "on" : "off"}`);
        return;

      case "spacing-plus":
        STATE.spacing = (STATE.spacing === "plus") ? "off" : "plus";
        setCookie("spacing", STATE.spacing);
        applyState();
        announce(STATE.spacing === "plus" ? "Text spacing increased" : "Text spacing normal");
        return;

      case "spacing-minus":
        STATE.spacing = (STATE.spacing === "minus") ? "off" : "minus";
        setCookie("spacing", STATE.spacing);
        applyState();
        announce(STATE.spacing === "minus" ? "Text spacing decreased" : "Text spacing normal");
        return;

      case "spacing-reset":
        STATE.spacing = "off";
        setCookie("spacing", "off");
        applyState();
        announce("Text spacing reset");
        return;

      case "cursor":
        STATE.cursor = (STATE.cursor === "off") ? "white" : (STATE.cursor === "white") ? "yellow" : "off";
        setCookie("cursor", STATE.cursor);
        applyState();
        announce(
          STATE.cursor === "off" ? "Large cursor off" :
          STATE.cursor === "white" ? "Large cursor white" :
          "Large cursor yellow"
        );
        return;

      case "ruler":
        STATE.ruler = !STATE.ruler;
        setCookie("ruler", STATE.ruler ? "1" : "0");
        if (STATE.ruler && !rulerY) rulerY = Math.floor(window.innerHeight / 2);
        applyState();
        announce(`Reading ruler ${STATE.ruler ? "on" : "off"}`);
        return;

      case "font-plus":
        STATE.fontPct = clamp(STATE.fontPct + 10, 90, 140);
        setCookie("fontPct", String(STATE.fontPct));
        applyState();
        announce("Text size increased");
        return;

      case "font-minus":
        STATE.fontPct = clamp(STATE.fontPct - 10, 90, 140);
        setCookie("fontPct", String(STATE.fontPct));
        applyState();
        announce("Text size decreased");
        return;

      case "font-reset":
        STATE.fontPct = 100;
        setCookie("fontPct", "100");
        applyState();
        announce("Text size reset");
        return;

      case "reset":
        ["contrast","underline","spacing","cursor","ruler","fontPct"].forEach(delCookie);
        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() {
    STATE.contrast = getCookie("contrast", "0") === "1";
    STATE.underline = getCookie("underline", "0") === "1";

    const spacing = getCookie("spacing", "off");
    STATE.spacing = (spacing === "plus" || spacing === "minus") ? spacing : "off";

    const cursor = getCookie("cursor", "off");
    STATE.cursor = (cursor === "white" || cursor === "yellow") ? cursor : "off";

    STATE.ruler = getCookie("ruler", "0") === "1";

    const fp = parseInt(getCookie("fontPct", "100"), 10);
    STATE.fontPct = clamp(isNaN(fp) ? 100 : fp, 90, 140);
  }

  // -------------------------------------------------------------------
  // Init
  // -------------------------------------------------------------------
  function init() {
    ensureUi();
    loadState();
    applyState();
  }

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", init);
  } else {
    init();
  }
})();