Skip to content

Instantly share code, notes, and snippets.

@johnie
Created October 4, 2023 13:27
Show Gist options
  • Save johnie/45143c2ad7890339db3fcc359e988648 to your computer and use it in GitHub Desktop.
Save johnie/45143c2ad7890339db3fcc359e988648 to your computer and use it in GitHub Desktop.
export const showErrorToast = (message: string) => {
const toastTemplate = document.getElementById('toast-template') as HTMLTemplateElement;
if (!toastTemplate) return;
const toastClone = document.importNode(toastTemplate.content, true);
document.body.appendChild(toastClone);
const newToast = document.body.lastElementChild as HTMLElement;
if (!newToast) return;
const messageElement = newToast.querySelector('.toast');
if (!messageElement) return;
messageElement.textContent = message;
setTimeout(() => {
newToast.classList.add('fade-in');
}, 10);
setTimeout(() => {
newToast.remove();
}, 3000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment