Code Monkey home page Code Monkey logo

focus-group's Introduction

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:

  • Use different keybindings to move focus through the nodes
  • Type (with letters) to jump focus to a specific node based on its text

Essentially, it mimics some of the essential keyboard interactions of a native <select>.

These kinds of powers are useful for:

  • Accessible menus, like react-aria-menubutton
  • Any other widgets whose keyboard UX will improve by enabling arrow-key navigation and letter-key jumping

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:

  • It can be manually specified when adding the member to the group, via setMembers() or addMember() (see below).
  • If the member's node has a data-focus-group-text attribute, that value will serve as the member's text.
  • If neither of the above is provided, the member's text will be the textContent of its node.

Keyboard Interactions

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

  • If you press one of your next keybindings (the down arrow by default), focus moves from the currently focused member to the next member in the group (or wraps back to the front, according to the wrap option).
  • If you press one of your prev keybindings (the up arrow by default), focus moves from the currently focused member to the previous member in the group (or wraps around to the back, according to the wrap option).
  • If you press a letter key, string searching begins (see below!).

String searching

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

  • When you start typing, focus moves to the first member whose registered text begins with whatever you've been typing.
  • As long as each keystroke occurs within stringSearchDelay, the search string will extend (e.g. f -> fa -> far -> farm) and focus will move accordingly.
  • If no text matches the search string, focus will not move.
  • After you have not typed any letters for stringSearchDelay, the search string resets and you can start over (e.g. you type fa 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.

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:

  • An array of DOM nodes (or a NodeList, like what's returned by querySelectorAll())
  • An array of member objects, each object with the following properties: node (the DOM node), and (optionally) 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.

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:

  • A DOM node
  • An object with the following properties:
    • node (the node itself)
    • (optionally) 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.

focusGroup.removeMember(member)

Remove a member from the group.

member can be any of the following:

  • A DOM node
  • An index for the member that should be removed.

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:

  • An array of DOM nodes (or a NodeList, like what's returned by querySelectorAll())
  • An array of member objects, each object with the following properties:
    • node (the node itself)
    • (optionally) 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.

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.

focus-group's People

Contributors

davidtheclark avatar dependabot[bot] avatar paulyoung avatar shirshendubhowmick avatar souporserious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

focus-group's Issues

Seeking co-maintainers!

I've been shifting my focus away from UI development, so don't plan on addressing new issues myself. If you use this library and want to see development continue, you can make that happen by becoming a co-maintainer โ€” with permissions to triage issues, merge PRs, cut releases, etc.

Please comment below if you're interested!

Another possibility is for a dedicated owner to fork this code and create a new package. I'd be happy to link to that library from the README of this one.

Delegate document listener

Do you think the document listener should be delegated? Then there is only one listener rather than a listener for every Focus Group. I can PR if you would like.

onFocus callback

How do you feel about an onFocus callback? So if you have an element that can't receive focus you can add a class yourself or handle the state in something like React and apply a style. I'm trying to build a combobox right now and need to have a fake focus group so the input can remain focused, but still allow choosing a result. Thoughts? I can put together a PR if you like the idea. Maybe something like this?

onFocus: options.onFocus || function (member) {
  focusNode(member.node)
}

Then it would be the user's responsibility to handle focusing the element however they wanted.

edit

Realized this can't be solved with just a callback. I'm guessing another option like groupNode would be required to determine what the focus group is. Any ideas on that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.