UI Components Accessibility Best Practices

Best PracticeDescription
Keyboard AccessibilityEnsure that all actions and functionality can be performed using only a keyboard.
Visible Focus StatesEnsure that links, buttons, and controls have a clearly visible focus state.
Text AlternativesPresent all content as text or provide a text equivalent (e.g., alt text for SVGs, aria-label for meaningful icons).
Dynamic State AnnouncementsEnsure that changes in a component’s state (e.g., toggling aria-expanded from “false” to “true”) are announced by screen readers.
Sufficient Color ContrastUse color combinations that have sufficient contrast for readability.

Buttons

  • Accessible Name: Use visible text or provide an aria-label for buttons so that screen readers announce their purpose.
  • Keyboard Operability: Ensure buttons can be activated via keyboard events (e.g., Enter, Space).
  • State Management: Use ARIA attributes such as aria-pressed for toggle buttons, and disable buttons using both the native disabled attribute and aria-disabled.

Links

  • Descriptive Text: Ensure links have clear, descriptive text that explains their destination or purpose.
  • Icon-Only Links: When using icons alone, provide an aria-label or include visually hidden text.
  • External Links: Use target="_blank" with rel="noopener noreferrer" for security when linking externally.

Modals

  • Role & Attributes: Use role="dialog" (or alertdialog) and aria-modal="true" to indicate the presence of a modal dialog.
  • Title Association: Link the modal title to the dialog with aria-labelledby.
  • Keyboard Handling: Trap focus within the modal and allow closing via the Escape key.
  • Backdrop Interaction: Allow the modal to be dismissed by clicking the backdrop, with clear instructions provided if needed.

Toast Notifications

  • Alert Role: Use role="alert" and aria-live="assertive" so that toast messages are announced immediately.
  • Dismissibility: Provide a clear way to dismiss the toast, with a dismiss button that has an appropriate aria-label.

Tags and Labels

  • Contrast and Legibility: Ensure text and background colors have sufficient contrast for readability.
  • Accessible Naming: For non-interactive tags, the visible text is typically sufficient; consider an aria-label override if needed.