Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

RovingController

Roving tabindex with arrows, Home and End, over the items getItems() returns.

Reach for it for tabs, toolbars, lists and grids with one tab stop.

Import

import { RovingController, createRovingController } from '@ecopages/radiant-ui/controllers';

Try it

One button is tabbable. Focus it and press ArrowRight, ArrowLeft, Home or End to move focus, and the tab stop moves with it.

<div data-example="roving">
	<div role="toolbar" aria-label="Formatting">
		<button type="button">Bold</button>
		<button type="button">Italic</button>
		<button type="button">Underline</button>
	</div>
</div>
import { RovingController } from '@ecopages/radiant-ui/controllers';

export function mount(root: HTMLElement): () => void {
	const toolbar = root.querySelector<HTMLElement>('[role="toolbar"]')!;

	const roving = new RovingController({
		getItems: () => [...toolbar.querySelectorAll('button')],
		getTarget: () => toolbar,
	});
	roving.attach();
	roving.sync();
	return () => roving.destroy();
}

mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.

Config

OptionDescription
getItemsThe items, read on every event and sync, so the collection can change without telling the controller.
getTargetThe element that receives keydown and focusin.
orientationhorizontal (default), vertical or none, which handles no keys and leaves them to the component.
wrapWrap past the first or last item. Default true.
getEnabledReturn false to ignore keys and focus.
onMoveCalled after a key moved focus to an item, so the component can select it or open its menu.
typeahead{ getLabel, timeout? } turns on type-to-find: a typed character moves focus to the next item whose label starts with it, and characters typed quickly match a prefix (typeahead). A match calls onMove. Off when omitted.

Lifecycle

attach() listens for keydown and focusin on the target. Call sync() from the host's connected callback and whenever the items change: it sets tabindex to 0 on the active item and -1 on the rest. destroy() removes the listeners and forgets the active item but leaves tabindex as it is. Also getActive(), setActive(item), focus(item) and navigate(current, key).

With a host, createRovingController(host, config) does the same on connect and disconnect; see bindController.

Accessibility: what the controller does and what you must provide

The controller keeps one tab stop and moves it with the arrows of orientation, Home and End. It ignores a key pressed with Alt, Ctrl or Meta held, and a key pressed in a text field that is not itself an item (a textarea, a contenteditable element, or an input of type text, search, email, url, tel, password, number, date, datetime-local, month, time or week), so a text field nested inside an item keeps its caret keys. Checkboxes, radios, buttons, ranges and selects nested in an item stay navigable. An input that is itself an item, such as a radio or a toolbar text field, navigates like any other item: keep a text field out of getItems() when it must keep Left, Right, Home and End. Shift does not stop navigation. With typeahead, a printable character pressed on an item moves focus too, except in a text field, with Alt, Ctrl or Meta held, or when the component already prevented the key. With orientation: 'horizontal', Left and Right are mirrored when the target lays out right to left (its computed direction, so an inherited dir="rtl" counts): Left moves to the next item. isRtl and logicalArrowKey from the same entry do the same for your own key handling.

You provide the roles (toolbar, tablist, listbox), an accessible name, and the state: the caller owns aria-selected, aria-current and aria-pressed, as moving focus does not select. sync() writes tabIndex only when it differs from the item's current tabIndex, and an li or div without a tabindex attribute reports -1, so it never sets the attribute on the inactive ones. Give rows tabindex="-1" in the markup.

Protected hooks

Extend the class and override these to change how it decides. A component never extends a controller; a subclass is registered with bindController.

HookDescription
resolveNext(items, current, key, orientation, wrap)Decides where a key moves focus. Override to change the keys or the order, such as skipping every other item or adding PageUp and PageDown. Return { handled: false } to leave the key alone. key is the key as pressed. The default is navigateRovingTabindex, with Left and Right swapped for a horizontal collection in a right-to-left layout.