Accessibility & A11y
Basically, accessibility deals with the question of enabling users with disabilities to access apps (and, generally speaking, technologies). For information, here is a great article by Uri Shaked about the concept of accessibility :
Of course, building apps accessible to disabled audiences is (luckily) not a core concern for a developer, but actually, the idea has been now extended to the broader question of how to improve accessibility beyond the regular mouse/screen interaction, and in particular of how to include keyboard events in our web apps.
For example, when a user opens a form, it might be convenient to get the first field already selected and ready to be filled (instead of having to click on it first). Or, it might be comfortable to let our user navigate through the form fields with keyboard arrows (instead of having to select it manually). Etc.
Read more: Mexican pokemon cards
So this is what brings us to the purpose of the Accessibility CDK feature, and its A11y package : giving tools to developers to easily implement a smooth and comfortable keyboard interaction between the user and your app.
The code !
Enough talking — let’s try to dig into some code.
Even though the code is annotated, it might be worth few explanations.
Read more: Meryl streep devil wears prada haircut
We are mainly using two elements:
- The FocusMonitor service and the ListKeyManager class to enable interaction with the keyboard arrows. For this, we need to listen to the keyboard events, and to call: – the ListKeyManager.onKeydown(event) method, which sets any focusable element selected by keyboard as ‘active’- the FocusMonitor.focusVia(…) method, which puts focus on an element selected by the method passed as second argument (‘keyboard’ here)For information, active and focused elements are not related with each other (an active element won’t be automatically focused, and vice versa), therefore it seems we have to do the two operations. This is also the reason why we need to differentiate the case where the user uses keyboard arrows (which updates the active element automatically via the onKeyDown method) and when the user hits tab (which doesn’t update the active element). This is so far the best solution I found, not sure it is the smartest one — any improvement would be very welcome.
So now, we should have a very basic form, which is nicely working with our keyboard arrows.
FocusTrap related directives :
The FocusTrap CDK also provides us a convenient directive, which might be worth mentioning.
Read more: Pooja jain generation investment management
The cdkTrapFocus directive creates a focus trap region, without needing using the FocusTrapFactory service, for example :
Nb: The cdkFocusRegionStart and cdkFocusRegionEnd directives are also convenient, but it seems there is currently something wrong with the RegionEnd directive (or at least, I could not let it work correctly).