Skip to content

Commit 44aa7dc

Browse files
feat(a11y): add hotkeys to search input aria-label (#2337)
1 parent 32ec976 commit 44aa7dc

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

bundlesize.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
{
1212
"path": "packages/docsearch-js/dist/umd/index.js",
13-
"maxSize": "35.05 kB"
13+
"maxSize": "35.08 kB"
1414
}
1515
]
1616
}

packages/docsearch-react/src/DocSearchButton.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,20 @@ export const DocSearchButton = React.forwardRef<HTMLButtonElement, DocSearchButt
3131
}
3232
}, []);
3333

34+
const [actionKeyReactsTo, actionKeyAltText, actionKeyChild] =
35+
key === ACTION_KEY_DEFAULT
36+
? // eslint-disable-next-line react/jsx-key -- false flag
37+
([ACTION_KEY_DEFAULT, 'Ctrl', <ControlKeyIcon />] as const)
38+
: (['Meta', 'Command', key] as const);
39+
3440
return (
35-
<button type="button" className="DocSearch DocSearch-Button" aria-label={buttonAriaLabel} {...props} ref={ref}>
41+
<button
42+
type="button"
43+
className="DocSearch DocSearch-Button"
44+
aria-label={`${buttonAriaLabel} (${actionKeyAltText}+K)`}
45+
{...props}
46+
ref={ref}
47+
>
3648
<span className="DocSearch-Button-Container">
3749
<SearchIcon />
3850
<span className="DocSearch-Button-Placeholder">{buttonText}</span>
@@ -41,9 +53,7 @@ export const DocSearchButton = React.forwardRef<HTMLButtonElement, DocSearchButt
4153
<span className="DocSearch-Button-Keys">
4254
{key !== null && (
4355
<>
44-
<DocSearchButtonKey reactsToKey={key === ACTION_KEY_DEFAULT ? ACTION_KEY_DEFAULT : 'Meta'}>
45-
{key === ACTION_KEY_DEFAULT ? <ControlKeyIcon /> : key}
46-
</DocSearchButtonKey>
56+
<DocSearchButtonKey reactsToKey={actionKeyReactsTo}>{actionKeyChild}</DocSearchButtonKey>
4757
<DocSearchButtonKey reactsToKey="k">K</DocSearchButtonKey>
4858
</>
4959
)}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('api', () => {
5959
);
6060
expect(document.querySelector(docSearchSelector)).toBeInTheDocument();
6161
expect(document.querySelector('.DocSearch-Button-Placeholder')?.innerHTML).toBe('Recherche');
62-
expect(document.querySelector('.DocSearch-Button')?.getAttribute('aria-label')).toBe('Recherche');
62+
expect(document.querySelector('.DocSearch-Button')?.getAttribute('aria-label')).toBe('Recherche (Ctrl+K)');
6363
});
6464

6565
it('overrides the default DocSearchModal startScreen text', async () => {

0 commit comments

Comments
 (0)