All files / app-play/src/ui dialog-focus.ts

100% Statements 5/5
100% Branches 7/7
100% Functions 3/3
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                      6x 6x               3x    
// Where the keyboard goes once a player dialog (Options, Help, the Safari install steps) closes: back to the play, not
// to the button that opened the dialog. A focused header button takes Enter as a click, so Enter on the select screen,
// meant to start the chart, opened the dialog again.
 
/** The focus target this releases: the document's active element, as far as it can lose focus. */
export interface FocusableDocument {
  readonly activeElement: object | null;
}
 
/** Takes focus off whatever holds it after a dialog closed (the opener the browser restored it to), so keys reach the play. */
export function returnKeysToPlay(doc: FocusableDocument = document): void {
  const active = doc.activeElement;
  if (active !== null && 'blur' in active && typeof active.blur === 'function') active.blur();
}
 
/** Calls {@link returnKeysToPlay} every time `dialog` closes. */
export function releaseFocusOnClose(
  dialog: { addEventListener(type: 'close', listener: () => void): void },
  doc: FocusableDocument = document,
): void {
  dialog.addEventListener('close', () => returnKeysToPlay(doc));
}