Skip to content

Commit 8543bfb

Browse files
fix: Allow closing the search via the search button (#424)
Especially on mobile devices, space is limited and users may not have enough space to close the search if they want to. This PR changes the "open search" behavior to a "toggle search" behavior (open if search is closed, close if search is open). In addition, two bugs are fixed: - The overlay wasn't displayed when the user navigated to the search field on the home page via tab. - The overlay wasn't closed if the search was opened via click and then moved out using the tab character (this will probably change when we implement the focus trapping, but for now it's the best solution I think). --------- Co-authored-by: Robert Schuster <77234379+therobrob@users.noreply.github.com>
1 parent a49f78e commit 8543bfb

File tree

5 files changed

+57
-31
lines changed

5 files changed

+57
-31
lines changed

assets/js/search.js

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,68 @@ const initSearch = () => {
3838

3939
const searchElement = search.querySelector("input");
4040

41+
const updateSearchButtonLabels = (isOpen) => {
42+
searchButtons.forEach((button) => {
43+
const openLabel = button.dataset.labelOpen;
44+
const closeLabel = button.dataset.labelClose;
45+
const label = isOpen ? closeLabel : openLabel;
46+
button.setAttribute("aria-label", label);
47+
button.setAttribute("title", label);
48+
});
49+
};
50+
4151
const closeSearch = () => {
4252
search.querySelector(".pagefind-ui__search-clear").click();
4353
overlay.classList.remove("overlay--show", "overlay--show-lv5");
4454
search.classList.remove("o-search--show");
55+
updateSearchButtonLabels(false);
4556
};
4657

47-
// Scroll to search on click
48-
if (search && isHome) {
49-
search.addEventListener("click", function () {
50-
overlay.classList.add("overlay--show", "overlay--show-lv5");
51-
search.scrollIntoView({ behavior: "smooth", block: "start" });
52-
});
53-
}
54-
55-
function showSearchOnContentPage() {
56-
search.classList.add("o-search--show");
58+
const openSearch = () => {
5759
overlay.classList.add("overlay--show", "overlay--show-lv5");
60+
search.classList.add("o-search--show");
5861
searchElement.focus();
5962
search.scrollIntoView({ behavior: "smooth", block: "start" });
60-
}
63+
updateSearchButtonLabels(true);
64+
};
6165

62-
function showSearchOnStartPage() {
63-
overlay.classList.add("overlay--show", "overlay--show-lv5");
64-
searchElement.focus();
65-
search.scrollIntoView({ behavior: "smooth", block: "start" });
66+
if (search && isHome) {
67+
searchElement.addEventListener("focus", () => {
68+
openSearch();
69+
});
70+
// If focus moves outside the search, close it
71+
search.addEventListener(
72+
"blur",
73+
(e) => {
74+
if (
75+
e.relatedTarget &&
76+
!search.contains(e.relatedTarget) &&
77+
!Array.from(searchButtons).includes(e.relatedTarget)
78+
) {
79+
closeSearch();
80+
}
81+
},
82+
true,
83+
);
6684
}
6785

68-
searchButtons.forEach((button) => {
69-
if (isHome) {
70-
button.addEventListener("click", showSearchOnStartPage);
71-
} else {
72-
button.addEventListener("click", showSearchOnContentPage);
86+
const toggleSearch = () => {
87+
if (search.classList.contains("o-search--show")) {
88+
closeSearch();
89+
return;
7390
}
91+
openSearch();
92+
};
93+
94+
searchButtons.forEach((button) => {
95+
button.addEventListener("click", toggleSearch);
7496
});
7597

76-
// Open search on Ctrl + K or Cmd + K
98+
// Toggle search on Ctrl + K or Cmd + K
7799
document.addEventListener("keydown", (e) => {
78100
if ((e.ctrlKey || e.metaKey) && e.key === "k") {
79101
e.preventDefault();
80-
if (isHome) {
81-
showSearchOnStartPage();
82-
} else {
83-
showSearchOnContentPage();
84-
}
102+
toggleSearch();
85103
}
86104
});
87105

i18n/de.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ menu:
6868
dropdown-label: Verfügbare Betreiber
6969
label: Betreiber
7070
title: Betreiber-Dropdown öffnen
71-
search: Suche öffnen
71+
search:
72+
close: Suche schließen
73+
open: Suche öffnen
7274
theme:
7375
switch-to-auto: Zu automatischem Modus wechseln
7476
switch-to-dark: Zu dunklem Modus wechseln

i18n/en.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ menu:
6767
dropdown-label: Available operators
6868
label: Operators
6969
title: Open Operator-Dropdown
70-
search: Open search
70+
search:
71+
close: Close search
72+
open: Open search
7173
theme:
7274
switch-to-auto: Switch to auto mode
7375
switch-to-dark: Switch to dark mode

i18n/fr.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ menu:
6767
dropdown-label: Opérateurs disponibles
6868
label: Opérateurs
6969
title: Ouvrir la liste déroulante de l'opérateur
70-
search: Ouvrir la recherche
70+
search:
71+
close: Fermer la recherche
72+
open: Ouvrir la recherche
7173
theme:
7274
switch-to-auto: Passer en mode automatique
7375
switch-to-dark: Passer en mode sombre

layouts/partials/search-button.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<button
22
class="o-header__button o-header__search"
3-
title="{{ T "menu.search" }}"
4-
aria-label="{{ T "menu.search" }}"
3+
title="{{ T "menu.search.open" }}"
4+
aria-label="{{ T "menu.search.open" }}"
5+
data-label-open="{{ T "menu.search.open" }}"
6+
data-label-close="{{ T "menu.search.close" }}"
57
type="button"
68
>
79
{{ partial "icon" "search" }}

0 commit comments

Comments
 (0)