Agnostic Framework Focus Manager
npm install tolefocusarray.findIndex, and require some polyfill like core-js in order to work in old browsers, like IE.
npm install tolefocus --save
``
Usage
To enable the focus manager you have to import focusManager from tolefocus module and call enable method.
`` javascript
import {focusManager} from "tolefocus";
focusManager.enable();
``
This load the content of body tag searching for ToleFocus attributes.
You can enable the built in DOM observer witch use MutationObserver in order to detect the DOM changes.
`` javascript
import {focusManager, focusObserver} from "tolefocus";
focusManager.enable();
focusObserver.enable();
``
ToleFocus handles by default the TAB key in the common focusables controls and sends focus to the next or previous control.
$3
You can set the focus order:
`` html
``
Focus order is zero based, you can set negative focus order to any element, the negative focus order are pushed to the first positions.
`` html
``
$3
You can create focus groups where each element can to have his own focus-order in the group.
`` html
``
$3
You can control the group loopback, you can set the head and tail behaivor. When behaivor is "stop" the focus doesn't go to the next element, the foucus stops in the last or first element. When behaivor is "loop" the focus go from last to first element in the group or from the first to the end element.
`` html
``
$3
You can send focus to other controls like div or spans, you have to set focus-order attribute.
`` html
``
$3
You can set an element as autofocus to set focus on it automatically. If you have the focusObserver enabled the element will focus when it's added to the DOM.
`` html
``
If you want to hide a element and get focused when it becomes visible you can set observe value to autofocus attribute.
`` html
``
$3
You can add selectors to handle focusable elements, for example you can add elements with class select2-selection as focusable elements, then tolefocus detect this elements as focusables.
``javascript
focusManager.addCustomSelector(".select2-selection");
``
$3
When tolefocus going to set focus to an element, it ask if the element can get focus, by default it look if element is visible and not disabled this way:
``javascript
const visible = element.offsetParent !== null;
const disable = !!element["disabled"];
return visible && !disable;
``
You can set a handler to decide if element can get focus, for example you can do that elements with class select2-hidden-accessible don't get the focus:
``javascript
focusManager.setCanElementGetFocusHandler((element) => {
if (element.classList.contains("select2-hidden-accessible")) {
return false;
}
});
``
If handler don't return a value tolefocus decide via visible and disabled.
#### Select2 support
Select2 hide the