oficina / base Public

Focus Ask Base's field when the page opens

On a wide screen with the chat open and ready, the composer takes focus
on load (without scrolling). Not on a phone, with the chat closed or not
ready, over existing focus, or over a URL fragment target.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 2f8334ba83f5debde05394033163f663b3ba7b70 parent 75b599f
2 files changed, +70 −0
  • internal/browsertest/ask_test.go +56 −0
  • internal/look/static/ask.js +14 −0
diff --git a/internal/browsertest/ask_test.go b/internal/browsertest/ask_test.go
index a9a6ec2..b4f4aa0 100644
--- a/internal/browsertest/ask_test.go
+++ b/internal/browsertest/ask_test.go
@@ -695,3 +695,59 @@ func TestAskMarkdownMatchesServer(t *testing.T) {
}
rig.Screen("#ask-panel", "the Assistant's simple Markdown")
}
+
+const focusedJS = `document.activeElement && document.activeElement.id`
+
+// The composer has focus when the page opens on a wide screen with the
+// chat open and ready, and only then.
+func TestAskFocusesFieldOnLoad(t *testing.T) {
+ load := func(t *testing.T, rig *harness.Rig, path string) string {
+ t.Helper()
+ drive(t, rig, chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible(chromedp.CSS(`body`)), chromedp.Sleep(500*time.Millisecond))
+ return harness.Evaluate[string](rig, focusedJS)
+ }
+ t.Run("desktop open", func(t *testing.T) {
+ rig, _ := askBase(t)
+ if got := load(t, rig, "/"); got != "ask-text" {
+ t.Errorf("focus is %q, want the composer", got)
+ }
+ if y := harness.Evaluate[float64](rig, `scrollY`); y != 0 {
+ t.Errorf("the page scrolled to %v", y)
+ }
+ if got := load(t, rig, "/ask"); got != "ask-text" {
+ t.Errorf("/ask focus is %q, want the composer", got)
+ }
+ })
+ t.Run("desktop closed", func(t *testing.T) {
+ rig, _, b := askBaseWith(t, nil)
+ openPanel(t, rig)
+ drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel .ask-head button.close`)))
+ askClosed(t, b, true)
+ if got := load(t, rig, "/"); got == "ask-text" {
+ t.Error("the closed chat's composer took focus")
+ }
+ })
+ t.Run("phone", func(t *testing.T) {
+ rig, _, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3)))
+ if got := load(t, rig, "/"); got == "ask-text" {
+ t.Error("the composer took focus on a phone")
+ }
+ if got := load(t, rig, "/ask"); got == "ask-text" {
+ t.Error("/ask's composer took focus on a phone")
+ }
+ })
+ t.Run("not connected", func(t *testing.T) {
+ var b *baseApp
+ rig := newRig(t, func(origin string) http.Handler {
+ b = newBase(t, baseConfig(origin))
+ return b.handler
+ })
+ signInByEmail(t, rig, rig.Origin, b.mail)
+ drive(t, rig, chromedp.EmulateViewport(1280, 800))
+ for _, path := range []string{"/", "/ask"} {
+ if got := load(t, rig, path); got == "ask-text" {
+ t.Errorf("%s: the composer took focus without a connection", path)
+ }
+ }
+ })
+}
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index 39844b8..dbc6a98 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -821,8 +821,21 @@ function start(root) {
if (out.turn) setTurn(out.turn);
});
+ // On a wide screen the field is ready when the page opens: only with the
+ // chat open and ready, never on a phone (it would raise the keyboard), and
+ // never over focus the person already has or a link's target (#...).
+ const target = location.hash;
+ function focusOnLoad() {
+ if (phone.matches || (dock && !isOpen()) || !chatting(root.dataset.state) || field.disabled) return;
+ if (target && target !== "#ask") return;
+ const at = document.activeElement;
+ if (at && at !== document.body && at !== document.documentElement) return;
+ field.focus({ preventScroll: true });
+ }
+
if (!dock) {
load();
+ focusOnLoad();
return;
}
@@ -973,4 +986,5 @@ function start(root) {
if (!isOpen()) setOpen(true);
}
sync();
+ focusOnLoad();
}