Skip to content

Commit 6bfdc03

Browse files
authored
Merge pull request #333 from 4site-interactive-studios/error-aria
Add role attribute if en__errorList is not empty
2 parents c005c8d + fc4324f commit 6bfdc03

5 files changed

Lines changed: 59 additions & 2 deletions

File tree

packages/scripts/dist/a11y.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export declare class A11y {
55
private addLabel;
66
private updateFrequencyLabel;
77
private setAutoGeneratedAltTags;
8+
private manageErrorListAlertRole;
89
}

packages/scripts/dist/a11y.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export class A11y {
88
this.updateFrequencyLabel();
99
const ecardImages = document.querySelectorAll('.en__ecarditems__list img');
1010
this.setAutoGeneratedAltTags(ecardImages);
11+
this.manageErrorListAlertRole();
1112
}
1213
addGroupRole() {
1314
// Add role="group" to all EN Radio fields
@@ -93,4 +94,29 @@ export class A11y {
9394
}
9495
});
9596
}
97+
manageErrorListAlertRole() {
98+
const errorList = document.querySelector('ul.en__errorList');
99+
if (!errorList)
100+
return;
101+
const hasErrorItems = () => Boolean(errorList.querySelector('li'));
102+
const enableAlert = () => {
103+
if (!errorList.hasAttribute('role')) {
104+
errorList.setAttribute('role', 'alert');
105+
}
106+
};
107+
const disableAlert = () => {
108+
if (errorList.hasAttribute('role')) {
109+
errorList.removeAttribute('role');
110+
}
111+
};
112+
hasErrorItems() ? enableAlert() : disableAlert();
113+
new MutationObserver(records => {
114+
for (const record of records) {
115+
if (record.type === 'childList') {
116+
hasErrorItems() ? enableAlert() : disableAlert();
117+
break;
118+
}
119+
}
120+
}).observe(errorList, { childList: true });
121+
}
96122
}

packages/scripts/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/scripts/src/a11y.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class A11y {
99
this.updateFrequencyLabel();
1010
const ecardImages: NodeListOf<HTMLImageElement> = document.querySelectorAll('.en__ecarditems__list img');
1111
this.setAutoGeneratedAltTags(ecardImages);
12+
this.manageErrorListAlertRole();
1213
}
1314

1415
private addGroupRole() {
@@ -116,4 +117,33 @@ export class A11y {
116117
}
117118
});
118119
}
120+
private manageErrorListAlertRole(): void {
121+
const errorList = document.querySelector<HTMLUListElement>('ul.en__errorList');
122+
if (!errorList) return;
123+
124+
const hasErrorItems = (): boolean => Boolean(errorList.querySelector('li'));
125+
126+
const enableAlert = (): void => {
127+
if (!errorList.hasAttribute('role')) {
128+
errorList.setAttribute('role', 'alert');
129+
}
130+
};
131+
132+
const disableAlert = (): void => {
133+
if (errorList.hasAttribute('role')) {
134+
errorList.removeAttribute('role');
135+
}
136+
};
137+
138+
hasErrorItems() ? enableAlert() : disableAlert();
139+
140+
new MutationObserver(records => {
141+
for (const record of records) {
142+
if (record.type === 'childList') {
143+
hasErrorItems() ? enableAlert() : disableAlert();
144+
break;
145+
}
146+
}
147+
}).observe(errorList, { childList: true });
148+
}
119149
}

packages/styles/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)