loopFocus()function

loopFocus(element: Nullish<Element>, nextTarget: Nullish<Element>): void
ParamType
elementNullish<Element>
The container to keep focus within. required
nextTargetNullish<Element>
The element focus is moving to. required

Loop focus inside an element so it can't escape — refocus the first focusable child when focus moves out.

  • When nextTarget falls outside element, focus is pulled back to the first focusable element inside it (a simple focus trap).

Examples

loopFocus(dialog, document.activeElement);