This repository was archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlightTimer.js
More file actions
53 lines (53 loc) · 1.4 KB
/
Copy pathhighlightTimer.js
File metadata and controls
53 lines (53 loc) · 1.4 KB
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
/**
* A delayed highlight setter
*
* @exports highlightTimer
* @kind function
*
* @example
* import {highlightTimer} from '@datawrapper/shared';
* const myTimer = highlightTimer(value => {
* if (value) {
* selection.style('opacity', d => d === value ? 1 : 0.3);
* } else {
* selection.style('opacity', 1);
* }
* });
*
* lines.on('mouseenter', d => myTimer.set(d));
* chart.on('mouseleave', myTimer.clear);
*
* @param {function} action - the highlight action callback
* @param {int} delay - how long something needs to be highlighted before
* the highlight triggers (in milliseconds)
* @returns {object}
*/
export default function highlightTimer(action, delay = 700) {
let setTimer;
let highlighted = false;
let inactive = true;
return {
set(value) {
if (highlighted !== value) {
if (setTimer) clearTimeout(setTimer);
setTimeout(
() => {
inactive = false;
action(highlighted);
},
inactive ? delay : 0
);
}
highlighted = value;
},
clear() {
highlighted = null;
inactive = true;
if (setTimer) clearTimeout(setTimer);
action(null);
},
get() {
return highlighted;
}
};
}