Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions src/BootstrapBlazor/Components/Toast/Toast.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,69 @@ import EventHandler from "../../modules/event-handler.js"

export function init(id, invoke, callback) {
const el = document.getElementById(id)
const progressElement = el.querySelector('.toast-progress')
const toast = {
element: el,
invoke,
callback,
toast: new bootstrap.Toast(el),
showProgress: () => {
return toast.toast._config.autohide
}
},
progressElement
}
Data.set(id, toast);

if (toast.showProgress()) {
toast.progressElement = toast.element.querySelector('.toast-progress')
const delay = toast.toast._config.delay / 1000
toast.progressElement.style.transition = `width linear ${delay}s`
progressElement.style.transition = `width linear ${delay}s`
}

EventHandler.on(toast.element, 'shown.bs.toast', () => {
EventHandler.on(el, 'shown.bs.toast', () => {
if (toast.showProgress()) {
toast.progressElement.style.width = '100%'
progressElement.style.width = '100%'
}
})
EventHandler.on(toast.element, 'hidden.bs.toast', () => {
toast.invoke.invokeMethodAsync(toast.callback)
EventHandler.on(el, 'hidden.bs.toast', () => {
invoke.invokeMethodAsync(toast.callback)
})
toast.toast.show()
EventHandler.on(progressElement, 'transitionend', e => {
toast.toast.hide();
});
Comment on lines +32 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Filter transitionend by property name

Guard with if (e.propertyName !== 'width') return; so only the width transition triggers hide and other transitions won’t call it.

Suggested change
EventHandler.on(progressElement, 'transitionend', e => {
toast.toast.hide();
});
EventHandler.on(progressElement, 'transitionend', e => {
if (e.propertyName !== 'width') return;
toast.toast.hide();
});


toast.toast.show();
}

export function update(id) {
const t = Data.get(id);
const { element, toast } = t;
const { element, toast, progressElement } = t;
const autoHide = element.getAttribute('data-bs-autohide') !== 'false';
if(autoHide) {
if (autoHide) {
const delay = parseInt(element.getAttribute('data-bs-delay'));
const progressElement = element.querySelector('.toast-progress');

toast._config.autohide = autoHide;
toast._config.delay = delay;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Specify radix in parseInt

Include a radix of 10 (parseInt(..., 10)) to ensure decimal parsing; without it, leading zeros can cause unexpected results.

Suggested change
const delay = parseInt(element.getAttribute('data-bs-delay'), 10);

progressElement.style.width = '100%';
progressElement.style.transition = `width linear ${delay / 1000}s`;
EventHandler.on(progressElement, 'transitionend', e => {
toast.hide();
});
}
else {
toast._config.autohide = false;
progressElement.style.removeProperty('width');
progressElement.style.removeProperty('transition');
}
}

export function dispose(id) {
const toast = Data.get(id)
Data.remove(id)

EventHandler.off(toast.element, 'shown.bs.toast')
EventHandler.off(toast.element, 'hidden.bs.toast')
const { element, progressElement } = toast;
EventHandler.off(element, 'shown.bs.toast');
EventHandler.off(element, 'hidden.bs.toast');
EventHandler.off(progressElement, 'transitionend');

if (toast.toast) {
toast.toast.dispose()
toast.toast.dispose();
}
}