Create a group of nodes with special focusing powers
MIT License
SEEKING CO-MAINTAINERS! Continued development of this project is going to require the work of one or more dedicated co-maintainers (or forkers). If you're interested, please comment in this issue.
Create a group of nodes with special focus-related powers.
Specifically, you can do the following with your focus group:
Essentially, it mimics some of the essential keyboard interactions of a native <select>
.
These kinds of powers are useful for:
A focus-group is composed of members.
The order of the members matters, because focus moves forwards and backwards through the group, in order.
Each member consists of a DOM node and some text associated with that node. The member's text will be used for letter-key jumping (a.k.a. string searching). Each member's text can be established in a few ways:
setMembers()
or addMember()
(see below).data-focus-group-text
attribute, that value will serve as the member's text.textContent
of its node.When focus is inside the focus-group, the following things should happen:
next
keybindings
(the down arrow by default), focus moveswrap
option).prev
keybindings
(the up arrow by default), focus moveswrap
option).If the option stringSearch
is true
and focus is within the group, the following things happen:
stringSearchDelay
,f
-> fa
-> far
-> farm
) and focus will movestringSearchDelay
, the searchfa
then wait and type go
to match gorge
).This all mimics the native <select>
behavior.
Note that like the native <select>
, typing only matches the beginning of words. So you can't focus David Clark
by typing Clark
.
This is the function you get when you require()
or import
the module.
var createFocusGroup = require('focus-group');
var myMegaMenuFocusGroup = createFocusGroup();
members { Array }: Designate initial members of the group. Can be any of the following:
querySelectorAll()
)node
(the DOM node),text
(the text that should be associated with that node for letter-navigation)You can omit this option and add members later with addMember()
or setMembers()
. Default: []
.
keybindings { Object of 'next'
, 'prev'
, 'first'
, or 'last'
}:
Specify which key events should move the focus forward, back, to the first member, or to the last member through the group. Provide objects (or arrays of objects) that describe the requirements of a KeyboardEvent that should trigger that keybinding. Undesignated modifier keys are false by default. So when using something like { keyCode: 38 }
, that keybinding will be ignored if key 38 is combined with meta, ctrl, or alt.
Default:
{
next: { keyCode: 40 }, // ArrowDown
prev: { keyCode: 38 }, // ArrowUp
}
Use arrays of objects for multiple key bindings:
{
next: [{ key: 'ArrowDown' }, { key: 'ArrowRight' }],
prev: [{ key: 'ArrowUp' }, { key: 'ArrowLeft' }],
}
Even add modifiers or any valid event properties:
{
first: { keyCode: 74, metaKey: true },
last: { keyCode: 75, metaKey: true },
}
wrap { Boolean }:
If true
, when the arrow keys are moving focus they will wrap around the group. That is, when focus is on the last item and you move focus forward, the first item will focus; and when focus is on the first item and you move focus back, the last item will focus.
stringSearch { Boolean }:
If true
, string searching is enabled (see below).
Default: false
.
stringSearchDelay { Number }:
The number of milliseconds that should elapse between the user's last letter entry (with the keyboard)
and a refresh of the string search (see below).
Default: 800
.
Start this group listening to keyboard events and responding accordingly.
Returns the focus group instance.
Stop this group listening to keyboard events.
Returns the focus group instance.
Add a member to the group.
member
can be any of the following:
node
(the node itself)text
: Text that should be associated with that node for letter-navigation. If none is provided, focus-group will check for a data-focus-group-text
attribute or fallback to the node's textContent.
If index
is provided, the member will be added at that index.
Otherwise, it will be added to the end of the group.
Returns the focus group instance.
Remove a member from the group.
member
can be any of the following:
Returns the focus group instance.
Empty the focus group of members.
Returns the focus group instance.
Set the focus group's members (clearing any that already exist).
members
can be any of the following:
querySelectorAll()
)node
(the node itself)text
: Text that should be associated with that node for letter-navigation. If none is provided, focus-group will check for a data-focus-group-text
attribute or fallback to the node's `textContent.Returns the focus group instance.
Returns the focus group's current array of members.
Each item in the array is an object with node
and text
properties.
Focuses the node at a particular index in the focus group's member array.
If no member exists at that index, does nothing.
Returns the focus group instance.
Moves the focus forward one member, if focus is already within the group.
If focus is not within the group, does nothing.
Returns the index of the newly focused member.
Moves the focus back one member, if focus is already within the group.
If focus is not within the group, does nothing.
Returns the index of the newly focused member.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.