Primitives / foldkit-primitives/interaction

RovingTabindex

namespace · packages/primitives/src/interaction/roving-tabindex.ts:8

export * as RovingTabindex from './roving-tabindex.js'

One tab stop for a set of items (a toolbar, a tab list, a listbox): arrows move focus between them, the rest stay out of the tab order. The current item's id is the Model slice, so a reorder keeps the same item current and a resumed page knows where focus was. Which item comes next is a pure function of the items and the key; the Behavior wires it to the slots.

In the guides

Members

RovingTabindex.Args

const · packages/primitives/src/interaction/roving-tabindex.ts:41

const Args: Schema.Struct<{
  readonly orientation: Schema.Literals<
    readonly ['vertical', 'horizontal', 'both']
  >
  readonly loop: Schema.Boolean
  readonly virtual: Schema.Boolean
}>

RovingTabindex.arrowChords

function · packages/primitives/src/interaction/roving-tabindex.ts:159

function arrowChords(
  orientation: Orientation,
): ReadonlyArray<Behaviors.Keys.Gesture>

The chords of an orientation's arrows, Shift up and down: what move takes beyond Home and End.

RovingTabindex.behavior

function · packages/primitives/src/interaction/roving-tabindex.ts:235

function behavior<Field extends string>(
  declared: Declared<typeof bundle, Field>,
  args: Args,
): {
  <Slots, Input extends { readonly [K in Field]: Model }, ParentMessage>(
    bound: SlotView.Bound<Slots, Input, ParentMessage>,
  ): <
    Container extends keyof Slots & string,
    Item extends keyof Slots & string,
  >(
    options: BehaviorOptions<Input, Slots, Container, Item>,
  ) => Behavior.NamedBehavior<
    Slots,
    Input,
    ParentMessage,
    | A11y.ProvidedOn<Container, 'navigation'>
    | A11y.ProvidedOn<Item, 'focus-strategy'>
  >
  <Slots>(
    slots: SlotView.Unbound<Slots>,
  ): <Input extends { readonly [K in Field]: Model }, ParentMessage>(
    options: BehaviorOptions<Input, Slots>,
  ) => Behavior.NamedBehavior<Slots, Input, ParentMessage>
}

Wires a placed RovingTabindex to the slots: the container's arrow, Home and End keys (keys) focus the next item (synchronously, then dispatch), each item gets tabindex 0 or -1 and reports Focused when focused. Under virtual DOM focus never leaves the container: the key only dispatches, the container carries aria-activedescendant, and items keep no tabindex.

Every handled key is default-prevented. Nothing is written on dispose: the attributes are data, so a view that no longer attaches this leaves no tabindex behind.

RovingTabindex.BehaviorOptions

interface · packages/primitives/src/interaction/roving-tabindex.ts:140

interface BehaviorOptions<
  Input,
  Slots,
  Container extends keyof Slots & string = keyof Slots & string,
  Item extends keyof Slots & string = keyof Slots & string,
> extends KeyOptions<Input> {
  readonly container: Container
  readonly item: Item
}

RovingTabindex.bundle

const · packages/primitives/src/interaction/roving-tabindex.ts:55

const bundle: Bundle.Bundle<
  'RovingTabindex',
  {
    readonly orientation: 'both' | 'horizontal' | 'vertical'
    readonly loop: boolean
    readonly virtual: boolean
  },
  { readonly current: string | null },
  { readonly _tag: 'Focused'; readonly id: string },
  never,
  never,
  never,
  NoView,
  {},
  {
    focus: (
      model: Model,
      id: string,
    ) => Readonly<{
      model: { readonly current: string | null }
      commands?: Update.Commands<
        { readonly _tag: 'Focused'; readonly id: string },
        never
      >
      outMessage?: never
    }>
  },
  undefined
>

RovingTabindex.Direction

type · packages/primitives/src/interaction/roving-tabindex.ts:67

type Direction = 'ltr' | 'rtl'

RovingTabindex.idSelector

function · packages/mixins/src/behaviors/id-selector.ts:6

function idSelector(id: string): string

An attribute selector for any id: #id throws or misses for ids such as a:b or 1-x. Quotes, backslashes and control characters are written as hex escapes, since a raw newline in a CSS string makes querySelector throw.

RovingTabindex.itemAttributes

function · packages/primitives/src/interaction/roving-tabindex.ts:170

function itemAttributes<ParentMessage>(
  h: HtmlBuilder<ParentMessage>,
  items: Behaviors.Collection.Items<unknown>,
  current: string | null,
  item: SlotItem,
  virtual: boolean,
  focused: (id: string) => ParentMessage,
): ReadonlyArray<Attribute<ParentMessage>>

The attributes one item gets: tabindex 0 when it holds the tab stop and -1 otherwise (none under virtual), and OnFocus reporting it current. Shared with ListNavigation.

RovingTabindex.KeyOptions

interface · packages/primitives/src/interaction/roving-tabindex.ts:134

interface KeyOptions<Input> {
  readonly items: (input: Input) => Behaviors.Collection.Items<unknown>
  readonly direction?: (input: Input) => Direction
}

RovingTabindex.keys

function · packages/primitives/src/interaction/roving-tabindex.ts:190

function keys<Field extends string>(
  declared: Declared<typeof bundle, Field>,
  args: Args,
): <Input extends { readonly [K in Field]: Model }, ParentMessage>(
  options: KeyOptions<Input>,
) => Behaviors.Keys.Route<Input, ParentMessage>

The route a RovingTabindex installs on its container: the arrows of the orientation, Home and End, each answering Focused and moving focus to the item (under virtual, only the Message). List it in a Keys.behavior beside routes of your own, as behavior lists it alone.

RovingTabindex.Message

const · packages/primitives/src/interaction/roving-tabindex.ts:35

const Message: MessageUnion<{
  readonly Focused: { readonly id: Schema.String }
}>

RovingTabindex.Model

const · packages/primitives/src/interaction/roving-tabindex.ts:29

const Model: Schema.Struct<{ readonly current: Schema.NullOr<Schema.String> }>

RovingTabindex.move

function · packages/primitives/src/interaction/roving-tabindex.ts:87

function move(
  enabled: ReadonlyArray<number>,
  current: number,
  key: string,
  modifiers: KeyboardModifiers,
  options: MoveOptions,
): number | undefined

The index the key moves to from current, over the enabled indices only, or undefined when the key is not a navigation key here. current may be -1 (nothing current yet): arrows then land on the first or last enabled item. Under rtl, left and right swap. PageUp and PageDown move by page and clamp at the ends, never wrapping.

RovingTabindex.MoveOptions

interface · packages/primitives/src/interaction/roving-tabindex.ts:69

interface MoveOptions {
  readonly orientation: Orientation
  readonly loop: boolean
  readonly direction: Direction
  readonly page?: number
}

RovingTabindex.Orientation

const · packages/primitives/src/interaction/roving-tabindex.ts:26

const Orientation: Schema.Literals<readonly ['vertical', 'horizontal', 'both']>

RovingTabindex.tabStop

function · packages/primitives/src/interaction/roving-tabindex.ts:125

function tabStop(
  items: Behaviors.Collection.Items<unknown>,
  current: string | null,
): number

The item that holds the tab stop: the current one, else the first enabled.

RovingTabindex.update

function · packages/primitives/src/interaction/roving-tabindex.ts:52

function update(model: Model, message: Message): Update.Return<Model, Message>