sweetalert: Tab key not working after opening two Sweet Alert Modals in a row

Hello,

It seems that when the SweetAlert dialog opens another one after it’s done, the tab key stops working, all other keystrokes are okay.

You can recreate this using the example on http://tristanedwards.me/sweetalert.

This one specifically:

swal({      title: "Are you sure?",   
            text: "Your will not be able to recover this imaginary file!",   
            type:  "warning",   
            showCancelButton: true,   
            confirmButtonColor: "#DD6B55",   
            confirmButtonText: "Yes, delete it!",   
            cancelButtonText: "No, cancel plx!",   
            closeOnConfirm: false,   
            closeOnCancel: false }, 
            function(isConfirm){  
                 if (isConfirm) {     
                 swal("Deleted!", "Your imaginary file has been deleted.", "success");  
                 } else {     
                 swal("Cancelled", "Your imaginary file is safe :)", "error");   
                 } });

I believe it’s copying over the keystroke handling from the previous modal, specifically in the closeModal() function, this part:

 // Reset the page to its previous state
    window.onkeydown = previousWindowKeyDown;
    document.onclick = previousDocumentClick;
    if (previousActiveElement) {
      previousActiveElement.focus();
    }

Perhaps it’s simply focusing on the previous modal and registering back it’s keydown and onclick?

About this issue

  • Original URL
  • State: open
  • Created 10 years ago
  • Reactions: 4
  • Comments: 21

Commits related to this issue

Most upvoted comments

To implement the fix by @jayquest without changing the library, I used the following code:

var previousWindowKeyDown = window.onkeydown; swal({ title: ‘Are you sure?’, closeOnConfirm: false, closeOnCancel: false }, function (isConfirm) { window.onkeydown = previousWindowKeyDown; if (isConfirm) { swal(‘Deleted!’, ‘Your imaginary file has been deleted.’, ‘success’); } else { swal(‘Cancelled’, ‘Your imaginary file is safe 😃’, ‘error’); } });

The simplest possible solution based on the workaround of @JustinWinthers that I could found (without modifying the library), is to hook only into the swal.close function and set the window.onkeydown to the previous window.onkeydown event handler:

(function (){
    var close = window.swal.close;
    var previousWindowKeyDown = window.onkeydown;
    window.swal.close = function() {
        close();
        window.onkeydown = previousWindowKeyDown;
    };
})();

or in my case even simpler:

(function (){
    var close = window.swal.close;
    window.swal.close = function() {
        close();
        window.onkeydown = null;
    };
})();

With this workaround it is now also possible to use the Enter and Escape keys in the dialog.

The error messages mentioned by @Mr-Anonymous and @EduardJS also no longer occur.

To expand on @amoralidad 's fix you can use this to include only once after you include the sweet alert library in your project and it will decorate the swal service so your code and the library stays unobstructed (without coding the fix above in every call) until a permanent fix is made.

(function (){

    var _swal = window.swal;

    window.swal = function(){

        var previousWindowKeyDown = window.onkeydown;

        _swal.apply(this, Array.prototype.slice.call(arguments, 0));

        window.onkeydown = previousWindowKeyDown;

    };

})();

i fixed it inserting the two line of code above on the begin of the function sweetAlert or swal at the line 215, i don´t know if its the best solution, but its working for me.

/*
 * Global sweetAlert function
 */
var sweetAlert, swal;

sweetAlert = swal = function() {

    if(previousWindowKeyDown !== undefined && window.onkeydown !== previousWindowKeyDown)`
        window.onkeydown = previousWindowKeyDown;

Sorry I didn’t make that clear!

Yes it’s tabbing IN the model, but after you close the second modal, you lose the ability navigate the web page with the tab key (No modals on screen just the web page).