
WCAG
WCAG first interacties
WCAG first interacties
Bij het toevoegen van interactie ontkom je vrijwel niet aan het gebruik van JavaScript. JavaScript stelt ons in staat om gebruikersinteracties, zoals een klik op een knop of een swipe-beweging op te vangen en er “iets” mee te doen.
Een van de meest voorkomende interacties is het klikken op een knop, wat vaak leidt tot acties op andere elementen, bijvoorbeeld het openen of sluiten van een popup. Het is belangrijk om de staat van deze actie bij te houden: Wordt het element geopend of juist gesloten? Dit moeten we weten om de correct werking te kunnen waarborgen.
In deze scenario’s kunnen we perfect gebruik maken van ARIA-attributen. Deze attributen bieden niet alleen informatie voor screenreader-gebruikers, maar stellen ons ook in staat om deze acties met JavaScript te beheren. De belangrijkste ARIA-attributen die hierbij worden gebruikt zijn aria-controls en aria-expanded.
- Op welke elementen moet de gebruikersactie worden toegepast?
De waarden inaria-controlsverwijzen naar de elementen waarop de actie invloed heeft.
Bijvoobreeld: Welke popup moet openen bij het klikken op een button? - Wat is de huidige staat van de actie?
Metaria-expandedkunnen we de huidige staat van het element bijhouden, bijvoorbeeld of een menu geopend (true) of gesloten (false) is.
Gebruik van ARIA-attributen
aria-controls
Het aria-controls-attribuut wordt gebruikt bij interactieve elementen die andere elementen op de pagina beïnvloeden. De waarde van dit attribuut bestaat uit een of meerdere ID’s die verwijzen naar de te controleren elementen. Als er meerdere ID’s zijn, worden deze gescheiden door spaties.
In JavaScript kunnen we het volgende stellen:
Elk element met het attribuut aria-controls verwacht een interactie. We kunnen deze elementen ophalen en het click-event afvangen. De waarden van aria-controls zijn de ID’s van de elementen die door de gebruikersactie worden beïnvloed. Deze elementen kunnen we vervolgens met JavaScript manipuleren.
aria-expanded voor true/false-staten
Het aria-expanded-attribuut geeft de huidige state van het te controleren element (dat in aria-controls wordt aangegeven) weer. Dit attribuut wordt alleen in combinatie met aria-controls gebruikt. Met JavaScript kunnen we de actuele staat van het te controleren element verkrijgen en deze manipuleren. Alle elementen die in aria-controls zijn gedefinieerd, krijgen een active-class bij een state van true, en de class wordt verwijderd bij een staat van false.
Interacties vinden op verschillende manieren plaats
Wij willen zowel met de muisgebruiker, dan ook met de toetsenbord gebruiker rekening houden. Daarom hebben wij niet alleen click en hover interacties, maar ook keydown en focusin interacties.
Vooral het focusin event is interessant:
Op elk element waar focus op gelegd wordt, moeten we op reageren. Stel je voor, je focus ligt op een submenu element. nadat je op het laatste submenu item bent, en je tabt verder naar het volgende item, komt de focus automatisch op het volgende hoofdmenu item en het submenu item zou ingeklapt moeten worden.
Custom event STATE_CHANGED
Dit event gebruiken wij voor meerdere doeleinden. Als data geven we de state (true/false) mee en elementen, die hiervan beïnvloed worden. Daarnaast kunnen we met eventbubbling sturen hoeveel invloed het event op andere, bovenliggende elementen moet hebben.
JavaScript voor ARIA-attributen en alle nodige interacties
De volgende JavaScript-classes behandelen het beheer van deze ARIA-attributen:
AwwwAriaControlsHolder:
Deze class gebruiken we om globaal alle elementen, die aria-controls hebben of door aria-controls beïnvloed worden, te handelen. Ook initiëren wij hier alle events die nodig zijn.
class AwwwAriaControlsHolder {
constructor() {
this._controlledItems = {};
this.activeItems = [];
this.initAriaControls();
this.enableEvents();
}
enableEvents() {
document.addEventListener("click", (e) => {
if (!e.hasOwnProperty("ariaClicked") && this.activeItems.length > 0) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target)) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
this.activeItems = [];
}
});
document.addEventListener("STATE_CHANGED", (e) => {
if (e.detail.hasOwnProperty("affectedItems") && e.detail.affectedItems.length > 0) {
this.activeItems = e.detail.affectedItems;
} else {
this.activeItems = [];
}
});
document.addEventListener('keyup', (e) => {
if (this.activeItems.length > 0) {
if (e.keyCode == 27) {
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').focus();
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').click();
}
if (e.keyCode == 9) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target) && e.target != item) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
}
}
});
document.addEventListener("AWWW_REGISTER_CONTROLS", (e) => {
this.initAriaControls(e.detail.section);
});
}
// Initialize aria controls within a given section (or document by default)
initAriaControls(section = document) {
section.querySelectorAll("[aria-controls]").forEach((button) => {
//only if the button is visible we wil add functionality to the button and the controls
if (button.checkVisibility()) {
const ids = button.getAttribute("aria-controls").split(' ');
const state = (button.getAttribute("aria-expanded") == "true") ? true : false;
const objects = this.getControlledItems(ids, state);
// Determine the type of button based on the attributes and create the appropriate control button
this.createControlButton(button, ids, objects);
}
});
}
// Retrieve controlled items based on the ids
getControlledItems(ids, state) {
return ids.map(id => {
if (this._controlledItems[id]) {
return this._controlledItems[id];
}
const element = document.getElementById(id);
if (element) {
const newItem = new AwwwControlledItem(id, element, state);
this._controlledItems[id] = element;
return element;
}
return null;
}).filter(Boolean);
}
// Create the appropriate aria control button based on the button's attributes
createControlButton(button, ids, objects) {
//...
new AwwwAriaControlButtonNormal(button, ids, objects);
//...
}
}
const awwwAriaControlsHolder = new AwwwAriaControlsHolder();
AwwwAriaControlButtonBase
Op dit moment handelen wij alleen een normale button click routine. Later gaan wij nog andere routines toevoegen. Alle classes maken uiteindelijk gebruik van onze AwwwAriaControlButtonBase class
class AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems) {
this._button = button;
this._ids = controlledIds;
this._controlledItems = controlledItems;
this._changed = false;
this._controlledItems.forEach(item => this.activateChange(item));
}
activateChange(item) {
item.addEventListener("STATE_CHANGED", (e) => {
let active = (e.target == item) ? e.detail.state : "true";
this._button.setAttribute("aria-expanded", active);
this._changed = true;
});
}
}
AwwwAriaControlButtonNormal
Deze class handelt onze true/false routine
class AwwwAriaControlButtonNormal extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems) {
super(button, controlledIds, controlledItems);
this.activateNormalHandling();
}
activateNormalHandling() {
this._button.addEventListener("click", (e) => {
e.preventDefault();
e.ariaClicked = true;
const isExpanded = this._button.getAttribute("aria-expanded") === "true";
this.changeState(!isExpanded);
});
document.addEventListener("STATE_CHANGED", () => {
if (!this._changed) {
this._button.setAttribute("aria-expanded", "false");
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false}}));
});
}
this._changed = false;
});
}
changeState(state) {
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: true, detail: {state: state, affectedItems: []}}));
});
}
}
AwwwControlledItem
Deze class gebruiken wij om alle te controleren elementen te verzamelen en aan te kunnen sturen.
class AwwwControlledItem {
constructor(id, item, state) {
this._id = id;
this._item = item;
this.childItems = this._item.querySelectorAll("a, input, textfield, select, checkbox, radio, button");
this._firstFocusableChild = this._item.querySelector("a, input, textfield, select, checkbox, radio, button");
this._isHidden = this._item.hasAttribute("hidden");
if (this._item.getAttribute("role") === "dialog") {
this.setupDialogFocusManagement();
}
this._item.addEventListener("STATE_CHANGED", (e) => {
this.handleStateChanged(e);
});
if(!state){
this.childItems.forEach((button) => {
button.setAttribute('tabindex', '-1');
});
}
this._item.addEventListener("click", (e) => {
e.ariaClicked = true;
});
}
handleStateChanged(e) {
const isSameItem = e.target === this._item;
const state = isSameItem ? e.detail.state === true : true;
this._item.classList.toggle('active', state);
if (isSameItem && state == true) {
this.childItems.forEach((button) => {
button.setAttribute('tabindex', '0');
});
if (this._firstFocusableChild) {
this._firstFocusableChild.focus();
}
} else {
this.childItems.forEach((button) => {
button.setAttribute('tabindex', '-1');
});
}
if (state == true && e.detail.hasOwnProperty("affectedItems")) {
e.detail.affectedItems.push(this._item);
}
}
setupDialogFocusManagement() {
const focusableElements = Array.from(this._item.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'));
this._firstElement = focusableElements[0];
this._lastElement = focusableElements[focusableElements.length - 1];
this._item.addEventListener("keydown", (e) => {
if (e.key === "Tab") {
if (e.shiftKey && document.activeElement === this._firstElement) {
e.preventDefault();
this._lastElement.focus();
} else if (!e.shiftKey && document.activeElement === this._lastElement) {
e.preventDefault();
this._firstElement.focus();
}
}
});
}
}
Andere interactieopties naast true/false
Hoewel we met ARIA-attributen en JavaScript de staat van een element kunnen manipuleren, zijn er andere situaties waarin extra informatie nodig is, zoals de positie (bijvoorbeeld 1 van 10, 2 van 10) of de richting (+1, -1). Dit kan handig zijn bij het maken van bijvoorbeeld vorige/volgende knoppen of bullet points in een slideshow.
Om dit te ondersteunen, gebruiken we aangepaste data-attributen en kunnen we alle aria-controls-elementen afvangen, en afhankelijk van welk passend attribuut (zoals aria-expanded, data-pos, data-dir) binnen het element aanwezig is, de juiste actie uitvoeren:
aria-expanded="[true/false]": in- of uitklappen (toggle)data-dir="[+1/-1]": de volgende of vorige items activeren (vooruit/terug knoppen)data-pos="[1/2/3/4…]": een specifiek item activeren (bullets bij een slideshow)
JavaScript-aanpassingen voor data-dir en data-pos attributen in de AwwwAriaControls class
class AwwwAriaControlsHolder {
constructor() {
this._controlledItems = {};
this.activeItems = [];
this.initAriaControls();
this.enableEvents();
}
enableEvents() {
document.addEventListener("click", (e) => {
if (!e.hasOwnProperty("ariaClicked") && this.activeItems.length > 0) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target)) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
this.activeItems = [];
}
});
document.addEventListener("STATE_CHANGED", (e) => {
if (e.detail.hasOwnProperty("affectedItems") && e.detail.affectedItems.length > 0) {
this.activeItems = e.detail.affectedItems;
} else {
this.activeItems = [];
}
});
document.addEventListener('keyup', (e) => {
if (this.activeItems.length > 0) {
if (e.keyCode == 27) {
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').focus();
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').click();
}
if (e.keyCode == 9) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target) && e.target != item) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
}
}
});
document.addEventListener("AWWW_REGISTER_CONTROLS", (e) => {
this.initAriaControls(e.detail.section);
});
}
// Initialize aria controls within a given section (or document by default)
initAriaControls(section = document) {
section.querySelectorAll("[aria-controls]").forEach((button) => {
//only if the button is visible we wil add functionality to the button and the controls
if (button.checkVisibility()) {
const ids = button.getAttribute("aria-controls").split(' ');
const state = (button.getAttribute("aria-expanded") == "true") ? true : false;
const objects = this.getControlledItems(ids, state);
// Determine the type of button based on the attributes and create the appropriate control button
this.createControlButton(button, ids, objects);
}
});
}
// Retrieve controlled items based on the ids
getControlledItems(ids) {
return ids.map(id => {
if (this._controlledItems[id]) {
return this._controlledItems[id];
}
const element = document.getElementById(id);
if (element) {
const newItem = new AwwwControlledItem(id, element);
this._controlledItems[id] = element;
return element;
}
return null;
}).filter(Boolean);
}
// Create the appropriate aria control button based on the button's attributes
createControlButton(button, ids, objects) {
if (button.hasAttribute("data-dir")) {
const dir = button.getAttribute("data-dir");
new AwwwAriaControlButtonDirectional(button, ids, objects, dir);
} else if (button.hasAttribute("data-pos")) {
const pos = button.getAttribute("data-pos");
new AwwwAriaControlButtonPositional(button, ids, objects, pos);
} else {
new AwwwAriaControlButtonNormal(button, ids, objects);
}
}
}
const awwwAriaControlsHolder = new AwwwAriaControlsHolder();
AwwwAriaControlButtonDirectional
Deze class handelt alle links/rechts, boven/beneden (+1/-1) interacties
class AwwwAriaControlButtonDirectional extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems, dir) {
super(button, controlledIds, controlledItems);
this._dir = dir;
this.activateDirectionalHandling();
}
activateDirectionalHandling() {
this._controlledItems.forEach(item => {
item.addEventListener("AWWW_CHANGED_ITEM", (e) => {
this._button.disabled = (this._dir === "-1" && e.detail.start) || (this._dir === "+1" && e.detail.end);
});
});
this._button.addEventListener("click", (e) => {
e.preventDefault();
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("AWWW_CHANGE_DIR", {detail: {dir: this._dir}}));
});
});
}
}
AwwwAriaControlButtonPositional
Deze class handelt alle positie gerelateerde interacties
class AwwwAriaControlButtonPositional extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems, pos) {
super(button, controlledIds, controlledItems);
this._pos = pos;
this.activatePositionalHandling();
}
activatePositionalHandling() {
const posAttribute = this._button.getAttribute('data-pos');
this._controlledItems.forEach(item => {
item.addEventListener("AWWW_CHANGED_ITEM", (e) => {
const isSelected = e.detail.pos == posAttribute;
this._button.setAttribute("aria-selected", isSelected);
});
});
this._button.addEventListener("click", (e) => {
e.preventDefault();
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("AWWW_CHANGE_POS", {detail: {pos: this._pos}}));
});
});
}
}
Afhankelijkheden van interacties
1. Globale afhankelijkheden
Wanneer een element wordt uitgeklapt (aria-expanded="true"), kan het weer worden ingeklapt door op een ander interactie-element te klikken of door ergens anders op de pagina te klikken. Een voorbeeld hiervan is een submenu dat sluit wanneer je een ander submenu opent of buiten het submenu klikt. Dit element kan alleen worden geopend door knoppen met de juiste aria-controls-waarden, maar kan worden gesloten door op andere elementen te klikken.
2. Regionale afhankelijkheden
Soms wil je de afhankelijkheid binnen een bepaalde regio beperken, zonder invloed van “buitenaf”. Een voorbeeld hiervan is een accordeonmenu: het openen en sluiten van content is alleen afhankelijk van de accordeonknoppen en reageert niet op klikken buiten de accordeon. Het actieve accordeonitem moet sluiten wanneer je een ander item opent. Hiervoor gebruiken we het data-attribuut data-parent="[id]", waarmee het element zelf niet van buitenaf wordt beïnvloed, maar afhankelijkheden binnen een parent-element creëert.
JavaScript-aanpassingen voor data-dir en data-pos attributen in de AwwwAriaControls class
class AwwwAriaControlsHolder {
constructor() {
this._controlledItems = {};
this.activeItems = [];
this.initAriaControls();
this.enableEvents();
}
enableEvents() {
document.addEventListener("click", (e) => {
if (!e.hasOwnProperty("ariaClicked") && this.activeItems.length > 0) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target)) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
this.activeItems = [];
}
});
document.addEventListener("STATE_CHANGED", (e) => {
if (e.detail.hasOwnProperty("affectedItems") && e.detail.affectedItems.length > 0) {
this.activeItems = e.detail.affectedItems;
} else {
this.activeItems = [];
}
});
document.addEventListener('keyup', (e) => {
if (this.activeItems.length > 0) {
if (e.keyCode == 27) {
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').focus();
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').click();
}
if (e.keyCode == 9) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target) && e.target != item) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
}
}
});
document.addEventListener("AWWW_REGISTER_CONTROLS", (e) => {
this.initAriaControls(e.detail.section);
});
}
// Initialize aria controls within a given section (or document by default)
initAriaControls(section = document) {
section.querySelectorAll("[aria-controls]").forEach((button) => {
//only if the button is visible we wil add functionality to the button and the controls
if (button.checkVisibility()) {
const ids = button.getAttribute("aria-controls").split(' ');
const state = (button.getAttribute("aria-expanded") == "true") ? true : false;
const objects = this.getControlledItems(ids, state);
// Determine the type of button based on the attributes and create the appropriate control button
this.createControlButton(button, ids, objects);
}
});
}
// Retrieve controlled items based on the ids
getControlledItems(ids) {
return ids.map(id => {
if (this._controlledItems[id]) {
return this._controlledItems[id];
}
const element = document.getElementById(id);
if (element) {
const newItem = new AwwwControlledItem(id, element);
this._controlledItems[id] = element;
return element;
}
return null;
}).filter(Boolean);
}
// Create the appropriate aria control button based on the button's attributes
createControlButton(button, ids, objects) {
if (button.hasAttribute("data-parent")) {
const parent = document.getElementById(button.getAttribute("data-parent"));
new AwwwAriaControlButtonParent(button, ids, objects, parent);
} else if (button.hasAttribute("data-dir")) {
const dir = button.getAttribute("data-dir");
new AwwwAriaControlButtonDirectional(button, ids, objects, dir);
} else if (button.hasAttribute("data-pos")) {
const pos = button.getAttribute("data-pos");
new AwwwAriaControlButtonPositional(button, ids, objects, pos);
} else {
new AwwwAriaControlButtonNormal(button, ids, objects);
}
}
}
const awwwAriaControlsHolder = new AwwwAriaControlsHolder();
AwwwAriaControlButtonParent
Deze class handelt alle interacties af, die binnen een bepaalde scope plaats vinden.
class AwwwAriaControlButtonParent extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems, parent) {
super(button, controlledIds, controlledItems);
this._parent = parent;
this.activateParentHandling();
}
activateParentHandling() {
this._button.addEventListener("click", (e) => {
e.preventDefault();
this._parent.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: true, detail: {state: true, target: this._button}}));
});
this._parent.addEventListener("STATE_CHANGED", (e) => {
const isSameButton = e.detail.target === this._button;
this._button.setAttribute("aria-expanded", isSameButton ? "true" : "false");
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: isSameButton}}));
});
});
}
}
Finale JavaScript:
class AwwwAriaControlsHolder {
constructor() {
this._controlledItems = {};
this.activeItems = [];
this.initAriaControls();
this.enableEvents();
}
enableEvents() {
document.addEventListener("click", (e) => {
if (!e.hasOwnProperty("ariaClicked") && this.activeItems.length > 0) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target)) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
this.activeItems = [];
}
});
document.addEventListener("STATE_CHANGED", (e) => {
if (e.detail.hasOwnProperty("affectedItems") && e.detail.affectedItems.length > 0) {
this.activeItems = e.detail.affectedItems;
} else {
this.activeItems = [];
}
});
document.addEventListener('keyup', (e) => {
if (this.activeItems.length > 0) {
if (e.keyCode == 27) {
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').focus();
document.querySelector('[aria-controls*="' + this.activeItems[0].id + '"]').click();
}
if (e.keyCode == 9) {
this.activeItems.forEach((item) => {
if (!item.contains(e.target) && e.target != item) {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false, affectedItems: []}}));
}
});
}
}
});
document.addEventListener("AWWW_REGISTER_CONTROLS", (e) => {
this.initAriaControls(e.detail.section);
});
}
// Initialize aria controls within a given section (or document by default)
initAriaControls(section = document) {
section.querySelectorAll("[aria-controls]").forEach((button) => {
//only if the button is visible we wil add functionality to the button and the controls
if (button.checkVisibility()) {
const ids = button.getAttribute("aria-controls").split(' ');
const state = (button.getAttribute("aria-expanded") == "true") ? true : false;
const objects = this.getControlledItems(ids, state);
// Determine the type of button based on the attributes and create the appropriate control button
this.createControlButton(button, ids, objects);
}
});
}
// Retrieve controlled items based on the ids
getControlledItems(ids) {
return ids.map(id => {
if (this._controlledItems[id]) {
return this._controlledItems[id];
}
const element = document.getElementById(id);
if (element) {
const newItem = new AwwwControlledItem(id, element);
this._controlledItems[id] = element;
return element;
}
return null;
}).filter(Boolean);
}
// Create the appropriate aria control button based on the button's attributes
createControlButton(button, ids, objects) {
if (button.hasAttribute("data-parent")) {
const parent = document.getElementById(button.getAttribute("data-parent"));
new AwwwAriaControlButtonParent(button, ids, objects, parent);
} else if (button.hasAttribute("data-dir")) {
const dir = button.getAttribute("data-dir");
new AwwwAriaControlButtonDirectional(button, ids, objects, dir);
} else if (button.hasAttribute("data-pos")) {
const pos = button.getAttribute("data-pos");
new AwwwAriaControlButtonPositional(button, ids, objects, pos);
} else {
new AwwwAriaControlButtonNormal(button, ids, objects);
}
}
}
class AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems) {
this._button = button;
this._ids = controlledIds;
this._controlledItems = controlledItems;
this._changed = false;
this._controlledItems.forEach(item => this.activateChange(item));
}
activateChange(item) {
item.addEventListener("STATE_CHANGED", (e) => {
let active = (e.target == item) ? e.detail.state : "true";
this._button.setAttribute("aria-expanded", active);
this._changed = true;
});
}
}
class AwwwAriaControlButtonNormal extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems) {
super(button, controlledIds, controlledItems);
this.activateNormalHandling();
}
activateNormalHandling() {
this._button.addEventListener("click", (e) => {
e.preventDefault();
e.ariaClicked = true;
const isExpanded = this._button.getAttribute("aria-expanded") === "true";
this.changeState(!isExpanded);
});
document.addEventListener("STATE_CHANGED", () => {
if (!this._changed) {
this._button.setAttribute("aria-expanded", "false");
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: false}}));
});
}
this._changed = false;
});
}
changeState(state) {
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: true, detail: {state: state, affectedItems: []}}));
});
}
}
class AwwwAriaControlButtonParent extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems, parent) {
super(button, controlledIds, controlledItems);
this._parent = parent;
this.activateParentHandling();
}
activateParentHandling() {
this._button.addEventListener("click", (e) => {
e.preventDefault();
this._parent.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: true, detail: {state: true, target: this._button}}));
});
this._parent.addEventListener("STATE_CHANGED", (e) => {
const isSameButton = e.detail.target === this._button;
this._button.setAttribute("aria-expanded", isSameButton ? "true" : "false");
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("STATE_CHANGED", {bubbles: false, detail: {state: isSameButton}}));
});
});
}
}
class AwwwAriaControlButtonDirectional extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems, dir) {
super(button, controlledIds, controlledItems);
this._dir = dir;
this.activateDirectionalHandling();
}
activateDirectionalHandling() {
this._controlledItems.forEach(item => {
item.addEventListener("AWWW_CHANGED_ITEM", (e) => {
this._button.disabled = (this._dir === "-1" && e.detail.start) || (this._dir === "+1" && e.detail.end);
});
});
this._button.addEventListener("click", (e) => {
e.preventDefault();
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("AWWW_CHANGE_DIR", {detail: {dir: this._dir}}));
});
});
}
}
class AwwwAriaControlButtonPositional extends AwwwAriaControlButtonBase {
constructor(button, controlledIds, controlledItems, pos) {
super(button, controlledIds, controlledItems);
this._pos = pos;
this.activatePositionalHandling();
}
activatePositionalHandling() {
const posAttribute = this._button.getAttribute('data-pos');
this._controlledItems.forEach(item => {
item.addEventListener("AWWW_CHANGED_ITEM", (e) => {
const isSelected = e.detail.pos == posAttribute;
this._button.setAttribute("aria-selected", isSelected);
});
});
this._button.addEventListener("click", (e) => {
e.preventDefault();
this._controlledItems.forEach(item => {
item.dispatchEvent(new CustomEvent("AWWW_CHANGE_POS", {detail: {pos: this._pos}}));
});
});
}
}
class AwwwControlledItem {
constructor(id, item) {
this._id = id;
this._item = item;
this._firstFocusableChild = this._item.querySelector("a, input, button");
this._isHidden = this._item.hasAttribute("hidden");
if (this._item.getAttribute("role") === "dialog") {
this.setupDialogFocusManagement();
}
this._item.addEventListener("STATE_CHANGED", (e) => {
this.handleStateChanged(e);
});
this._item.addEventListener("click", (e) => {
e.ariaClicked = true;
});
}
handleStateChanged(e) {
const isSameItem = e.target === this._item;
const state = isSameItem ? e.detail.state === true : true;
this._item.classList.toggle('active', state);
if(this._isHidden){
this._item.hidden = !state;
}
if (isSameItem && state == true && this._firstFocusableChild) {
this._firstFocusableChild.focus();
}
if (state == true && e.detail.hasOwnProperty("affectedItems")) {
e.detail.affectedItems.push(this._item);
}
}
setupDialogFocusManagement() {
const focusableElements = Array.from(this._item.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'));
this._firstElement = focusableElements[0];
this._lastElement = focusableElements[focusableElements.length - 1];
this._item.addEventListener("keydown", (e) => {
if (e.key === "Tab") {
if (e.shiftKey && document.activeElement === this._firstElement) {
e.preventDefault();
this._lastElement.focus();
} else if (!e.shiftKey && document.activeElement === this._lastElement) {
e.preventDefault();
this._firstElement.focus();
}
}
});
}
}
const awwwAriaControlsHolder = new AwwwAriaControlsHolder();
document.addEventListener("AWWW_PAGE_RELOAD_FIRST", function () {
const awwwAriaControlsHolder = new AwwwAriaControlsHolder();
});
Conclusie
Het effectief gebruik van ARIA-attributen in combinatie met JavaScript biedt een robuuste manier om interactiviteit op een toegankelijke manier te implementeren. Door het volgen van de beschreven methoden en het zorgvuldig afhandelen van afhankelijkheden, kun je ervoor zorgen dat zowel zichtbare als niet-zichtbare gebruikersinteracties correct worden beheerd. Dit draagt niet alleen bij aan een betere gebruikerservaring voor iedereen, maar verhoogt ook de toegankelijkheid van je website, wat essentieel is voor naleving van WCAG-richtlijnen.

