oficina / base Public

Return focus to the pill, and close a job view with the chat

Review round 1. A tap outside an open job view now gives focus back to
the pill unless the tap put it on something focusable. Closing the chat
by any route (its X from the keyboard, Escape, a screen narrowed to a
phone's width) closes the view too, so Escape is never taken afterwards
and nothing focuses an inert pill. TestAskJobViewOpens asserts focus
after both kinds of outside tap; TestAskJobViewClosesWithTheChat covers
keyboard dismissal and breakpoint changes. The view is measured once its
rise has finished.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev b9b76d992a589d74cafd73f1f9d784c6d0b8e86f parent 348a62f
2 files changed, +85 −6
  • internal/browsertest/cards_test.go +64 −2
  • internal/look/static/ask.js +21 −4
diff --git a/internal/browsertest/cards_test.go b/internal/browsertest/cards_test.go
index 28dd0cf..9b91c3a 100644
--- a/internal/browsertest/cards_test.go
+++ b/internal/browsertest/cards_test.go
@@ -304,6 +304,7 @@ func jobViewJS(id, status, side string) string {
if (!d.open) p.push("the details are closed");
if (s.getAttribute("aria-expanded") !== "true") p.push("aria-expanded " + s.getAttribute("aria-expanded"));
if (!v.checkVisibility({ visibilityProperty: true })) return p.concat("the view is not drawn").join("; ");
+ v.getAnimations().forEach((a) => a.finish()); // measured where it rests, not mid-rise
const r = v.getBoundingClientRect(), pill = j.querySelector(".pill").getBoundingClientRect();
const chat = document.querySelector("#ask-panel").getBoundingClientRect(), head = document.querySelector("#ask-panel .ask-head").getBoundingClientRect();
if (r.left < chat.left + 15.5 || r.right > chat.right - 15.5 || r.top < head.bottom || r.bottom > chat.bottom) p.push("outside the chat: " + JSON.stringify(r) + " in " + JSON.stringify(chat));
@@ -436,9 +437,19 @@ func TestAskJobViewOpens(t *testing.T) {
settle()
drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel .ask-head h2`)))
settle()
+ // A tap on the chat's own words (nothing to focus there) gives focus
+ // back to the pill.
+ if got := pending(closedViewJS(id)); got != "" {
+ t.Errorf("after a tap outside on the chat's header: %s", got)
+ }
+ // A tap on something that takes focus, the composer, keeps it there.
+ drive(t, rig, chromedp.Click(chromedp.CSS(summary)))
+ settle()
+ drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel textarea`)))
+ settle()
if got := pending(`(() => { const j = document.querySelector(` + strconv.Quote(job) + `);
- return j.querySelector("details").open + " " + j.querySelector(".jobview").checkVisibility() + " " + j.querySelector("summary").getAttribute("aria-expanded"); })()`); got != "false false false" {
- t.Errorf("after a tap outside: open, shown, expanded: %s", got)
+ return j.querySelector("details").open + " " + j.querySelector(".jobview").checkVisibility() + " " + j.querySelector("summary").getAttribute("aria-expanded") + " " + document.activeElement.tagName; })()`); got != "false false false TEXTAREA" {
+ t.Errorf("after a tap outside on the composer: open, shown, expanded, focus: %s", got)
}
// A new revision while it is open: still open, the new status, focus
@@ -494,3 +505,54 @@ func TestAskJobViewOpens(t *testing.T) {
t.Errorf("after the redraws: %s", got)
}
}
+
+// The job view goes with the chat: closing the docked chat from the
+// keyboard (its X, with Enter), or a narrowing screen that puts the chat
+// away, closes the view too. Escape afterwards is the page's own again,
+// and focus stays where closing the chat put it.
+func TestAskJobViewClosesWithTheChat(t *testing.T) {
+ rig, f := askBase(t)
+ openPanel(t, rig)
+ id := f.AskJob("Building your website")
+ job := `#ask-panel li.job[data-job="` + id + `"]`
+ drive(t, rig, chromedp.WaitVisible(chromedp.CSS(job+" summary")),
+ chromedp.Evaluate[chromedp.Void](`window.__esc = []; document.addEventListener("keydown", (e) => { if (e.key === "Escape") window.__esc.push(e.defaultPrevented); })`))
+ gone := `(() => { const j = document.querySelector(` + strconv.Quote(job) + `), p = [];
+ if (j.querySelector("details").open || j.querySelector("summary").getAttribute("aria-expanded") !== "false") p.push("still open");
+ if (j.querySelector(".jobview").matches(":popover-open")) p.push("the popover shows");
+ if (document.querySelector("#ask-panel .scrim")?.checkVisibility()) p.push("the scrim shows");
+ return p.join("; "); })()`
+ escape := func(when, focus string) {
+ t.Helper()
+ drive(t, rig, chromedp.Evaluate[chromedp.Void](`window.__esc = []`), chromedp.KeyEvent(kb.Escape), chromedp.Sleep(200*time.Millisecond))
+ if got := harness.Evaluate[string](rig, `JSON.stringify(window.__esc) + " " + document.activeElement.className`); got != "[false] "+focus {
+ t.Errorf("%s, Escape (taken, focus): %s, want [false] %s", when, got, focus)
+ }
+ }
+
+ // Closed from the keyboard: the chat's X, with Enter.
+ drive(t, rig, chromedp.Click(chromedp.CSS(job+" summary")), chromedp.Sleep(250*time.Millisecond),
+ chromedp.Focus(chromedp.CSS(`#ask-panel [data-ask-close]`)), chromedp.KeyEvent(kb.Enter), chromedp.Sleep(600*time.Millisecond))
+ if got := harness.Evaluate[string](rig, gone); got != "" {
+ t.Errorf("the chat closed from its X: %s", got)
+ }
+ escape("the chat closed from its X", "base-btn")
+
+ // Put away by a narrowing screen: a wide screen's open chat is not a
+ // phone's, which opens from the springboard.
+ drive(t, rig, chromedp.Click(chromedp.CSS(".base-btn")), chromedp.Poll[chromedp.Void](settledJS, chromedp.WithPollingTimeout(3*time.Second)),
+ chromedp.WaitVisible(chromedp.CSS(job+" summary")), chromedp.Click(chromedp.CSS(job+" summary")), chromedp.Sleep(250*time.Millisecond))
+ if !harness.Evaluate[bool](rig, `document.querySelector(`+strconv.Quote(job+" .jobview")+`).matches(":popover-open")`) {
+ t.Fatalf("the view did not open again")
+ }
+ drive(t, rig, chromedp.EmulateViewport(390, 844), chromedp.Sleep(600*time.Millisecond))
+ if got := harness.Evaluate[string](rig, gone); got != "" {
+ t.Errorf("narrowed to a phone: %s", got)
+ }
+ drive(t, rig, chromedp.Evaluate[chromedp.Void](`document.body.focus()`))
+ escape("narrowed to a phone", "")
+ drive(t, rig, chromedp.EmulateViewport(1280, 800), chromedp.Sleep(600*time.Millisecond))
+ if got := harness.Evaluate[string](rig, gone); got != "" {
+ t.Errorf("widened again: %s", got)
+ }
+}
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index 1cc3bd6..76c96c0 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -550,19 +550,33 @@ function start(root) {
else if (!e.target.open && viewing === li.dataset.job) closeView(false);
}, true);
scrim.addEventListener("click", () => closeView(true));
+ // A tap outside closes the view. Focus goes back to the pill, unless
+ // the tap put it on something else that takes it (once the tap's own
+ // focusing is done).
document.addEventListener("pointerdown", (e) => {
if (!viewing || e.target === scrim) return;
const li = item("data-job", viewing);
- if (li) {
- const { summary, view } = parts(li);
- if (view.contains(e.target) || summary.contains(e.target)) return;
+ if (!li) {
+ closeView(false);
+ return;
}
+ const { summary, view } = parts(li);
+ if (view.contains(e.target) || summary.contains(e.target)) return;
closeView(false);
+ setTimeout(() => {
+ const at = document.activeElement;
+ if ((!at || at === document.body) && summary.isConnected && !summary.closest("[inert]")) summary.focus({ preventScroll: true });
+ });
});
- // Escape closes the view before anything else hears it (the docked
+ // Escape closes an open view before anything else hears it (the docked
// chat stays open).
document.addEventListener("keydown", (e) => {
if (e.key !== "Escape" || !viewing || e.defaultPrevented) return;
+ const view = item("data-job", viewing)?.querySelector(".jobview");
+ if (!view || !view.matches(":popover-open")) {
+ closeView(false);
+ return;
+ }
e.preventDefault();
closeView(true);
}, true);
@@ -816,6 +830,9 @@ function start(root) {
let shown = false;
function sync() {
const open = isOpen();
+ // The job view goes with the chat, however it closes (its X, Escape,
+ // a screen narrowed past a phone's width).
+ if (!open && viewing) closeView(false);
dock.inert = !open;
if (slot) slot.inert = open && !phone.matches;
if (home) home.inert = open && phone.matches;