@@ -7,10 +7,29 @@ const ESCAPE_KEY = "Escape";
77const searchInput = document . getElementById ( "search" ) ;
88const searchResults = document . getElementById ( "search-results" ) ;
99const searchResultsItems = document . getElementById ( "search-results__items" ) ;
10+ const searchContainer = document . querySelector ( ".site-header__search" ) ;
11+ const headerContainer = document . querySelector ( ".site-header__container" ) ;
1012
1113let searchItemSelected = null ;
1214let 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
124144function 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