Skip to content

Commit 19fbfa7

Browse files
authored
Add label to search bar component for accessibility (#1139)
1 parent 972e250 commit 19fbfa7

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added label to search bar component for accessibility and CSS to hide the label on the screen.
13+
1014
## [3.178.1] - 2025-04-14
1115

1216
### Changed

react/__tests__/components/__snapshots__/SearchBar.test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ exports[`<SearchBar /> should match snapshot 1`] = `
1515
class="relative-m w-100 searchBarInnerContainer"
1616
role="combobox"
1717
>
18+
<label
19+
class="visuallyHidden"
20+
for="downshift-1-input"
21+
id="downshift-1-label"
22+
>
23+
Search
24+
</label>
1825
<div
1926
class="autoCompleteOuterContainer"
2027
>
@@ -24,6 +31,7 @@ exports[`<SearchBar /> should match snapshot 1`] = `
2431
<label>
2532
<input
2633
aria-autocomplete="list"
34+
aria-label="Search for products"
2735
aria-labelledby="downshift-1-label"
2836
autocomplete="off"
2937
data-error="false"

react/components/SearchBar/SearchBar.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,16 @@
3434
box-shadow: none;
3535
}
3636
}
37+
38+
.visuallyHidden {
39+
border: 0;
40+
clip: rect(0 0 0 0);
41+
clip-path: inset(50%);
42+
height: 1px;
43+
margin: -1px;
44+
overflow: hidden;
45+
padding: 0;
46+
position: absolute;
47+
white-space: nowrap;
48+
width: 1px;
49+
}

react/components/SearchBar/SearchBar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ function SearchBar({
273273
getInputProps,
274274
getItemProps,
275275
getMenuProps,
276+
getLabelProps,
276277
selectedItem,
277278
highlightedIndex,
278279
isOpen,
@@ -285,6 +286,9 @@ function SearchBar({
285286
[isOpen ? 'opened' : '', inputValue ? 'filled' : '']
286287
)}`}
287288
>
289+
<label {...getLabelProps()} className={styles.visuallyHidden}>
290+
{formatIOMessage({ id: placeholder, intl }) as string}
291+
</label>
288292
<AutocompleteInput
289293
// eslint-disable-next-line jsx-a11y/no-autofocus
290294
autoFocus={autoFocus}

0 commit comments

Comments
 (0)