mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 17:14:05 -08:00
57 lines
962 B
Vue
57 lines
962 B
Vue
|
<template>
|
||
|
<div ref="root">
|
||
|
<slot></slot>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
|
||
|
<script lang="ts">
|
||
|
|
||
|
import Vue from 'vue';
|
||
|
|
||
|
export default Vue.extend({
|
||
|
name: 'IntersectionObserver',
|
||
|
props: ['threshold', 'enabled'],
|
||
|
data() {
|
||
|
return {
|
||
|
observer: null,
|
||
|
};
|
||
|
},
|
||
|
mounted() {
|
||
|
if (!this.$props.enabled) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
const options = {
|
||
|
root: this.$refs.root as Element,
|
||
|
rootMargin: '0px',
|
||
|
threshold: this.$props.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.$props.enabled) {
|
||
|
this.$data.observer.disconnect();
|
||
|
}
|
||
|
},
|
||
|
});
|
||
|
</script>
|