Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docsearch-react): add aria-hidden to SearchIcon and add accessible label to SearchBox input #2193

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-VisuallyHidden {
hbuchel marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion packages/docsearch-react/src/DocSearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const DocSearchButton = React.forwardRef<
ref={ref}
>
<span className="DocSearch-Button-Container">
<SearchIcon />
<SearchIcon aria-hidden="true" />
<span className="DocSearch-Button-Placeholder">{buttonText}</span>
</span>

Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -66,7 +68,8 @@ export function SearchBox({ translations = {}, ...props }: SearchBoxProps) {
onReset={onReset}
>
<label className="DocSearch-MagnifierLabel" {...props.getLabelProps()}>
<SearchIcon />
<SearchIcon aria-hidden="true" />
<span className="DocSearch-VisuallyHidden">{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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like we're using container anywhere besides in the before/afterEach? So I removed them.


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
3 changes: 2 additions & 1 deletion packages/docsearch-react/src/icons/SearchIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react';

export function SearchIcon() {
export function SearchIcon({ ...rest }) {
hbuchel marked this conversation as resolved.
Show resolved Hide resolved
return (
<svg
width="20"
height="20"
className="DocSearch-Search-Icon"
viewBox="0 0 20 20"
{...rest}
>
<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