52
Angular A11y ESLint Rules
Making a web application accessible can feel like a daunting task, particularly if you aren't used to taking accessibility into consideration.
The @angular-eslint
repo includes a number of linting rules that can help enforce accessibility best practices within Angular component templates.
It is important to note, however, that these rules do not check attributes and roles set using the host section of the @Component
decorator, or ones set using @HostBinding
decorators.
While passing these rules isn't a guarantee or metric for ensuring full accessibility, it is a step in the right direction.
For convenience, I've divided the rules I will be covering into three groups. The first group, general, covers ARIA roles and attributes. The second group, content, includes rules related to textual representations of content. The last group, interactivity, includes rules pertaining to user interaction and event handling.
This rule makes sure that all role
and aria-*
attributes used are valid.
Checks visual elements <img>
, <object>
, <area>
and <input>
(of type 'image'
) to make sure they have descriptive text.
-
<img>
elements must have analt
attribute -
<object>
elements must have either atitle
oraria-label
attribute -
<area>
elements must have either analt
oraria-label
attribute -
<input>
elements withtype='image'
must have either analt
oraria-label
attribute
Related Guidelines:
Checks heading, anchor and button elements to make sure they contain accessible content. This can be in the form of text, binding to [innerText]
, [innerHtml]
or [outerHTML]
, or use of aria-label
or title
attributes.
Related guidelines:
Check that label elements and label components are associated with form elements.
Default behavior expects label
elements to either be associated with an input element via the for
attribute, or to contain a <button>
, <input>
, <meter>
, <output>
, <progress>
, <select>
, or <textarea>
element.
Configuration Options:
{
"controlComponents": [],
"labelComponents": [],
"labelAttributes": []
}
-
controlComponents
- Specify components to treat as input elements (in addition to ones mentioned above) -
labelComponents
- Specify components to treat as label elements (in addition tolabel
) -
labelAttributes
- Specify attributes that can satisfy label being associated with an input element (in addition tofor
andhtmlFor
)
Related guidelines:
This rule checks to make sure tabindex
is only ever being set to 0
(element is tab focusable) or -1
(element is not tab focusable), and not a positive value that interferes with the automatic tab order of elements.
Related Guidelines:
Requires elements with click event handlers also handle at least one key event (keyup
, keydown
or keypress
)
Related Guidelines:
Requires any element with a mouseout
event handler to also handle blur
events, and any element with a mouseover
event handler to also handle focus
events.
Related Guidelines:
Passing these rules is an easy way to start the journey of making your Angular applications accessible, and hopefully encourage you to consider accessibility as you develop.
Additional Reading:
52