Files: 55fc93a9190c25f467ead205ab8d676b5191dbd4 / lib / anchor-hook.js
799 bytesRaw
1 | const watch = require('mutant/watch') |
2 | |
3 | module.exports = AnchorHook |
4 | |
5 | function AnchorHook (name, current, cb) { |
6 | return function (element) { |
7 | return watch(current, (current) => { |
8 | if (current === name || (current && current.anchor === name)) { |
9 | element.classList.add('-anchor') |
10 | window.requestAnimationFrame(() => { |
11 | element.scrollIntoView() |
12 | if (typeof cb === 'function') cb(element) |
13 | |
14 | // HACK: due to a browser bug, sometimes the window scrolls down below the top bar |
15 | // Why not just hack it!!! |
16 | const topBar = document.querySelector('.MainWindow > div.top') |
17 | if (topBar) { |
18 | topBar.scrollIntoViewIfNeeded() |
19 | } |
20 | }) |
21 | } else { |
22 | element.classList.remove('-anchor') |
23 | } |
24 | }) |
25 | } |
26 | } |
27 |
Built with git-ssb-web