Tips on how to improve the accessibility of your JavaScript components and provide users with more and better ways to interact with your website or web app.
This article was originally published on Medium.
In my first post Writing HTML with accessibility in mind I explained why and how I got started with web accessibility. I also shared some tips on how you can improve your markup in order to make your websites more accessible. Some of these were pretty basic but nevertheless valuable. It all boils down to two of the most important unwritten rules in front-end development: Learn the basics and take enough time to plan and write HTML. Both you and your users will benefit from clean and semantic markup.
Luckily, HTML is not the only language we have to make websites, but the more complex the language, the easier things can go wrong and JavaScript can get very complex. Whilst being content that our code works, it's easy to forget about users with other input devices than a mouse or touch pad, e.g. keyboard or screen reader users. In this second article of four about web accessibility I have gathered some tips on what to consider when writing JavaScript and how to make your JavaScript components more accessible.
JavaScript Is Not the Enemy
Before you read my tips I want to point out one important thing — making an accessible site doesn't mean that you have to decide whether to use JavaScript or not. Accessibility is about making content available to as many people as possible, which also includes users with old browsers and computers, slow internet connections, strict security restrictions (e.g. no JavaScript) and so on. The experience under conditions like these, where JavaScript may not work or take too long to load, might not be ideal but is still good enough if the website is accessible and usable.
If JavaScript is executable it can even be used to improve accessibility. Sara Soueidan has written about her experiences creating a tooltip widget in Building a fully-accessible help tooltip… is harder than I thought. She explains how "every single no-JS solution came with a very bad downside that negatively affected the user experience" and why JavaScript is important for accessibility.
Marco Zehe wrote a lot more about JavaScript and accessibility in his article JavaScript is not an enemy of accessibility! I highly suggest you read his post.
But enough with the introductory talk! Let's get to it ...
Great Focus Management Is Essential
It's important to make sure that our websites are navigable by keyboard. A lot of users rely on a keyboard when they surf the web. Among them are people with motor disabilities, blind people and people who don't have hands or cannot use a mouse or track pad for whatever reason.
Navigating a site via keyboard means jumping from one focusable element to another in DOM order. This is usually accomplished by using Tab key or Shift + Tab for the reverse direction. Focusable elements are amongst others links, buttons and form elements. They can be selected with the Enter key and sometimes the Spacebar. By being focusable and selectable in different ways they come with very useful default functionalities. Therefore it just makes sense to use correct semantic elements and write HTML in a logical order.
Elements like <p>
, <h2>
or <div>
cannot be focused by default. We often use tags like these to create custom components powered by JavaScript, which might be problematic for keyboard users.
Making non-focusable elements focusable
It's possible to make non-focusable elements focusable by adding the tabindex attribute with an integer value. If the value is set to 0
the element becomes focusable and reachable via keyboard. If the value is a negative number, the element is programatically focusable (e.g. with JavaScript), but not reachable via keyboard. You can also use a value greater than 0
, but that changes the natural tab order and is considered an anti-pattern.
<h2 tabindex="0">A focusable heading</h2>
If you want to learn more about tabindex
, watch the A11ycasts episode Controlling focus with tabindex by Rob Dodson.
Focusing elements with JavaScript
Even if elements are focusable, sometimes they are not in the right DOM order. To illustrate that I created a simple modal window component in HTML, CSS and JS (demo and editable Pen).
Continue reading %Writing JavaScript with Accessibility in Mind%
by Manuel Matuzovic via SitePoint
No comments:
Post a Comment