Skip to content

Commit

Permalink
Added Delayed Render component
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Dec 17, 2024
1 parent d2dc1be commit bee2ecc
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script lang="ts">
import type { Snippet } from 'svelte';
interface Props {
children: Snippet;
delay?: number;
visible: boolean;
}
let { children, delay = 50, visible = true }: Props = $props();
let shouldRender = $state(false);
let timeout: ReturnType<typeof setTimeout>;
$effect(() => {
if (visible) {
timeout = setTimeout(() => {
shouldRender = true;
}, delay);
} else {
shouldRender = false;
clearTimeout(timeout);
}
return () => clearTimeout(timeout);
});
</script>

{#if shouldRender && children}
{@render children()}
{/if}

0 comments on commit bee2ecc

Please sign in to comment.