skip to Main Content

I’m using sweetAlert2 and I’m trying to use bootstrap 4 to style buttons, setting the properties:

buttonsStyling: false,
confirmButtonClass: 'btn btn-primary btn-lg',
cancelButtonClass: 'btn btn-lg'

It works, however the showLoaderOnConfirm option is being shown in a very ugly style when I set those properties above.

You can check the examples below:

Steps to reproduce:

  • Input a valid email;
  • Press Submit;
  • See the loader (style) for the first (that’s using bs4 and for the second, with the default style from swal2).
$(function() {
  $('#button').click(() => {
    swal({
      title: 'Submit email to run ajax request',
      input: 'email',
      showCancelButton: true,
      confirmButtonText: 'Submit',
      showLoaderOnConfirm: true,
      buttonsStyling: false,
      confirmButtonClass: 'btn btn-primary btn-lg',
      cancelButtonClass: 'btn btn-lg',
      preConfirm: function(email) {
        return new Promise(function(resolve, reject) {
          setTimeout(function() {
            if (email === '[email protected]') {
              reject('This email is already taken.')
            } else {
              resolve()
            }
          }, 2000)
        })
      },
      allowOutsideClick: false
    }).then(function(email) {
      swal({
        type: 'success',
        title: 'Ajax request finished!',
        html: 'Submitted email: ' + email
      })
    }).catch(swal.noop)
  });

  $('#button1').click(() => {
    swal({
      title: 'Submit email to run ajax request',
      input: 'email',
      showCancelButton: true,
      confirmButtonText: 'Submit',
      showLoaderOnConfirm: true,
      preConfirm: function(email) {
        return new Promise(function(resolve, reject) {
          setTimeout(function() {
            if (email === '[email protected]') {
              reject('This email is already taken.')
            } else {
              resolve()
            }
          }, 2000)
        })
      },
      allowOutsideClick: false
    }).then(function(email) {
      swal({
        type: 'success',
        title: 'Ajax request finished!',
        html: 'Submitted email: ' + email
      })
    }).catch(swal.noop)
  });
});
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.4.2/sweetalert2.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.4.2/sweetalert2.min.css" />
</head>

<body>
  <button id="button">Show (Bootstrap)</button>
  <hr />
  <button id="button1">Show (w/o bootstrap)</button>
</body>

</html>

The question is: How can I let the default style for the loader (using bs4)? Or maybe customize the style for the showLoaderOnConfirm option…

2

Answers


  1. Live demo from the official website: https://sweetalert2.github.io/recipe-gallery/bootstrap.html

    Swal.fire({
      title: 'SweetAlert2 + Bootstrap 4',
      input: 'text',
      buttonsStyling: false,
      showCancelButton: true,
      customClass: {
        confirmButton: 'btn btn-primary btn-lg',
        cancelButton: 'btn btn-danger btn-lg',
        loader: 'custom-loader'
      },
      loaderHtml: '<div class="spinner-border text-primary"></div>',
      preConfirm: () => {
        Swal.showLoading()
        return new Promise((resolve) => {
          setTimeout(() => {
            resolve(true)
          }, 5000)
        })
      }
    })
    .btn {
      margin: 0 6px;
    }
    
    .custom-loader {
      animation: none !important;
      border-width: 0 !important;
    }
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> 
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.css">
    Login or Signup to reply.
  2. If the modal don’t show, just add to your CSS:

    <style>
        .swal2-container.fade {
           opacity: 1
        }
    </style>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search