Skip to content

A small repo to demonstrate the problems with attribute "disabled" on HTML buttons

License

Notifications You must be signed in to change notification settings

chriswilty/disabled-buttons-demo

Repository files navigation

disabled-buttons-demo

Using the "disabled" attribute on HTML buttons is problematic in the context of screen readers. This really simple app demonstrates how to use aria-disabled instead, and thereby include everyone 🫶

The problem

<button disabled onclick="submitForm()">
  problem child
</button>
  • Visually-able users can see the button. We can also style it to indicate it is disabled.
  • However, buttons that are "disabled" are ignored by screen readers, and are not focusable, so the user will not even be aware they exist. This treats visually-impaired users as second-class citizens.
  • Because disabled buttons are not focusable, we cannot give them context specific information to explain why they are disabled, other than by using hover states. This excludes all users that do not use a mouse.

The solution

<button aria-disabled="true" aria-describedby="why-disabled" onclick="submitForm()">
  goodboi
</button>
<div role="tooltip" id="why-disabled">
  Fill all mandatory fields to enable submit
</div>
  • Buttons that are "aria-disabled" are seen and read by screen readers, which will announce them as unavailable.
  • aria-disabled elements can still be focused, so we can provide context-specific information such as why the button is disabled and/or how to make the button available.
  • We programmatically disable the onclick functionality of the button depending on application state, instead of relying on the button being disabled to prevent unwanted actions.
  • We apply styles to the tooltip, so it is only visible when the button is disabled and in hover or focused state.

Heavily inspired by

About

A small repo to demonstrate the problems with attribute "disabled" on HTML buttons

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published