n8n/packages/editor-ui/src/components/IntersectionObserver.vue
Alex Grozav 7cd45885bf
fix: Fix tags overflow handler in workflows header (#6784)
* fix: fix tags container overflowing

* fix: fix intersection observer error
2023-07-31 15:17:05 +03:00

73 lines
1.3 KiB
Vue

<template>
<div ref="root">
<slot></slot>
</div>
</template>
<script lang="ts">
import type { PropType } from 'vue';
import { defineComponent } from 'vue';
import type { EventBus } from 'n8n-design-system/utils';
import { createEventBus } from 'n8n-design-system/utils';
export default defineComponent({
name: 'IntersectionObserver',
props: {
threshold: {
type: Number,
default: 0,
},
enabled: {
type: Boolean,
default: false,
},
eventBus: {
type: Object as PropType<EventBus>,
default: () => createEventBus(),
},
},
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.observer = observer;
this.eventBus.on('observe', (observed: Element) => {
if (observed) {
observer.observe(observed);
}
});
this.eventBus.on('unobserve', (observed: Element) => {
observer.unobserve(observed);
});
},
beforeUnmount() {
if (this.enabled) {
this.observer.disconnect();
}
},
});
</script>