aboutsummaryrefslogtreecommitdiff
path: root/src/app/js/toast.js
blob: 501bf423bd032eddc52f776ad21bd389b4f65d84 (plain)
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
function Toast(properties) {
	let toast = {
		timeout: 3000,
		fg: "#FFFFFF",
		bg: "var(--selbg)",
		callback: () => {},
		title: "Untitled Toast",
		description: "No description provided for toast",
		...properties
	}

	switch(toast.scheme) {
		case "error":
			toast.fg = "#FFFFFF";
			toast.bg = "rgb(var(--red))";
			break
		case "success":
			toast.fg = "#FFFFFF";
			toast.bg = "#60D394";
			break
		case "warning":
			toast.fg = "#FFFFFF";
			toast.bg = "#FF9B85";
			break
	}


	let id = Date.now();
	if (document.getElementById(id)) {id = id + 1}
	let el = document.createElement("div");

	el.classList.add("toast");

	el.style.color = toast.fg;
	el.style.background = toast.bg;

	el.id = id;
	el.addEventListener("click", () => {
		dismissToast(id);
		toast.callback();
	})

	el.innerHTML = `
		<div class="title">${toast.title}</div>
		<div class="description">${toast.description}</div>
	`

	if (! toast.title) {
		el.querySelector(".title").remove();
	}

	if (! toast.description) {
		el.querySelector(".description").remove();
	}

	toasts.appendChild(el);

	setTimeout(() => {
		dismissToast(id);
	}, toast.timeout)
}

function dismissToast(id) {
	id = document.getElementById(id);
	if (id) {
		id.classList.add("hidden");
		setTimeout(() => {
			id.remove();
		}, 500)
	}
}

ipcRenderer.on("toast", (_, properties) => {
	Toast(properties);
})