Skip to content

Commit 3f9952c

Browse files
Do not collapse search bar when focus is lost
1 parent c1776f4 commit 3f9952c

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

css/components/search.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
}
88

99
@media (min-width: 1040px) {
10-
.site-header__search:focus-within {
10+
.site-header__search:focus-within,
11+
.site-header__search.has-content {
1112
max-width: 600px;
1213
}
1314
}

static/mobile-menu.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,26 @@ document.addEventListener('DOMContentLoaded', function() {
9595
}
9696
});
9797

98-
// Collapse search on blur (mobile only)
98+
// Collapse search on blur (mobile only) - only if empty
9999
searchInput.addEventListener('blur', function() {
100100
if (isMobile()) {
101101
// Small delay to allow clicking search results
102102
setTimeout(function() {
103-
headerContainer.classList.remove('search-expanded');
103+
// Only remove search-expanded if the input is empty
104+
if (searchInput.value.trim() === "") {
105+
headerContainer.classList.remove('search-expanded');
106+
}
104107
}, 200);
105108
}
106109
});
107110

108111
// Re-check on window resize
109112
window.addEventListener('resize', function() {
110113
if (!isMobile()) {
111-
headerContainer.classList.remove('search-expanded');
114+
// Only remove if empty when switching to desktop
115+
if (searchInput.value.trim() === "") {
116+
headerContainer.classList.remove('search-expanded');
117+
}
112118
}
113119
});
114120
});

static/search.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,29 @@ const ESCAPE_KEY = "Escape";
77
const searchInput = document.getElementById("search");
88
const searchResults = document.getElementById("search-results");
99
const searchResultsItems = document.getElementById("search-results__items");
10+
const searchContainer = document.querySelector(".site-header__search");
11+
const headerContainer = document.querySelector(".site-header__container");
1012

1113
let searchItemSelected = null;
1214
let resultsItemsIndex = -1;
1315

16+
// Function to update search container class based on input content
17+
function updateSearchContainerClass() {
18+
if (searchInput && searchContainer) {
19+
if (searchInput.value.trim() !== "") {
20+
searchContainer.classList.add("has-content");
21+
if (headerContainer) {
22+
headerContainer.classList.add("search-expanded");
23+
}
24+
} else {
25+
searchContainer.classList.remove("has-content");
26+
if (headerContainer) {
27+
headerContainer.classList.remove("search-expanded");
28+
}
29+
}
30+
}
31+
}
32+
1433
////////////////////////////////////
1534
// Interaction with the search input
1635
////////////////////////////////////
@@ -47,6 +66,7 @@ document.addEventListener("keydown", function (keyboardEvent) {
4766
searchInput.value = "";
4867
searchResults.style.display = "none";
4968
searchInput.blur();
69+
updateSearchContainerClass();
5070
break;
5171
}
5272
}
@@ -122,6 +142,8 @@ if (document.readyState === "complete" || (document.readyState !== "loading" &&
122142
}
123143

124144
function initSearch() {
145+
updateSearchContainerClass();
146+
125147
elasticlunr.trimmer = function (token) {
126148
if (token === null || token === undefined) {
127149
throw new Error("token should not be undefined");
@@ -181,11 +203,15 @@ function initSearch() {
181203

182204
searchItemSelected = null;
183205
resultsItemsIndex = -1;
206+
updateSearchContainerClass();
184207
debounce(showResults(index), 150)();
185208
});
186209

187210
// Hide results when user press on the "x" placed inside the search field
188-
searchInput.addEventListener("search", () => searchResults.style.display = "");
211+
searchInput.addEventListener("search", () => {
212+
searchResults.style.display = "";
213+
updateSearchContainerClass();
214+
});
189215
searchInput.addEventListener("focusin", function () {
190216
if (searchInput.value !== "") {
191217
showResults(index)();
@@ -270,7 +296,10 @@ function createMenuItem(result, index) {
270296
searchItemSelected = mouseEvent.target;
271297
resultsItemsIndex = index;
272298
})
273-
item.addEventListener("click", () => searchInput.value = "")
299+
item.addEventListener("click", () => {
300+
searchInput.value = "";
301+
updateSearchContainerClass();
302+
})
274303
searchResultsItems.appendChild(item);
275304
}
276305

0 commit comments

Comments
 (0)