oficina / base Public

Keep focus in place when a job's pill is redrawn

addJob moved a job's cards into the new pill before it read where focus
was, so a focused go-ahead or Publish button lost focus on every new
revision; focus on the pill's link was dropped, and focus on a link in
the open job view went to Close. It now reads focus before moving
anything and puts it back on the same card control, or on the new
pill's twin of what had it (the pill, its link, the view's title, its
link, Close), falling back to Close in an open view and then the pill.

Test: focus on each of those, and on a decision drawn before its job,
survives a redraw.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 488b9fc5874424dbb9598fed3a248aa4bf056d0d parent e00529d
2 files changed, +91 −5
  • internal/browsertest/cards_test.go +74 −0
  • internal/look/static/ask.js +17 −5
diff --git a/internal/browsertest/cards_test.go b/internal/browsertest/cards_test.go
index 9b91c3a..6baeebd 100644
--- a/internal/browsertest/cards_test.go
+++ b/internal/browsertest/cards_test.go
@@ -556,3 +556,77 @@ func TestAskJobViewClosesWithTheChat(t *testing.T) {
t.Errorf("widened again: %s", got)
}
}
+
+// A job redrawn (a new revision) keeps focus where it was: on a decision
+// under its pill, which moves to the new pill; on the pill or its link to
+// the work; and with its view open, on the view's title, its link to the
+// work, or Close. A decision drawn before its job keeps focus as the job
+// arrives and the decision moves under it.
+func TestAskJobRedrawKeepsFocus(t *testing.T) {
+ rig, f := askBase(t)
+ // Wide enough for a ready pill's link beside its status.
+ drive(t, rig, chromedp.EmulateViewport(1500, 900))
+ openPanel(t, rig)
+ wait := func(js string) {
+ t.Helper()
+ drive(t, rig, chromedp.Poll[chromedp.Void](js, chromedp.WithPollingTimeout(10*time.Second)))
+ }
+ options := []equipo.Option{{Value: "go", Label: "Go ahead"}, {Value: "later", Label: "Not yet"}}
+ try := equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}
+ id := f.AskJob("Building your website")
+ f.SetJob(id, "ready", "Ready to try", "Have a look.", try)
+ goAhead := f.AskOutcomeFor(id, "This is ready. Go ahead?", options, nil)
+ job := `#ask-panel li.job[data-job="` + id + `"]`
+ wait(jobJS(id) + ` === ` + strconv.Quote("1 ready Ready to try [Try it] "+goAhead))
+ focusedJS := func(sel string) string {
+ return `(() => { const a = document.activeElement, want = document.querySelector(` + strconv.Quote(sel) + `);
+ if (!want) return "no " + ` + strconv.Quote(sel) + `;
+ return a === want ? "" : "focus on " + a.tagName + "." + a.className + " " + (a.textContent || "").trim().slice(0, 30); })()`
+ }
+ rev := 2
+ redraw := func(where, sel string) {
+ t.Helper()
+ drive(t, rig, chromedp.Focus(chromedp.CSS(sel)))
+ if got := harness.Evaluate[string](rig, focusedJS(sel)); got != "" {
+ t.Fatalf("focusing %s: %s", where, got)
+ }
+ rev++
+ f.SetJob(id, "ready", "Ready to try", fmt.Sprintf("Revision %d.", rev), try)
+ wait(`document.querySelector(` + strconv.Quote(job) + `)?.dataset.revision === "` + strconv.Itoa(rev) + `"`)
+ drive(t, rig, chromedp.Sleep(150*time.Millisecond))
+ if got := harness.Evaluate[string](rig, focusedJS(sel)); got != "" {
+ t.Errorf("focused on %s, redrawn: %s", where, got)
+ }
+ }
+ redraw("the go-ahead's button", job+` ol.job-cards [data-card="`+goAhead+`"] button[value="go"]`)
+ redraw("the pill", job+` summary`)
+ redraw("the pill's link", job+` .pill-link`)
+ drive(t, rig, chromedp.Click(chromedp.CSS(job+` summary`)), chromedp.Sleep(250*time.Millisecond))
+ redraw("the job view's title", job+` .jobview a.jv-title`)
+ redraw("the job view's link", job+` .jobview a.jv-go`)
+ redraw("the job view's Close", job+` .jobview [data-job-close]`)
+ if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector(`+strconv.Quote(job)+`);
+ return j.querySelector("details").open + " " + j.querySelector(".jobview").matches(":popover-open"); })()`); got != "true true" {
+ t.Errorf("after the redraws the view (open, shown): %s", got)
+ }
+ drive(t, rig, chromedp.KeyEvent(kb.Escape), chromedp.Sleep(200*time.Millisecond))
+
+ // A decision of a job not yet drawn, focused, as the job arrives. (The
+ // fake numbers its cards in turn: the job is the card after it.)
+ n, err := strconv.Atoi(strings.TrimPrefix(goAhead, "c_"))
+ if err != nil {
+ t.Fatalf("card id %q", goAhead)
+ }
+ early := f.AskOutcomeFor(fmt.Sprintf("c_%04d", n+2), "And this one?", options, nil)
+ button := `#ask-panel [data-card="` + early + `"] button[value="go"]`
+ wait(`!!document.querySelector('#ask-panel .thread > [data-card="` + early + `"]')`)
+ drive(t, rig, chromedp.Focus(chromedp.CSS(button)))
+ if other := f.AskJob("Fixing the login"); other != fmt.Sprintf("c_%04d", n+2) {
+ t.Fatalf("the next job is %s", other)
+ }
+ wait(`!!document.querySelector('#ask-panel li.job ol.job-cards > [data-card="` + early + `"]')`)
+ drive(t, rig, chromedp.Sleep(150*time.Millisecond))
+ if got := harness.Evaluate[string](rig, focusedJS(button)); got != "" {
+ t.Errorf("focused on a decision as its job arrived: %s", got)
+ }
+}
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index 6b9d2d8..39844b8 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -348,10 +348,17 @@ function start(root) {
const href = go && typeof go.url === "string" && webURL(go.url);
return href ? { href, label: go.label || href } : null;
}
+ // What in a pill can hold focus, each with its twin in the redrawn pill.
+ const twins = ["summary.pill-main", "a.pill-link", "a.jv-title", "a.jv-go", "[data-job-close]"];
function addJob(c) {
if (!c || !c.id || !jobStates.includes(c.state)) return;
const old = item("data-job", c.id);
if (old && Number(old.dataset.revision) > c.revision) return;
+ // Where focus is, read before anything moves: a node that moves loses
+ // it, and a card that moves here is the same node.
+ const focus = document.activeElement;
+ const twin = old && old.contains(focus) ? twins.find((s) => old.querySelector(s) === focus) : undefined;
+ const inView = !!old && !!old.querySelector(".jobview")?.contains(focus);
const li = document.createElement("li");
li.className = "job";
li.dataset.job = c.id;
@@ -379,9 +386,6 @@ function start(root) {
if (old) list.append(...old.querySelectorAll(":scope > ol.job-cards > li"));
list.append(...thread.querySelectorAll(`:scope > [data-for="${CSS.escape(c.id)}"]`));
li.append(pill, list);
- const focus = document.activeElement;
- const inView = !!old && !!old.querySelector(".jobview")?.contains(focus);
- const onPill = !!old && old.querySelector("summary") === focus;
if (popovers) {
summary.setAttribute("aria-expanded", "false");
} else {
@@ -389,8 +393,16 @@ function start(root) {
}
if (old) old.replaceWith(li);
else thread.append(li);
- if (popovers && viewing === c.id) openView(li, inView, true);
- else if (onPill) summary.focus({ preventScroll: true });
+ if (popovers && viewing === c.id) openView(li, false, true);
+ // Focus goes back: to the card that moved here, or to the twin of what
+ // had it; in the view, when its twin has gone, to Close; else the pill.
+ let to = null;
+ if (focus && li.contains(focus)) to = focus;
+ else if (twin || inView) {
+ to = [twin && li.querySelector(twin), inView && li.querySelector("[data-job-close]"), summary]
+ .find((el) => el && !el.hidden && el.checkVisibility({ visibilityProperty: true }));
+ }
+ if (to && document.activeElement !== to) to.focus({ preventScroll: true });
scroll();
}
// The job view, opened from the pill, drawn as the server draws it (the