Skip to content

Instantly share code, notes, and snippets.

@arniebradfo
Last active May 27, 2018 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arniebradfo/31a737d1ca48763fb3576e8f7de211d8 to your computer and use it in GitHub Desktop.
Save arniebradfo/31a737d1ca48763fb3576e8f7de211d8 to your computer and use it in GitHub Desktop.
Detect Tab vs Click Navigation
form button:focus,
.input-tabbing button:focus
{
border: 1px solid DeepPink;
}
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