-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4847 from jonboiser/focus-styles
Handle keyboard-dependent focus outlines in Vuex
- Loading branch information
Showing
8 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* global kolibriGlobal */ | ||
/** | ||
* Adapted from https://github.com/alice/modality | ||
* Version: 1.0.2 | ||
* | ||
* For Kolibri, this mirrors in Vuex the DOM updates done by modality.js in Keen-UI, | ||
* which gives the code in theme.js easier access to the current modality. | ||
*/ | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
let hadKeyboardEvent = false; | ||
const keyboardModalityWhitelist = [ | ||
'input:not([type])', | ||
'input[type=text]', | ||
'input[type=number]', | ||
'input[type=date]', | ||
'input[type=time]', | ||
'input[type=datetime]', | ||
'textarea', | ||
'[role=textbox]', | ||
'[supports-modality=keyboard]', | ||
].join(','); | ||
|
||
let isHandlingKeyboardThrottle; | ||
|
||
const matcher = (() => { | ||
const el = document.body; | ||
|
||
if (el.matchesSelector) { | ||
return el.matchesSelector; | ||
} | ||
|
||
if (el.webkitMatchesSelector) { | ||
return el.webkitMatchesSelector; | ||
} | ||
|
||
if (el.mozMatchesSelector) { | ||
return el.mozMatchesSelector; | ||
} | ||
|
||
if (el.msMatchesSelector) { | ||
return el.msMatchesSelector; | ||
} | ||
|
||
/* eslint-disable-next-line */ | ||
console.error("Couldn't find any matchesSelector method on document.body."); | ||
})(); | ||
|
||
const disableFocusRingByDefault = function() { | ||
const css = 'body:not([modality=keyboard]) :focus { outline: none; }'; | ||
const head = document.head || document.getElementsByTagName('head')[0]; | ||
const style = document.createElement('style'); | ||
|
||
style.type = 'text/css'; | ||
style.id = 'disable-focus-ring'; | ||
|
||
if (style.styleSheet) { | ||
style.styleSheet.cssText = css; | ||
} else { | ||
style.appendChild(document.createTextNode(css)); | ||
} | ||
|
||
head.insertBefore(style, head.firstChild); | ||
}; | ||
|
||
const focusTriggersKeyboardModality = function(el) { | ||
let triggers = false; | ||
|
||
if (matcher) { | ||
triggers = | ||
matcher.call(el, keyboardModalityWhitelist) && matcher.call(el, ':not([readonly])'); | ||
} | ||
|
||
return triggers; | ||
}; | ||
|
||
disableFocusRingByDefault(); | ||
|
||
document.body.addEventListener( | ||
'keydown', | ||
() => { | ||
hadKeyboardEvent = true; | ||
|
||
if (isHandlingKeyboardThrottle) { | ||
clearTimeout(isHandlingKeyboardThrottle); | ||
} | ||
|
||
isHandlingKeyboardThrottle = setTimeout(() => { | ||
hadKeyboardEvent = false; | ||
}, 100); | ||
}, | ||
true | ||
); | ||
|
||
document.body.addEventListener( | ||
'focus', | ||
e => { | ||
if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) { | ||
kolibriGlobal.coreVue.vuex.store.commit('SET_MODALITY', 'keyboard'); | ||
} | ||
}, | ||
true | ||
); | ||
|
||
document.body.addEventListener( | ||
'blur', | ||
() => { | ||
kolibriGlobal.coreVue.vuex.store.commit('SET_MODALITY', null); | ||
}, | ||
true | ||
); | ||
}); |
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
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
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
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
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
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