Skip to content

Commit 3aa6dbe

Browse files
aloysbAloys BergeraloysreidbarberLFDanLu
authored
#3578 Add the ability to add a data attribute to <HiddenSelect /> to handle false a11y positives (#4759)
* Add option to add data attribute to hidden select This allows to use the data attribute as a reference for other tools to ignore a11y issues. * Add the ability to add a data-attr to handle a11y false positive This solves #3578 Some tools such as AXE or pa11y might give false positive on hidden select. This add a new prop to expose a data-attribute (dataset) that can be used to ignore these false positives. * Improve grammar, fix typos * Make the aria-rs-a11y-ignore attribute always available There is no harm in making this available, as discussed in the PR review. * Fix docs building issue This fixes an issue in the docs where the component was being parsed when it shouldn't have been parsed. * Update packages/@react-aria/select/docs/useSelect.mdx * Update packages/@react-aria/select/src/HiddenSelect.tsx * Update packages/@react-aria/select/src/HiddenSelect.tsx * Update packages/@react-aria/select/test/HiddenSelect.test.tsx * Move data attribute to container and add ignore rule to storybook axe * Update storybook selector Co-authored-by: Michael Jordan <[email protected]> * updating aria-attribute * fix tests --------- Co-authored-by: Aloys Berger <[email protected]> Co-authored-by: aloys <[email protected]> Co-authored-by: Reid Barber <[email protected]> Co-authored-by: Daniel Lu <[email protected]> Co-authored-by: Daniel Lu <[email protected]> Co-authored-by: Michael Jordan <[email protected]> Co-authored-by: Yihui Liao <[email protected]> Co-authored-by: Kyle Taborski <[email protected]>
1 parent a53e4df commit 3aa6dbe

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

.storybook/preview.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@ export const parameters = {
1414
options: {
1515
storySort: (a, b) => a[1].kind === b[1].kind ? 0 : a[1].id.localeCompare(b[1].id, undefined, { numeric: true }),
1616
},
17-
a11y: {},
17+
a11y: {
18+
config: {
19+
rules: [
20+
{
21+
id: 'aria-hidden-focus',
22+
selector: 'body *:not([data-a11y-ignore="aria-hidden-focus"])',
23+
}
24+
]
25+
}
26+
},
1827
layout: 'fullscreen',
1928
// Stops infinite loop memory crash when saving CSF stories https://github.com/storybookjs/storybook/issues/12747#issuecomment-1151803506
2029
docs: {

packages/@react-aria/select/docs/useSelect.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,9 @@ content that is passed into the select.
520520
In right-to-left languages, the select should be mirrored. The arrow should be on the left,
521521
and the selected value should be on the right. In addition, the content of list options should
522522
flip. Ensure that your CSS accounts for this.
523+
524+
### False positive on `<HiddenSelect />` in accessibility tools
525+
526+
When using &lt;<TypeLink links={docs.links} type={docs.exports.HiddenSelect} />&gt; some accessibility tools such as AXE or pa11y might give a false positive.
527+
The data attribute `data-rsp-a11y-ignore` is included on the hidden select element and can be used to ignore the false positives in these tools.
528+

packages/@react-aria/select/src/HiddenSelect.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export function useHiddenSelect<T>(props: AriaHiddenSelectOptions, state: Select
7979
return {
8080
containerProps: {
8181
...visuallyHiddenProps,
82-
'aria-hidden': true
82+
'aria-hidden': true,
83+
['data-a11y-ignore']: 'aria-hidden-focus'
8384
},
8485
inputProps: {
8586
type: 'text',
@@ -114,7 +115,7 @@ export function HiddenSelect<T>(props: HiddenSelectProps<T>) {
114115
// autofill will work. Otherwise, use an <input type="hidden">.
115116
if (state.collection.size <= 300) {
116117
return (
117-
<div {...containerProps}>
118+
<div {...containerProps} data-testid="hidden-select-container">
118119
<input {...inputProps} />
119120
<label>
120121
{label}

packages/@react-aria/select/test/HiddenSelect.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ describe('<HiddenSelect />', () => {
6060
userEvent.selectOptions(select, '5');
6161
expect(onSelectionChange).toBeCalledWith('5');
6262
});
63+
64+
it('should always add a data attribute data-a11y-ignore="aria-hidden-focus"', () => {
65+
render(
66+
<HiddenSelectExample items={makeItems(5)} />
67+
);
68+
69+
expect(screen.getByTestId('hidden-select-container')).toHaveAttribute('data-a11y-ignore', 'aria-hidden-focus');
70+
});
6371
});

0 commit comments

Comments
 (0)