Master

Refactoring an inaccessible component

Most real accessibility work is not building a new widget, it is replacing one that exists. The refactor has a fixed order: name the interface, replace it with the simplest natively accessible one, then move the styling onto the new element. Doing it in any other order leaves CSS that no longer applies.

What you will see

Each link reached in visual order with a visible ring.

About 1 min of reading

Try It

Refactor one widget end to end

1

Find the worst widget

grep -nE 'class="[^"]*(menu|dropdown|modal|accordion)[^"]*"' index.html | head -3

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The candidate. Pick the one with the most JS attached.
2

Replace the markup

printf '\n' | tee -a index.html

Before you run it, say out loud what it will print. Then open the answer.

What it prints
A list of links: focusable, named, in reading order, with no handlers needed.
3

Delete the handlers

grep -n 'dropdown' public/js/main.js | head -5

Before you run it, say out loud what it will print. Then open the answer.

What it prints
The code that made the old widget work. It should now be deleted, not ported.
4

Verify with the tab walk

open the page and Tab through the new nav

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Each link reached in visual order with a visible ring.
Do not leave the old click handler attached to the new element. A handler on a link duplicates the navigation and can fire twice.

Data Flow

Old widget to new markup
1 Name the interface and its states
↓
2 Choose the simplest native element
↓
3 Replace the markup and delete the handlers
↓
4 Move the styling onto the new element
↓
5 Run the six checks and commit
Why this works
The order that works

Markup first, CSS second, JS last and usually deleted. Each step is verified before the next, so a failure is localized to one file.

When the refactor is not worth it

If the component is only reachable from one page and that page can be redesigned, redesign the page. A component nobody links to does not need to be made accessible.

Examples

Replace before you restyle

Swap the div menu for a list of links first, then delete the old rules. If you restyle first, you write CSS for an element you are about to delete.

Keep the behavior, drop the machinery

The old widget had a click handler and a keyboard handler. The new one needs neither. Deleting the JS is the win; porting it is usually a mistake.

One component at a time, verified

Refactor one interface, run the six checks, commit. Two at once means a regression could be in either and the bisect is wasted.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The patterns worth memorizing
Previous Lesson
The patterns worth memorizing
Next Lesson
Auditing a whole site, not one page
Auditing a whole site, not one page →