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

Fixes for aria-live region #3781

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .changeset/violet-beans-study/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "react-select", "type": "patch" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/violet-beans-study/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix aria-live region to work properly with screen readers
14 changes: 10 additions & 4 deletions packages/react-select/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1787,11 +1787,17 @@ export default class Select extends Component<Props, State> {
}

renderLiveRegion() {
if (!this.state.isFocused) return null;
const { ariaLiveSelection, isFocused } = this.state;
const ariaLiveMessage = this.constructAriaLiveMessage();

return (
<A11yText aria-live="polite">
<p id="aria-selection-event">&nbsp;{this.state.ariaLiveSelection}</p>
<p id="aria-context">&nbsp;{this.constructAriaLiveMessage()}</p>
<A11yText aria-live="polite" aria-relevant="additions text" aria-atomic="true">
{isFocused && (
<React.Fragment>
<span id="aria-selection-event" key={ariaLiveSelection}>&nbsp;{ariaLiveSelection}</span>
<span id="aria-context" key={ariaLiveMessage}>&nbsp;{ariaLiveMessage}</span>
</React.Fragment>
)}
</A11yText>
);
}
Expand Down
43 changes: 42 additions & 1 deletion packages/react-select/src/__tests__/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ cases(
focusedOption,
}) => {
let onChangeSpy = jest.fn();
props = { ...props, onChange: onChangeSpy, menuIsOpen: true, hideSelectedOptions: false, isMulti: true, menuIsOpen: true };
props = { ...props, onChange: onChangeSpy, hideSelectedOptions: false, isMulti: true, menuIsOpen: true };
let selectWrapper = mount(<Select {...props} />);

let selectOption = selectWrapper
Expand Down Expand Up @@ -1626,6 +1626,26 @@ cases(
}
);

test('accessibility > renders aria-live region even when not focused', () => {
const selectWrapper = mount(<Select {...BASIC_PROPS} />);

expect(selectWrapper.find('span[aria-live="polite"]').exists()).toBe(true);
});

test('accessibility > aria-live region should not have content initially', () => {
const selectWrapper = mount(<Select {...BASIC_PROPS} />);

expect(selectWrapper.find('span[aria-live="polite"]').text()).toBe('');
expect(selectWrapper.find('span[aria-live="polite"]').children()).toHaveLength(0);
});

test('accessibility > aria-live region should have aria-atomic and aria-relevant defined', () => {
const selectWrapper = mount(<Select {...BASIC_PROPS} />);

expect(selectWrapper.find('span[aria-live="polite"]').prop('aria-atomic')).toBe('true');
expect(selectWrapper.find('span[aria-live="polite"]').prop('aria-relevant')).toBe('additions text');
});

test('accessibility > to show the number of options available in A11yText when the menu is Open', () => {
let selectWrapper = mount(<Select {...BASIC_PROPS} inputValue={''} autoFocus menuIsOpen />);
const liveRegionId = '#aria-context';
Expand Down Expand Up @@ -1670,6 +1690,27 @@ test('accessibility > interacting with disabled options shows correct A11yText',
);
});

test('accessibility > aria-live region descendants should have unique keys so that they are remounted in DOM and not just updated (NVDA in FF bug)', () => {
const selectWrapper = mount(<Select {...BASIC_PROPS} options={OPTIONS_DISABLED} inputValue={''} autoFocus menuIsOpen />);
const liveRegionId = '#aria-context';
const liveRegionEventId = '#aria-selection-event';

selectWrapper.setState({ isFocused: true });
selectWrapper.update();

const liveRegionKey = selectWrapper.find(liveRegionId).key();
const liveRegionEventKey = selectWrapper.find(liveRegionEventId).key();

selectWrapper
.find(Menu)
.simulate('keyDown', { keyCode: 40, key: 'ArrowDown' })
.simulate('keyDown', { keyCode: 40, key: 'ArrowDown' })
.simulate('keyDown', { keyCode: 13, key: 'Enter' });

expect(selectWrapper.find(liveRegionId).key()).not.toEqual(liveRegionKey);
expect(selectWrapper.find(liveRegionEventId).key()).not.toEqual(liveRegionEventKey);
});

