accessible-focus.js 618 B

12345678910111213141516171819
  1. var keyboardNavigation = false,
  2. keyboardNavigationKeycodes = [ 9, 32, 37, 38, 39, 40 ]; // keyCodes for tab, space, left, up, right, down respectively
  3. document.addEventListener( 'keydown', function( event ) {
  4. if ( keyboardNavigation ) {
  5. return;
  6. }
  7. if ( keyboardNavigationKeycodes.indexOf( event.keyCode ) !== -1 ) {
  8. keyboardNavigation = true;
  9. document.documentElement.classList.add( 'accessible-focus' );
  10. }
  11. } );
  12. document.addEventListener( 'mouseup', function() {
  13. if ( ! keyboardNavigation ) {
  14. return;
  15. }
  16. keyboardNavigation = false;
  17. document.documentElement.classList.remove( 'accessible-focus' );
  18. } );