Skip to content

fix(material/input): Do not set aria-invalid on empty matInputs #22779

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

Conversation

ByzantineFailure
Copy link
Contributor

Updates the logic for setting aria-invalid on matInput to not set
the value at all if the input has no value.

Prior to this PR matInput sets aria-invalid="false" for any empty
matInput, including required ones. This suppresses screen readers'
announcement to users that such inputs are in an invalid state.

Fixes #22777

@google-cla google-cla bot added the cla: yes PR author has agreed to Google's Contributor License Agreement label May 24, 2021
Copy link
Contributor

@mmalerba mmalerba left a comment

Choose a reason for hiding this comment

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

LGTM

@mmalerba mmalerba added G This is is related to a Google internal issue action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release and removed action: merge The PR is ready for merge by the caretaker labels May 24, 2021
@mmalerba
Copy link
Contributor

It looks like there's a lint failure because we need to add the same logic/test for the MDC-based input

@@ -83,7 +83,7 @@ const _MatInputBase = mixinErrorState(class {
'[attr.readonly]': 'readonly && !_isNativeSelect || null',
// Only mark the input as invalid for assistive technology if it has a value since the
// state usually overlaps with `aria-required` when the input is empty and can be redundant.
'[attr.aria-invalid]': 'errorState && !empty',
'[attr.aria-invalid]': 'empty ? null : errorState',
Copy link
Collaborator

Choose a reason for hiding this comment

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

While we're here, is it slightly more correct to change this to '(empty && required) ? null : errorState'? I'm thinking of an edge case where an input is empty, but not required, but still in an error state (maybe because there's validation that looks for duplicates and there's already an empty input). If so, we'd want to update the comment above as well.

Copy link
Member

Choose a reason for hiding this comment

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

This behavior is intentional, see #21609. There's also a linked issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep! The reason #21609 was requested was because without it aria-invalid was being applied to aria-required inputs with no value. This violates the ARIA spec (see here : https://www.w3.org/TR/wai-aria/#aria-invalid ) and caused #18140 to be filed.

Unfortunately, setting aria-invalid="false" on these inputs suppresses any native indication to a screen reader user that the input is in an invalid state. The only information they receive is the error message itself. If worded correctly the error message can be enough (e.g. if it includes "Invalid" in the text) but this requires component users to be aware of this fact and actively account for it.

Without a well-worded error message, this results in a different experience for SR users than visual users -- the input is flagged as invalid and displayed prominently to visual users as invalid, but screen reader users get no native notification at all when they focus the input.

I think @zelliott might have a good point here that we may wish to do (empty && required) rather than simply empty for our null state.

@@ -83,7 +83,7 @@ const _MatInputBase = mixinErrorState(class {
'[attr.readonly]': 'readonly && !_isNativeSelect || null',
// Only mark the input as invalid for assistive technology if it has a value since the
// state usually overlaps with `aria-required` when the input is empty and can be redundant.
'[attr.aria-invalid]': 'errorState && !empty',
'[attr.aria-invalid]': 'empty ? null : errorState',
Copy link
Member

Choose a reason for hiding this comment

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

This behavior is intentional, see #21609. There's also a linked issue.

Updates the logic for setting `aria-invalid` on `matInput` to not set
the value at all if the input has no value.

Prior to this PR `matInput` sets `aria-invalid="false"` for any empty
`matInput`, including `required` ones.  This suppresses screen readers'
announcement to users that such inputs are in an invalid state.

Fixes angular#22777

Add MDC implementation.  Whoops.
@ByzantineFailure ByzantineFailure force-pushed the byzantinefailure/matInput-aria-required branch from f72ebd5 to 6218534 Compare May 24, 2021 20:27
Copy link
Contributor

@mmalerba mmalerba left a comment

Choose a reason for hiding this comment

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

I thought I remembered seeing some recent changes to this part of the code. It sounds like we may not want to make this change, but I'll leave it open for discussion for now

Remove some superfluous diffs in input spec files

Additional diff cleanup

Resolve linter errors
@ByzantineFailure ByzantineFailure force-pushed the byzantinefailure/matInput-aria-required branch from db3d8db to 0cdf884 Compare May 26, 2021 00:27
@ByzantineFailure ByzantineFailure deleted the byzantinefailure/matInput-aria-required branch May 26, 2021 00:32
@ByzantineFailure
Copy link
Contributor Author

All right, it has been a minute since I've properly used git and I accidentally closed this branch when rebasing it to squash it down to one commit. I'll... be right with you.

@ByzantineFailure ByzantineFailure restored the byzantinefailure/matInput-aria-required branch May 26, 2021 00:42
@ByzantineFailure
Copy link
Contributor Author

I'mma just close and re-open this with the correctly-named branch. Apologies, folks.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement G This is is related to a Google internal issue target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug(matInput): Screen Readers do not announce that an empty matInput[required] is invalid
4 participants