Last active
May 27, 2018 23:18
-
-
Save arniebradfo/31a737d1ca48763fb3576e8f7de211d8 to your computer and use it in GitHub Desktop.
Detect Tab vs Click Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class TabDetectionService { | |
private isTabbing: boolean = false; | |
private _element: HTMLElement = document.body; | |
private _endTabbingRef = this._endTabbing.bind(this); | |
private _startTabbingRef = this._startTabbing.bind(this); | |
constructor() { | |
this._endTabbing(); | |
} | |
private _endTabbing() { | |
document.removeEventListener('mousedown', this._endTabbingRef, true); | |
document.addEventListener('keydown', this._startTabbingRef, true); | |
this._element.classList.remove('input-tabbing'); | |
this.isTabbing = false; | |
} | |
private _startTabbing(event: KeyboardEvent) { | |
if (event.which !== 9) // 9 is tab, 32 is spacebar, 13 is return | |
return; | |
document.addEventListener('mousedown', this._endTabbingRef, true); | |
document.removeEventListener('keydown', this._startTabbingRef, true); | |
this._element.classList.add('input-tabbing'); | |
this.isTabbing = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment