When the Modal Closes, Focus Has to Go Home
When a modal closes, focus must return to the element that opened it. If it lands at the top of the page instead, the keyboard user has lost their place and has to tab back through everything they already passed.
The trap is only half the job
Most teams test the modal trap and stop there. Focus enters the dialog, tab cycles inside it, escape closes it, and the test passes. What nobody tests is where focus goes after the close. On too many storefronts it goes to the body, which means the top of the page, which means the keyboard user is back at the header, miles from the product they were looking at when they opened the size guide or the shipping estimator. The trap worked and the return failed, and the failure is worse than no trap at all, because the user did everything right and still lost their place.
The W3C practices guide is explicit about this: when a dialog closes, focus returns to the element that invoked it. That is the contract. The trigger is the anchor the user understands, and returning to it tells them the interruption is over and they are where they were. Anything else is a teleport, and teleports are the thing the focus trap was built to prevent.
Where focus should land
Store the trigger element when the modal opens, and restore focus to it on close, every time, including when the user closes with escape, with the close button, or by clicking the overlay. The three close paths are three chances to forget the return, and the overlay click is the one most often missed, because the developer who wired it thought about the mouse, not the keyboard. If the trigger no longer exists, because the page re-rendered underneath the modal, fall back to the nearest surviving ancestor or the heading of the section that held it. Never fall back to the body.
Test the return with the cart drawer, the size guide, and the login modal, which are the three modals a shopper actually opens. Open each one, close it, and watch where the focus indicator lands. If it lands on the trigger, the modal is done. If it lands at the top, the return is missing, and the fix is a few lines: save document.activeElement on open, call focus on it after close. The code is trivial. The reason it ships broken is that nobody tested the close.
The test that takes 30 seconds
Open the modal, press escape, and look. That is the whole test, and it catches the failure every time, because the failure is visible: the focus indicator sits at the top of the page instead of on the button that opened the dialog. Run it after every change to the modal component, because a refactor that replaces the trigger element breaks the stored reference silently. Add it to the release checklist next to the trap test, and make it a rule that no modal ships until both the trap and the return have been watched by a human with a keyboard. Scanners cannot test this. The trap is in the DOM and the return is in the behavior, and only a person pressing keys can see the behavior.
Frequently asked questions
Where should focus go when a modal closes?
Back to the element that opened it. That is the rule in the W3C practices guide, and it is the only landing spot the user understands, because it is where they were before the interruption.
What if the trigger element no longer exists when the modal closes?
Fall back to the nearest surviving ancestor, or the heading of the section that held the trigger. Never fall back to the body, which dumps the user at the top of the page.
Does the return matter if the user closes with the mouse?
Yes, for keyboard users who opened the modal with the keyboard and then close it any way at all. The return is about where focus goes, and focus exists whether the close came from a key or a click.
Can an automated scan catch a missing focus return?
No. The trap is visible in the DOM, but the return is runtime behavior. The only reliable test is a person opening the modal, closing it, and watching where focus lands.