Sweetalert2: The .close() doesn't call the close callback function

Created on 5 Jan 2017  路  3Comments  路  Source: sweetalert2/sweetalert2

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);

feature

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):

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).

All 3 comments

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)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

limonte picture limonte  路  3Comments

limonte picture limonte  路  3Comments

jharrvis picture jharrvis  路  3Comments

samturrell picture samturrell  路  3Comments

dirchev picture dirchev  路  3Comments