Todae

Todae

useRovingFocus

One tab stop for a group of controls. Tab enters the group on one item and leaves it on the next Tab; arrow keys, Home and End move between items. Tabs, Menu and Tree are built on it, and a company uses it for its own toolbars, segmented controls and grids of cards.

On this page

Spread getContainerProps() on the group and getItemProps(value, { disabled }) on each item. Items are found in the DOM by an attribute the hook owns, so an item inside your own wrapper still counts, and order is document order. Disabled items are skipped. The browser has no built-in focus group yet, so this stays in JavaScript.

import { Button, useRovingFocus } from '@tounsoo/todae';

// Actions, not toggles: a toggle would also need aria-pressed.
const TOOLS = ['Cut', 'Copy', 'Paste', 'Delete'];

export default function RovingToolbar() {
  const roving = useRovingFocus({ orientation: 'horizontal', defaultActiveValue: 'Cut' });
  return (
    <div role="toolbar" aria-label="Clipboard" style={{ display: 'flex', gap: 'var(--todae-space-1)' }} {...roving.getContainerProps()}>
      {TOOLS.map((tool) => (
        <Button.Root key={tool} variant="ghost" {...roving.getItemProps(tool, { disabled: tool === 'Paste' })} disabled={tool === 'Paste'}>
          {tool}
        </Button.Root>
      ))}
    </div>
  );
}

orientation picks the arrow keys: vertical (default), horizontal, or both. Left and Right swap when the container's direction is rtl. loop (default true) wraps at the ends. typeahead moves to the next item whose text starts with what was typed; pass textValue when an item's text is not its label. tabStop: false makes every item tabIndex={-1}, for a widget that focus only enters from code, such as a menu opened from its trigger. findItems(container) finds the items instead of getItemProps, as Toolbar does with the controls inside it. Such items have no value, so the hook keeps no tab stop and only moves focus between them. tree: true moves the way a tree view does, as Tree uses it: Up and Down skip items inside an item with aria-expanded="false" and do not wrap, Right calls onExpandedChange(value, true) on a closed item or moves to the first item inside an open one, and Left calls onExpandedChange(value, false) on an open item or moves to the item that holds this one. orientation and loop are ignored then, and values() and reachableItems() leave out items inside a closed item.

The tab stop follows activeValue, falling back to the first enabled item when it is null, disabled or not rendered. Items are read from the DOM, so server-rendered HTML has a tab stop only on the item named by activeValue or defaultActiveValue; set one when the group must be reachable before hydration. onActiveValueChange fires whenever an item other than activeValue takes focus, from a key, a click or a script. That includes a fallback tab stop, so a controlled owner can keep it. Natively disabled items are skipped like ones passed { disabled: true }. Shift with an arrow key, Home or End is left to you, for example to extend a selection. focus(value), focusFirst() and focusLast() move focus from your own handlers, getItemNode(value) finds an item's element, values() reads the enabled values in order, and reachableItems() returns the enabled items focus can reach now.

The hook does not set roles. Give the container the role your widget needs, such as toolbar, tablist, menu or radiogroup.

Accessible composition

Todae handles: The tab stop and the keys. getItemProps gives one item tabIndex={0} and the rest -1 (all -1 with tabStop: false), and the container's key handler moves focus as described above. It keeps up as items are added, removed, hidden or disabled. That is all: it sets no role, name or state.

You supply: The container's role and its name (aria-label or aria-labelledby), and aria-orientation when it differs from the role's default. Each item's role and state, such as aria-selected, aria-checked or aria-pressed. The item's own disabled or aria-disabled. Spread the props on the focusable element itself, and pass your own onFocus through the props argument, so it runs with the hook's.

type ToolProps = Omit<ButtonRootProps, 'value'> & { value: string; roving: UseRovingFocusResult };

function Tool({ value, roving, disabled, onFocus, ...props }: ToolProps) {
  return (
    <Button.Root
      variant="ghost"
      {...props}
      {...roving.getItemProps(value, { disabled }, { onFocus })}
      disabled={disabled}
    />
  );
}

Watch for: A handler written after the spread, such as onFocus, replaces the hook's. Item props spread on a wrapper div put the tab stop on something that is not the control. For an item that people should still reach and hear as unavailable, as Menu does, keep it enabled in the hook and set aria-disabled.

When to use

When: A composite widget where one Tab stop and arrow keys are expected: toolbars, segmented controls, tab lists, menus, card grids.

When not: A form or a list of links where each control is its own tab stop. A listbox that keeps focus on an input → aria-activedescendant, as OptionList and the Combobox recipe do.

Props

useRovingFocus props
NameTypeDescription
orientation"horizontal" | "vertical" | "both"

Which arrow keys move focus. Left and Right swap in a right-to-left container.

Default: "vertical"

loopboolean

Wrap from the last item to the first and back.

Default: true

typeaheadboolean

Typing focuses the next item whose text starts with the typed characters.

Default: false

tabStopboolean

Keep one item tabbable. False when focus only enters from code, as in a menu: every item gets tabIndex -1.

Default: true

findItems(container: HTMLElement) => HTMLElement[]

Finds the items, enabled or not, instead of getItemProps. Such items have no value, so no tab stop is kept: the hook only moves focus.

activeValuestring | null

Controlled tab stop. Falls back to the first enabled item when null, disabled or missing.

defaultActiveValuestring | null

Initial tab stop when uncontrolled.

onActiveValueChange(value: string) => void

Fires when an item other than activeValue takes focus, including a fallback tab stop, so a controlled owner can pin it.

treeboolean

Tree view keys. Up and Down skip items inside an item with aria-expanded="false" and do not wrap. Right opens a closed item or moves into an open one; Left closes an open item or moves to its parent. orientation and loop are ignored.

Default: false

onExpandedChange(value: string, expanded: boolean) => void

In tree mode, fires when Right asks to open a closed item or Left asks to close an open one.