Skip to content

fix(core/mat-option): don't remove aria-selected from deselected options #25749

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 12 additions & 11 deletions src/material/core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,6 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
}
}

/**
* Gets the `aria-selected` value for the option. We explicitly omit the `aria-selected`
* attribute from single-selection, unselected options. Including the `aria-selected="false"`
* attributes adds a significant amount of noise to screen-reader users without providing useful
* information.
*/
_getAriaSelected(): boolean | null {
return this.selected || (this.multiple ? false : null);
}

/** Returns the correct tabindex for the option depending on disabled state. */
_getTabIndex(): string {
return this.disabled ? '-1' : '0';
Expand Down Expand Up @@ -255,7 +245,18 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
'[class.mat-mdc-option-active]': 'active',
'[class.mdc-list-item--disabled]': 'disabled',
'[id]': 'id',
'[attr.aria-selected]': '_getAriaSelected()',
// The aria-selected attribute applied to the option conforms to WAI ARIA best practices for
// listbox interaction pattern.
//
// From [WAI ARIA Listbox authoring practices guide](
// https://www.w3.org/WAI/ARIA/apg/patterns/listbox/):
// "If any options are selected, each selected option has either aria-selected or aria-checked
// set to true. All options that are selectable but not selected have either aria-selected or
// aria-checked set to false."
//
// Set `aria-selected="false"` on not-selected listbox options that are selectable to fix
// VoiceOver reading every option as "selected" (#21491).
'[attr.aria-selected]': 'selected',
'[attr.aria-disabled]': 'disabled.toString()',
'(click)': '_selectViaInteraction()',
'(keydown)': '_handleKeydown($event)',
Expand Down
15 changes: 14 additions & 1 deletion src/material/legacy-core/option/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ import {MatLegacyOptgroup} from './optgroup';
* Single option inside of a `<mat-select>` element.
* @deprecated Use `MatOption` from `@angular/material/core` instead. See https://material.angular.io/guide/mdc-migration for information about migrating.
* @breaking-change 17.0.0
*
* The aria-selected attribute applied to the option conforms to WAI ARIA best practices for listbox
* interaction patterns.
*
* From [WAI ARIA Listbox authoring practices guide](
* https://www.w3.org/WAI/ARIA/apg/patterns/listbox/):
* "If any options are selected, each selected option has either aria-selected or aria-checked
* set to true. All options that are selectable but not selected have either aria-selected or
* aria-checked set to false."
*
* Set `aria-selected="false"` on not-selected listbox options that are selectable to fix
* VoiceOver reading every option as "selected" (#25736). Also fixes chromevox not announcing
* options as selectable.
*/
@Component({
selector: 'mat-option',
Expand All @@ -38,7 +51,7 @@ import {MatLegacyOptgroup} from './optgroup';
'[class.mat-option-multiple]': 'multiple',
'[class.mat-active]': 'active',
'[id]': 'id',
'[attr.aria-selected]': '_getAriaSelected()',
'[attr.aria-selected]': 'selected',
'[attr.aria-disabled]': 'disabled.toString()',
'[class.mat-option-disabled]': 'disabled',
'(click)': '_selectViaInteraction()',
Expand Down
8 changes: 4 additions & 4 deletions src/material/legacy-select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,9 +1135,9 @@ describe('MatSelect', () => {
}));

it('should set aria-selected on each option for single select', fakeAsync(() => {
expect(options.every(option => !option.hasAttribute('aria-selected')))
expect(options.every(option => option.getAttribute('aria-selected') === 'false'))
.withContext(
'Expected all unselected single-select options not to have ' + 'aria-selected set.',
'Expected all unselected single-select options to have aria-selected="false" set.',
)
.toBe(true);

Expand All @@ -1152,9 +1152,9 @@ describe('MatSelect', () => {
.withContext('Expected selected single-select option to have aria-selected="true".')
.toEqual('true');
options.splice(1, 1);
expect(options.every(option => !option.hasAttribute('aria-selected')))
expect(options.every(option => option.getAttribute('aria-selected') === 'false'))
.withContext(
'Expected all unselected single-select options not to have ' + 'aria-selected set.',
'Expected all unselected single-select options to have aria-selected="false" set.',
)
.toBe(true);
}));
Expand Down
8 changes: 4 additions & 4 deletions src/material/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,9 +1168,9 @@ describe('MDC-based MatSelect', () => {
}));

it('should set aria-selected on each option for single select', fakeAsync(() => {
expect(options.every(option => !option.hasAttribute('aria-selected')))
expect(options.every(option => option.getAttribute('aria-selected') === 'false'))
.withContext(
'Expected all unselected single-select options not to have ' + 'aria-selected set.',
'Expected all unselected single-select options to have aria-selected="false" set.',
)
.toBe(true);

Expand All @@ -1187,9 +1187,9 @@ describe('MDC-based MatSelect', () => {
)
.toEqual('true');
options.splice(1, 1);
expect(options.every(option => !option.hasAttribute('aria-selected')))
expect(options.every(option => option.getAttribute('aria-selected') === 'false'))
.withContext(
'Expected all unselected single-select options not to have ' + 'aria-selected set.',
'Expected all unselected single-select options to have aria-selected="false" set.',
)
.toBe(true);
}));
Expand Down
1 change: 0 additions & 1 deletion tools/public_api_guard/material/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecke
set disabled(value: BooleanInput);
get disableRipple(): boolean;
focus(_origin?: FocusOrigin, options?: FocusOptions): void;
_getAriaSelected(): boolean | null;
_getHostElement(): HTMLElement;
getLabel(): string;
_getTabIndex(): string;
Expand Down