Accessible modal with focus trap

8676
0

Modals are accessibility traps—literally. Without focus management, keyboard users can tab into the page behind the modal and get lost. I trap focus within the dialog while it’s open, restore focus to the trigger on close, and support Escape to dismiss. I also set aria-modal and label the dialog so screen readers announce it properly. I’m not aiming for perfection; I’m aiming for ‘doesn’t break basic accessibility expectations’. In practice, I’ll often use a well-tested library (like Radix), but the pattern is still worth understanding because you’ll eventually need a custom dialog for something.