FocusManager
A focus management helper for moving and restoring focus across interactive DOM regions.
import { FocusManager } from "@schukai/monster/source/dom/focusmanager.mjs";FocusManager stores, restores and moves focus across interactive regions. Use it for overlays, dialogs, menus or custom controls where focus must return to the right trigger after temporary interaction.
That makes it an accessibility helper, not just a convenience wrapper. Correct focus restoration is part of a stable keyboard experience.
Store And Restore Focus Inside A Dialog Flow
import { FocusManager } from "@schukai/monster/source/dom/focusmanager.mjs";
const output = document.getElementById("focus-output");
const trigger = document.getElementById("focus-trigger");
const first = document.getElementById("focus-first");
const second = document.getElementById("focus-second");
const manager = new FocusManager();
trigger.addEventListener("click", () => {
manager.storeFocus();
manager.focus(first);
output.textContent = "Focus stored and moved to the first field.";
});
second.addEventListener("focus", () => {
output.textContent = "Focus reached the second field.";
});
first.addEventListener("keydown", (event) => {
if (event.key !== "Enter") {
return;
}
manager.focus(second);
});
second.addEventListener("keydown", (event) => {
if (event.key !== "Escape") {
return;
}
manager.restoreFocus();
output.textContent = "Original trigger focus restored.";
});Exported
FocusManagerDerived from
BaseWithOptionsOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the BaseWithOptions.
Properties
The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the BaseWithOptions.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the BaseWithOptions.
Constructor
constructor(options)options{object|undefined}: options
Structural methods
getActive()- {Element}
getFocusable(query)query{string|undefined}: query
- {array}
{TypeError}value is not an instance of
Static methods
[instanceSymbol]()2.1.0- {symbol}
instanceof operator.Other methods
focus(element,preventScroll)element{node}: elementpreventScroll{boolean}: preventScroll
- {Monster.DOM.FocusManager}
{TypeError}value is not an instance of
focusNext(query)query{string}: query
- {Monster.DOM.FocusManager}
focusPrev(query)query{string}: query
- {Monster.DOM.FocusManager}
restoreFocus()- {Monster.DOM.FocusManager}
storeFocus()- {Monster.DOM.FocusManager}
Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.