-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfocus-fix.js
24 lines (20 loc) · 1003 Bytes
/
focus-fix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* focus-fix | https://github.com/wilddeer/focus-fix/ | CC0 */
(function() {
var mouseFocusedClass = 'is-mouse-focused';
document.body.addEventListener('mousedown', function() {
//wait for `document.activeElement` to change
setTimeout(function() {
//find focused element
var activeElement = document.activeElement;
//if found and it's not body...
if (activeElement && activeElement !== document.body) {
//add special class, remove it after `blur`
activeElement.className += ' ' + mouseFocusedClass;
activeElement.addEventListener('blur', function(e) {
e.target.removeEventListener(e.type, arguments.callee);
activeElement.className = activeElement.className.replace(new RegExp('(\\s+|^)'+mouseFocusedClass+'(\\s+|$)', 'g'), ' ').replace(/^\s+|\s+$/g, '');
});
}
}, 0);
});
})();