January 1, 2024
6 min read

Building Accessible React Components

Essential accessibility practices for React components, including ARIA attributes, keyboard navigation, and screen reader support.

React
Accessibility
A11y
Best Practices

Building Accessible React Components

Accessibility should be a fundamental consideration in every React component we build. Here's how to ensure your components work for everyone.

Essential ARIA Attributes

``jsx

function Button({ children, onClick, disabled, ...props }) {

return (

onClick={onClick}

disabled={disabled}

aria-disabled={disabled}

{...props}

>

{children}

)

}

`

Keyboard Navigation

`jsx

function Modal({ isOpen, onClose, children }) {

useEffect(() => {

if (isOpen) {

const handleEscape = (e) => {

if (e.key === 'Escape') onClose()

}

document.addEventListener('keydown', handleEscape)

return () => document.removeEventListener('keydown', handleEscape)

}

}, [isOpen, onClose])

return isOpen ? (

role="dialog"

aria-modal="true"

className="fixed inset-0 z-50"

>

{children}

) : null

}

`

Screen Reader Support

Always provide meaningful labels and descriptions:

`jsx

type="email"

id="email"

aria-label="Email address"

aria-describedby="email-help"

required

/>

Enter your email address to receive updates

``

Testing Accessibility

Use tools like:

  • axe-core for automated testing
  • Screen readers for manual testing
  • Keyboard-only navigation testing

Conclusion

Accessibility isn't optional—it's essential. By building these practices into your workflow, you create better experiences for all users.

Josh Matthew R. del Mundo

Full Stack Developer specializing in JavaScript, React, and Next.js