Focus Traversal API
- Implements proposed Focus Traversal API standard.
- Programatically move the focus forward or backwards.
- Identify the next and previous items to receive focus.
- Supports handling focus in Shadow DOM hidden components.
- History of the last n focus holders.
- Works on all modern browsers including Internet Explorer 9 or later.
Install from npm...
npm install focus-traversal-api-polyfill
Copy the polyfill into your own project...
cp focus-traversal-api-polyfill/focus-traversal-api-polyfill.min.js .
Include in your index.html...
You may now use the focusManager as described in the section below.
window.focusManager.currentlyFocused- Contains the element currently holding the focus, if any.
window.focusManager.previouslyFocused- Contains the element that held the focus prior to the current focus, if any.
window.focusManager.history- An array of the last n historical focus holders.
window.focusManager.historyLimit- A number indicating how may focus history events should be retained. Defaults to 50.
window.focusManager.focus(element)- Focus on the given element. Functionally the same as
element.focus(). Returns void.
window.focusManager.forward()- Move the focus to the next focusable element. Returns void.
window.focusManager.backward()- Move the focus to the previous focusable element. Returns void.
window.focusManager.next(element)- Returns the element that would revieve the focus if
window.focusManager.forward()was called when the given element has the focus. If no element is given, the currently focused element is used.
window.focusManager.previous(element)- Returns the element that would revieve the focus if
window.focusManager.backward()was called when the given element has the focus. If no element is given, the currently focused element is used.
window.focusManager.orderedElements()- Returns an array of all focusable elements in the order that focus traversal would occur
If you would like to comment on the proposal, please open an issue.
The proposal and the polyfill library are available under the MIT License.