Accessible Filters and Sorting Options in Menus #
Description:
Key Accessibility Practices for Filters and Sorting #
- Use Semantic HTML and ARIA Roles:
- Use
<fieldset>and<legend>elements to group filters and provide context. - Use ARIA roles and attributes like
aria-expandedfor expandable filters andaria-liveto announce changes in sorting.
- Use
- Keyboard Navigation:
- Ensure that all filter and sorting options are keyboard accessible.
- Allow users to open and close filter sections using the keyboard (e.g.,
EnterorSpacekeys).
- Visual Indicators and Focus Management:
- Include clear visual indicators for focus and active states.
- Manage focus when sections open or close, moving focus to the first focusable element within the section.
- Screen Reader Compatibility:
- Use
aria-labelledbyandaria-describedbyto provide context for filters and sorting options. - Announce live updates when sorting is applied or filters are selected using
aria-live.
- Use
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Filters and Sorting Menu</title>
<style>
/* Basic styles */
.filters, .sorting-options {
margin: 20px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.filter-section {
margin-bottom: 15px;
}
/* Accessible focus indicator */
.filter-option:focus, .sort-option:focus {
outline: 2px solid #005fcc;
}
.filter-toggle {
background: none;
border: none;
color: #0056b3;
cursor: pointer;
font-size: 1em;
padding: 5px;
text-align: left;
}
.filter-content {
display: none;
padding-top: 10px;
}
.filter-content[aria-hidden="false"] {
display: block;
}
</style>
</head>
<body>
<!-- Sorting Options -->
<section aria-labelledby="sorting-options">
<h2 id="sorting-options">Sort by</h2>
<div class="sorting-options">
<fieldset>
<legend class="sr-only">Sort Products</legend>
<label class="sort-option">
<input type="radio" name="sort" aria-live="polite" value="price-asc"> Price: Low to High
</label><br>
<label class="sort-option">
<input type="radio" name="sort" aria-live="polite" value="price-desc"> Price: High to Low
</label><br>
<label class="sort-option">
<input type="radio" name="sort" aria-live="polite" value="new-arrivals"> New Arrivals
</label><br>
<label class="sort-option">
<input type="radio" name="sort" aria-live="polite" value="best-sellers"> Best Sellers
</label>
</fieldset>
</div>
</section>
<!-- Filter Options -->
<section aria-labelledby="filter-options">
<h2 id="filter-options">Filters</h2>
<div class="filters">
<div class="filter-section">
<button class="filter-toggle" aria-expanded="false" aria-controls="price-filter" id="toggle-price">
Price Range
</button>
<div class="filter-content" id="price-filter" aria-hidden="true">
<label class="filter-option">
<input type="checkbox" name="price" value="0-50"> $0 - $50
</label><br>
<label class="filter-option">
<input type="checkbox" name="price" value="50-100"> $50 - $100
</label><br>
<label class="filter-option">
<input type="checkbox" name="price" value="100-200"> $100 - $200
</label>
</div>
</div>
<div class="filter-section">
<button class="filter-toggle" aria-expanded="false" aria-controls="brand-filter" id="toggle-brand">
Brand
</button>
<div class="filter-content" id="brand-filter" aria-hidden="true">
<label class="filter-option">
<input type="checkbox" name="brand" value="brand-a"> Brand A
</label><br>
<label class="filter-option">
<input type="checkbox" name="brand" value="brand-b"> Brand B
</label><br>
<label class="filter-option">
<input type="checkbox" name="brand" value="brand-c"> Brand C
</label>
</div>
</div>
</div>
</section>
<script>
// Toggle filter sections
document.querySelectorAll('.filter-toggle').forEach(button => {
button.addEventListener('click', () => {
const expanded = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', !expanded);
const filterContent = document.getElementById(button.getAttribute('aria-controls'));
filterContent.setAttribute('aria-hidden', expanded);
});
// Keyboard accessibility
button.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
button.click();
}
});
});
</script>
</body>
</html>
Explanation #
- Sorting Options:
- The sorting options are grouped in a
<fieldset>with a hidden<legend>(sr-only) to provide screen reader context. - Radio buttons allow only one sorting option to be selected at a time. Each option has
aria-live="polite"to announce changes for screen reader users.
- The sorting options are grouped in a
- Filter Sections:
- Each filter section (e.g., “Price Range,” “Brand”) uses a button with
aria-expandedandaria-controlsto indicate whether the filter content is expanded or collapsed. - The
aria-hidden="true"attribute on the.filter-contentdiv keeps collapsed content hidden from screen readers. - Toggling the button changes both
aria-expandedandaria-hiddenattributes, updating screen readers on the visibility of the filter options.
- Each filter section (e.g., “Price Range,” “Brand”) uses a button with
- Keyboard Accessibility:
- Users can expand/collapse filter sections by pressing
EnterorSpaceon the filter button. This behavior is managed with a keyboard event listener. - Tab navigation allows users to move through sorting and filter options easily, with a visible focus indicator (
outline: 2px solid #005fcc) to track focus.
- Users can expand/collapse filter sections by pressing
- Accessible Labels:
aria-labelledbyon<section>elements helps screen readers associate sections with their corresponding headings.- Descriptive labels (like “Price Range” and “Brand”) provide clear context for each filter section.
- Live Updates with ARIA:
aria-live="polite"on the radio buttons announces any sorting changes without interrupting users, making it accessible for screen readers.
Key Takeaways #
- ARIA Roles and Attributes: Use
aria-expanded,aria-hidden, andaria-liveto manage and announce changes in filters and sorting for screen readers. - Keyboard-Accessible Controls: Ensure that all filter options and sorting selections are accessible by keyboard with proper focus management and toggle functionality.
- Clear Focus and Active States: Visual indicators for focus and active states improve usability for keyboard and mouse users alike.
- Grouping Options with Fieldsets and Legends: Group related filter and sorting options using
<fieldset>and<legend>to provide context for screen reader users.
Screen reader: NVDA
Video: