Files
galaxy-game/ui/frontend/src/lib/a11y/restore-focus.ts
T
Ilia Denisov 642c5b7322
Tests · UI / test (push) Waiting to run
Tests · UI / test (pull_request) Successful in 2m9s
feat(ui): accessibility pass — WCAG 2.2 AA for login/lobby/shell (F2)
Add the a11y foundation and bring login, lobby, and the in-game shell to
WCAG 2.2 AA:

- Primitives: .sr-only + .skip-link (base.css), trapFocus (modal focus
  trap + restore) and restoreFocus (menu focus restore) actions, the
  --color-focus visible ring.
- In-game shell: skip link + focusable main landmark; WAI-ARIA sidebar
  tabs (roving tabindex, arrow/Home/End, tabpanel wiring); menu Escape +
  focus restore (account / view / turn-navigator / map-toggles /
  bottom-tabs); mail compose as a role=dialog modal with a focus trap.
- login / lobby / lobby-create: skip link + main landmark, field labels,
  role=alert / role=status live regions.
- Map canvas: aria-label naming it a visual overview, with its data
  reachable by keyboard via the sidebar inspector and tables (accessible
  alternative; in-canvas keyboard nav deferred).

Gates (chromium-desktop): tests/e2e/a11y-axe.spec.ts scans every
top-level view for WCAG 2.2 AA violations (zero); a11y-keyboard.spec.ts
covers the skip link, menu Escape+restore, and tab roving. Adds
@axe-core/playwright. Docs: ui/docs/a11y.md (+ index). Marks F1 and F2
done in ui/PLAN-finalize.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 08:25:14 +02:00

35 lines
1.2 KiB
TypeScript

/**
* Focus restoration for transient popovers and menus.
*
* `restoreFocus` is a Svelte action for a surface that is rendered only
* while open (an `{#if open}` popover/menu/drawer). On mount it records
* the element that had focus — the trigger that opened it — and on
* destroy it returns focus there, so dismissing the surface (Escape,
* outside click, or selecting an item) never drops keyboard focus to the
* document body.
*
* Unlike `trapFocus`, it neither traps focus nor moves it into the
* surface: menus are non-modal, so the user tabs in only if they want.
* Focus is restored only when it would otherwise be lost (it is inside
* the closing surface or already on `<body>`); if the user has
* deliberately moved focus to another control, that choice is left
* alone.
*/
export function restoreFocus(node: HTMLElement): { destroy(): void } {
const trigger =
document.activeElement instanceof HTMLElement
? document.activeElement
: null;
return {
destroy(): void {
const active = document.activeElement;
const lost =
active === null ||
active === document.body ||
node.contains(active);
if (lost) trigger?.focus();
},
};
}