diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-01-15 21:56:10 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-01-15 21:56:10 +0900 |
commit | d1a81a877f0e7364b7d69e214ec74151d76dd25c (patch) | |
tree | fe4d02be637b7ab64615187741269f9c984d73ac /src/shared/utils | |
parent | 03e7e333a3183ea7bf10938c2650b1d74362589f (diff) |
focus on visible elements
Diffstat (limited to 'src/shared/utils')
-rw-r--r-- | src/shared/utils/dom.js | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/shared/utils/dom.js b/src/shared/utils/dom.js index d4fd68a..f138c33 100644 --- a/src/shared/utils/dom.js +++ b/src/shared/utils/dom.js @@ -81,4 +81,26 @@ const viewportRect = (e) => { }; }; -export { isContentEditable, viewportRect }; +const isVisible = (element) => { + let rect = element.getBoundingClientRect(); + if (rect.width === 0 || rect.height === 0) { + return false; + } + if (rect.right < 0 && rect.bottom < 0) { + return false; + } + if (window.innerWidth < rect.left && window.innerHeight < rect.top) { + return false; + } + if (element.nodeName === 'INPUT' && element.type.toLowerCase() === 'hidden') { + return false; + } + + let { display, visibility } = window.getComputedStyle(element); + if (display === 'none' || visibility === 'hidden') { + return false; + } + return true; +}; + +export { isContentEditable, viewportRect, isVisible }; |