diff --git a/app/static/onboarding.js b/app/static/onboarding.js index ae8e055ca..1daa9ad6e 100644 --- a/app/static/onboarding.js +++ b/app/static/onboarding.js @@ -152,6 +152,11 @@ if (e.target.closest('#onb-open')) { S.open = !S.open; save(); return renderPanel(); } if (pointed && pointed.contains(e.target)) unpoint(); }); + // Escape on the checklist or its toggle closes it, and the focus goes back to the toggle + document.addEventListener('keydown', e => { + if (e.key !== 'Escape' || !S.open || !(railItem?.contains(document.activeElement) || panel?.contains(document.activeElement))) return; + S.open = false; save(); unpoint(); renderPanel(); railItem?.focus(); + }); // ---- on load: the checklist read, this page's visit kept, then whatever was under way carried on nulliusFetch(STATE_URL, {headers: {Accept: 'application/json'}}).then(r => r.ok ? r.json() : {sections: []}).catch(() => ({sections: []})).then(j => { diff --git a/app/static/style.css b/app/static/style.css index 78accf262..dd3da7bb8 100644 --- a/app/static/style.css +++ b/app/static/style.css @@ -1599,8 +1599,8 @@ a.chip { text-decoration: none; } /* small screens: the rail is a drawer */ @media (max-width: 900px) { .shell, :root[data-rail="collapsed"] .shell { grid-template-columns: 1fr; } - .side { position: fixed; left: 0; top: 0; width: var(--rail-w); transform: translateX(-100%); transition: transform .16s ease; box-shadow: 0 0 40px rgba(0, 0, 0, .3); } - .side.drawer-open { transform: none; } + .side { position: fixed; left: 0; top: 0; width: var(--rail-w); transform: translateX(-100%); transition: transform .16s ease; } + .side.drawer-open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .3); } body.drawer-active { overflow: hidden; } :root[data-rail="collapsed"] .side-top { flex-direction: row; } :root[data-rail="collapsed"] .nav { overflow-y: auto; gap: .9rem; } diff --git a/app/ui_tests/test_onboarding_dialog.py b/app/ui_tests/test_onboarding_dialog.py index 17acd2218..7d3981311 100644 --- a/app/ui_tests/test_onboarding_dialog.py +++ b/app/ui_tests/test_onboarding_dialog.py @@ -1,5 +1,6 @@ """The first-open question, run under Node with a small stand-in DOM: Tab stays on its two buttons, -Escape answers as No does, and the focus goes back where it was.""" +Escape answers as No does, and the focus goes back where it was. The First steps panel closes on Escape +from its toggle or from inside it, the focus on its toggle.""" import json from pathlib import Path import shutil @@ -49,14 +50,50 @@ class El { ''' -def ask(answer: str) -> dict: +PANEL = r''' +const given = JSON.parse(require('fs').readFileSync(0, 'utf8')), store = () => { const m = new Map(); + return {getItem: k => m.has(k) ? m.get(k) : null, setItem: (k, v) => m.set(k, v), removeItem: k => m.delete(k)}; }; +class El { + constructor(tag) { this.tag = tag; this.attrs = {}; this.c = []; this.classList = {toggle() {}}; } + setAttribute(k, v) { this.attrs[k] = v; } + querySelector() { return {}; } + contains(x) { return x === this || this.c.includes(x); } + append(x) { this.c.push(x); } + focus() { document.activeElement = this; } + remove() { this.removed = true; } +} +const toggle = new El('button'), other = new El('a'), made = [], keys = []; +global.document = {currentScript: {dataset: {state: '/onboarding', tour: '[]'}}, body: new El('body'), activeElement: other, + getElementById: id => id === 'onb-open' ? toggle : null, querySelector: () => null, querySelectorAll: () => [], + addEventListener: (t, f) => { if (t === 'keydown') keys.push(f); }, createElement: tag => { const e = new El(tag); made.push(e); return e; }}; +Object.assign(global, {localStorage: store(), sessionStorage: store(), location: {pathname: '/', search: '', href: '/'}, + addEventListener() {}, matchMedia: () => ({matches: false}), ResizeObserver: class { observe() {} disconnect() {} }, + nulliusFetch: () => Promise.resolve({ok: true, json: () => ({sections: []})})}); +localStorage.setItem('nullius-onboarding', JSON.stringify({asked: true, open: true})); +require(given.script); +setImmediate(() => { + const panel = made.find(e => e.tag === 'aside'), inside = new El('button'); + panel.append(inside); + document.activeElement = {toggle, inside, other}[given.focus]; + keys.forEach(f => f({key: 'Escape'})); + process.stdout.write(JSON.stringify({open: JSON.parse(localStorage.getItem('nullius-onboarding')).open, + expanded: toggle.attrs['aria-expanded'], removed: !!panel.removed, focus: document.activeElement === toggle})); +}); +''' + + +def node(program: str, given: dict) -> dict: if not shutil.which("node"): - raise unittest.SkipTest("Node is required for the first-open question") - out = subprocess.run(["node", "-e", ASK], input=json.dumps({"script": str(SCRIPT), "answer": answer}), + raise unittest.SkipTest("Node is required for the onboarding script") + out = subprocess.run(["node", "-e", program], input=json.dumps({"script": str(SCRIPT), **given}), capture_output=True, text=True, check=True) return json.loads(out.stdout) +def ask(answer: str) -> dict: + return node(ASK, {"answer": answer}) + + class FirstOpenQuestion(unittest.TestCase): def test_it_is_modal_and_tab_stays_on_its_buttons(self): out = ask("no") @@ -72,5 +109,15 @@ def test_escape_answers_as_no_does_and_the_focus_goes_back(self): self.assertTrue(out["closed"]); self.assertTrue(out["focus"]) +class FirstStepsPanel(unittest.TestCase): + def test_escape_on_its_toggle_or_inside_it_closes_it_and_focuses_the_toggle(self): + for focus in ("toggle", "inside"): + with self.subTest(focus=focus): + self.assertEqual(node(PANEL, {"focus": focus}), {"open": False, "expanded": "false", "removed": True, "focus": True}) + + def test_escape_elsewhere_leaves_it_open(self): + self.assertEqual(node(PANEL, {"focus": "other"}), {"open": True, "expanded": "true", "removed": False, "focus": False}) + + if __name__ == "__main__": unittest.main()