Building an Accessible Modal Dialog From Scratch

Code is read far more often than it is written. Every shortcut that saves you five minutes today costs the next person who touches the file a little more time, and that next person is very often you, six months later, with no memory of why you did it.

Where things went wrong

None of this needs fancy tooling. A browser's developer tools, a terminal and a notebook to write down what you tried will get you surprisingly far. The discipline of recording each experiment matters more than the instruments you use.

The tempting fix was to add another condition and move on. We had done that before, and the code was already a thicket of special cases that nobody fully understood. This time we decided to step back and ask why the problem was possible in the first place.

  • Use semantic HTML elements wherever possible
  • Make sure every interactive element can be reached by keyboard
  • Give images meaningful alternative text
  • Check colour contrast in both light and dark modes

Simplicity is prerequisite for reliability.

Edsger W. Dijkstra
Late-night debugging session
A whiteboard sketch of the new architecture

What I would do differently

By the end of the week we had a fix we were proud of, a test that would catch the problem if it ever came back, and a short write-up for the rest of the team. That last part is the one most people skip, and it is often the most valuable.

It started, as these things usually do, with a bug report that made no sense. The page loaded fine for everyone on the team, but a handful of customers were seeing a blank screen. It took two days and one very patient support engineer to work out that the problem only appeared on older phones with a particular browser extension installed.

A whiteboard sketch of the new architecture

Common pitfalls

Small pull requests get reviewed faster, get better feedback and are far easier to roll back when something goes wrong. If a change is too big to review in one sitting, it is probably too big to merge safely.

If you take only one thing from this post, let it be this: make the problem visible first. Logs, metrics, a simple counter on a dashboard. You cannot fix what you cannot see, and you will be amazed how many problems disappear once someone is watching them.

  • A short summary of what changed
  • Why the change was needed
  • How it was tested
  • Anything the reviewer should look at closely
The team planning board after sprint review
Benchmark results before and after the change

Putting it all together

A good rule of thumb is to measure before you change anything. It is remarkable how often the part of the system you are sure is slow turns out to be fine, while something you never thought about is quietly eating half your budget.

Documentation does not have to be long to be useful. A short note explaining why a decision was made is worth more than pages describing what the code already says. Future readers can work out the what; the why is the part that gets lost.

Accessibility is easiest when it is part of the design from the start rather than something you bolt on before launch. Semantic HTML does most of the heavy lifting for free, and the remaining work is usually about focus management and clear labels.

  • Measure the current state
  • Find the single biggest cost
  • Fix it and measure again
  • Stop when the result is good enough

Programs must be written for people to read, and only incidentally for machines to execute.

Harold Abelson

Writing tests for the happy path is easy and mostly pointless. The tests that earn their keep are the ones that cover the strange edge cases you discovered in production, because those are the bugs that will come back if nobody is watching.

Posted in

jamie

Leave a Reply

Your email address will not be published. Required fields are marked *