Spaces:
Running
Running
File size: 5,861 Bytes
64aaca8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
const bodySty = document.body;
const htmlSty = document.documentElement.style;
// Enable accessibility settings when JavaScript is permitted
getElement('a11y').disabled = false;
// Close console
const closeA11yConsole = () => getElement('has-a11y').removeAttribute('open');
// Color scheme and contrast functions
const matchMediaColor = () => {
lightSwitch.checked = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (window.matchMedia('(prefers-contrast: more)').matches) {
moreContrast.checked = true;
} else if (window.matchMedia('(prefers-contrast: less)').matches) {
lessContrast.checked = true;
} else {
defaultContrast.checked = true;
}
};
function setColor() {
const styles = {
light: {
default: '--off: #000; --fg: var(--fg-light); --mid: var(--midtone); --ac: var(--ac-light); --bg: var(--bg-light);',
less: '--off: #000; --fg: var(--fg-light-less); --mid: var(--midtone-less); --ac: var(--ac-light-less); --bg: var(--bg-light-less);',
more: '--off: #000; --fg: var(--fg-light-more); --mid: var(--midtone-more); --ac: var(--ac-light-more); --bg: var(--bg-light-more); --border: 1pt solid var(--fg); --bound: var(--border);'
},
dark: {
default: '--off: #fff; --fg: var(--fg-dark); --mid: var(--midtone); --ac: var(--ac-dark); --bg: var(--bg-dark);',
less: '--off: #fff; --fg: var(--fg-dark-less); --mid: var(--midtone-less); --ac: var(--ac-dark-less); --bg: var(--bg-dark-less);',
more: '--off: #fff; --fg: var(--fg-dark-more); --mid: var(--midtone-more); --ac: var(--ac-dark-more); --bg: var(--bg-dark-more); --border: 1pt solid var(--fg); --bound: var(--border);'
}
};
const scheme = lightSwitch.checked ? 'dark' : 'light';
const logomarkDark = getElement('logomark--dark');
if (logomarkDark) {
const logomark = getElement('logomark');
logomark.style.display = lightSwitch.checked ? 'none' : 'inline-block';
logomarkDark.style.display = lightSwitch.checked ? 'inline-block' : 'none';
}
const contrast = lessContrast.checked ? 'less' : (moreContrast.checked ? 'more' : 'default');
bodySty.setAttribute('style', styles[scheme][contrast]);
};
// Flash guard
addEvent(document, 'DOMContentLoaded', () => {
setTimeout(() => htmlSty.setProperty('--flashGuard', '1s ease-in 0.1s'), 99);
});
// Switch to keyboard-friendly mode
addEvent(document, 'keydown', (element) => {
if (element.key === 'Tab') {
document.body.classList.add('keydown');
}
if (element.key === 'Escape') {
document.body.classList.remove('keydown');
}
});
// Color palette functions
function setColorPalette() {
document.body.className = colorPalette.value;
};
// OpenDyslexic functions
function useOpenDyslexic() {
if (OpenDyslexic.checked) {
htmlSty.setProperty('--rm', 'OpenDyslexic');
htmlSty.setProperty('--sf', 'OpenDyslexic');
} else {
htmlSty.removeProperty('--rm');
htmlSty.removeProperty('--sf');
}
recalcLogotypeWidth();
};
// Font size functions
function setFontSize() {
fontSizeState.value = fontSize.value;
htmlSty.setProperty('--fontScale', fontSize.value / 10);
};
// Baseline stretch functions
function setStretch() {
baselineStretchState.value = baselineStretch.value;
htmlSty.setProperty('--baselineStretch', baselineStretch.value);
};
// Initialize localStorage
function hasLocalStorage() {
try {
localStorage.is = 'enable';
localStorage.removeItem('is');
return true;
} catch(e) {
defaultContrast.checked = true;
getElement('noLocalStorage').className = '';
return false;
};
};
if (hasLocalStorage()) {
getElement('a11y-menu').className = '';
saveButton.disabled = false;
resetButton.disabled = false;
// Reset function
function resetA11y() {
localStorage.clear();
matchMediaColor();
colorPalette.reset;
fontSize.value = '';
baselineStretch.value = '';
OpenDyslexic.checked = false;
setTimeout(() => window.location.reload(), 100);
};
// Save function
function saveA11y() {
setTimeout(() => closeA11yConsole(), 618);
localStorage.scheme = lightSwitch.checked ? 'light' : 'dark';
if (defaultContrast.checked) localStorage.contrast = 'default';
if (lessContrast.checked) localStorage.contrast = 'less';
if (moreContrast.checked) localStorage.contrast = 'more';
localStorage.colorPalette = colorPalette.value;
localStorage.font = OpenDyslexic.checked ? 'OpenDyslexic' : '';
localStorage.fontSize = fontSize.value;
localStorage.stretchSize = baselineStretch.value;
};
// Read settings from localStorage
if (!localStorage.getItem('scheme') && !localStorage.getItem('contrast')) {
matchMediaColor();
} else {
lightSwitch.checked = localStorage.scheme !== 'dark';
if (localStorage.contrast === 'more') {
moreContrast.checked = true;
} else if (localStorage.contrast === 'less') {
lessContrast.checked = true;
} else {
defaultContrast.checked = true;
}
setColor();
}
if (localStorage.getItem('colorPalette')) {
colorPalette.value = localStorage.colorPalette;
setColorPalette();
}
if (localStorage.font === 'OpenDyslexic') {
OpenDyslexic.setAttribute('checked', 'checked');
useOpenDyslexic();
}
if (localStorage.getItem('fontSize')) {
fontSize.value = localStorage.fontSize;
setFontSize();
}
if (localStorage.getItem('stretchSize')) {
baselineStretch.value = localStorage.stretchSize;
setStretch();
}
} |