Files
yellowjacket/frontend/src/utils/roving-rows.ts
T
logan 1ed4167634 feat(a11y): give the context menu a keyboard, and the app a voice
The context menu was the only route to Play, Add to Queue, Play Next,
Add to Playlist, Favourite and Track Details, and it opened on
right-click alone: the panel had no role=menu, so its six menuitems were
orphaned, nothing moved focus into it, and nothing handled arrows or
Escape (a11y.3). Phase 1 deferred this deliberately so it would land
with the dialogs, as one focus-management implementation.

MenuKeyboard is that model. It is standalone rather than part of
ContextMenuController because playlist-view renders a menu without the
controller, and the only thing worse than a menu with no keyboard model
is two menus with two of them. Shift+F10 and the ContextMenu key open it
from a focused row, anchored to that row, and focus returns there.

Three lists had no focused row to open it from, so they gained a roving
tab stop (utils/roving-rows.ts, written once rather than three times).
track-list keeps its own: it predates this, carries selection semantics
the other three do not have, and is pinned by its own tests.

Also the ARIA tail this is one story with: aria-sort on the column
headers (role=columnheader arrived in Phase 1 without it), listbox and
option on the four selectable grids — aria-selected on role=button is
invalid and was being dropped, so the state the whole ctrl/shift
interaction exists to produce was invisible — and live regions on the
four async surfaces that changed in silence.

Two things a reproduction taught that reading could not: the
wa-dropdown-items have not set their role when the host's updateComplete
resolves, so querying by role then finds nothing and the menu opens
without taking focus; and focus() on a popup that has not positioned
itself is a silent no-op.
2026-08-12 11:07:34 -04:00

71 lines
2.2 KiB
TypeScript

/**
* A roving tab stop over a virtualized list of rows.
*
* Three lists needed the same thing at once — the queue panel and both
* playlist detail views — because a context menu opened with Shift+F10
* needs a focused row to open *from*, and none of the three had one:
* their rows were plain `<div>`s with no `tabindex` and no `role`.
*
* `track-list` deliberately does not use this. Its equivalent predates
* it, carries selection semantics (shift-extend, ctrl-toggle) that the
* other three do not have, and is pinned by its own tests; converting it
* would be a rewrite of the one list that already worked.
*
* Two things here are not optional:
*
* - **The virtualizer is told the index changed.** Rows come from the
* `virtualize` directive, which re-renders on the virtualizer's *own*
* properties — a host re-render does not move a `tabindex`.
* - **Focus is taken after the update.** The row for an index that was
* off-screen does not exist until the virtualizer has scrolled to it.
*/
import type { LitVirtualizer } from '@lit-labs/virtualizer';
export interface RovingRowsHost {
requestUpdate(): void;
updateComplete: Promise<boolean>;
}
/** The keys this handles, and what they mean given a row count. */
export function nextRovingIndex(
key: string,
current: number,
count: number,
): number | null {
switch (key) {
case 'ArrowDown':
return Math.min(current + 1, count - 1);
case 'ArrowUp':
return Math.max(current - 1, 0);
case 'Home':
return 0;
case 'End':
return count - 1;
default:
return null;
}
}
/**
* Move the tab stop to `index` and put focus on it.
*
* `rowSelector` receives the index and must return a selector matching
* that row inside the virtualizer's light DOM.
*/
export async function focusRovingRow(
host: RovingRowsHost,
virtualizer: LitVirtualizer | undefined,
index: number,
rowSelector: (index: number) => string,
): Promise<void> {
host.requestUpdate();
virtualizer?.requestUpdate();
virtualizer?.scrollToIndex(index, 'nearest');
await host.updateComplete;
virtualizer
?.querySelector<HTMLElement>(rowSelector(index))
?.focus();
}