Accessibility Patterns
Interactive components must be accessible. Common patterns:
1. Screen reader announcements:
const announceToScreenReader = (message) => {
const announcement = document.createElement('div');
announcement.setAttribute('aria-live', 'polite');
announcement.setAttribute('aria-atomic', 'true');
announcement.className = 'sr-only';
announcement.style.cssText =
'position:absolute;left:-10000px;width:1px;height:1px;overflow:hidden;';
document.body.appendChild(announcement);
announcement.textContent = message;
setTimeout(() => {
document.body.removeChild(announcement);
}, 1000);
};
announceToScreenReader(`Now showing slide ${index + 1} of ${total}`);
2. Keyboard navigation:
element.addEventListener('keydown', (e) => {
switch(e.key) {
case 'ArrowLeft':
e.preventDefault();
showPreviousSlide();
break;
case 'ArrowRight':
e.preventDefault();
showNextSlide();
break;
case 'Home':
e.preventDefault();
showSlide(0);
break;
case 'End':
e.preventDefault();
showSlide(slides.length - 1);
break;
}
});
3. ARIA attributes:
button.setAttribute('aria-pressed', isPressed);
button.setAttribute('aria-disabled', isDisabled);
button.setAttribute('aria-expanded', isExpanded);
status.setAttribute('aria-live', 'polite');
status.setAttribute('aria-atomic', 'true');
handle.setAttribute('role', 'slider');
handle.setAttribute('aria-valuemin', '0');
handle.setAttribute('aria-valuemax', '100');
handle.setAttribute('aria-valuenow', currentValue);
handle.setAttribute('aria-label', 'Image comparison');