Accessible invite modal that traps and restores focus
A team settings page has an Invite teammate button that opens a modal dialog with an email form. Build the dialog so that keyboard and screen-reader users can use it as easily as mouse users: it announces itself, takes focus when it opens, keeps focus inside while open, and gives focus back when it closes.
TEAM_NAME (Orbit Studio), and a button labelled Invite teammate.Invite sent to {email}.Clicking Invite teammate opens the dialog. Render it as a full-screen backdrop element whose direct child is the dialog panel. The panel:
role="dialog" and aria-modal="true",aria-labelledby,aria-label is Close),@ and at least one after it.Enter a valid email address inside it.Invite sent to {email} on the page.Click "Invite teammate" dialog opens, focus on Close
Tab on Send invite focus wraps to Close
Shift+Tab on Close focus wraps to Send invite
Press Escape dialog closes, focus on "Invite teammate"
Open, click Send invite "Enter a valid email address", dialog stays open
Type "sam@example.com"
Click Send invite dialog closes, page shows "Invite sent to sam@example.com"
The tests click your buttons, type into the input, and dispatch keydown events (key: 'Tab', with shiftKey for Shift+Tab, and key: 'Escape') on the focused element. You don’t need test ids or class names. The test frame is hidden, so the browser can’t move focus by itself there. The tests track focus through element.focus() calls and document.activeElement, so move focus only with .focus() (React’s autoFocus also works), and don’t rely on the HTML autofocus attribute or on the browser’s default Tab behaviour. They check that:
role="dialog" element until it is clicked.role="dialog", aria-modal="true", and an aria-labelledby element whose text is Invite a teammate. It contains an input labelled Email address and Cancel and Send invite buttons.document.activeElement is the Close button (found by its aria-label or text) right after opening.mousedown, mouseup and click on the panel’s parent element) each close the dialog and leave document.activeElement on Invite teammate. The same events on the dialog title leave it open.Enter a valid email address and Invite sent to {email} appear as described.A closed dialog can be removed from the page or hidden (hidden, display: none, or visibility: hidden). Capitalization and extra spacing don’t matter.