Skip to content

Commit

Permalink
fix(a11y): error message announcement in form elements (VIV-1593) (#1662
Browse files Browse the repository at this point in the history
)

* fix: error message announcement in form elements

* chore: fixes stylelint

* chore: address stylelint

* chore: fixes broken ui test

* chore: addresses pr comments

* chore: fixes broken unit test

---------

Co-authored-by: James Taylor <jstaylor@vonht2fg32k90.home>
  • Loading branch information
TaylorJ76 and James Taylor authored Apr 10, 2024
1 parent 80775df commit 3bc7707
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
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)]
)}"
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

0 comments on commit 3bc7707

Please sign in to comment.