Dependency free focus manager with built-in universal key navigation.
npm i --save aim
Register your focusable targets with Aim and Aim will map these into a virtual table-esque view.
Aim will now listen for key events, delegate these to the currently focused target and execute default cursor navigation if not prevented by target handlers.
aim.register({
onFocus (target) {
// fires when target is focused
// return false to prevent focus
},
onBlur (target) {
// fires when target is unfocused
},
onRight (target) {
// fires when right is pressed
// return false to prevent default behaviour (ie. moving cursor to the right)
},
onEnter (target) {
// fires when enter is pressed
}
}, [0, 0, 1])
Fires when target is focused.
Param | Type | Description |
---|---|---|
target | Object |
Registered target. |
Fires on target when removing focus.
Param | Type | Description |
---|---|---|
target | Object |
Registered target. |
Fires on focused target when user presses "enter".
Param | Type | Description |
---|---|---|
target | Object |
Registered target. |
Current focused leaf.
X start position of virtual view.
X end position of virtual view.
Y start position of virtual view.
Y end position of virtual view.
Children of top level view.
Top level direction of layout, y being column, x being row.
Focus one item upward.
Focus one item downward.
Focus one to the left.
Focus one to the right.
Will delegate given event to currently focused item.
Parameters
e
Event
Examples
window.addEventListener('keydown', e => aim.handleKeyEvent(e))
Registers target on given position.
Parameters
Returns Object target
Unregisters target.
Parameters
target
Object Target to unregister.
Gets target on given position.
Parameters
Returns (Object | undefined) target
Focuses passed target.
Parameters
target
Object Target to focus.