All files / src/dom accessible-outline.ts

0% Statements 0/19
100% Branches 0/0
0% Functions 0/8
0% Lines 0/19

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                                                                                                                                                                                                 
import * as func from '../func/func';
 
enum AccessibleOutlineInputTypes {
  MOUSE = 'mouse',
  KEYBOARD = 'keyboard',
}
 
/**
 * A class that helps with enabling outline on a page ONLY when the user
 * is interacting with the keyboard.
 *
 *
 * Note it's recommended you use:
 * http://css-tricks.com/the-focus-visible-trick/
 * instead of this class now.
 *
 * It is common to do outline: none to disable button or inputs for
 * aesthetic reasons but this not good for accessibility.  To address the
 * issue, this class will apply outline: none only in the event,
 * the user is interacting with the keyboard.
 *
 *
 * Common usage is to instantiate this class with the body element.
 * This will add a data-current-input to the body element will indicates whether
 * the user is engaged in mouse or keyboard mode.
 *
 * ```ts
 *
 * new AccessibleOutline(document.body);
 * ```
 *
 * ```sass
 * [data-current-input='mouse'] *:focus
 *    outline: none
 * ```
 *
 *
 */
export class AccessibleOutline {
  public element: HTMLElement;
  private listenerDisposers: Array<Function>;
 
  constructor(element: HTMLElement) {
    this.element = element;
 
    this.listenerDisposers = [];
 
    // Set it so that we only run the setCurrentInput when param properties
    // change from previous to avoid layout thrashing.
    this.setCurrentInput = func.runOnceOnChange(
      this.setCurrentInput.bind(this)
    );
 
    // 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));
  }
 
  setKeyboard() {
    this.setCurrentInput(AccessibleOutlineInputTypes.KEYBOARD);
  }
 
  setMouse() {
    this.setCurrentInput(AccessibleOutlineInputTypes.MOUSE);
  }
 
  addEvents(name: string, callback: EventListener) {
    document.addEventListener(name, callback);
    this.listenerDisposers.push(() => {
      document.removeEventListener(name, callback);
    });
  }
 
  /**
   * Sets the current input type by updating the data attribute on the
   * root element.
   * @param type
   */
  setCurrentInput(type: AccessibleOutlineInputTypes) {
    this.element.setAttribute('data-current-input', type);
  }
 
  dispose() {
    this.listenerDisposers.forEach((disposer) => {
      disposer();
    });
  }
}