Hi,
If I call the .close() (or.closeModal()) method, it doesn't fire the defined close callback function, however if I click on the gray overlay or press ESC, it's firing.
I use it like this:
var openFn = function () {
...
};
var closeFn = function () {
...
};
swal({
html: currentCardContent,
showConfirmButton: false,
showCancelButton: false,
buttonsStyling: false,
showCloseButton: true,
onOpen: openFn,
onClose: closeFn
}).catch(swal.noop);
Reading the source code, it seems intentional, in the sense that it's not a bug, but a feature. But... is that the behaviour we want @limonte? I think that closeModal should read modalParameters.onClose directly and in all cases instead of receiving it as a parameter.
However, some people may want to close the modal programatically, without having the callback called, to differentiate between user and script close-requests. Changing the current behavior is therefore a breaking change.
If we change the current bahavior, a possibility would be to add an argument to the callback signature (so we still have a way to differentiate user or script close-requests):
interface SweetAlertOptions {
onClose: (modal: HTMLElement, closedByUser: boolean) => void;
}
(closedByUser would be false when calling swal.close(), and also in case of timer expiration)
@SasSam: Meanwhile, you can workaround by using swal.close(myCallback).
@toverux Yes, it does make sense. I call the close() with a parameter and it works.
The current behavior is intentional, I personally want to keep it.
As @toverux mentioned, users should be able to close a modal without calling a close callback function.
If you want to close a modal programmatically using swal.close(), just pass the function to it:
swal.close(myCloseCallback)
Most helpful comment
Reading the source code, it seems intentional, in the sense that it's not a bug, but a feature. But... is that the behaviour we want @limonte? I think that closeModal should read modalParameters.onClose directly and in all cases instead of receiving it as a parameter.
However, some people may want to close the modal programatically, without having the callback called, to differentiate between user and script close-requests. Changing the current behavior is therefore a breaking change.
If we change the current bahavior, a possibility would be to add an argument to the callback signature (so we still have a way to differentiate user or script close-requests):
(
closedByUserwould be false when callingswal.close(), and also in case of timer expiration)@SasSam: Meanwhile, you can workaround by using
swal.close(myCallback).