Files: 91a7ee50b4fb44a1b2e9f036f697555c912d549d / lib / observe-link-hover.js
810 bytesRaw
1 | var Sustained = require('./sustained') |
2 | var Value = require('mutant/value') |
3 | |
4 | module.exports = function (root, delay) { |
5 | var target = Value() |
6 | root.addEventListener('mouseover', (ev) => { |
7 | var anchor = null |
8 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
9 | if (n.nodeName === 'A') { |
10 | anchor = n |
11 | break |
12 | } |
13 | } |
14 | target.set(anchor) |
15 | }) |
16 | |
17 | root.addEventListener('mouseleave', (ev) => { |
18 | target.set(false) |
19 | }) |
20 | |
21 | var active = Value(false) |
22 | |
23 | var result = Sustained(Sustained(target, 50), delay || 500, (v) => { |
24 | // immediately switch to new hover (with 50 ms delay) if there is a current hover in place |
25 | return v === undefined || (active() && v && v !== result()) |
26 | }) |
27 | |
28 | result.cancel = () => target.set(undefined) |
29 | result.active = active |
30 | return result |
31 | } |
32 |
Built with git-ssb-web