Appropriate Use of ARIA, Custom Widgets and Components #
Chat widget #
An accessible chat widget is crucial for ensuring that all users can engage in real-time conversations and receive support effectively. The following best practices promote the development of accessible chat widgets.
- The chat widget has a clear and descriptive title attribute that informs screen reader users of its purpose.
- The text input area for chat messages has a properly associated label.
- All interactive elements, such as buttons for sending messages and closing the chat, are accessible via keyboard navigation.
- When the chat widget is opened, focus automatically moves to the text input field, allowing users to begin typing immediately.
- New incoming messages are announced by the screen reader.
- Any error messages related to message sending or connectivity issues are clearly communicated to the user via the screen reader.
- Previous messages in the chat are easily navigable, allowing screen reader users to read through the conversation history.
- Any visible or hidden text (e.g., system messages, typing indicators) have appropriate ARIA attributes to ensure they are announced correctly by screen readers.
Screen reader: NVDA
When to use ARIA roles, states, and properties, Common pitfalls #
ARIA, or Accessible Rich Internet Applications, is a set of features designed to make websites and apps more accessible for people who use assistive technologies like screen readers. It enables developers to include specific tags and descriptions within elements on a page, so assistive technologies can better interpret and convey the content to users. For instance, ARIA can provide labels for buttons, describe changes in dynamic content, and improve the organization of complex navigation, making it easier for all users, especially those using accessibility tools, to interact with online content effectively and independently.
ARIA was developed by the World Wide Web Consortium (W3C) to enhance web accessibility. It is designed to work alongside HTML, allowing developers to use ARIA attributes within HTML elements to provide additional accessibility information. The framework is based on the principles of web standards and is primarily implemented using JavaScript to manage dynamic content and interactions.
Caution is essential when using ARIA, as excessive or unnecessary attributes can overwhelm users of assistive technologies and lead to misunderstandings. For example, using ARIA roles to define elements that already have native HTML semantics—like marking a button with `role=”button”`—can create redundancy and confusion rather than enhancing accessibility. Additionally, some screen readers, such as older versions of JAWS and certain mobile screen readers, may not interpret ARIA attributes correctly, making the user experience more challenging for individuals relying on these tools.
List of web page elements that frequently use ARIA attributes to enhance accessibility: #
- Navigation Menu: Utilizes `aria-label` and `aria-expanded` to clarify the purpose and state of the menu.
- Buttons: Employs `aria-label` to provide clear, descriptive labels, especially when visual text is insufficient.
- Forms: Implements `aria-required`, `aria-invalid`, and `aria-describedby` to convey information about form fields and validation messages.
- Tabs: Uses `role=”tablist”`, `role=”tab”`, and `role=”tabpanel”` to create accessible tabbed interfaces.
- Sliders: Applies `aria-valuemin`, `aria-valuemax`, and `aria-valuenow` to communicate the current value and range of a slider.
- Alerts: Implements `role=”alert”` to notify users of important messages or updates that require immediate attention.
- Modal Dialogs: Uses `aria-labelledby` and `aria-modal` to define modal dialogs and provide context for users.
- Accordion: Employs `aria-expanded` to indicate whether each section of the accordion is open or closed.
- Live Regions: Utilizes `aria-live` to notify users of dynamically updated content, ensuring they are aware of changes without needing to refresh the page.
Screen reader: NVDA
Video:
To ensure appropriate use of ARIA in HTML, focus on applying ARIA attributes only when native HTML elements do not provide sufficient semantic meaning or accessibility support. Here’s a example with clear guidance for when ARIA is truly necessary from a general HTML perspective:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Appropriate Use of ARIA in HTML</title>
<style>
.accordion{border:1px solid #ddd;padding:10px;margin:5px 0;cursor:pointer}
.panel{display:none;padding:10px;border-top:none}
.panel[aria-expanded=true]{display:block}
</style>
<script>
function togglePanel(button) {
const panel = document.getElementById(button.getAttribute('aria-controls'));
const isExpanded = button.getAttribute('aria-expanded') === "true";
button.setAttribute('aria-expanded', !isExpanded);
panel.setAttribute('aria-expanded', !isExpanded);
}
</script>
</head>
<body>
<h1>Best Practices for ARIA Usage in HTML</h1>
<!-- Using ARIA appropriately for dynamic content: Accordion component -->
<section>
<h2>Frequently Asked Questions</h2>
<!-- Accordion Button with ARIA attributes for dynamic state management -->
<button
class="accordion"
aria-expanded="false"
aria-controls="panel1"
onclick="togglePanel(this)"
id="accordion1"
>
What is web accessibility?
</button>
<!-- Accordion Panel with ARIA-expanded to indicate visibility status -->
<div id="panel1" class="panel" aria-labelledby="accordion1" aria-expanded="false">
<p>Web accessibility ensures that websites, tools, and technologies are designed so that people with disabilities can use them effectively.</p>
</div>
<button
class="accordion"
aria-expanded="false"
aria-controls="panel2"
onclick="togglePanel(this)"
id="accordion2"
>
Why is ARIA important?
</button>
<div id="panel2" class="panel" aria-labelledby="accordion2" aria-expanded="false">
<p>ARIA helps improve accessibility for dynamic content and user interface components that lack native support for accessibility.</p>
</div>
</section>
<!-- Avoiding ARIA when HTML provides adequate semantic meaning -->
<section>
<h2>Common ARIA Pitfalls</h2>
<!-- Redundant ARIA Role on Button -->
<button>Submit</button>
<p><em>Note:</em> Avoid adding <code>role="button"</code> to a <code><button></code> element, as it already has this semantic meaning in HTML.</p>
<!-- Unnecessary ARIA Label when <label> is sufficient -->
<label for="email">Email:</label>
<input type="email" id="email">
<p><em>Note:</em> Avoid using <code>aria-label="Email address"</code> when a <code><label></code> element is present, as this can lead to redundant announcements for screen reader users.</p>
</section>
<!-- Correctly applied roles and attributes -->
<section>
<h2>Summary of Correct ARIA Usage</h2>
<p>ARIA should be used to enhance HTML when:</p>
<ul>
<li>The HTML element lacks necessary accessibility support for dynamic behavior, as seen in the accordion example above.</li>
<li>There are complex custom components (like carousels or modals) where states and roles are needed to guide users effectively.</li>
<li>It is necessary to describe elements beyond HTML's capabilities, such as alert states or expanded content.</li>
</ul>
</section>
</body>
</html>
Explanation of Proper ARIA Use and Pitfalls in HTML: #
- Using ARIA Only When HTML Lacks Accessibility Support:
- In the accordion component, ARIA attributes (
aria-expanded,aria-controls, andaria-labelledby) are used to convey the visibility state of each panel. This is a suitable use of ARIA, as these attributes make the dynamic behavior clear to screen reader users.
- In the accordion component, ARIA attributes (
- Avoiding Redundant ARIA Attributes:
- HTML elements like
<button>and<label>already have inherent semantic meaning. Adding ARIA roles or labels to these elements can lead to redundancy:- Redundant Role on Button: Avoid adding
role="button"to<button>elements, as it can cause screen readers to repeat or confuse the user. - Unnecessary ARIA Label on Input: When
<label>is associated with an input, using an additionalaria-labelis redundant and can lead to confusing screen reader output.
- Redundant Role on Button: Avoid adding
- HTML elements like
- Summary of Correct ARIA Usage:
- A brief list is provided to summarize when ARIA should be used, focusing on dynamic behavior, complex custom components, and scenarios where HTML alone cannot provide the necessary context for accessibility.
Key Takeaways: #
- Prioritize Native HTML: Use ARIA attributes only when HTML lacks sufficient accessibility support.
- Avoid Redundant Roles and Labels: Adding ARIA roles like
role="button"to<button>elements oraria-labelto inputs with<label>is unnecessary and can reduce usability. - Enhance, Don’t Overwhelm: Use ARIA to supplement HTML for dynamic content or complex interactions but avoid adding unnecessary attributes that can overwhelm users.