-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
OnDemandRender.svelte
58 lines (52 loc) · 1.47 KB
/
OnDemandRender.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<script lang="ts">
import { performHide } from "store";
import { getContext, onDestroy, onMount } from "svelte";
interface Props {
cssClass?: string;
isVisible?: boolean;
children?: import('svelte').Snippet<[any]>;
}
let { cssClass = "", isVisible = $bindable(false), children }: Props = $props();
let hidingScheduled = $state(false);
const { observe, unobserve } = getContext("observer") as {
observe: (el: Element, callback: (visibility: boolean) => void) => void;
unobserve: (el: Element) => void;
};
function setIsVisible(visibility: boolean) {
if (isVisible != visibility) {
if (visibility) {
isVisible = visibility;
}
}
hidingScheduled = !visibility;
}
onMount(() => {
performHide.subscribe(() => {
if (hidingScheduled) {
isVisible = false;
hidingScheduled = false;
}
});
});
onDestroy(() => {
if (_el) {
unobserve(_el);
}
});
let _el = $state<HTMLDivElement>();
let el = $state<HTMLDivElement>();
$effect(() => {
if (_el != el) {
if (_el) {
unobserve(_el);
}
_el = el;
if (el) {
observe(el, setIsVisible);
}
}
});
</script>
<div class={cssClass} bind:this={el}>
{@render children?.({ isVisible, })}
</div>