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
36 changes: 31 additions & 5 deletions src/lib/stepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const selectTabs = (tabs, isOn) => tabs.forEach((b) => {
const on = isOn(b);
b.setAttribute('aria-selected', String(on));
b.tabIndex = on ? 0 : -1;
// a tab row that scrolls sideways (a narrow phone) shows its selected tab —
// moving the row only, never the page
const row = b.parentElement;
if (on && row.scrollWidth > row.clientWidth) row.scrollLeft = b.offsetLeft - (row.clientWidth - b.offsetWidth) / 2;
});

/* Arrow keys, Home and End inside a tablist: move to that tab and select it. */
Expand Down Expand Up @@ -117,6 +121,7 @@ export function mountLesson(cfg) {
timer: null, // set while playing
input: structuredClone(cfg.input),
varNames: [],
open: null, // part 3 solutions shown open
};

/* ---------------- building ---------------- */
Expand Down Expand Up @@ -417,6 +422,7 @@ export function mountLesson(cfg) {
stop();
state.mode = card.dataset.mode;
selectTabs(root.querySelectorAll('[data-mode]'), (b) => b.dataset.mode === state.mode);
showSolution(state, state.mode, true);
rebuild();
return;
}
Expand Down Expand Up @@ -550,7 +556,7 @@ export function mountLesson(cfg) {
function paintAll() {
paint();
renderExamples(cfg, loadInput);
renderSolutions(cfg, state.lang);
renderSolutions(cfg, state);
rebuild(state.i);
}

Expand Down Expand Up @@ -636,10 +642,25 @@ function approachBlock(a, cost) {
</div>`;
}

function renderSolutions(cfg, activeLang) {
/* Part 3 opens on the approach the reader chose in 2·1; the others fold to
* their heading, copyable still. Choosing another approach in 2·1 opens that
* one too. A lesson with a single approach has nothing to fold. */
function showSolution(state, mode, open) {
if (!state.open) return;
if (open) state.open.add(mode); else state.open.delete(mode);
document.querySelectorAll(`#solutions .src[data-src="${mode}"]`).forEach((src) => {
src.querySelector('.src-toggle')?.setAttribute('aria-expanded', String(open));
src.querySelector('pre.full').hidden = !open;
});
}

