Skip to content

Commit

Permalink
fix: add accessible label to SearchBox input (#2193)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbuchel authored Feb 28, 2024
1 parent 874e16a commit df60b8e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
6 changes: 3 additions & 3 deletions bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"files": [
{
"path": "packages/docsearch-css/dist/style.css",
"maxSize": "3 kB"
"maxSize": "3.25 kB"
},
{
"path": "packages/docsearch-react/dist/umd/index.js",
"maxSize": "22.80 kB"
"maxSize": "23 kB"
},
{
"path": "packages/docsearch-js/dist/umd/index.js",
"maxSize": "30.70 kB"
"maxSize": "31 kB"
}
]
}
12 changes: 12 additions & 0 deletions packages/docsearch-css/src/modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@ svg.DocSearch-Hit-Select-Icon {
width: 20px;
}

/* Hide element accessibly, so that it is still accessible to
assistive tech users */
.DocSearch-VisuallyHiddenForAccessibility {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}

/* Responsive */
@media (max-width: 768px) {
:root {
Expand Down
5 changes: 5 additions & 0 deletions packages/docsearch-react/src/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type SearchBoxTranslations = Partial<{
resetButtonAriaLabel: string;
cancelButtonText: string;
cancelButtonAriaLabel: string;
searchInputLabel: string;
}>;

interface SearchBoxProps
Expand All @@ -39,6 +40,7 @@ export function SearchBox({ translations = {}, ...props }: SearchBoxProps) {
resetButtonAriaLabel = 'Clear the query',
cancelButtonText = 'Cancel',
cancelButtonAriaLabel = 'Cancel',
searchInputLabel = 'Search',
} = translations;
const { onReset } = props.getFormProps({
inputElement: props.inputRef.current,
Expand Down Expand Up @@ -67,6 +69,9 @@ export function SearchBox({ translations = {}, ...props }: SearchBoxProps) {
>
<label className="DocSearch-MagnifierLabel" {...props.getLabelProps()}>
<SearchIcon />
<span className="DocSearch-VisuallyHiddenForAccessibility">
{searchInputLabel}
</span>
</label>

<div className="DocSearch-LoadingIndicator">
Expand Down
36 changes: 15 additions & 21 deletions packages/docsearch-react/src/__tests__/api.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,8 @@ function noResultSearch(_queries: any, _requestOptions?: any): Promise<any> {
}

describe('api', () => {
let container: HTMLDivElement;

const docSearchSelector = '.DocSearch';

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('renders with minimal parameters', () => {
render(<DocSearch />);

Expand All @@ -68,10 +56,10 @@ describe('api', () => {
);
expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
expect(
document.querySelector('.DocSearch-Button-Placeholder').innerHTML
document.querySelector('.DocSearch-Button-Placeholder')?.innerHTML
).toBe('Recherche');
expect(
document.querySelector('.DocSearch-Button').getAttribute('aria-label')
document.querySelector('.DocSearch-Button')?.getAttribute('aria-label')
).toBe('Recherche');
});

Expand Down Expand Up @@ -154,30 +142,36 @@ describe('api', () => {
resetButtonAriaLabel: 'Effacer',
cancelButtonText: 'Annuler',
cancelButtonAriaLabel: 'Annuler',
searchInputLabel: 'Recherche',
},
},
}}
/>
);

expect(document.querySelector(docSearchSelector)).toBeInTheDocument();

await act(async () => {
fireEvent.click(await screen.findByText('Search'));
});

expect(document.querySelector('.DocSearch-Cancel').innerHTML).toBe(
const searchInputLabel = document.querySelector(
'.DocSearch-MagnifierLabel'
);

expect(document.querySelector(docSearchSelector)).toBeInTheDocument();

expect(document.querySelector('.DocSearch-Cancel')?.innerHTML).toBe(
'Annuler'
);
expect(
document.querySelector('.DocSearch-Cancel').getAttribute('aria-label')
document.querySelector('.DocSearch-Cancel')?.getAttribute('aria-label')
).toBe('Annuler');
expect(
document.querySelector('.DocSearch-Reset').getAttribute('title')
document.querySelector('.DocSearch-Reset')?.getAttribute('title')
).toBe('Effacer');
expect(
document.querySelector('.DocSearch-Reset').getAttribute('aria-label')
document.querySelector('.DocSearch-Reset')?.getAttribute('aria-label')
).toBe('Effacer');
expect(searchInputLabel?.textContent).toBe('Recherche');
});

it('overrides the default DocSearchModal footer text', async () => {
Expand Down Expand Up @@ -292,7 +286,7 @@ describe('api', () => {
expect(screen.getByText(/No results for/)).toBeInTheDocument();
const link = document.querySelector('.DocSearch-Help a');
expect(link).toBeInTheDocument();
expect(link.getAttribute('href')).toBe(
expect(link?.getAttribute('href')).toBe(
'https://github.com/algolia/docsearch/issues/new?title=q'
);
});
Expand Down
1 change: 1 addition & 0 deletions packages/docsearch-react/src/icons/SearchIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function SearchIcon() {
height="20"
className="DocSearch-Search-Icon"
viewBox="0 0 20 20"
aria-hidden="true"
>
<path
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"
Expand Down
1 change: 1 addition & 0 deletions packages/website/docs/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ const translations: DocSearchTranslations = {
resetButtonAriaLabel: 'Clear the query',
cancelButtonText: 'Cancel',
cancelButtonAriaLabel: 'Cancel',
searchInputLabel: 'Search',
},
startScreen: {
recentSearchesTitle: 'Recent',
Expand Down

0 comments on commit df60b8e

Please sign in to comment.