Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions src/lib/stepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,33 @@ const $ = (sel, el = document) => el.querySelector(sel);
const esc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[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 = '') => `
<div class="lang-bar${cls}" role="tablist">${langs.map((l) =>
`<button class="lang" role="tab" data-lang="${l.id}" aria-selected="${l.id === active}">${esc(l.name)}</button>`).join('')}
`<button class="lang" role="tab" data-lang="${l.id}" ${tabAttrs(l.id === active)}>${esc(l.name)}</button>`).join('')}
</div>`;

/* Chrome owned by the engine rather than by any one lesson. */
Expand All @@ -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' },
Expand Down Expand Up @@ -146,7 +170,7 @@ export function mountLesson(cfg) {
</div>` : '';

const tabs = cfg.modes.map((m) => `
<button class="atab" role="tab" data-mode="${m.id}" aria-selected="${m.id === state.mode}">
<button class="atab" role="tab" data-mode="${m.id}" ${tabAttrs(m.id === state.mode)}>
<span class="atab-name">${esc(pick(m.name))}${m.sub ? ` <span class="sub-name">&middot; ${esc(pick(m.sub))}</span>` : ''}</span>
<span class="atab-desc">${esc(pick(m.desc ?? ''))}</span>
<span class="atab-cost">${esc(pick(m.cost ?? ''))}</span>
Expand Down Expand Up @@ -183,7 +207,7 @@ export function mountLesson(cfg) {
<input type="range" data-scrub min="0" max="0" value="0" aria-label="${esc(pick(UI.scrub))}">
<span class="counter" data-count>0 / 0</span>
</div>
<div class="narration">
<div class="narration" data-narration aria-live="polite" aria-atomic="true">
<span class="tag" data-tag>${esc(pick(UI.step))}</span>
<p class="text" data-note></p>
</div>
Expand Down Expand Up @@ -253,6 +277,9 @@ export function mountLesson(cfg) {
const scrub = $('[data-scrub]', root);
scrub.max = String(state.steps.length - 1);
scrub.value = String(state.i);
// what a screen reader says for the scrubber: "Step 2 of 12: tally"
const where = pick(UI.stepOf).replace('{i}', state.i + 1).replace('{n}', state.steps.length);
scrub.setAttribute('aria-valuetext', pick(s.tag) ? `${where}: ${pick(s.tag)}` : where);
$('[data-count]', root).textContent = `${state.i + 1} / ${state.steps.length}`;
}

Expand Down Expand Up @@ -287,15 +314,21 @@ export function mountLesson(cfg) {
state.i = Math.max(0, Math.min(state.steps.length - 1, i));
render();
}
// The narration is a live region, so a screen reader reads each step the
// reader moves to. While playing it goes quiet — a step every 700 ms would
// queue up faster than it can be read — and speaks again on stop.
const narrate = (on) => $('[data-narration]', root)?.setAttribute('aria-live', on ? 'polite' : 'off');
function stop() {
clearInterval(state.timer);
state.timer = null;
const b = $('[data-act="play"]', root);
if (b) b.textContent = pick(UI.play);
narrate(true);
}
function play() {
if (state.i >= state.steps.length - 1) state.i = 0;
$('[data-act="play"]', root).textContent = pick(UI.pause);
narrate(false);
state.timer = setInterval(() => {
if (state.i >= state.steps.length - 1) return stop();
go(state.i + 1);
Expand All @@ -310,8 +343,7 @@ export function mountLesson(cfg) {

function setLangAll(id) {
state.lang = id;
document.querySelectorAll('[data-lang]').forEach((b) =>
b.setAttribute('aria-selected', String(b.dataset.lang === id)));
selectTabs(document.querySelectorAll('[data-lang]'), (b) => b.dataset.lang === id);
document.querySelectorAll('[data-pane]').forEach((p) => { p.hidden = p.dataset.pane !== id; });
render();
}
Expand Down Expand Up @@ -358,8 +390,7 @@ export function mountLesson(cfg) {
if (card) {
stop();
state.mode = card.dataset.mode;
root.querySelectorAll('[data-mode]').forEach((b) =>
b.setAttribute('aria-selected', String(b.dataset.mode === state.mode)));
selectTabs(root.querySelectorAll('[data-mode]'), (b) => b.dataset.mode === state.mode);
rebuild();
return;
}
Expand All @@ -372,10 +403,15 @@ export function mountLesson(cfg) {
});

// Arrow keys and space — never while someone is typing, and only while the
// walkthrough is on screen, so a widget elsewhere keeps its own keys.
// walkthrough is on screen, so a widget elsewhere keeps its own keys. A
// modified key is the browser's (Alt+← is Back), a tab's arrows move between
// tabs, and space on a focused control presses that control, not Play.
root.addEventListener('keydown', tabKeys);
document.addEventListener('keydown', (e) => {
if (e.altKey || e.ctrlKey || e.metaKey) return;
if (e.target.closest('input,textarea,select,[contenteditable]')) return;
if (e.target.closest('#q-widget')) return;
if (e.target.closest('#q-widget, [role="tab"]')) return;
if (e.key === ' ' && e.target.closest('button, a[href], summary, [role="button"]')) return;
const r = root.getBoundingClientRect();
if (r.bottom < 0 || r.top > innerHeight) return;
const name = { ArrowRight: 'next', ArrowLeft: 'prev', ' ': 'play' }[e.key];
Expand Down Expand Up @@ -538,6 +574,7 @@ function renderSolutions(cfg, activeLang) {
}).join('')}
</div>`).join('')}`;

host.onkeydown = tabKeys;
host.onclick = (e) => {
const langBtn = e.target.closest('[data-lang]');
// the live code panel's tab switches every tab and pane on the page
Expand Down
35 changes: 35 additions & 0 deletions src/styles/lesson.css
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,41 @@ td.mono{font-variant-numeric:tabular-nums}
.player .bench > .panel{border-left:1px solid var(--line);padding:16px 18px}
@media (max-width:900px){.player .bench > .panel{border-left:0;border-top:1px solid var(--line)}}

/* The transport and narration stay under the masthead while the reader looks
down at the stage and the code, and let go at the end of the player. Not on
a short window, where they would cover too much of it. */
@media (min-height:600px){
.player-run{position:sticky;top:56px;z-index:5;box-shadow:0 8px 12px -12px rgba(0,0,0,.4)}
/* whatever takes keyboard focus below scrolls clear of the bar */
.player .bench *{scroll-margin-top:200px}
}
/* On a phone only the buttons stick, as one row (two below ~330px): with
the narration too it took a third of the screen. The narration stays in
the flow above the stage. */
@media (max-width:560px){
.player-run{display:contents}
.player .transport{
position:sticky;top:56px;z-index:5;gap:6px;
padding:8px 14px;background:var(--sunk);border-top:1px solid var(--line);
box-shadow:0 8px 12px -12px rgba(0,0,0,.4);
}
.player .transport .btn{min-height:40px;padding-inline:9px}
.player .transport input[type=range]{flex:1 1 36px;min-width:36px}
.player .transport .counter{font-size:12px}
.player .narration{margin:10px 14px 14px}
.player .bench *{scroll-margin-top:130px}
}

/* The array strip wraps onto more rows rather than scrolling sideways, with
room under each row for the pointer labels. */
[data-strip]{flex-wrap:wrap}
[data-strip]:has(.ptr){row-gap:26px}
@media (max-width:560px){
[data-strip]{gap:5px}
[data-strip] > .cell,[data-strip] > .has-ptr > .cell{width:36px;height:44px;font-size:15px}
[data-strip] > .cell.wide,[data-strip] > .has-ptr > .cell.wide{width:auto;min-width:40px;padding-inline:6px}
}

/* 2·3 — going deeper: the price, then the questions */
.deeper{display:flex;flex-direction:column;gap:22px}
.deeper-block{display:flex;flex-direction:column;gap:6px}
Expand Down
7 changes: 6 additions & 1 deletion src/styles/tokens.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* Palette and shape, in all three theme states: system, forced light,
forced dark. Every other stylesheet reads colour only through these.
Text colours clear WCAG AA (4.5:1) on every background they sit on —
ground, surface, sunk and the soft tints; npm run audit checks it. */
ground, surface, sunk and the soft tints; npm run audit checks it.
color-scheme follows the theme so native parts — scrollbars, range
tracks, number spinners — are drawn dark on the dark theme too. */
:root{
color-scheme:light;
--ground:#EEF2F2;
--surface:#FFFFFF;
--sunk:#E3E9EA;
Expand All @@ -24,6 +27,7 @@
}
@media (prefers-color-scheme: dark){
:root:not([data-theme="light"]){
color-scheme:dark;
--ground:#0C1315;
--surface:#141E21;
--sunk:#0F1A1D;
Expand All @@ -44,6 +48,7 @@
}
}
:root[data-theme="dark"]{
color-scheme:dark;
--ground:#0C1315;
--surface:#141E21;
--sunk:#0F1A1D;
Expand Down
Loading