function renderSolutions(cfg, state) {
const host = document.getElementById('solutions');
if (!host) return;
const langs = cfg.languages;
const activeLang = state.lang;
const folds = cfg.modes.length > 1;
state.open ??= new Set([state.mode]);

// verification[lang] is one badge for every approach, or { [mode]: badge }
// when one approach in that language behaves differently from the other.
Expand All @@ -657,15 +678,18 @@ function renderSolutions(cfg, activeLang) {
${cfg.modes.map((m) => {
const meta = cfg.solutions?.[m.id] || {};
const lines = (cfg.code[m.id]?.[l.id] || []).map(([, html]) => html).join('\n');
const open = !folds || state.open.has(m.id);
const id = `src-${l.id}-${m.id}`;
const title = `${esc(pick(m.name))}${m.sub ? ` <span class="sub-name">&middot; ${esc(pick(m.sub))}</span>` : ''}${meta.tag ? `<span class="tagpill">${esc(meta.tag)}</span>` : ''}`;
return `
<div class="src">
<div class="src" data-src="${m.id}">
<div class="src-head">
<h3 class="as-h2">${esc(pick(m.name))}${m.sub ? ` <span class="sub-name">&middot; ${esc(pick(m.sub))}</span>` : ''}${meta.tag ? `<span class="tagpill">${esc(meta.tag)}</span>` : ''}</h3>
<h3 class="as-h2">${folds ? `<button class="src-toggle" aria-expanded="${open}" aria-controls="${id}"><span>${title}</span></button>` : title}</h3>
${badge(l.id, m.id)}
<button class="btn copy" data-copy>${esc(pick(UI.copy))}</button>
${meta.desc ? `<p class="sub">${pick(meta.desc)}</p>` : `<p class="sub mono">${esc(pick(m.cost))}</p>`}
</div>
<pre class="full" tabindex="0">${lines}</pre>
<pre class="full" id="${id}" tabindex="0"${open ? '' : ' hidden'}>${lines}</pre>
</div>`;
}).join('')}
</div>`).join('')}`;
Expand All @@ -675,6 +699,8 @@ function renderSolutions(cfg, activeLang) {
const langBtn = e.target.closest('[data-lang]');
// the live code panel's tab switches every tab and pane on the page
if (langBtn) return document.querySelector(`#lesson [data-lang="${langBtn.dataset.lang}"]`)?.click();
const fold = e.target.closest('.src-toggle');
if (fold) return showSolution(state, fold.closest('.src').dataset.src, fold.getAttribute('aria-expanded') !== 'true');
const copy = e.target.closest('[data-copy]');
if (copy) {
const text = copy.closest('.src').querySelector('pre.full').textContent;
Expand Down
29 changes: 28 additions & 1 deletion src/styles/lesson.css
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ td.mono{font-variant-numeric:tabular-nums}
}

/* ---------- the question ---------- */
.question{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.05fr);gap:18px;margin-bottom:22px;align-items:start}
/* The statement and the widget end on one line: whichever is shorter keeps
its spare room inside its own card rather than leaving a hole in the page. */
.question{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.05fr);gap:18px;margin-bottom:22px;align-items:stretch}
@media (max-width:900px){.question{grid-template-columns:minmax(0,1fr)}}
.q-statement{background:var(--surface);border:1px solid var(--line);border-radius:var(--radius);padding:18px;box-shadow:var(--shadow)}
.constraints{display:flex;flex-wrap:wrap;gap:6px;margin-top:13px}
Expand Down Expand Up @@ -201,6 +203,30 @@ td.mono{font-variant-numeric:tabular-nums}
.lang:hover{color:var(--accent)}
.lang[aria-selected="true"]{background:var(--surface);color:var(--ink);font-weight:600;box-shadow:0 1px 2px rgba(0,0,0,.12)}
.src-head h3{margin-right:auto}
/* A folding solution: its heading is the button, a chevron shows which way. */
.src-toggle{
display:inline-flex;align-items:baseline;gap:9px;text-align:left;cursor:pointer;
font:inherit;letter-spacing:inherit;color:inherit;background:none;border:0;padding:0;margin:0;
}
.src-toggle::before{
content:"";flex:0 0 auto;width:7px;height:7px;align-self:center;margin-top:-3px;
border-right:2px solid var(--ink-3);border-bottom:2px solid var(--ink-3);transform:rotate(45deg);
}
.src-toggle[aria-expanded="false"]::before{transform:rotate(-45deg);margin-top:0}
.src-toggle:hover{color:var(--accent)}
.src:has(pre.full[hidden]) .src-head{margin-bottom:0}
@media (prefers-reduced-motion: no-preference){.src-toggle::before{transition:transform .15s}}
/* The language tabs stay in reach while the reader scrolls part 3, and let go
at its end; a solution scrolled to by focus lands below them. */
#solutions > .lang-bar{position:sticky;top:56px;z-index:4}
#solutions .src{scroll-margin-top:120px}
/* On a phone the language tabs keep to one row — two rows of touch-sized
tabs made a 94px sticky bar — and scroll sideways if they must. */
@media (max-width:560px){
.lang-bar{flex-wrap:nowrap;overflow-x:auto;scrollbar-width:none}
.lang-bar::-webkit-scrollbar{display:none}
.lang-bar .lang{flex:0 0 auto;padding-inline:10px}
}
.tagpill{
font-family:"IBM Plex Mono",monospace;font-size:10.5px;font-weight:500;color:var(--accent);
background:var(--accent-soft);border-radius:5px;padding:2px 7px;margin-left:8px;
Expand Down Expand Up @@ -476,5 +502,6 @@ td.mono{font-variant-numeric:tabular-nums}
so rows keep their line breaks. */
@media (pointer:coarse),(max-width:560px){
.chip,.lang,.lang-bar.mini .lang,.lang-opt,.copy,.btn.ex-load{min-height:40px}
.src-toggle{min-height:40px;align-items:center}
.jump a{min-height:40px}
}
Loading