Created by Adam Davarel Nostradatito from Notion
Powered by ChatGPT,
Accessibility standards ensure that digital content and interfaces can be used by everyone, including people with disabilities. When building components like buttons, following accessibility guidelines helps ensure that your UI is inclusive and meets legal requirements, such as WCAG (Web Content Accessibility Guidelines).
Here are some key aspects of accessibility to consider when building components, along with best practices:
Tab
key should allow users to navigate through focusable elements, and the Enter
or Space
keys should trigger actions on buttons and links.Best Practice:
Make sure buttons are focusable (<button>
elements are natively focusable).
Use the tabindex
attribute to control focus order if necessary (but avoid overusing it).
<a href="<https://www.w3schools.com/>" tabindex="2">W3Schools</a>
<a href="<http://www.google.com/>" tabindex="1">Google</a>
<a href="<http://www.microsoft.com/>" tabindex="3">Microsoft</a>
aria-label
or aria-labelledby
to provide clear descriptions for elements like buttons, links, and input fields.role
attributes to define the role of non-semantic elements like div
or span
when they're used for interactive components.Best Practice:
<button aria-label="Submit form" disabled={isDisabled}>
{isLoading ? 'Loading...' : 'Submit'}
</button>
aria-disabled
: If a button is disabled, use aria-disabled="true"
to communicate this to assistive technologies:
<button aria-disabled={isDisabled} disabled={isDisabled}>
Submit
</button>
Ensure that text and interactive elements have sufficient color contrast. The minimum contrast ratio is:
Use tools like WebAIM Contrast Checker to ensure your color choices meet WCAG guidelines.
Best Practice:
.button {
color: #ffffff; /* Text color */
background-color: #007BFF; /* Background color */
}
aria-label
to describe its purpose.Best Practice:
<button aria-label="Save document">
<SaveIcon />
</button>
Best Practice:
<button className="focus:outline-none focus:ring-2 focus:ring-blue-500">Click me</button>
<label for="id">
or by wrapping inputs in a <label>
element.aria-describedby
to provide users with additional context about inputs (e.g., instructions, error messages).Best Practice:
<label htmlFor="email">Email Address</label>
<input id="email" type="email" aria-describedby="emailHelp" /><small id="emailHelp">We'll never share your email with anyone else.</small>
aria-live
regions for real-time error alerts.Best Practice:
<form>
<label htmlFor="username">Username</label><input id="username" type="text" aria-invalid={hasError} aria-describedby="usernameError" />
{hasError && <span id="usernameError" role="alert">This field is required</span>}
</form>
Best Practice:
button {
min-width: 44px;
min-height: 44px;
}
<button>
, <a>
, <section>
, <nav>
, <header>
, <footer>
). This helps assistive technologies understand the structure and functionality of your content.div
or span
for interactive elements unless you absolutely need custom styling and behavior, and in that case, use proper ARIA attributes.Best Practice:
<button>Submit</button> <!-- Correct, semantic -->
<div role="button" tabindex="0" aria-pressed="false">Submit</div> <!-- Only if necessary --
aria-live
to communicate changes in content that are important for users (e.g., form validation errors, success messages) so that screen readers can announce the updates automatically.Best Practice:
<div aria-live="polite">
{isFormSubmitted && <p>Form submitted successfully!</p>}
</div>
Accessibility ensures your components are usable by all users, including those with disabilities. Follow these best practices to make sure your UI is WCAG-compliant, keyboard-friendly, screen reader-accessible, and generally inclusive. By adhering to accessibility standards, you improve the user experience, meet legal requirements, and create more robust and versatile applications.