n8n/packages/editor-ui/src/components/IntersectionObserver.vue
Alex Grozav 9c94050deb
feat: Replace Vue.extend with defineComponent in editor-ui (no-changelog) (#6033)
* refactor: replace Vue.extend with defineComponent in editor-ui

* fix: change $externalHooks extractions from mixins

* fix: refactor externalHooks mixin
2023-04-21 18:51:08 +03:00

55 lines
961 B
Vue

<template>
<div ref="root">
<slot></slot>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'IntersectionObserver',
props: ['threshold', 'enabled'],
data() {
return {
observer: null,
};
},
mounted() {
if (!this.enabled) {
return;
}
const options = {
root: this.$refs.root as Element,
rootMargin: '0px',
threshold: this.threshold,
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(({ target, isIntersecting }) => {
this.$emit('observed', {
el: target,
isIntersecting,
});
});
}, options);
this.$data.observer = observer;
this.$on('observe', (observed: Element) => {
observer.observe(observed);
});
this.$on('unobserve', (observed: Element) => {
observer.unobserve(observed);
});
},
beforeDestroy() {
if (this.enabled) {
this.$data.observer.disconnect();
}
},
});
</script>