diff options
author | Kushal Pandya <kushalspandya@gmail.com> | 2019-08-07 20:05:46 +0530 |
---|---|---|
committer | Kushal Pandya <kushalspandya@gmail.com> | 2019-08-19 14:28:39 +0530 |
commit | 6f1985833eab65a90d885b53026af1d694aae628 (patch) | |
tree | 68e1402f6b734c0d136e643140030e44a37decb7 /app | |
parent | 224db2f8901964a34851018dd93b962a45a3032f (diff) | |
download | gitlab-ce-6f1985833eab65a90d885b53026af1d694aae628.tar.gz |
Add `autofocus` directive for input elementskp-add-vue-input-autofocus-directive
Diffstat (limited to 'app')
-rw-r--r-- | app/assets/javascripts/vue_shared/directives/autofocusonshow.js | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/app/assets/javascripts/vue_shared/directives/autofocusonshow.js b/app/assets/javascripts/vue_shared/directives/autofocusonshow.js new file mode 100644 index 00000000000..4659ec20ceb --- /dev/null +++ b/app/assets/javascripts/vue_shared/directives/autofocusonshow.js @@ -0,0 +1,39 @@ +/** + * Input/Textarea Autofocus Directive for Vue + */ +export default { + /** + * Set focus when element is rendered, but + * is not visible, using IntersectionObserver + * + * @param {Element} el Target element + */ + inserted(el) { + if ('IntersectionObserver' in window) { + // Element visibility is dynamic, so we attach observer + el.visibilityObserver = new IntersectionObserver(entries => { + entries.forEach(entry => { + // Combining `intersectionRatio > 0` and + // element's `offsetParent` presence will + // deteremine if element is truely visible + if (entry.intersectionRatio > 0 && entry.target.offsetParent) { + entry.target.focus(); + } + }); + }); + + // Bind the observer. + el.visibilityObserver.observe(el, { root: document.documentElement }); + } + }, + /** + * Detach observer on unbind hook. + * + * @param {Element} el Target element + */ + unbind(el) { + if (el.visibilityObserver) { + el.visibilityObserver.disconnect(); + } + }, +}; |