diff --git a/src/lib/stepper.js b/src/lib/stepper.js index 35cd81a..82b0066 100644 --- a/src/lib/stepper.js +++ b/src/lib/stepper.js @@ -41,10 +41,33 @@ const $ = (sel, el = document) => el.querySelector(sel); const esc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); const nameOf = (k) => (typeof k === 'string' ? k : k?.en ?? ''); +/* A tab's selection attributes. Only the selected tab sits in the Tab order; + * the arrow keys move between the rest (tabKeys below). */ +const tabAttrs = (on) => `aria-selected="${on}" tabindex="${on ? 0 : -1}"`; +const selectTabs = (tabs, isOn) => tabs.forEach((b) => { + const on = isOn(b); + b.setAttribute('aria-selected', String(on)); + b.tabIndex = on ? 0 : -1; +}); + +/* Arrow keys, Home and End inside a tablist: move to that tab and select it. */ +function tabKeys(e) { + const tab = e.target.closest('[role="tab"]'); + if (!tab || e.altKey || e.ctrlKey || e.metaKey) return; + const tabs = [...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]')]; + const i = tabs.indexOf(tab); + const j = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 }[e.key]; + if (j == null) return; + e.preventDefault(); + const to = tabs[(j + tabs.length) % tabs.length]; + to.click(); + to.focus(); +} + /* The code-language tabs: `mini` above the live code, plain above part 3. */ const langBar = (langs, active, cls = '') => `
${langs.map((l) => - ``).join('')} + ``).join('')}
`; /* Chrome owned by the engine rather than by any one lesson. */ @@ -58,6 +81,7 @@ const UI = { pause: { en: 'Pause', my: 'ရပ်' }, scrub: { en: 'Scrub through steps', my: 'အဆင့်များကို ဆွဲကြည့်ရန်' }, step: { en: 'step', my: 'အဆင့်' }, + stepOf: { en: 'Step {i} of {n}', my: 'အဆင့် {i} / {n}' }, answer: { en: 'answer', my: 'answer' }, codeLive: { en: 'The code, live', my: 'အလုပ်လုပ်နေသော code' }, input: { en: 'Input', my: 'Input' }, @@ -146,7 +170,7 @@ export function mountLesson(cfg) { ` : ''; const tabs = cfg.modes.map((m) => ` -