open-webui/src/lib/components/common/Loader.svelte

47 lines
943 B
Svelte
Raw Normal View History

2024-08-04 14:36:44 +00:00
<script lang="ts">
2024-09-23 22:27:22 +00:00
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
2024-08-04 14:36:44 +00:00
const dispatch = createEventDispatcher();
let loaderElement: HTMLElement;
2024-09-23 22:27:22 +00:00
let observer;
let intervalId;
2024-08-04 14:36:44 +00:00
onMount(() => {
2024-09-23 22:27:22 +00:00
observer = new IntersectionObserver(
2024-08-04 14:36:44 +00:00
(entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
2024-09-23 22:27:22 +00:00
intervalId = setInterval(() => {
dispatch('visible');
}, 100);
// dispatch('visible');
2024-08-04 14:36:44 +00:00
// observer.unobserve(loaderElement); // Stop observing until content is loaded
2024-09-23 22:27:22 +00:00
} else {
clearInterval(intervalId);
2024-08-04 14:36:44 +00:00
}
});
},
{
root: null, // viewport
rootMargin: '0px',
threshold: 0.1 // When 10% of the loader is visible
2024-08-04 14:36:44 +00:00
}
);
observer.observe(loaderElement);
});
2024-09-23 22:27:22 +00:00
onDestroy(() => {
observer.disconnect();
if (intervalId) {
clearInterval(intervalId);
}
});
2024-08-04 14:36:44 +00:00
</script>
<div bind:this={loaderElement}>
<slot />
</div>