Skip to content

Commit 354309c

Browse files
committed
refactor code
1 parent efed748 commit 354309c

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

web-components/nav-header/nav-header.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,17 @@ input {
242242
.logo h4 {
243243
font-size: 1em;
244244
}
245+
246+
form input {
247+
font-size: 1em;
248+
}
245249
}
246250

247251
/* 1023px and below screen sizes */
248252
@media screen and (max-width: 63.9375em) {
249253
#search-input {
250254
display: none;
251255
}
252-
253-
/* form input {
254-
font-size: 1em;
255-
} */
256256
}
257257

258258
/* 1024px and above screen sizes */

web-components/nav-header/nav-header.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,37 +83,23 @@ function createComponent(html) {
8383
// Event listeners for search
8484
// --------------------------------------------------------------------------------------------------
8585

86+
// Toggle open search bar (small screens)
8687
searchIcon.addEventListener("click", () => {
8788
if (window.innerWidth <= 1023) {
8889
if (siteHeader.style.display === "block") {
89-
siteHeader.style.display = "none";
90-
searchInput.style.display = "block";
91-
navigation.classList.add("search-bar-open");
92-
searchForm.classList.add("search-bar-open");
93-
searchBar.classList.add("search-bar-open");
90+
openSearchBar();
9491
} else {
95-
siteHeader.style.display = "block";
96-
searchInput.style.display = "none";
97-
navigation.classList.remove("search-bar-open");
98-
searchForm.classList.remove("search-bar-open");
99-
searchBar.classList.remove("search-bar-open");
92+
closeSearchBar("block", "none");
10093
}
10194
}
10295
});
10396

97+
// Toggle open search bar (on screen resize)
10498
window.addEventListener("resize", () => {
10599
if (window.innerWidth >= 1024) {
106-
siteHeader.style.display = "block";
107-
searchInput.style.display = "block";
108-
navigation.classList.remove("search-bar-open");
109-
searchForm.classList.remove("search-bar-open");
110-
searchBar.classList.remove("search-bar-open");
100+
closeSearchBar("block", "block");
111101
} else {
112-
siteHeader.style.display = "block";
113-
searchInput.style.display = "none";
114-
navigation.classList.remove("search-bar-open");
115-
searchForm.classList.remove("search-bar-open");
116-
searchBar.classList.remove("search-bar-open");
102+
closeSearchBar("block", "none");
117103
}
118104
});
119105

@@ -218,6 +204,23 @@ function createComponent(html) {
218204
arrow.classList.toggle("rotArrow");
219205
desc.classList.toggle("search_content_drop");
220206
}
207+
208+
// Open search bar
209+
function openSearchBar() {
210+
siteHeader.style.display = "none";
211+
searchInput.style.display = "block";
212+
navigation.classList.add("search-bar-open");
213+
searchForm.classList.add("search-bar-open");
214+
searchBar.classList.add("search-bar-open");
215+
}
216+
217+
function closeSearchBar(headerState, inputState) {
218+
siteHeader.style.display = headerState;
219+
searchInput.style.display = inputState;
220+
navigation.classList.remove("search-bar-open");
221+
searchForm.classList.remove("search-bar-open");
222+
searchBar.classList.remove("search-bar-open");
223+
}
221224
}
222225
}
223226

0 commit comments

Comments
 (0)