Keyboard Navigation Implementation in Vanilla-Grid

This document explains how keyboard navigation is implemented in Vanilla-Grid, including event gating, key-to-scroll translation, repeat behavior, momentum guards, and interaction with wheel/custom scrollbar input.


1. Feature Scope

Keyboard navigation supports scrolling by:

Behavior is integrated with virtualization, custom scrollbar state, and momentum rejection safeguards.


2. Event Wiring and Lifetime

Shared listener registry (default). Rather than every grid instance registering its own keydown/keyup (document) and resize/blur (window) listeners, vanilla-grid.js maintains a module-level singleton registry (_vgKeyboardRegistry) that installs exactly one listener of each type, regardless of how many grids are on the page. _initEventListeners() calls _vgKeyboardRegistry.add(this), which:

destroy() calls _vgKeyboardRegistry.remove(this); the shared listeners are removed once the last registered grid is destroyed.

Cleanup in destroy() deregisters the instance from the shared registry and resets key-scrolling state.

On window blur, handleWindowBlur() calls stopKeyScroll() and resets gesture state to avoid stuck interactions.


3. Keyboard State Model

Internal fields:

These fields allow keyboard input to coexist with wheel/drag/programmatic scroll paths.


4. Input Gating (shouldHandleKeyboardEvent)

Keyboard events are ignored when:

Events are handled only when viewport is effectively active:

This avoids hijacking typing interactions elsewhere in the page. The button exemption exists so a focused <button> keeps its own native activation instead of the grid claiming Space as a page-down scroll: an in-cell one (row grouping's caption toggle, a tree row's toggle, a command column's button) or any other on the page, such as a toolbar command. It covers Space only. Enter is never a grid key, and the arrow, Page, Home and End keys do nothing on a button, so they go on to the focus / hover rule below: a toolbar button left focused by a click doesn't stop a hovered grid from scrolling, and on a focused in-cell button they take the grid's row step and key repeat instead of the browser's own line scroll of the viewport. This is a shared-path rule, not feature-specific: the command button got the same native-activation behavior for free, and so will any later in-cell button. Both buttons are Tab stops, and so is a tree row's expand / collapse toggle unless tree.toggleTabStop is false (then tabindex="-1", and the host gives the keyboard path, as <vn-gantt> does with ArrowRight / ArrowLeft). Space still pages the grid normally whenever focus is on the viewport itself, not on a button (regression-tested in tests/playwright/keyboard-navigation.spec.js).


5. Keydown Flow (handleKeydown)

handleKeydown(e) sequence:

  1. guard checks (viewport, defaultPrevented, loading state, ctrl key, keyboard eligibility)
  2. map supported key (from here on _handleScrollKey(e), which forwardKeydown(event) calls too)
  3. preventDefault() to fully own scroll behavior
  4. compute deltaPixels
  5. dispatch by key type:
    • Home / End: immediate jump and short momentum guard
    • repeatable keys (Arrow*, Page*, Space): startKeyScroll(...)
    • fallback immediate delta apply

Key mapping:

Before the guard checks, _handleGroupToggleShiftEnter() claims Shift+Enter on a focused group caption toggle or tree-row toggle: the subtree form of the toggle, handled on keydown because Firefox drops modifiers from a keyboard activation's click (Tree Rows § 10). Plain Enter and Space on either toggle are the button's own activation, which shouldHandleKeyboardEvent leaves alone.


6. Keyup and Idle Stop

handleKeyup(e) stops key scrolling when released key matches active key (including space alias handling).

startKeyScroll(key, shiftKey, isRepeat):

  1. stores active key state
  2. clears previous idle timer
  3. computes normalized key step via getKeyScrollDelta(...)
  4. applies delta through applyScrollDelta(..., 'key')
  5. schedules idle stop (scheduleKeyScrollStop())

scheduleKeyScrollStop() clears key-active state after 300ms of inactivity and arms momentum guard.

stopKeyScroll() performs explicit key-state reset and guard arming.


7. Step Sizing and Repeat Behavior

getKeyScrollDelta(key, shiftKey, isRepeat) scales movement:

This produces responsive repeat scrolling while avoiding extreme jumps.


8. Scroll Application Path

All keyboard movement uses applyScrollDelta(deltaPixels, 'key').

applyScrollDelta(...):

  1. clamps target scroll top to viewport bounds
  2. records programmatic write metadata
  3. sets viewport.scrollTop

Because the same path is used by wheel/scrollbar inputs (with different source labels), downstream guard logic can reason about input origin consistently.

The viewport API for followers (Row Virtualization § 1.18) reaches the same path:

Caller Source label
setScrollTop(), scrollTopBy(), scrollToRow() 'programmatic'
forwardWheel(event) 'wheel', through handleWheel() itself
forwardKeydown(event) 'key', through handleKeydown()'s scrolling without its gate (§ 4): not ignored for hover or focus, only before initialization, while loading and with Ctrl

The momentum guard never rejects a 'programmatic' write (§ 9 rejects only 'wheel', 'key' and 'scrollbar' sources), and a forwarded wheel or key event is guarded exactly like one over the grid. A forwarded key's keyup reaches the grid through the registry, which hands every keyup to every grid, so its key scroll stops as for its own key.


9. Momentum Guard and Scroll Rejection

9.1 Guard arming

armMomentumGuard(duration) sets a future window where uncontrolled follow-up scroll is scrutinized.

Keyboard code arms this after:

9.2 Rejection (shouldRejectUncontrolledScroll)

During active guard window, scroll events are rejected when they appear uncontrolled relative to the last programmatic write.

Rejection is bypassed while user is actively interacting via:

Direct follow-up events close in time/position to programmatic writes are allowed.

This reduces momentum run-on and resize/gesture feedback loops.


10. Interaction with Wheel and Scrollbar Input

Keyboard navigation is part of a unified input model:

The result is predictable arbitration across mixed input modalities.


11. Virtualization Coupling

Keyboard updates scrollTop; virtualization then reacts in handleScroll() and renderVisibleRows(...).

Because keyboard uses deterministic pixel deltas and clamp logic:


12. Practical Notes


13. Example Usage

No extra API call is required; keyboard support is active by default.

To ensure good UX: