a11y toggle

(a11y = a(11 letters)y = accessibility)

WIP accessibility toggle component. persists toggle state between a) browser sessions b) all pages that use it

to be useful, needs buy-in from components/themes that benefit from it

for a theme/component to use it:
1. it needs to have css rules that disable inaccessible stuff, and prefix one of the following to them based on which toggle you want:

  • :root:has(#a11y-toggle[style*="1"]): the user wants to disable animations
  • :root:has(#a11y-toggle[style*="2"]): unassigned
  • :root:has(#a11y-toggle[style*="3"]): unassigned
  • :root:has(#a11y-toggle[style*="4"]): unassigned

e.g.

.spinner { animation: spin 10s infinite; }

/* Respect preference set by a11y-toggle component */
:root:has(#a11y-toggle[style*=1]) .spinner { animation: none; }

/* Respect preference set by browser/OS */
@media (prefers-reduced-motion: reduce) {
.spinner { animation: none; }
}

2. it needs to include instructions for the user of the theme/component (i.e. the author of the page that uses it): "also include <this component>(linked) if you want an animation toggle" or similar

TODO:

  • add more toggles
    • disable fonts?
    • colourblind friendly mode?
  • let the consumer of this component decide which toggles to include - not all will be relevant, save the screen space
    • that means the component height is non static (hardcoded to 1rem atm, and needs to be !important to override the inline style)
  • all actions are negative atm - i.e. unchecked is the default and the default is the least accessible state. can't do e.g. "[x] Show animations". is that fine? are pages cool with opening on a bunch of negative words like "disable"?

Source code

<html lang="en">
<head>
<script src="https://interwiki.scpwiki.com/resizeIframe.js" defer></script>
<script>
window.resize = () => {}
 
const toggles = [
  { toggleName: "a11y-toggle-disable-animations", toggleId: 1 },
]
 
const activeToggleIds = new Set()
 
function applyToggles() {
  window.resize([...activeToggleIds].reduce((a, b) => a + b, 0))
}
 
function getToggleValue(toggleName) {
  return Boolean(localStorage.getItem(toggleName))
}
 
function setToggleValue(toggleName, toggleId, value) {
  localStorage.setItem(toggleName, value ? "true" : "")
  if (value) activeToggleIds.add(toggleId)
  else activeToggleIds.delete(toggleId)
  applyToggles()
}
 
addEventListener("load", () => {
  window.resize = window.resizeIframe.createResizeIframe(
    document.referrer,
    location.href.replace(/^.*\//, "/"),
    100,
  )
 
  for (const { toggleName, toggleId } of toggles) {
    const checkbox = document.querySelector(`input[data-toggle-name="${toggleName}"]`)
 
    if (getToggleValue(toggleName)) checkbox.checked = true
    setToggleValue(toggleName, toggleId, checkbox.checked)
 
    checkbox.addEventListener("change", () => {
      setToggleValue(toggleName, toggleId, checkbox.checked)
    })
 
    // React to toggle events from other tabs/windows
    addEventListener("storage", event => {
      if (event.key === null) {
        // Caused by clearing all storage
        setToggleValue(toggleName, toggleId, false)
        checkbox.checked = false
      }
      if (event.key === toggleName) {
        setToggleValue(toggleName, toggleId, event.newValue)
        checkbox.checked = Boolean(event.newValue)
      }
    })
  }
})
</script>
 
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
  font-size: 0.625rem;
}
label {
  display: flex;
  align-items: center;
  gap: 1ch;
}
</style>
</head>
 
<body style="height: 1rem">
  <label>
    <input type="checkbox" data-toggle-name="a11y-toggle-disable-animations"> Disable animations
  </label>
</body>
</html>