toastify-js icon indicating copy to clipboard operation
toastify-js copied to clipboard

Adding a class doesn't override backgroundColor

Open CharlieIGG opened this issue 5 years ago • 5 comments

If I do NOT set a backgroundColor option when calling Toastify, but add a className that maps for instance to a Bootstrap alert class, the Toast's default backgroundColor still takes precedence over the class' background color.

i.e.

 Toastify({
    text: "A simple warning alert—check it out!",
    duration: 3000,
    close: true,
    gravity: "bottom",
    positionLeft: false,
    className:"alert alert-warning>"
    }).showToast();

Should yield the following:

image

Instead I get:

image

CharlieIGG avatar Mar 06 '19 04:03 CharlieIGG

Hey @CharlieIGG, The issue that you are facing is a side-effect of using a gradient background as default. Linear gradient requires to use background property where as bootstrap sets the color of alert using background-color. Setting a default background was done in order to make Toastify a simple drop-in library for anyone to use.

But for the issue of style not being applied, you will need to import the bootstrap css from your JS file for it to take effect. Demo: https://codesandbox.io/s/8kyk48zoll

Let me know your thoughts of this.

apvarun avatar Mar 16 '19 17:03 apvarun

@apvarun What about adding a class for the default gradient that is the default value of the className property?

AlexisTM avatar Sep 03 '19 14:09 AlexisTM

FYI I ended up doing the following to achieve what I wanted (thankfully I was already packing SCSS):

@each $color,
$value in $theme-colors {

  .toastify.alert-#{$color},
  .alert-#{$color} {
    @include alert-variant(theme-color-level($color, $alert-bg-level), theme-color-level($color, $alert-border-level), color-yiq(theme-color-level($color, $alert-bg-level)));

    .close,
    .alert-link {
      color: color-yiq(theme-color-level($color, $alert-bg-level));
    }
  }
}

CharlieIGG avatar Sep 10 '19 18:09 CharlieIGG

I had exactly this same issue wanting to use bg-success or bg-danger from bootstrap. (alert added a div/block which messed with the layout.)

I found you can set a null backgroundColor as "unset", "transparent" or "none" and then the className works.

https://caniuse.com/mdn-css_types_color_transparent https://caniuse.com/css-unset-value

p.s. the options table didn't have a default value for backgroundColor so I expected that would be null. Perhaps remove it from the CSS and instead add it if both backgroundColor and className aren't specified?

avimar avatar Dec 16 '20 09:12 avimar

I added one-line CSS to solve that

.toastify {
  background-image: unset;
}

patrickliu-hk avatar Oct 07 '21 02:10 patrickliu-hk