42_ft_transcendence/frontend/static/js/utils/noticeUtils.js
2024-03-31 10:59:40 +02:00

26 lines
922 B
JavaScript

export function createNotification(text, timer = 3000) {
if (!createNotification.templateToast) {
createNotification.templateToast = new DOMParser().parseFromString(`
<div class='toast' role='alert' data-bs-delay='${timer}'>
<div class='toast-header'>
<strong class='me-auto'>Notification</strong>
<button type='button' class='btn-close' data-bs-dismiss='toast'></button>
</div>
<div class='toast-body'>
</div>
</div>
`, 'text/html')
.querySelector('body')
.firstChild;
}
const toastElement = createNotification.templateToast.cloneNode(true);
toastElement.getElementsByClassName('toast-body')[0].innerHTML = text;
toastElement.addEventListener('hidden.bs.toast', e => e.target.remove());
new bootstrap.Toast(toastElement).show();
const toastContainer = document.getElementById('toastContainer');
toastContainer.insertBefore(toastElement, toastContainer.firstChild);
}