Prompt
simple javascript class to display a toast.
white text, dark green backdrop.
display for 1 second, then close
Code
class Toast {
static show(message, duration = 1000) {
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = message;
document.body.appendChild(toast);
// Force reflow to trigger transition
void toast.offsetWidth;
toast.classList.add('show');
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300); // allow fade-out transition
}, duration);
}
}
// Example usage:
Toast.show('Saved!')
.toast {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background-color: #064420; /* dark green */
color: white;
padding: 10px 20px;
border-radius: 8px;
font-family: sans-serif;
opacity: 0;
transition: opacity 0.3s ease;
pointer-events: none;
z-index: 10000;
}
.toast.show {
opacity: 1;
}