test('accessibility > screenReaderStatus function prop > to pass custom text to A11yText', () => {
const screenReaderStatus = ({ count }) =>
`There are ${count} options available`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`defaults - snapshot 1`] = `
.emotion-8 {
.emotion-9 {
position: relative;
box-sizing: border-box;
}

.emotion-7 {
.emotion-0 {
z-index: 9999;
border: 0;
-webkit-clip: rect(1px,1px,1px,1px);
clip: rect(1px,1px,1px,1px);
height: 1px;
width: 1px;
position: absolute;
overflow: hidden;
padding: 0;
white-space: nowrap;
}

.emotion-8 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
Expand Down Expand Up @@ -36,11 +49,11 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-7:hover {
.emotion-8:hover {
border-color: hsl(0,0%,70%);
}

.emotion-2 {
.emotion-3 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
Expand All @@ -62,7 +75,7 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-0 {
.emotion-1 {
color: hsl(0,0%,50%);
margin-left: 2px;
margin-right: 2px;
Expand All @@ -74,7 +87,7 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-1 {
.emotion-2 {
margin: 2px;
padding-bottom: 2px;
padding-top: 2px;
Expand All @@ -83,7 +96,7 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-6 {
.emotion-7 {
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
Expand All @@ -101,7 +114,7 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-3 {
.emotion-4 {
-webkit-align-self: stretch;
-ms-flex-item-align: stretch;
align-self: stretch;
Expand All @@ -112,7 +125,7 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-5 {
.emotion-6 {
color: hsl(0,0%,80%);
display: -webkit-box;
display: -webkit-flex;
Expand All @@ -124,11 +137,11 @@ exports[`defaults - snapshot 1`] = `
box-sizing: border-box;
}

.emotion-5:hover {
.emotion-6:hover {
color: hsl(0,0%,60%);
}

.emotion-4 {
.emotion-5 {
display: inline-block;
fill: currentColor;
line-height: 1;
Expand Down Expand Up @@ -296,9 +309,21 @@ exports[`defaults - snapshot 1`] = `
}
>
<div
className=" emotion-8"
className=" emotion-9"
onKeyDown={[Function]}
>
<A11yText
aria-atomic="true"
aria-live="polite"
aria-relevant="additions text"
>
<span
aria-atomic="true"
aria-live="polite"
aria-relevant="additions text"
className="emotion-0"
/>
</A11yText>
<Control
clearValue={[Function]}
cx={[Function]}
Expand Down Expand Up @@ -399,7 +424,7 @@ exports[`defaults - snapshot 1`] = `
}
>
<div
className=" emotion-7"
className=" emotion-8"
onMouseDown={[Function]}
onTouchEnd={[Function]}
>
Expand Down Expand Up @@ -494,7 +519,7 @@ exports[`defaults - snapshot 1`] = `
}
>
<div
className=" emotion-2"
className=" emotion-3"
>
<Placeholder
clearValue={[Function]}
Expand Down Expand Up @@ -589,7 +614,7 @@ exports[`defaults - snapshot 1`] = `
}
>
<div
className=" emotion-0"
className=" emotion-1"
>
Select...
</div>
Expand Down Expand Up @@ -691,7 +716,7 @@ exports[`defaults - snapshot 1`] = `
value=""
>
<div
className="emotion-1"
className="emotion-2"
>
<AutosizeInput
aria-autocomplete="list"
Expand Down Expand Up @@ -871,7 +896,7 @@ exports[`defaults - snapshot 1`] = `
}
>
<div
className=" emotion-6"
className=" emotion-7"
>
<IndicatorSeparator
clearValue={[Function]}
Expand Down Expand Up @@ -965,7 +990,7 @@ exports[`defaults - snapshot 1`] = `
}
>
<span
className=" emotion-3"
className=" emotion-4"
/>
</IndicatorSeparator>
<DropdownIndicator
Expand Down Expand Up @@ -1068,7 +1093,7 @@ exports[`defaults - snapshot 1`] = `
>
<div
aria-hidden="true"
className=" emotion-5"
className=" emotion-6"
onMouseDown={[Function]}
onTouchEnd={[Function]}
>
Expand All @@ -1078,7 +1103,7 @@ exports[`defaults - snapshot 1`] = `
>
<svg
aria-hidden="true"
className="emotion-4"
className="emotion-5"
focusable="false"
height={20}
viewBox="0 0 20 20"
Expand Down
Loading