File size: 4,393 Bytes
3df1e9f
 
 
 
22dad9f
19d1d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df1e9f
 
19d1d46
 
 
 
 
 
3df1e9f
 
19d1d46
3df1e9f
 
19d1d46
3df1e9f
19d1d46
3df1e9f
5500bfc
3df1e9f
 
 
 
19d1d46
3df1e9f
19d1d46
3df1e9f
 
 
 
 
22dad9f
3df1e9f
22dad9f
3df1e9f
 
 
 
 
 
 
 
 
22dad9f
3df1e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22dad9f
19d1d46
3df1e9f
 
 
 
 
 
 
 
19d1d46
5500bfc
19d1d46
3df1e9f
 
 
19d1d46
3df1e9f
 
 
 
 
 
 
 
 
 
 
19d1d46
 
3df1e9f
 
9b4caaa
 
3df1e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d1d46
3df1e9f
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<script lang="ts">
	import { fly } from "svelte/transition";
	import { toaster } from "./toaster.svelte.js";
	import { Progress } from "melt/components";
	import Close from "~icons/carbon/close";
	import { omit } from "$lib/utils/object.js";
	import { session } from "$lib/state/session.svelte.js";
	import { AnimationFrames } from "runed";

	let toastHeights = $state<number[]>([]);
	new AnimationFrames(() => {
		const rootEl = document.getElementById(toaster.root.id);
		if (!rootEl) return;

		const toastEls = Array.from(rootEl.querySelectorAll("[data-melt-toaster-toast-content]"));
		toastHeights = toastEls.map(el => el.clientHeight);
		// console.log(toastHeights);
	});

	const isComparing = $derived(session.project.conversations.length > 1);

	const GAP = 8;

	function getToastStyle(i: number) {
		// Remember, the order is reversed! Meaning i=0 was the first toast, so its the last
		// we want to show.
		const n = toaster.toasts.length - i - 1;
		if (n === 0) return "";
		const reversedHeights = toastHeights.toReversed();
		const yHover = -1 * reversedHeights.slice(0, n).reduce((a, b) => a + b + GAP, 0);

		const y = -n * 10;

		return `
			--y-hover: ${yHover}px;
			--y: ${y}px;
		`;
	}

	function getRootStyle() {
		const heightHover = toastHeights.reduce((a, b) => a + b + GAP, 0);
		return `
			--h-hover: ${heightHover}px;
		`;
	}
</script>

<div
	{...omit(toaster.root, "popover")}
	class={["absolute right-2 bottom-23 flex w-[300px] flex-col ", !isComparing && "md:right-0"]}
	style:--toasts={toaster.toasts.length}
	style={getRootStyle()}
>
	{#each toaster.toasts as toast, i (toast.id)}
		<div
			class="flex w-full flex-col justify-center rounded-xl bg-white px-4 py-4 text-left transition dark:bg-gray-800"
			{...toast.content}
			style:--n={toaster.toasts.length - i}
			in:fly={{ y: 20, opacity: 0 }}
			out:fly={{ y: 20 }}
			style={getToastStyle(i)}
		>
			<h3 {...toast.title} class="text-sm font-semibold whitespace-nowrap text-gray-700 dark:text-gray-300">
				{toast.data.title}
			</h3>

			{#if toast.data.description}
				<p {...toast.description} class="max-w-[200px] text-xs text-gray-700 dark:text-gray-300">
					{toast.data.description}
				</p>
			{/if}

			<button
				{...toast.close}
				aria-label="dismiss toast"
				class="absolute top-2 right-2 bg-transparent text-gray-300 hover:text-gray-400 dark:hover:text-gray-100"
			>
				<Close class="size-4" />
			</button>

			{#if toast.closeDelay !== 0}
				<div class="absolute right-4 bottom-4 h-[4px] w-[30px] overflow-hidden rounded-full">
					<Progress value={toast.percentage}>
						{#snippet children(progress)}
							<div {...progress.root} class="relative h-full w-full overflow-hidden bg-gray-200 dark:bg-gray-950">
								<div
									{...progress.progress}
									class="h-full w-full -translate-x-(--progress)"
									class:bg-green-400={toast.data.variant === "success"}
									class:bg-orange-400={toast.data.variant === "warning"}
									class:bg-red-500={toast.data.variant === "error"}
								></div>
							</div>
						{/snippet}
					</Progress>
				</div>
			{/if}
		</div>
	{/each}
</div>

<style>
	:global([popover]) {
		inset: unset;
	}

	[data-melt-toaster-root] {
		--gap: 0.75rem;
		--hover-offset: 0rem;
		/* --toast-height: 4.5rem; */
		--hidden-offset: 0.75rem;

		--hidden-toasts: calc(var(--toasts) - 1);

		overflow: visible;
		gap: 0;
		background: unset;
		padding: 0;

		border: none;
		height: var(--h);
	}

	[data-melt-toaster-root]:hover {
		height: var(--h-hover);
	}

	[data-melt-toaster-toast-content] {
		position: absolute;
		pointer-events: auto;
		bottom: 0;
		left: 0;
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);

		transform-origin: 50% 0%;
		transition: all 350ms ease;

		translate: 0 var(--y);
	}

	:global(.dark [data-melt-toaster-toast-content]) {
		box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.25);
	}

	[data-melt-toaster-toast-content]:nth-last-child(n + 4) {
		z-index: 1;
		scale: 0.925;
		opacity: 0;
	}

	[data-melt-toaster-toast-content]:nth-last-child(-n + 3) {
		z-index: 2;
		scale: 0.95;
	}

	[data-melt-toaster-toast-content]:nth-last-child(-n + 2) {
		z-index: 3;
		scale: 0.975;
	}

	[data-melt-toaster-toast-content]:nth-last-child(-n + 1) {
		z-index: 4;
		scale: 1;
	}

	[data-melt-toaster-root]:hover [data-melt-toaster-toast-content] {
		scale: 1;
		opacity: 1;
		translate: 0 var(--y-hover);
	}
</style>