Skip to content

Instantly share code, notes, and snippets.

@meirlamdan
Created August 28, 2025 11:14
Show Gist options
  • Select an option

  • Save meirlamdan/c7bbb20a2fefbb4780cda46848c12158 to your computer and use it in GitHub Desktop.

Select an option

Save meirlamdan/c7bbb20a2fefbb4780cda46848c12158 to your computer and use it in GitHub Desktop.
export function showToast(message, type = "success", duration = 3000) {
const id = Date.now();
createToast(id, message, type);
setTimeout(() => {
removeToast(id);
}, duration);
}
const toastContainer = () => {
const existingContainer = document.querySelector(".toast-container");
if (existingContainer) return existingContainer;
const container = document.createElement("div");
container.classList.add("toast-container");
document.body.appendChild(container);
return container;
}
const removeToast = (id) => {
document.querySelector(`[data-id="${id}"]`).remove();
}
const createToast = (id, message, type) => {
const toast = document.createElement("div");
toast.classList.add("toast", type, "show");
toast.dataset.id = id;
toast.innerHTML = `
<span class="icon">${type === "success" ? "✔" : "✖"}</span>
<span class="message">${message}</span>
`;
toastContainer().appendChild(toast);
toast.addEventListener("click", () => removeToast(id));
}
const style = document.createElement("style");
style.textContent = `
.toast-container {
position: fixed;
top: 20px;
right: 20px;
display: flex;
flex-direction: column;
gap: 10px;
z-index: 9999;
font-family: "Segoe UI", Arial, sans - serif;
}
.toast {
display: flex;
align-items: center;
gap: 8px;
min-width: 220px;
max-width: 300px;
padding: 12px 16px;
border-radius: 12px;
color: #333;
font-size: 14px;
font-weight: 500;
background: #fff;
border: 1px solid #e0e0e0;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
opacity: 0;
transform: translateX(100 %);
transition: all 0.4s ease;
}
.toast.show {
opacity: 1;
transform: translateX(0);
}
.toast.success {
border-left: 5px solid #81c784;
background: #f1f8f5;
}
.toast.error {
border-left: 5px solid #e57373;
background: #fdf5f5;
}
.toast.icon {
font-size: 16px;
font-weight: bold;
color: inherit;
}
.toast.success.icon {
color: #388e3c;
}
.toast.error.icon {
color: #c62828;
}
.toast.message {
flex: 1;
line-height: 1.4;
}
`;
document.head.appendChild(style);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment