@@ -199,7 +199,6 @@ if (searchInput)
199
199
}
200
200
201
201
searchQuery = query
202
-
203
202
} ) ;
204
203
205
204
@@ -218,17 +217,15 @@ if (poweredByLink) {
218
217
}
219
218
220
219
document . addEventListener ( 'keydown' , function ( e ) {
221
-
222
220
const searchResults = Array . from ( document . querySelectorAll ( '.search-results__item' ) ) ;
221
+ const isArrowKey = e . key === "ArrowUp" || e . key === "ArrowDown" ;
223
222
224
223
if ( e . key === "Escape" ) {
225
224
e . preventDefault ( ) ;
226
225
if ( searchResults . length ) {
227
226
clearSearchResults ( true ) ;
228
227
searchInput . focus ( ) ;
229
- } else if (
230
- document . activeElement === searchInput
231
- ) {
228
+ } else if ( document . activeElement === searchInput ) {
232
229
clearNoResults ( ) ;
233
230
searchInput . blur ( ) ;
234
231
}
@@ -242,21 +239,23 @@ document.addEventListener('keydown', function (e) {
242
239
243
240
if ( ! searchResults . length ) return ;
244
241
245
- switch ( e . key ) {
246
- case "ArrowUp" :
247
- e . preventDefault ( ) ;
242
+ if ( isArrowKey ) {
243
+ e . preventDefault ( ) ;
244
+
245
+ if ( e . key === "ArrowUp" ) {
248
246
activeIndex = activeIndex - 1 < 0 ? searchResults . length - 1 : activeIndex - 1 ;
249
- break ;
250
- case "ArrowDown" :
251
- e . preventDefault ( ) ;
247
+ } else if ( e . key === "ArrowDown" ) {
252
248
activeIndex = activeIndex + 1 < searchResults . length ? activeIndex + 1 : 0 ;
253
- break ;
254
- }
249
+ }
250
+
251
+ if ( activeIndex !== - 1 ) {
252
+ const activeSearchResult = searchResults [ activeIndex ] ;
253
+ activeSearchResult . querySelector ( 'a' ) . focus ( ) ;
255
254
256
- if ( activeIndex === - 1 ) return ;
257
- const activeSearchResult = searchResults [ activeIndex ] ;
258
- activeSearchResult . querySelector ( 'a' ) . focus ( ) ;
259
- if ( isScrollable ( resultsElement ) ) {
260
- maintainScrollVisibility ( activeSearchResult , resultsElement ) ;
255
+ if ( isScrollable ( resultsElement ) ) {
256
+ maintainScrollVisibility ( activeSearchResult , resultsElement ) ;
257
+ }
258
+ }
261
259
}
262
260
} ) ;
261
+
0 commit comments