setAccessibility method
Sets the accessibility attributes of this element (for assistive
technologies and browser automation): role (role), ariaLabel
(aria-label), any other aria-* attribute in aria and tabIndex
(tabindex).
- A parameter left
nullkeeps the current attribute. - An
ariakey with or without its prefix, in any case:hidden,aria-hidden,ariaHiddenandaria_hiddenarearia-hidden. An empty key throws an ArgumentError. - An
ariavalue is a string: aboolis"true"or"false"(anaria-*attribute isn't a boolean attribute, whose presence alone would meantrue), an Iterable its items joined by spaces (an ID list:aria-describedby,aria-controls...), an Enum itsname(ascendingforaria-sort), anything else itstoString(). Anullvalue removes the attribute. ariaLabelwins over anarialabelentry.- Set on a generated element (isGenerated), the real one is updated
too (e.g. toggling
aria-expandedon a click).
A clickable non-interactive element (a div with a click listener)
needs a role (button, link...), a name (ariaLabel, or its text)
and, to be reached by the keyboard, a tabIndex of 0.
Implementation
DOMElement setAccessibility({
String? role,
String? ariaLabel,
Map<String, Object?>? aria,
int? tabIndex,
}) {
if (role != null) _setAccessibilityAttribute('role', role);
if (aria != null) {
for (var e in aria.entries) {
_setAccessibilityAttribute(
ariaAttributeName(e.key),
ariaAttributeValue(e.value),
);
}
}
if (ariaLabel != null) _setAccessibilityAttribute('aria-label', ariaLabel);
if (tabIndex != null) _setAccessibilityAttribute('tabindex', '$tabIndex');
return this;
}