From 2ae61bdc448a48e98bea34420ea966789dfcf515 Mon Sep 17 00:00:00 2001 From: devmtnaing Date: Sun, 27 Sep 2026 10:06:50 +0630 Subject: [PATCH 1/2] Fold part 3 to the chosen approach, keep its language tabs in reach MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Each part 3 solution's heading is a disclosure button (aria-expanded, aria-controls). The approach chosen in 2·1 starts open, the others fold to their heading; choosing another approach opens it too. Folds hold across the five language panes, a page-language switch and deep links. Copy still copies a folded solution. - The part 3 language tabs stick under the masthead until part 3 ends. - On phones the language tabs keep to one row (two rows of touch-sized tabs made a 94px sticky bar) and scroll sideways only at ~320px, where the selected tab is kept in view without moving the page. --- src/lib/stepper.js | 36 +++++++++++++++++++++++++++++++----- src/styles/lesson.css | 25 +++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/lib/stepper.js b/src/lib/stepper.js index d9a97a4..c58a93a 100644 --- a/src/lib/stepper.js +++ b/src/lib/stepper.js @@ -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. */ @@ -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 ---------------- */ @@ -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; } @@ -550,7 +556,7 @@ export function mountLesson(cfg) { function paintAll() { paint(); renderExamples(cfg, loadInput); - renderSolutions(cfg, state.lang); + renderSolutions(cfg, state); rebuild(state.i); } @@ -636,10 +642,25 @@ function approachBlock(a, cost) { `; } -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. @@ -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 ? ` · ${esc(pick(m.sub))}` : ''}${meta.tag ? `${esc(meta.tag)}` : ''}`; return ` -
+
-

${esc(pick(m.name))}${m.sub ? ` · ${esc(pick(m.sub))}` : ''}${meta.tag ? `${esc(meta.tag)}` : ''}

+

${folds ? `` : title}

${badge(l.id, m.id)} ${meta.desc ? `

${pick(meta.desc)}

` : `

${esc(pick(m.cost))}

`}
-
${lines}
+
${lines}
`; }).join('')}
`).join('')}`; @@ -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; diff --git a/src/styles/lesson.css b/src/styles/lesson.css index 4af8239..21003ec 100644 --- a/src/styles/lesson.css +++ b/src/styles/lesson.css @@ -201,6 +201,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; @@ -476,5 +500,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} } From ac6cac0d5ab441ef0003813aee89c7fb03b5ad05 Mon Sep 17 00:00:00 2001 From: devmtnaing Date: Sun, 27 Sep 2026 11:28:36 +0630 Subject: [PATCH 2/2] Part 1: the statement and the widget end on one line The two cards stretch to their row's height instead of aligning to the top, so the shorter one keeps its spare room inside its own card rather than leaving a hole beside the taller one. Which is taller varies by lesson (the statement on 20 of 46, the widget on 26), so this fixes both directions; below 900px they stack as before. --- src/styles/lesson.css | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/styles/lesson.css b/src/styles/lesson.css index 21003ec..ae75253 100644 --- a/src/styles/lesson.css +++ b/src/styles/lesson.css @@ -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}