-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle keyboard-dependent focus outlines in Vuex #4847
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this was probably just copied over during the port so fine to leave as-is. However heads-up that we have two throttling libraries already included:
|
||
|
||
document.body.addEventListener( | ||
'focus', | ||
e => { | ||
if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) { | ||
kolibriGlobal.coreVue.vuex.store.commit('SET_MODALITY', 'keyboard'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we can't just import the store here, rather than referencing the global? |
||
} | ||
}, | ||
true | ||
); | ||
|
||
document.body.addEventListener( | ||
'blur', | ||
() => { | ||
kolibriGlobal.coreVue.vuex.store.commit('SET_MODALITY', null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above. |
||
}, | ||
true | ||
); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this function here? Presumably the Keen-UI helper is still doing this part?