From c1153f19e6323bbc76145d3defe0bca6b2b6088e Mon Sep 17 00:00:00 2001 From: 0neGal Date: Tue, 8 Feb 2022 22:36:25 +0100 Subject: added toasts No functional toasts yet, just the code for them, I'll add the actual toasts later... --- src/app/toast.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/app/toast.js (limited to 'src/app/toast.js') diff --git a/src/app/toast.js b/src/app/toast.js new file mode 100644 index 0000000..8d78e37 --- /dev/null +++ b/src/app/toast.js @@ -0,0 +1,58 @@ +function Toast(properties) { + let toast = { + fg: "#000000", + bg: "#FFFFFF", + timeout: 3000, + title: "Untitled Toast", + description: "No description provided for toast", + ...properties + } + + switch(toast.scheme) { + case "error": + toast.fg = "#FFFFFF"; + toast.bg = "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(); + let el = document.createElement("div"); + + el.classList.add("toast"); + + el.style.color = toast.fg; + el.style.background = toast.bg; + + el.id = id; + el.setAttribute("onclick", `dismissToast(${id})`); + + el.innerHTML = ` +
${toast.title}
+
${toast.description}
+ ` + + 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) + } +} -- cgit v1.2.3 From 0813769008f5436e1cea2ad97f1a527c8ce94df7 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Tue, 8 Feb 2022 23:08:13 +0100 Subject: two toasts can now be added at the same time There used to be an error where if you spawned two toasts simultaneously they'll get the same ID, this fixes that by upping the ID by one if the ID is already taken. --- src/app/toast.js | 1 + 1 file changed, 1 insertion(+) (limited to 'src/app/toast.js') diff --git a/src/app/toast.js b/src/app/toast.js index 8d78e37..2a8555e 100644 --- a/src/app/toast.js +++ b/src/app/toast.js @@ -25,6 +25,7 @@ function Toast(properties) { let id = Date.now(); + if (document.getElementById(id)) {id = id + 1} let el = document.createElement("div"); el.classList.add("toast"); -- cgit v1.2.3