Manage browser focus using a simple API.
Current Version: 1.0.0
Focus Manager allows you to manage browser focus via an easy to use Javascript API. It's features include:
- Store a DOM element to eventually return focus to
- Send focus to any DOM element
- Restore focus to a previously stored DOM element
Install focusManager
using bower (WIP)
Initialize in your view:
focusManager.init();
When you want to record an element for future focus restoration, store that element and give it a contextual label.
$('#openModal').click(function() {
focusManager.store($(this), 'myModalButton');
});
Even if an element is normally not focusable (i.e. a div
), you can send focus to it using focusManager
. A tabindex
will be added if one is missing to ensure focus is possible.
$('#openModal').click(function() {
focusManager.store($(this), 'myModalButton');
// open and reveal the modal
focusManager.send('#myModal');
});
When you want to return to a previously focused element, restore its focus using the same label you provided when it was stored.
$('#closeModal').click(function() {
focusManager.restore('myModalButton');
});
Stores a DOM element in memory for future focus restoration.
Parameter name | Description |
---|---|
$element | The DOM element you want to store for future focus restoration |
context | The contextual label used to reference and restore the stored element |
Sends focus to any DOM element. A tabindex
will be added to the target DOM element if one isn't already present.
Parameter name | Description |
---|---|
$element | The DOM element you want to send the browser focus to |
Restores focus to a DOM element in memory, referenced by the provided context.
Parameter name | Description |
---|---|
context | The context label used to reference the memory that is storing a DOM element |
Resets the FocusManager
's memory to it's initial default state (an empty array).
Returns the memory stack as an array. This array is not the memory itself, as such changes to this array does not effect the actual memory. It is only meant to be used as a means of reference.
grunt
orgrunt build
- builds a distributable releasegrunt test
- runs the test suitegrunt test:browser
- runs a server that allows you to run the test suite in your browser