Skip to content

Commit e1352de

Browse files
gkorlandCopilot
andauthored
fix(nav): focus search input when mobile menu opens (#523)
Code review of #518 found that moving the search box to the top of the mobile menu (visually) left keyboard/screen-reader focus order unchanged: Tab still traverses the whole nav list before reaching search, since DOM order wasn't touched, only CSS position. That's now inconsistent with the new visual order. Focus the search input as soon as the mobile menu opens, matching its new position at the top. The listener runs after Just the Docs' own (head_custom.html loads after just-the-docs.js), so nav-open has already been toggled by the time it checks. Confirmed this doesn't prematurely expand the search results UI (that's gated on non-empty input, unaffected by focus alone) and doesn't affect desktop, where the menu button is hidden. Verified via headless Chromium: menu open -> focus lands on #search-input; typing still triggers results normally; desktop unaffected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d0e8fe4 commit e1352de

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

_includes/head_custom.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@
5353
}
5454
</style>
5555

56+
<!-- Mobile menu: move focus to search when the menu opens. The search box
57+
now appears visually at the top of the mobile menu (see above), but
58+
keyboard/screen-reader focus order still follows the unchanged DOM
59+
order (nav links, then search). Moving focus to the search input on
60+
open keeps focus order consistent with what's visually first, without
61+
changing tab order for anyone not using the menu button.
62+
63+
Ordering notes for future maintainers:
64+
- This listener is attached after Just the Docs' own (head_custom.html
65+
is included after just-the-docs.js, see theme's _includes/head.html),
66+
so nav-open has already been toggled by the time it runs.
67+
- The check is intentionally synchronous (no setTimeout/microtask
68+
deferral): iOS Safari only shows the on-screen keyboard for a
69+
programmatic focus() call made synchronously within the original
70+
click handler. Deferring the check would move focus silently
71+
without opening the keyboard on iOS Safari, defeating the point of
72+
this fix for a large share of mobile users. -->
73+
<script>
74+
document.addEventListener("DOMContentLoaded", function () {
75+
var menuButton = document.getElementById("menu-button");
76+
var mainHeader = document.getElementById("main-header");
77+
var searchInput = document.getElementById("search-input");
78+
if (!menuButton || !mainHeader || !searchInput) return;
79+
80+
menuButton.addEventListener("click", function () {
81+
if (mainHeader.classList.contains("nav-open")) {
82+
searchInput.focus();
83+
}
84+
});
85+
});
86+
</script>
87+
5688
<!-- Sticky right-side Table of Contents -->
5789
<style>
5890
.toc-sidebar {

0 commit comments

Comments
 (0)