File size: 1,382 Bytes
421ce25
98051f8
76725c9
421ce25
a1a6daf
 
 
421ce25
 
a1a6daf
99ce0c3
 
a1a6daf
 
76725c9
 
421ce25
 
 
 
76725c9
 
 
 
99ce0c3
 
 
 
 
 
 
 
 
a1a6daf
99ce0c3
 
a1a6daf
421ce25
 
 
 
284d5a8
a1a6daf
6a0861b
45dbaaf
421ce25
 
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
<script lang="ts">
	import { fade } from "svelte/transition";
	import IconChevron from "./icons/IconChevron.svelte";

	interface Props {
		scrollNode: HTMLElement;
		class?: string;
	}

	let { scrollNode, class: className = "" }: Props = $props();

	let visible = $state(false);
	let observer: ResizeObserver | null = $state(null);

	function updateVisibility() {
		if (!scrollNode) return;
		visible =
			Math.ceil(scrollNode.scrollTop) + 200 < scrollNode.scrollHeight - scrollNode.clientHeight;
	}

	function destroy() {
		observer?.disconnect();
		scrollNode?.removeEventListener("scroll", updateVisibility);
	}
	const cleanup = $effect.root(() => {
		$effect(() => {
			if (scrollNode) {
				if (window.ResizeObserver) {
					observer = new ResizeObserver(() => updateVisibility());
					observer.observe(scrollNode);
					cleanup();
				}
				scrollNode?.addEventListener("scroll", updateVisibility);
			}
		});
		return () => destroy();
	});
</script>

{#if visible}
	<button
		transition:fade={{ duration: 150 }}
		onclick={() => scrollNode.scrollTo({ top: scrollNode.scrollHeight, behavior: "smooth" })}
		class="btn absolute flex h-[41px] w-[41px] rounded-full border bg-white shadow-md transition-all hover:bg-gray-100 dark:border-gray-600 dark:bg-gray-700 dark:shadow-gray-950 dark:hover:bg-gray-600 {className}"
		><IconChevron classNames="mt-[2px]" /></button
	>
{/if}