forked from julkue/flying-focus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flying-focus.js
151 lines (128 loc) · 3.82 KB
/
flying-focus.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
(function() {
if (!window.addEventListener) {
return;
}
'use strict';
var DURATION = 150;
var ringElem = null;
var movingId = 0;
var prevFocused = null;
var keyDownTime = 0;
var win = window;
var doc = document;
var docElem = doc.documentElement;
var body = doc.body;
docElem.addEventListener('keydown', function(event) {
var code = event.which;
// Show animation only upon Tab or Arrow keys press.
if (code === 9 || (code > 36 && code < 41)) {
keyDownTime = Date.now();
}
}, false);
docElem.addEventListener('mousedown', function(event) {
if(ringElem) {
onEnd();
}
}, false);
docElem.addEventListener('focus', function(event) {
var target = event.target;
if (target.id === 'flying-focus') {
return;
}
var isFirstFocus = false;
if (!ringElem) {
isFirstFocus = true;
initialize();
}
var offset = offsetOf(target);
ringElem.style.left = offset.left + 'px';
ringElem.style.top = offset.top + 'px';
ringElem.style.width = target.offsetWidth + 'px';
ringElem.style.height = target.offsetHeight + 'px';
if(isFirstFocus){
return;
}
if (!isJustPressed()) {
onEnd();
return;
}
onEnd(true);
target.classList.add('flying-focus_target');
ringElem.classList.add('flying-focus_visible');
prevFocused = target;
//movingId = setTimeout(onEnd, DURATION);
}, true);
docElem.addEventListener('blur', function() {
onEnd(true);
}, true);
function initialize() {
ringElem = doc.createElement('flying-focus'); // use uniq element name to decrease the chances of a conflict with website styles
ringElem.id = 'flying-focus';
ringElem.style.transitionDuration = ringElem.style.WebkitTransitionDuration = DURATION / 1000 + 's';
body.appendChild(ringElem);
}
function onEnd(onlyIf) {
if (onlyIf && !movingId) {
return;
}
if(movingId){
clearTimeout(movingId);
movingId = 0;
}
ringElem.classList.remove('flying-focus_visible');
if(prevFocused){
prevFocused.classList.remove('flying-focus_target');
prevFocused = null;
}
}
function isJustPressed() {
return Date.now() - keyDownTime < 42
}
function offsetOf(elem) {
var rect = elem.getBoundingClientRect();
var clientLeft = docElem.clientLeft || body.clientLeft;
var clientTop = docElem.clientTop || body.clientTop;
var scrollLeft = win.pageXOffset || docElem.scrollLeft || body.scrollLeft;
var scrollTop = win.pageYOffset || docElem.scrollTop || body.scrollTop;
var left = rect.left + scrollLeft - clientLeft;
var top = rect.top + scrollTop - clientTop;
return {
top: top || 0,
left: left || 0
};
}
var style = doc.createElement('style');
style.textContent = "#flying-focus {\
position: absolute;\
margin: 0;\
background: transparent;\
-webkit-transition-property: left, top, width, height;\
transition-property: left, top, width, height;\
-webkit-transition-timing-function: cubic-bezier(0,1,0,1);\
transition-timing-function: cubic-bezier(0,1,0,1);\
visibility: hidden;\
pointer-events: none;\
box-shadow: 0 0 2px 3px #eb0000, 0 0 2px #eb0000 inset; border-radius: 2px;\
}\
#flying-focus.flying-focus_visible {\
visibility: visible;\
z-index: 9999;\
}\
.flying-focus_target {\
outline: none !important; /* Doesn't work in Firefox :( */\
}\
/* http://stackoverflow.com/questions/71074/how-to-remove-firefoxs-dotted-outline-on-buttons-as-well-as-links/199319 */\
.flying-focus_target::-moz-focus-inner {\
border: 0 !important;\
}\
/* Replace it with @supports rule when browsers catch up */\
@media screen and (-webkit-min-device-pixel-ratio: 0) {\
#flying-focus {\
box-shadow: none;\
outline: 5px auto -webkit-focus-ring-color;\
outline-offset: -3px;\
}\
}\
";
body.appendChild(style);
})();