All files / src/dom keyboard-navigation-watcher.ts

0% Statements 0/31
0% Branches 0/4
0% Functions 0/10
0% Lines 0/31

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
import {
  type EventCallback,
  type EventDispatcher,
  EventManager,
} from '../ui/events';
 
export enum KeyboardNavigationEvents {
  ANY_CHANGE = 'any',
  MOUSE = 'mouse',
  KEYBOARD = 'keyboard',
}
 
/**
 * A class that monitors mouse versus keyboard navigation on the page.
 *
 * Example:
 * ```
 * import {KeyboardNavigationEvents, KeyboardNavigationWatcher} from '@blinkk/degu/lib/dom/keyboard-navigation-watcher';
 *
 * const watcher = new KeyboardNavigationWatcher();
 *
 * watcher.on(KeyboardNavigationEvents.MOUSE, ()=> {
 *    // user switched to mouse
 * })
 *
 * watcher.on(KeyboardNavigationEvents.KEYBOARD, ()=> {
 *    // user switched to keyboard
 * })
 *
 * watcher.on(KeyboardNavigationEvents.ANY_CHANGE, ()=> {
 *     //  *  console.log(watcher.state)
 * })
 *
 * watcher.dispose();
 * ```
 */
export class KeyboardNavigationWatcher implements EventDispatcher {
  private listenerDisposers: Array<Function>;
  private eventManager: EventManager;
  public state: KeyboardNavigationEvents | null = null;
 
  constructor() {
    this.eventManager = new EventManager();
 
    this.listenerDisposers = [];
 
    // Mouse related events.
    this.addEvents('mousemove', this.setMouse.bind(this));
    this.addEvents('mousedown', this.setMouse.bind(this));
    this.addEvents('mouseup', this.setMouse.bind(this));
    this.addEvents('touchmove', this.setMouse.bind(this));
    this.addEvents('touchstart', this.setMouse.bind(this));
    this.addEvents('touchend', this.setMouse.bind(this));
 
    // Keyboard
    this.addEvents('keyup', this.setKeyboard.bind(this));
    this.addEvents('keydown', this.setKeyboard.bind(this));
  }
 
  on(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  off(event: string, callback: EventCallback) {
    this.eventManager.on(event, callback);
  }
 
  private setKeyboard() {
    if (this.state !== KeyboardNavigationEvents.KEYBOARD) {
      this.state = KeyboardNavigationEvents.KEYBOARD;
      this.eventManager.dispatch(KeyboardNavigationEvents.KEYBOARD);
      this.eventManager.dispatch(KeyboardNavigationEvents.ANY_CHANGE);
    }
  }
 
  private setMouse() {
    if (this.state !== KeyboardNavigationEvents.MOUSE) {
      this.state = KeyboardNavigationEvents.MOUSE;
      this.eventManager.dispatch(KeyboardNavigationEvents.MOUSE);
      this.eventManager.dispatch(KeyboardNavigationEvents.ANY_CHANGE);
    }
  }
 
  private addEvents(name: string, callback: EventListener) {
    document.addEventListener(name, callback);
    this.listenerDisposers.push(() => {
      document.removeEventListener(name, callback);
    });
  }
 
  dispose() {
    this.eventManager.dispose();
    this.listenerDisposers.forEach((disposer) => {
      disposer();
    });
  }
}