Skip to content

Instantly share code, notes, and snippets.

@superRaytin
Created December 19, 2022 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superRaytin/d06faea2072dc1836e3d2558cdfc942a to your computer and use it in GitHub Desktop.
Save superRaytin/d06faea2072dc1836e3d2558cdfc942a to your computer and use it in GitHub Desktop.
Simple Toast
const wrapStyle = `display: flex; justify-content: center; padding: 2rem 1.5rem; font-size: 1.4rem; color: #fff; background: rgba(0, 0, 0, 0.7); border-radius: 1.6rem; position: fixed; top: 50%; left: 50%; z-index: var(--dxm-toast-z-index); transform: translate3d(-50%, -50%, 1px);`;
const contentStyle = `display: flex; flex-direction: column; align-items: center; text-align: center;`;
const simpleToast = {
info: (content, duration = 2) => {
if (window.simpleToastTimer) {
clearTimeout(window.simpleToastTimer);
}
const toastEleId = '__simple_toast';
const toastInnerHTML = `<div style="${wrapStyle}"><div style="${contentStyle}">${content}</div></div>`;
const toastEl = document.getElementById(toastEleId);
if (toastEl) {
toastEl.innerHTML = toastInnerHTML;
} else {
const newToastEl = document.createElement('div');
newToastEl.setAttribute('id', toastEleId);
newToastEl.innerHTML = toastInnerHTML;
document.body.appendChild(newToastEl);
}
window.simpleToastTimer = setTimeout(() => {
const curToastEl = document.getElementById(toastEleId);
if (curToastEl) {
curToastEl.parentNode.removeChild(curToastEl);
}
}, duration * 1000);
},
};
export default simpleToast;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment