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

fix(a11y): error message announcement in form elements (VIV-1593) #1662

Merged
merged 8 commits into from
Apr 10, 2024
Merged
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
4 changes: 2 additions & 2 deletions libs/components/src/lib/date-range-picker/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ test.describe('constraints validation', async () => {

await expect(
page.locator('vwc-date-range-picker .error-message')
).toBeVisible();
).not.toHaveClass(/sr-only/);
});

test('should hide a validation error after resetting the form', async ({
Expand All @@ -206,6 +206,6 @@ test.describe('constraints validation', async () => {

await expect(
page.locator('vwc-date-range-picker .error-message')
).not.toBeVisible();
).toHaveClass(/sr-only/);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe('getFeedbackTemplate', () => {

const getMessage = (type: string) => {
const messageEl = element.shadowRoot!.querySelector(
`.${type}-message.message--visible`
`.${type}-message.message--visible:not(.sr-only)`
);
if (!messageEl) {
return null;
Expand Down
16 changes: 12 additions & 4 deletions libs/components/src/shared/patterns/form-elements/form-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ type FeedbackConfig = {
name: string;
slottedContentProperty: '_helperTextSlottedContent';
};
role: 'status' | 'none';
};
const feedback: Record<string, FeedbackConfig> = {
helper: {
Expand All @@ -185,16 +186,19 @@ const feedback: Record<string, FeedbackConfig> = {
name: 'helper-text',
slottedContentProperty: '_helperTextSlottedContent',
},
role: 'none',
},
error: {
messageProperty: 'errorValidationMessage',
className: 'error',
iconType: 'info-line',
role: 'status',
},
success: {
messageProperty: 'successText',
className: 'success',
iconType: 'check-circle-line',
role: 'none',
},
};

Expand Down Expand Up @@ -249,10 +253,14 @@ function getFeedbackTypeTemplate(

return html<SomeFormElement>`<div
class="${(x) =>
classNames('message', `${config.className}-message`, [
'message--visible',
shouldShow(x),
])}"
classNames(
'message',
`${config.className}-message`,
['message--visible', config.role === 'status' || shouldShow(x)],
['sr-only', !shouldShow(x)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed? Because when the error is not visually displayed it also shouldn't be accessible to SR, right?

Suggested change
['sr-only', !shouldShow(x)]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When it is hidden with display: none the message is not announced.

)}"
role="${config.role}"
aria-atomic="false"
>
${when(
(x) => shouldShow(x) && config.iconType,
Expand Down
10 changes: 10 additions & 0 deletions libs/components/src/shared/patterns/form-elements/message.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

$low-ink-color: --_low-ink-color;

.sr-only {
position: absolute;
overflow: hidden;
width: 1px;
height: 1px;
clip: rect(0 0 0 0);
clip-path: inset(50%);
white-space: nowrap;
}

.message {
display: none;
contain: inline-size;
Expand Down
Loading