Home

Awesome

focus-group

Build Status Coverage Status


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:

Concepts

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:

Keyboard Interactions

When focus is inside the focus-group, the following things should happen:

String searching

If the option stringSearch is true and focus is within the group, the following things happen:

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.

API

var focusGroup = createFocusGroup([options])

This is the function you get when you require() or import the module.

var createFocusGroup = require('focus-group');
var myMegaMenuFocusGroup = createFocusGroup();

Options

members { Array }: Designate initial members of the group. Can be any of the following:

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.

focusGroup.activate()

Start this group listening to keyboard events and responding accordingly.

Returns the focus group instance.

focusGroup.deactivate()

Stop this group listening to keyboard events.

Returns the focus group instance.

focusGroup.addMember(member[, index])

Add a member to the group.

member can be any of the following:

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.

focusGroup.removeMember(member)

Remove a member from the group.

member can be any of the following:

Returns the focus group instance.

focusGroup.clearMembers()

Empty the focus group of members.

Returns the focus group instance.

focusGroup.setMembers(members)

Set the focus group's members (clearing any that already exist).

members can be any of the following:

Returns the focus group instance.

focusGroup.getMembers()

Returns the focus group's current array of members.

Each item in the array is an object with node and text properties.

focusGroup.focusNodeAtIndex(index)

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.

focusGroup.moveFocusForward()

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.

focusGroup.moveFocusBack()

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.

Contributing

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.