Skip to content

Commit 818fff5

Browse files
committed
Global Styles: do not announce a result when the block list is empty
An empty list renders its message as the wrapper's only child, so the child count read for the announcement was 1 and a screen reader heard "1 result found." alongside "You haven't customized any blocks yet." Reachable on the filter's own path, where the list can be empty with no search term, so count the children only when there are results.
1 parent ea85b2f commit 818fff5

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎packages/global-styles-ui/src/screen-block-list.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ function BlockList( { filterValue, styleFilter }: BlockListProps ) {
243243
const blockTypesListRef = useRef< HTMLDivElement >( null );
244244

245245
// Announce result count on change
246+
const hasResults = filteredBlockTypes.length > 0;
246247
useEffect( () => {
247248
if ( ! filterValue && styleFilter === 'all' ) {
248249
return;
@@ -255,14 +256,18 @@ function BlockList( { filterValue, styleFilter }: BlockListProps ) {
255256
// fragile and depends on the number of rendered elements of `BlockMenuItem`,
256257
// which is now one.
257258
// @see https://github.com/WordPress/gutenberg/pull/39117#discussion_r816022116
258-
const count = blockTypesListRef.current?.childElementCount || 0;
259+
// An empty list renders the empty state message as its only child, so
260+
// only count the children when there are results to count.
261+
const count = hasResults
262+
? blockTypesListRef.current?.childElementCount || 0
263+
: 0;
259264
const resultsFoundMessage = sprintf(
260265
/* translators: %d: number of results. */
261266
_n( '%d result found.', '%d results found.', count ),
262267
count
263268
);
264269
debouncedSpeak( resultsFoundMessage, 'polite' );
265-
}, [ filterValue, styleFilter, debouncedSpeak ] );
270+
}, [ filterValue, styleFilter, hasResults, debouncedSpeak ] );
266271

267272
return (
268273
<div

0 commit comments

Comments
 (0)