Created
December 19, 2022 06:56
-
-
Save superRaytin/d06faea2072dc1836e3d2558cdfc942a to your computer and use it in GitHub Desktop.
Simple Toast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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