Skip to content

Commit df60b8e

Browse files
authored
fix: add accessible label to SearchBox input (#2193)
1 parent 874e16a commit df60b8e

File tree

6 files changed

+37
-24
lines changed

6 files changed

+37
-24
lines changed

bundlesize.config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"files": [
33
{
44
"path": "packages/docsearch-css/dist/style.css",
5-
"maxSize": "3 kB"
5+
"maxSize": "3.25 kB"
66
},
77
{
88
"path": "packages/docsearch-react/dist/umd/index.js",
9-
"maxSize": "22.80 kB"
9+
"maxSize": "23 kB"
1010
},
1111
{
1212
"path": "packages/docsearch-js/dist/umd/index.js",
13-
"maxSize": "30.70 kB"
13+
"maxSize": "31 kB"
1414
}
1515
]
1616
}

packages/docsearch-css/src/modal.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,18 @@ svg.DocSearch-Hit-Select-Icon {
551551
width: 20px;
552552
}
553553

554+
/* Hide element accessibly, so that it is still accessible to
555+
assistive tech users */
556+
.DocSearch-VisuallyHiddenForAccessibility {
557+
clip: rect(0 0 0 0);
558+
clip-path: inset(50%);
559+
height: 1px;
560+
overflow: hidden;
561+
position: absolute;
562+
white-space: nowrap;
563+
width: 1px;
564+
}
565+
554566
/* Responsive */
555567
@media (max-width: 768px) {
556568
:root {

packages/docsearch-react/src/SearchBox.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type SearchBoxTranslations = Partial<{
1616
resetButtonAriaLabel: string;
1717
cancelButtonText: string;
1818
cancelButtonAriaLabel: string;
19+
searchInputLabel: string;
1920
}>;
2021

2122
interface SearchBoxProps
@@ -39,6 +40,7 @@ export function SearchBox({ translations = {}, ...props }: SearchBoxProps) {
3940
resetButtonAriaLabel = 'Clear the query',
4041
cancelButtonText = 'Cancel',
4142
cancelButtonAriaLabel = 'Cancel',
43+
searchInputLabel = 'Search',
4244
} = translations;
4345
const { onReset } = props.getFormProps({
4446
inputElement: props.inputRef.current,
@@ -67,6 +69,9 @@ export function SearchBox({ translations = {}, ...props }: SearchBoxProps) {
6769
>
6870
<label className="DocSearch-MagnifierLabel" {...props.getLabelProps()}>
6971
<SearchIcon />
72+
<span className="DocSearch-VisuallyHiddenForAccessibility">
73+
{searchInputLabel}
74+
</span>
7075
</label>
7176

7277
<div className="DocSearch-LoadingIndicator">

packages/docsearch-react/src/__tests__/api.test.tsx

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,8 @@ function noResultSearch(_queries: any, _requestOptions?: any): Promise<any> {
3434
}
3535

3636
describe('api', () => {
37-
let container: HTMLDivElement;
38-
3937
const docSearchSelector = '.DocSearch';
4038

41-
beforeEach(() => {
42-
container = document.createElement('div');
43-
document.body.appendChild(container);
44-
});
45-
46-
afterEach(() => {
47-
document.body.removeChild(container);
48-
container = null;
49-
});
50-
5139
it('renders with minimal parameters', () => {
5240
render(<DocSearch />);
5341

@@ -68,10 +56,10 @@ describe('api', () => {
6856
);
6957
expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
7058
expect(
71-
document.querySelector('.DocSearch-Button-Placeholder').innerHTML
59+
document.querySelector('.DocSearch-Button-Placeholder')?.innerHTML
7260
).toBe('Recherche');
7361
expect(
74-
document.querySelector('.DocSearch-Button').getAttribute('aria-label')
62+
document.querySelector('.DocSearch-Button')?.getAttribute('aria-label')
7563
).toBe('Recherche');
7664
});
7765

@@ -154,30 +142,36 @@ describe('api', () => {
154142
resetButtonAriaLabel: 'Effacer',
155143
cancelButtonText: 'Annuler',
156144
cancelButtonAriaLabel: 'Annuler',
145+
searchInputLabel: 'Recherche',
157146
},
158147
},
159148
}}
160149
/>
161150
);
162151

163-
expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
164-
165152
await act(async () => {
166153
fireEvent.click(await screen.findByText('Search'));
167154
});
168155

169-
expect(document.querySelector('.DocSearch-Cancel').innerHTML).toBe(
156+
const searchInputLabel = document.querySelector(
157+
'.DocSearch-MagnifierLabel'
158+
);
159+
160+
expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
161+
162+
expect(document.querySelector('.DocSearch-Cancel')?.innerHTML).toBe(
170163
'Annuler'
171164
);
172165
expect(
173-
document.querySelector('.DocSearch-Cancel').getAttribute('aria-label')
166+
document.querySelector('.DocSearch-Cancel')?.getAttribute('aria-label')
174167
).toBe('Annuler');
175168
expect(
176-
document.querySelector('.DocSearch-Reset').getAttribute('title')
169+
document.querySelector('.DocSearch-Reset')?.getAttribute('title')
177170
).toBe('Effacer');
178171
expect(
179-
document.querySelector('.DocSearch-Reset').getAttribute('aria-label')
172+
document.querySelector('.DocSearch-Reset')?.getAttribute('aria-label')
180173
).toBe('Effacer');
174+
expect(searchInputLabel?.textContent).toBe('Recherche');
181175
});
182176

183177
it('overrides the default DocSearchModal footer text', async () => {
@@ -292,7 +286,7 @@ describe('api', () => {
292286
expect(screen.getByText(/No results for/)).toBeInTheDocument();
293287
const link = document.querySelector('.DocSearch-Help a');
294288
expect(link).toBeInTheDocument();
295-
expect(link.getAttribute('href')).toBe(
289+
expect(link?.getAttribute('href')).toBe(
296290
'https://github.com/algolia/docsearch/issues/new?title=q'
297291
);
298292
});

packages/docsearch-react/src/icons/SearchIcon.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export function SearchIcon() {
77
height="20"
88
className="DocSearch-Search-Icon"
99
viewBox="0 0 20 20"
10+
aria-hidden="true"
1011
>
1112
<path
1213
d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z"

packages/website/docs/api.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const translations: DocSearchTranslations = {
171171
resetButtonAriaLabel: 'Clear the query',
172172
cancelButtonText: 'Cancel',
173173
cancelButtonAriaLabel: 'Cancel',
174+
searchInputLabel: 'Search',
174175
},
175176
startScreen: {
176177
recentSearchesTitle: 'Recent',

0 commit comments

Comments
 (0)