Skip to content

Commit

Permalink
Close docs search box when an entry is selected
Browse files Browse the repository at this point in the history
This comes up when clicking a link that changes
the fragment only, meaning the page doesn't transition
and the box doesn't get closed that way.
  • Loading branch information
rtfeldman committed Jan 15, 2025
1 parent ec2b8f9 commit c5d07c0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions crates/docs/src/static/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ const setupSearch = () => {
let searchForm = document.getElementById("module-search-form");
let topSearchResultListItem = undefined;

// Hide the results whenever anyone clicks outside the search results.
// Hide the results whenever anyone clicks outside the search results,
// or on a specific search result.
window.addEventListener("click", function (event) {
if (!searchForm?.contains(event.target)) {
if (!searchForm?.contains(event.target) || event.target.closest("#search-type-ahead a")) {
searchTypeAhead.classList.add("hidden");
}
});
Expand Down Expand Up @@ -101,6 +102,12 @@ const setupSearch = () => {

break;
}
case "Enter": {
// In case this is just an anchor link (which will move the scroll bar but not
// reload the page), hide the search bar.
searchTypeAhead.classList.add("hidden");
break;
}
}
}

Expand Down

0 comments on commit c5d07c0

Please sign in to comment.