Modal dialogs with focus trapping and accessibility

2299
0

Accessible modals require focus management, keyboard navigation, and ARIA attributes. I build a reusable Modal component that traps focus inside when open, moves focus to the first interactive element, and returns it to the trigger on close. The Escape key closes the modal, and clicking the backdrop optionally dismisses it. ARIA attributes like role='dialog', aria-modal='true', and aria-labelledby provide semantic meaning for screen readers. I prevent body scroll when the modal is open and use React Portal to render modals at the document root, avoiding z-index conflicts. This pattern ensures modals work for all users regardless of input method.