sweetalert icon indicating copy to clipboard operation
sweetalert copied to clipboard

Cannot set default button classes

Open matthew-inamdar opened this issue 7 years ago • 8 comments

I've read the documentation and cannot figure this out.

How does one set the default button class for the confirm and cancel buttons?

The following does not work:

swal.setDefaults({
    buttons: {
        cancel: {
            className: 'btn btn-light'
        },
        confirm: {
            className: 'btn btn-success'
        }
    }
});

matthew-inamdar avatar Jan 10 '18 13:01 matthew-inamdar

Css

.red-bg{ background :red } .blue-bg{ background :blue } JS

swal({
  buttons: {
    dance : {
      text : "Button 1",
      className : 'red-bg'
    },
    confirm: {
      text : 'Button 2',
      className : 'blue-bg'
    }
  }
});

Demo : https://codepen.io/girishpatil/pen/YYOvVG Know more here https://sweetalert.js.org/docs/#buttons

theevilhead avatar Jan 16 '18 14:01 theevilhead

@girishpatil - this doesn't solve the problem, unfortunately. This requires you to set the button classes on every call to swal() (which is what I'm currently doing).

This results in duplicated code.

As the title says, I cannot set the default button class.

matthew-inamdar avatar Jan 16 '18 14:01 matthew-inamdar

@matthew-inamdar yeah sorry for that .. although I couldn't find a way of assigning default external classes but digging into the code [https://github.com/t4t5/sweetalert/blob/master/src/modules/class-list/index.ts] found out that there are three types of default buttons. You can use their default class names to style them Or you can even extend @extend your own class (btn btn-left as mentined above) into these below classes

/* style overall buttons */ .swal-button{ border: 1px dashed #333; // @extend .yourclass } /* style cancel buttons */ .swal-button--cancel{ color: #333; } .swal-button--confirm{ color: green } .swal-button--danger{ color: red; } hope it helps in some way you can see the demo here https://codepen.io/girishpatil/pen/wpERBB

theevilhead avatar Jan 16 '18 15:01 theevilhead

Or you can even extend @extend your own class (btn btn-left as mentined above) into these below classes

This is an interesting solution. Not as clean as I would have liked, but may do the trick.

I would like this issue to remain open, as hopefully there is still an actual way with the default config?

If this is currently not possible (for someone to confirm) - I may open a PR if I get the time.

matthew-inamdar avatar Jan 16 '18 15:01 matthew-inamdar

I agree that this should work, so yeah, this could be classified as a bug.

I might tackle it sometime in the future, but if you have the time to submit a PR, that would be great!

t4t5 avatar Feb 15 '18 10:02 t4t5

Thanks for getting back to me @t4t5 - I'll find some time this week hopefully

matthew-inamdar avatar Feb 15 '18 11:02 matthew-inamdar

@t4t5 @matthew-inamdar I'm looking for something to contribute between today and tomorrow, I can take over this, is that ok?

jsainthilaire avatar Feb 16 '18 19:02 jsainthilaire

can i work on this bug?

Riya123-code avatar Jan 22 '23 17:01 Riya123-code