YUI 3: FocusManager Node Plugin [beta]
When designing widgets that manage a set of descendant controls (i.e. buttons in a toolbar, tabs in a tablist, menuitems in a menu, etc.) it is important to limit the number of descendants in the browser's default tab flow. The fewer number of descendants in the default tab flow, the easier it is for keyboard users to navigate between widgets by pressing the tab key. When a widget has focus it should provide a set of shortcut keys (typically the arrow keys) to move focus among its descendants.
To this end, the Focus Manager Node Plugin makes it easy to define a Node's
focusable descendants, define which descendant should be in the default tab
flow, and define the keys that move focus among each descendant.
Additionally, as the CSS
:focus
pseudo class is not supported on all elements in all
A-Grade browsers,
the Focus Manager Node Plugin provides an easy, cross-browser means of
styling focus.
More Information
- Examples: Focus Manager Node Plugin in action.
- API Documentation: View the full API documentation for the Focus Manager Node Plugin.
- Download: Focus Manager Node Plugin as part of the full YUI Library.
- Free Hosting on our fast edge servers with combo-loading.
- License: BSD.
Getting Started
Include Dependencies
The easiest way to include the source files for and its dependencies is to add the YUI seed file to your page, using the following script tag, and allow the YUI instance to download any additional files which may be required:
The YUI instance will automatically pull down 's source files and any missing dependencies when the node-focusmanager module is used. This helps you avoid having to manually manage the list of files needed on your page to support multiple components while also optimizing your initial page weight by loading files only when they are required.
If you do want to include file dependencies manually on your page, the YUI Dependency Configurator can be used to determine the list of files you need to include in order to use .
The YUI Instance
Once you have the YUI seed file on your page (yui-min.js), you can
create a new YUI instance for your application to use and populate it
with the modules you need, specified as the first set of arguments to the
use method:
The last argument passed to use is a callback function. The callback function will be invoked as soon as the YUI instance is done downloading any required files missing from your page. Once those files are loaded, your local YUI instance will be supplemented with the classes which make up the node-focusmanager module and any modules it depends on. A reference to the populated YUI instance (Y) is passed back to your callback function. Within your callback, then, you can start writing your application code based on your own custom instance of YUI.
For more information on creating instances of YUI and the
use method, see the
YUI Global object documentation.
Using the Focus Manager Node Plugin
To use the Focus Manager Node Plugin, start by identifying the parent element of the elements whose focus is to be managed. For example, consider the following toolbar HTML:
To manage the focus of each button in the toolbar, start by retrieving a
Node instance representing the toolbar's root element
(<div id="toolbar-1">).
Next, call the Node's
plug
method, passing in a reference to the Focus Manager Node Plugin
(Y.Plugin.NodeFocusManager) as the first argument, followed by an
object literal of configuration attributes as the second.
Use the
descendants
configuration attribute to specify the child nodes of the root node whose
focus is to be managed. The
descendants
configuration attribute accepts a string representing a CSS selector, making it
very easy to identify the elements whose focus should be managed. For the
toolbar, a value of "input" will be passed as the value of the
descendants
configuration attribute.
Use the
keys
configuration attribute to identify which keys move focus between each of the
specified descendant Nodes. The
keys
configuration attribute accepts an object literal, the format of which is
{ next: "down:40", previous: "down:38" }. The value for the
next and previous sub attributes are used to attach
key event listeners. Each
value represents the type of event
("down" for mousedown, "up" for mouseup, and
"press" for keypress) and key codes ("40" for the down arrow key)
to use to navigate to the next/previous descendant. For the
toolbar the
keys
configuration attribute will be set to a value of
{ next: "down:39", previous: "down:37" }, enabling the user to
move focus among each button using the left and right arrows keys.
(See
the Using the key Event section of
the Event documentation for more information on "key" event listeners.)
