Skip to content

Commit debee54

Browse files
committed
Improve nav menu search
Adds wp.a11y.speak() on search results, clears results when no text.
1 parent a113c05 commit debee54

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/js/_enqueues/lib/nav-menu.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,14 +1406,25 @@
14061406

14071407
updateQuickSearchResults : function(input) {
14081408
var panel, params,
1409-
minSearchLength = 2,
1410-
q = input.val();
1409+
minSearchLength = 1,
1410+
q = input.val(),
1411+
pageSearchChecklist = $( '#page-search-checklist' );
14111412

14121413
/*
1413-
* Minimum characters for a search. Also avoid a new Ajax search when
1414-
* the pressed key (e.g. arrows) doesn't change the searched term.
1414+
* Avoid a new Ajax search when the pressed key (e.g. arrows)
1415+
* doesn't change the searched term.
14151416
*/
1416-
if ( q.length < minSearchLength || api.lastSearch == q ) {
1417+
if ( api.lastSearch == q ) {
1418+
return;
1419+
}
1420+
1421+
/*
1422+
* Reset results when search is less than or equal to
1423+
* minimum characters for searched term.
1424+
*/
1425+
if ( q.length <= minSearchLength ) {
1426+
pageSearchChecklist.empty();
1427+
wp.a11y.speak( wp.i18n.__( 'Search results cleared' ) );
14171428
return;
14181429
}
14191430

@@ -1770,12 +1781,14 @@
17701781
$item;
17711782

17721783
if( ! $items.length ) {
1784+
let noResults = wp.i18n.__( 'No results found.' );
17731785
const li = $( '<li>' );
1774-
const p = $( '<p>', { text: wp.i18n.__( 'No results found.' ) } );
1786+
const p = $( '<p>', { text: noResults } );
17751787
li.append( p );
17761788
$('.categorychecklist', panel).empty().append( li );
17771789
$( '.spinner', panel ).removeClass( 'is-active' );
17781790
wrapper.addClass( 'has-no-menu-item' );
1791+
wp.a11y.speak( noResults, 'assertive' );
17791792
return;
17801793
}
17811794

@@ -1802,6 +1815,7 @@
18021815
});
18031816

18041817
$('.categorychecklist', panel).html( $items );
1818+
wp.a11y.speak( wp.i18n.sprintf( wp.i18n.__( '%d Search Results Found' ), $items.length ), 'assertive' );
18051819
$( '.spinner', panel ).removeClass( 'is-active' );
18061820
wrapper.removeClass( 'has-no-menu-item' );
18071821

0 commit comments

Comments
 (0)