Skip to content
Open
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
7 changes: 4 additions & 3 deletions app/static/onboarding.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* First steps: the first-open question, the tour's spotlight, the checklist panel docked on the right,
and the pointer at what to do. Colours from the page's own tokens, so both themes follow. */
.onb-scrim { position: fixed; inset: 0; z-index: 90; background: rgba(0, 0, 0, .55); display: grid; place-items: center; padding: 16px; }
.onb-dialog { background: var(--surface); border: 1px solid var(--border-soft); border-radius: 14px; padding: 1.6rem; max-width: 440px; width: 100%;
display: grid; gap: .9rem; box-shadow: 0 20px 60px rgba(0, 0, 0, .35); }
.onb-dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border-soft); border-radius: 14px; padding: 1.6rem; width: min(440px, calc(100vw - 32px)); max-width: none;
gap: .9rem; box-shadow: 0 20px 60px rgba(0, 0, 0, .35); }
.onb-dialog[open] { display: grid; }
.onb-dialog::backdrop { background: rgba(0, 0, 0, .55); }
.onb-dialog .onb-mark { font-size: 1.9rem; line-height: 1; }
.onb-dialog h2 { margin: 0; font-size: 1.35rem; text-wrap: balance; }
.onb-dialog p { margin: 0; color: var(--text-dim); }
Expand Down
32 changes: 21 additions & 11 deletions app/static/onboarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,31 @@

// ---- the first-open question
function welcome() {
const layer = document.createElement('div');
layer.className = 'onb-scrim';
layer.innerHTML = `<div class="onb-dialog" role="dialog" aria-modal="true" aria-labelledby="onb-w-h"><div class="onb-mark" aria-hidden="true">∅</div>
// a native modal: it keeps the focus, makes the page behind it inert and closes on Escape
const d = document.createElement('dialog');
d.className = 'onb-dialog';
d.setAttribute('aria-labelledby', 'onb-w-h');
d.innerHTML = `<div class="onb-mark" aria-hidden="true">∅</div>
<h2 id="onb-w-h">Welcome to Nullius. Want a guided start?</h2>
<p>A one-minute look at the main pages, then a checklist that takes you to a first result:</p>
<ul><li>Set up a key and where runs happen</li><li>A free check run: oracle and nop call no model</li><li>Read the result, then compare an agent's run</li></ul>
<div class="onb-actions"><button class="btn ghost" type="button" data-a="no">No, I'll explore</button><button class="btn" type="button" data-a="yes">Yes, guide me</button></div></div>`;
<div class="onb-actions"><button class="btn ghost" type="button" data-a="no">No, I'll explore</button><button class="btn" type="button" data-a="yes">Yes, guide me</button></div>`;
const back = document.activeElement;
document.body.append(layer);
layer.querySelector('[data-a=yes]').focus();
layer.addEventListener('click', e => {
const a = e.target.closest('[data-a]')?.dataset.a;
if (!a) return;
S.asked = true; S.open = a === 'yes'; save(); layer.remove(); back?.focus();
if (a === 'yes') startTour(); else renderPanel();
document.body.append(d);
d.showModal();
d.querySelector('[data-a=yes]').focus();
d.addEventListener('click', e => { const a = e.target.closest('[data-a]')?.dataset.a; if (a) d.close(a); });
// Tab wraps between its two buttons instead of leaving for the browser's own controls
d.addEventListener('keydown', e => {
if (e.key !== 'Tab') return;
const b = [...d.querySelectorAll('button')], j = b.indexOf(document.activeElement) + (e.shiftKey ? -1 : 1);
e.preventDefault(); b[(j + b.length) % b.length].focus();
});
// Escape leaves returnValue empty: the same answer as No
d.addEventListener('close', () => {
const yes = d.returnValue === 'yes';
S.asked = true; S.open = yes; save(); d.remove(); back?.focus();
if (yes) startTour(); else renderPanel();
});
}

Expand Down
76 changes: 76 additions & 0 deletions app/ui_tests/test_onboarding_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""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."""
import json
from pathlib import Path
import shutil
import subprocess
import unittest

SCRIPT = Path(__file__).resolve().parents[1] / "static" / "onboarding.js"

ASK = 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, attrs = {}) { this.tag = tag; this.attrs = attrs; this.c = []; this.on = {}; this.classList = {toggle() {}}; }
set innerHTML(h) { this.c = [...h.matchAll(/data-a="(\w+)"/g)].map(m => Object.assign(new El('button', {'data-a': m[1]}), {parent: this, dataset: {a: m[1]}})); }
setAttribute(k, v) { this.attrs[k] = v; }
querySelector(s) { return this.c.find(b => s === '[data-a=' + b.dataset.a + ']') || null; }
querySelectorAll(s) { return s === 'button' ? this.c : []; }
closest(s) { return s === '[data-a]' && this.dataset?.a ? this : null; }
addEventListener(t, f) { (this.on[t] ||= []).push(f); }
fire(t, e = {}) { (this.on[t] || []).forEach(f => f(Object.assign({type: t, preventDefault() { e.prevented = true; }}, e))); return e; }
focus() { document.activeElement = this; }
append(x) { this.c.push(x); x.parent = this; }
remove() { this.removed = true; }
showModal() { this.open = true; }
close(v) { if (v !== undefined) this.returnValue = v; this.open = false; this.fire('close'); }
}
const replay = new El('button', {'data-onb': 'replay'}), made = [];
global.document = {currentScript: {dataset: {state: '/onboarding', tour: '[]'}}, body: new El('body'), activeElement: replay,
getElementById: () => null, querySelector: () => null, querySelectorAll: () => [], addEventListener() {},
createElement: tag => { const e = new El(tag); e.returnValue = ''; 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: []})})});
require(given.script);
setImmediate(() => {
const d = made.find(e => e.tag === 'dialog'), at = () => document.activeElement.dataset?.a;
const out = {modal: !!d.open, first: at(), tabs: []};
for (const shift of [false, false, false, true, true, true]) {
const e = d.fire('keydown', {key: 'Tab', shiftKey: shift}); out.tabs.push([at(), !!e.prevented]);
}
if (given.answer === 'escape') d.close(); // the browser's close request: no returnValue
else d.fire('click', {target: d.querySelector('[data-a=' + given.answer + ']')});
Object.assign(out, {closed: !d.open && !!d.removed, focus: document.activeElement === replay,
state: JSON.parse(localStorage.getItem('nullius-onboarding'))});
process.stdout.write(JSON.stringify(out));
});
'''


def ask(answer: str) -> 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}),
capture_output=True, text=True, check=True)
return json.loads(out.stdout)


class FirstOpenQuestion(unittest.TestCase):
def test_it_is_modal_and_tab_stays_on_its_buttons(self):
out = ask("no")
self.assertEqual((out["modal"], out["first"]), (True, "yes"))
self.assertEqual(out["tabs"], [["no", True], ["yes", True], ["no", True], ["yes", True], ["no", True], ["yes", True]])

def test_escape_answers_as_no_does_and_the_focus_goes_back(self):
no, escape, yes = ask("no"), ask("escape"), ask("yes")
self.assertEqual(escape["state"], no["state"])
self.assertEqual((no["state"]["asked"], no["state"]["open"]), (True, False))
self.assertTrue(yes["state"]["open"]) # Yes still starts the guide
for out in (no, escape, yes):
self.assertTrue(out["closed"]); self.assertTrue(out["focus"])


if __name__ == "__main__":
unittest.main()
Loading