Primitives / foldkit-primitives/interaction

GridNavigation

namespace · packages/primitives/src/interaction/grid-navigation.ts:10

export * as GridNavigation from './grid-navigation.js'

Two-dimensional arrow keys over a set of items laid out in rows of columns (a calendar grid, a color swatch picker, an emoji palette). The Model slice is the current item's id, as with RovingTabindex, so a reorder keeps the same item current. Which cell a key lands on is the pure move; the Behavior wires it to a container slot and a per-item slot and shares RovingTabindex's item attributes, so the two are interchangeable on a view.

In the guides

Members

GridNavigation.Args

const · packages/primitives/src/interaction/grid-navigation.ts:32

const Args: Schema.Struct<{
  readonly columns: Schema.Number
  readonly wrap: Schema.Boolean
  readonly virtual: Schema.Boolean
  readonly page: Schema.optionalKey<Schema.Number>
}>

GridNavigation.behavior

function · packages/primitives/src/interaction/grid-navigation.ts:253

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, ParentMessage, 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, ParentMessage>,
  ) => Behavior.NamedBehavior<Slots, Input, ParentMessage>
}

Wires a placed GridNavigation to the slots exactly as RovingTabindex does, with move above deciding the cell: the container's arrows, Home and End (and PageUp and PageDown, given page) (keys) focus the next cell (synchronously, then dispatch) or, under virtual, only dispatch and point with aria-activedescendant; each cell gets tabindex 0 or -1 and reports Focused when focused. routes are the application's keys on the same container keydown.

GridNavigation.BehaviorOptions

interface · packages/primitives/src/interaction/grid-navigation.ts:178

interface BehaviorOptions<
  Input,
  Slots,
  ParentMessage,
  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
  readonly routes?: ReadonlyArray<Behaviors.Keys.Route<Input, ParentMessage>>
}

GridNavigation.bundle

const · packages/primitives/src/interaction/grid-navigation.ts:52

const bundle: Bundle.Bundle<
  'GridNavigation',
  {
    readonly columns: number
    readonly wrap: boolean
    readonly virtual: boolean
    readonly page?: number
  },
  { 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
>

GridNavigation.KeyOptions

interface · packages/primitives/src/interaction/grid-navigation.ts:172

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

GridNavigation.keys

function · packages/primitives/src/interaction/grid-navigation.ts:203

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 GridNavigation installs on its container: the four arrows, Home and End, and Ctrl+Home and Ctrl+End for the grid's corners, each answering Focused and moving focus to the cell (under virtual, only the Message).

GridNavigation.Message

const · packages/primitives/src/interaction/grid-navigation.ts:26

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

GridNavigation.Model

const · packages/primitives/src/interaction/grid-navigation.ts:20

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

GridNavigation.move

function · packages/primitives/src/interaction/grid-navigation.ts:87

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

The index the key moves to from current over a grid of count cells in rows of columns, skipping cells not in enabled, or undefined when the key is not a grid key. Left and right step within the row, and across rows under wrap; up and down step within the column, and across columns under wrap. Home and End are the row's first and last enabled cell; with Ctrl they are the grid's. At an edge without wrap the key is consumed and current is returned. PageUp and PageDown, when page is given, move that many rows in the same column and stop at the first or last row, never wrapping; a disabled landing cell continues in the direction of travel as the arrows do, and when nothing beyond it is enabled the nearest enabled cell back toward current is taken, else current. From -1 (nothing current) a forward key lands on the first enabled cell and a backward key on the last. Under rtl, left and right swap.

GridNavigation.MoveOptions

interface · packages/primitives/src/interaction/grid-navigation.ts:64

interface MoveOptions {
  readonly columns: number
  readonly wrap: boolean
  readonly direction: RovingTabindex.Direction
  readonly page?: number
}