2019-12-04 20:38:33 +05:30
|
|
|
/**
|
|
|
|
* 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
|
2021-03-08 18:12:59 +05:30
|
|
|
el.visibilityObserver = new IntersectionObserver((entries) => {
|
|
|
|
entries.forEach((entry) => {
|
2019-12-04 20:38:33 +05:30
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|