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:
ArrowUp/ArrowDownPageUp/PageDownSpace/Shift+SpaceHome/End
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:
- lazily attaches the four shared listeners the first time any grid registers;
- on
keydown/keyup, dispatches togrid.handleKeydown(e)/grid.handleKeyup(e)for every registered instance — the per-instance gate below (shouldHandleKeyboardEvent) is unchanged and still decides whether that instance reacts, so hover-without-focus scrolling works exactly as before; - on
resize/blur, dispatches togrid._handleResize()/grid.handleWindowBlur()for every registered instance unconditionally (these were never gated by hover/focus).
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:
activeScrollKeyactiveScrollKeyShiftisKeyActivekeyIdleTimerkeyScrollTimer(kept for compatibility; active flow uses idle timeout)- momentum fields:
momentumGuardUntil,lastScrollInputSource,lastProgrammaticScrollAt,lastProgrammaticScrollTop
These fields allow keyboard input to coexist with wheel/drag/programmatic scroll paths.
4. Input Gating (shouldHandleKeyboardEvent)
Keyboard events are ignored when:
- no viewport exists
- an earlier listener already handled the key (
e.defaultPrevented): a host can take a key before the grid, as<vn-gantt>takes the arrow, Page, Home and End keys on its task grid (Task Moving § 4) with a capture-phase listener that prevents their default. A page listener that prevents a key's default stops the grid's scroll too, as the DOM contract says - target is content-editable
- target is
input,textareaorselect(every key) - target is a
buttonand the key is Space (' 'or'Spacebar')
Events are handled only when viewport is effectively active:
- active element is viewport
- or active element is inside viewport
- or viewport is hovered (
isViewportHovered)
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:
- guard checks (
viewport,defaultPrevented, loading state, ctrl key, keyboard eligibility) - map supported key (from here on
_handleScrollKey(e), whichforwardKeydown(event)calls too) preventDefault()to fully own scroll behavior- compute
deltaPixels - dispatch by key type:
Home/End: immediate jump and short momentum guard- repeatable keys (
Arrow*,Page*,Space):startKeyScroll(...) - fallback immediate delta apply
Key mapping:
ArrowUp:-rowHeightArrowDown:+rowHeightPageUp:-viewportHeightPageDown:+viewportHeightSpace: page-down; withShiftpage-upHome: large negative delta (clamped to top)End: large positive delta (clamped to bottom)
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):
- stores active key state
- clears previous idle timer
- computes normalized key step via
getKeyScrollDelta(...) - applies delta through
applyScrollDelta(..., 'key') - 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:
- Arrow keys:
- first press:
rowHeight * 1 - repeat:
rowHeight * 3
- first press:
- Page/Space keys:
- first press:
viewportHeight * 0.85 - repeat:
viewportHeight * 0.95
- first press:
This produces responsive repeat scrolling while avoiding extreme jumps.
8. Scroll Application Path
All keyboard movement uses applyScrollDelta(deltaPixels, 'key').
applyScrollDelta(...):
- clamps target scroll top to viewport bounds
- records programmatic write metadata
- 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:
- Home/End jump
- key stop / idle expiration
- other stop paths
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:
- wheel (
isWheelActive) - keyboard (
isKeyActive) - scrollbar drag (
_scrollbarDragging)
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:
- wheel path (
handleWheel) normalizes and applies deltas via sameapplyScrollDelta(...) - custom scrollbar drag also sets
lastScrollInputSource stopActiveScroll()halts active input modes and applies guards- window blur cleanup resets key + wheel activity
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:
- row range updates remain stable
- near-bottom detection and prefetch remain coherent
- rendering avoids partial uncontrolled jumps
12. Practical Notes
- Keyboard scrolling is disabled while shimmer loading is active.
Ctrl+keycombinations are ignored by keyboard-scrolling handler.- Global listeners mean no extra setup is needed once grid is instantiated.
- Focus/hover gating keeps keyboard behavior scoped to grid intent.
- A focused form field (
input,textarea,select) or editable element keeps every key; a focused<button>keeps only Space.
13. Example Usage
No extra API call is required; keyboard support is active by default.
To ensure good UX:
- keep
viewportfocusable (tabindexis auto-added by grid if missing) - avoid overlay elements that permanently steal focus from viewport
- use reasonable
rowHeightfor predictable arrow-step movement