Skip to content

Commit

Permalink
fix: ensure error message is announced by VoiceOver (#8384)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored and vaadin-bot committed Dec 23, 2024
1 parent cbff111 commit 3371aa0
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ snapshots["vaadin-checkbox-group host error"] =
</label>
<div
id="error-message-vaadin-checkbox-group-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ snapshots["vaadin-checkbox host error"] =
</label>
<div
id="error-message-vaadin-checkbox-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ snapshots["vaadin-combo-box host error"] =
</label>
<div
id="error-message-vaadin-combo-box-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ snapshots["vaadin-custom-field host error"] =
</label>
<div
id="error-message-vaadin-custom-field-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ snapshots["vaadin-date-picker host error"] =
</label>
<div
id="error-message-vaadin-date-picker-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ snapshots["vaadin-date-time-picker host error"] =
</label>
<div
id="error-message-vaadin-date-time-picker-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@ snapshots["vaadin-email-field host error"] =
</label>
<div
id="error-message-vaadin-email-field-2"
role="alert"
slot="error-message"
>
Error
Expand Down
9 changes: 4 additions & 5 deletions packages/field-base/src/error-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright (c) 2021 - 2024 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { announce } from '@vaadin/a11y-base/src/announce.js';
import { SlotChildObserveController } from '@vaadin/component-base/src/slot-child-observe-controller.js';

/**
Expand Down Expand Up @@ -108,12 +109,10 @@ export class ErrorController extends SlotChildObserveController {
errorNode.textContent = hasError ? errorMessage : '';
errorNode.hidden = !hasError;

// Role alert will make the error message announce immediately
// as the field becomes invalid
if (hasError) {
errorNode.setAttribute('role', 'alert');
} else {
errorNode.removeAttribute('role');
// Assertive mode ensures VoiceOver reads
// the error message on commit with Enter.
announce(errorMessage, { mode: 'assertive' });
}
}

Expand Down
39 changes: 28 additions & 11 deletions packages/field-base/test/field-mixin.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@vaadin/chai-plugins';
import { aTimeout, defineLit, definePolymer, fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
import sinon from 'sinon';
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { FieldMixin } from '../src/field-mixin.js';
Expand Down Expand Up @@ -142,30 +143,46 @@ const runTests = (defineHelper, baseMixin) => {
await nextUpdate(element);
expect(element.hasAttribute('has-error-message')).to.be.false;
});
});

describe('announcements', () => {
let announceRegion, clock;

beforeEach(async () => {
element = fixtureSync(`<${tag}></${tag}>`);
await nextRender();
clock = sinon.useFakeTimers({ toFake: ['setTimeout'] });
announceRegion = document.querySelector('[aria-live]');
announceRegion.textContent = '';
});

it('should not set alert role with no error', () => {
expect(error.hasAttribute('role')).to.be.false;
afterEach(() => {
clock.restore();
});

it('should set alert role when attribute is set', async () => {
it('should announce error message set via attribute when field is invalid', async () => {
element.invalid = true;
element.setAttribute('error-message', 'This field is required');
await nextUpdate(element);
expect(error.getAttribute('role')).to.equal('alert');
clock.tick(150);
expect(announceRegion.textContent).to.equal('This field is required');
expect(announceRegion.getAttribute('aria-live')).to.equal('assertive');
});

it('should set alert role when property is set', async () => {
it('should announce error message set via property when field is invalid', async () => {
element.invalid = true;
element.errorMessage = 'This field is required';
await nextUpdate(element);
expect(error.getAttribute('role')).to.equal('alert');
clock.tick(150);
expect(announceRegion.textContent).to.equal('This field is required');
expect(announceRegion.getAttribute('aria-live')).to.equal('assertive');
});

it('should remove alert role when field is valid', async () => {
it('should not announce error message when field is valid', async () => {
element.errorMessage = 'This field is required';
await nextUpdate(element);

element.invalid = false;
await nextUpdate(element);
expect(error.hasAttribute('role')).to.be.false;
clock.tick(150);
expect(announceRegion.textContent).to.equal('');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ snapshots["vaadin-integer-field host error"] =
</label>
<div
id="error-message-vaadin-integer-field-2"
role="alert"
slot="error-message"
>
Error
Expand Down
4 changes: 0 additions & 4 deletions packages/login/test/dom/__snapshots__/login-form.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ snapshots["vaadin-login-form host required"] =
</label>
<div
id="error-message-vaadin-text-field-2"
role="alert"
slot="error-message"
>
Username is required
Expand Down Expand Up @@ -196,7 +195,6 @@ snapshots["vaadin-login-form host required"] =
</label>
<div
id="error-message-vaadin-password-field-5"
role="alert"
slot="error-message"
>
Password is required
Expand Down Expand Up @@ -390,7 +388,6 @@ snapshots["vaadin-login-form host i18n-required"] =
</label>
<div
id="error-message-vaadin-text-field-2"
role="alert"
slot="error-message"
>
Käyttäjätunnus vaaditaan
Expand Down Expand Up @@ -427,7 +424,6 @@ snapshots["vaadin-login-form host i18n-required"] =
</label>
<div
id="error-message-vaadin-password-field-5"
role="alert"
slot="error-message"
>
Salasana vaaditaan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ snapshots["vaadin-multi-select-combo-box host error"] =
</label>
<div
id="error-message-vaadin-multi-select-combo-box-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ snapshots["vaadin-number-field host error"] =
</label>
<div
id="error-message-vaadin-number-field-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ snapshots["vaadin-password-field host error"] =
</label>
<div
id="error-message-vaadin-password-field-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ snapshots["vaadin-radio-group host error"] =
</label>
<div
id="error-message-vaadin-radio-group-2"
role="alert"
slot="error-message"
>
Error
Expand Down
1 change: 0 additions & 1 deletion packages/select/test/dom/__snapshots__/select.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ snapshots["vaadin-select host error"] =
</label>
<div
id="error-message-vaadin-select-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ snapshots["vaadin-text-area host error"] =
</label>
<div
id="error-message-vaadin-text-area-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ snapshots["vaadin-text-field host error"] =
</label>
<div
id="error-message-vaadin-text-field-2"
role="alert"
slot="error-message"
>
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ snapshots["vaadin-time-picker host error"] =
</label>
<div
id="error-message-vaadin-time-picker-2"
role="alert"
slot="error-message"
>
Error
Expand Down

0 comments on commit 3371aa0

Please sign in to comment.