Offline shell (precache, offline notice) and accessibility layer (skip link, button names, sheet focus and Escape)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
df8c6ce586
commit
4625848048
5 files changed
+81
-7
No files matched your search
@@ -0,0 +1,56 @@
|
||||
// App-shell niceties that do not belong to any one tab: offline notice and an accessibility layer
|
||||
// (names for icon-only buttons, a skip link, keyboard handling and focus for sheets).
|
||||
(function () {
|
||||
// ---- Offline notice
|
||||
const bar = document.createElement('div');
|
||||
bar.id = 'offline'; bar.setAttribute('role', 'status'); bar.hidden = true;
|
||||
bar.textContent = 'You are offline. Showing what was saved on this device; changes will not sync until you are back online.';
|
||||
document.body.appendChild(bar);
|
||||
const sync = () => { bar.hidden = navigator.onLine; };
|
||||
addEventListener('online', sync); addEventListener('offline', sync); sync();
|
||||
|
||||
// ---- Skip link and landmarks
|
||||
const skip = document.createElement('a');
|
||||
skip.href = '#view'; skip.className = 'skip'; skip.textContent = 'Skip to content';
|
||||
skip.addEventListener('click', e => { e.preventDefault(); const v = document.getElementById('view'); if (v) { v.setAttribute('tabindex', '-1'); v.focus(); } });
|
||||
document.body.insertBefore(skip, document.body.firstChild);
|
||||
const nav = document.getElementById('nav'); if (nav && !nav.getAttribute('aria-label')) nav.setAttribute('aria-label', 'Main');
|
||||
|
||||
// ---- Give every button or link that has only an icon a name (from its title, tooltip, id or class)
|
||||
const NAMES = { more: 'More', navcol: 'Collapse the menu', sx: 'Cancel', x: 'Close', close: 'Close', back: 'Back', fwd: 'Forward', menu: 'Menu', play: 'Play', pause: 'Pause', next: 'Next', prev: 'Previous', shuffle: 'Shuffle', repeat: 'Repeat', send: 'Send', clip: 'Attach a file', lyrics: 'Lyrics', full: 'Full screen', refresh: 'Refresh', ext: 'Open in a new tab' };
|
||||
const named = el => (el.getAttribute('aria-label') || el.getAttribute('aria-labelledby') || el.textContent.trim() || el.getAttribute('title') || '');
|
||||
function label(root) {
|
||||
root.querySelectorAll('button, a[href], [role="button"]').forEach(el => {
|
||||
if (named(el)) { if (!el.getAttribute('aria-label') && !el.textContent.trim()) el.setAttribute('aria-label', el.getAttribute('title')); return; }
|
||||
const key = (el.id || '').replace(/^(btn|b)-?/, '') || [...el.classList].find(c => NAMES[c]) || '';
|
||||
const l = NAMES[key] || NAMES[el.id] || (el.querySelector('img[alt]') || {}).alt;
|
||||
if (l) el.setAttribute('aria-label', l);
|
||||
});
|
||||
root.querySelectorAll('input:not([type=hidden]):not([type=checkbox]):not([type=radio]), select, textarea').forEach(el => {
|
||||
if (el.getAttribute('aria-label') || el.getAttribute('aria-labelledby') || (el.id && document.querySelector(`label[for="${CSS.escape(el.id)}"]`)) || el.closest('label')) return;
|
||||
const l = el.getAttribute('placeholder') || el.getAttribute('title'); if (l) el.setAttribute('aria-label', l);
|
||||
});
|
||||
}
|
||||
let pending = false;
|
||||
new MutationObserver(() => { if (pending) return; pending = true; requestAnimationFrame(() => { pending = false; label(document.body); }); })
|
||||
.observe(document.body, { childList: true, subtree: true });
|
||||
label(document.body);
|
||||
|
||||
// ---- Sheets (bottom sheets and dialogs): Escape closes, focus moves in and returns, Tab stays inside
|
||||
let lastFocus = null;
|
||||
new MutationObserver(() => {
|
||||
const s = document.getElementById('sheet');
|
||||
if (s && !s._a11y) { s._a11y = true; lastFocus = document.activeElement; setTimeout(() => { if (!s.contains(document.activeElement)) { const f = s.querySelector('input:not([type=hidden]), textarea, select, button'); if (f) f.focus(); } }, 50); }
|
||||
if (!s && lastFocus) { try { lastFocus.focus(); } catch {} lastFocus = null; }
|
||||
}).observe(document.body, { childList: true });
|
||||
addEventListener('keydown', e => {
|
||||
const s = document.getElementById('sheet'); if (!s) return;
|
||||
if (e.key === 'Escape' && typeof closeSheet === 'function') { e.preventDefault(); closeSheet(); return; }
|
||||
if (e.key !== 'Tab') return;
|
||||
const f = [...s.querySelectorAll('a[href], button:not([disabled]), input:not([type=hidden]):not([disabled]), select, textarea, [tabindex]:not([tabindex="-1"])')].filter(x => x.offsetParent !== null);
|
||||
if (!f.length) return;
|
||||
const first = f[0], last = f[f.length - 1];
|
||||
if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); }
|
||||
else if (!e.shiftKey && (document.activeElement === last || !s.contains(document.activeElement))) { e.preventDefault(); first.focus(); }
|
||||
});
|
||||
})();
|
||||
Reference in new issue
Block a user