Skip to content

Commit 42bbca2

Browse files
Sisanuryelle
authored andcommitted
Adds aria-label to the search button, as accessibility enhancement (#38136)
* Adds aria-label to the search button, as accessibility enhancement * Strip all tags for the aria label attribute * Add aria label only when the button is an icon Co-authored-by: Kelly Dwan <ryelle@users.noreply.github.com>
1 parent b2bd2cf commit 42bbca2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

packages/block-library/src/search/index.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function render_block_core_search( $attributes ) {
7373
if ( $show_button ) {
7474
$button_internal_markup = '';
7575
$button_classes = $color_classes;
76+
$aria_label = '';
7677

7778
if ( ! $is_button_inside ) {
7879
$button_classes .= ' ' . $border_color_classes;
@@ -82,17 +83,19 @@ function render_block_core_search( $attributes ) {
8283
$button_internal_markup = wp_kses_post( $attributes['buttonText'] );
8384
}
8485
} else {
86+
$aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['label'] ) ) );
8587
$button_classes .= ' has-icon';
8688
$button_internal_markup =
8789
'<svg id="search-icon" class="search-icon" viewBox="0 0 24 24" width="24" height="24">
88-
<path d="M13.5 6C10.5 6 8 8.5 8 11.5c0 1.1.3 2.1.9 3l-3.4 3 1 1.1 3.4-2.9c1 .9 2.2 1.4 3.6 1.4 3 0 5.5-2.5 5.5-5.5C19 8.5 16.5 6 13.5 6zm0 9.5c-2.2 0-4-1.8-4-4s1.8-4 4-4 4 1.8 4 4-1.8 4-4 4z"></path>
89-
</svg>';
90+
<path d="M13.5 6C10.5 6 8 8.5 8 11.5c0 1.1.3 2.1.9 3l-3.4 3 1 1.1 3.4-2.9c1 .9 2.2 1.4 3.6 1.4 3 0 5.5-2.5 5.5-5.5C19 8.5 16.5 6 13.5 6zm0 9.5c-2.2 0-4-1.8-4-4s1.8-4 4-4 4 1.8 4 4-1.8 4-4 4z"></path>
91+
</svg>';
9092
}
9193

9294
$button_markup = sprintf(
93-
'<button type="submit" class="wp-block-search__button %s" %s>%s</button>',
95+
'<button type="submit" class="wp-block-search__button %s" %s %s>%s</button>',
9496
esc_attr( $button_classes ),
9597
$inline_styles['button'],
98+
$aria_label,
9699
$button_internal_markup
97100
);
98101
}

0 commit comments

Comments
 (0)