20

Is there any way to style the text message within an ionic 2 toast?

I have tried this:

    let toast = Toast.create({
      message: "Some text on one line. <br /><br /> Some text on another line.",
      duration: 15000,
      showCloseButton: true,
      closeButtonText: 'Got it!',
      dismissOnPageChange: true
    });

    toast.onDismiss(() => {
      console.log('Dismissed toast');
    });

    this.nav.present(toast);
  }

But clearly you can't use html in the text so I am guessing the answer to my question is no?

Bill Noble
  • 6,466
  • 19
  • 74
  • 133

11 Answers11

32

You must add 'cssClass: "yourCssClassName"' in your toastCtrl function.

 let toast = Toast.create({
      message: "Some text on one line. <br /><br /> Some text on another line.",
      duration: 15000,
      showCloseButton: true,
      closeButtonText: 'Got it!',
      dismissOnPageChange: true,
      cssClass: "yourCssClassName",
    });

than you can add any feature to the your css class. But your css feature went outside the default page'css. Exmp:

   page-your.page.scss.name {
     //bla bla
    }
 .yourCssClassName {
   text-align:center;
  }
Burhan Gül
  • 501
  • 6
  • 17
25

I was able to achieve a toaster color change by adding a custom class on the toaster create

let toast = this.toastCtrl.create({
    message: 'Foobar was successfully added.',
    duration: 5000,
    cssClass: "toast-success"
  });
  toast.present();
}

In that pages scss file i then went outside the default nested page name ( because the toaster is NOT inside the root of ion page name thing). And all though this is a bit hacky i just explicitly targeted the next div element after the custom class that i added

.toast-success {
  > div{
       background-color:#32db64!important;
   }
}

I say its hacky because you have to use the !important on it. You can avoid the !important by wrapping the .toast-success with .md,.ios,.wp{...

You can override the style default by overriding the main toaster variables in the theme/variables.scss file.

$toast-ios-background:(#32db64);
$toast-md-background:(#32db64);
$toast-wp-background:(#32db64);

This will only override the default value though and not a custom value. there are a few more variables that can be styled as well.

  • This is working. I had to remove the `background-color` I had defined in class `.toast-message` for the custom cssClass background to be visible. – A.W. Nov 11 '16 at 11:07
  • You should emphasize that you put .toast-success outside of the page-name thing. I overlooked it and spent 1 hour debugging because I was careless. =( – Ka Mok Dec 28 '16 at 19:24
  • Ok the div stuff did the trick it was changing all of my screen to color red because I was missing the div :( – Skizo-ozᴉʞS ツ Jun 01 '17 at 18:52
7

First, import toast controller from ionic-angular and make object of that in constructor.

import { ToastController } from "ionic-angular";

constructor(private _tc: ToastController) {
}

After that wherever you want to show your toast message write that.

let options = {
  message: "Your toast message show here",
  duration: 3000,
  cssClass: "toast.scss"
 };

this._tc.create(options).present();

Here is my scss:

.toast-message {
  text-align: center;
}

Or you can check best example from this link. I think it will help you. :)

Or else check the answer on this link.

skm
  • 1,192
  • 1
  • 11
  • 23
Bhavsang Jam
  • 374
  • 4
  • 12
6

If you define your own css class in app.scss (not in page.scss) you can style it with .toast-wrapper and .toast.message No need to use > div{

Example:

.yourtoastclass {
  .toast-wrapper {
    background: blue;
    opacity: 0.8;
    border-radius: 5px;
    text-align: center;
  }
  .toast-message {
    font-size: 3.0rem;
    color: white;
  }
}

in theme/variables.scss you can make a default

Example (red and little transparent):

$toast-width:   75%;  /* default 100% */
$toast-ios-background: rgba(153, 0, 0, .8);
$toast-md-background: rgba(153, 0, 0, 0.8);
4

Ionic 2 provide a very useful way to override their component style you can override the toaster SASS variable in src/theme/variables.scss by adding

$toast-ios-title-color: #f00 ;
$toast-md-title-color:#f00;

this will override the default style please refer to this Overriding Ionic Sass variable

3

You can accomplish, however you need to modify the toast component template itself.

Via explorer: \node_modules\ionic-angular\components\toast\toast.js

Change line 194 (template): {{d.message}} to <div [innerHTML]='d.message'></div>

Mark
  • 39
  • 1
  • 2
    Not advised as you will be overwriting the core ionic files and therefor any updates from ionic will break the changes you made. It will also make maintaining the project a pain. Especially if other devs are working on it. –  Feb 04 '17 at 08:11
  • 1
    this is however the right answer to this question. So far ionic toast do not support that behaviour. Maybe it will be good to request an update – aorfevre Feb 19 '19 at 14:44
2

You should be able to change any of the message styling in the css using .toast-message selector:

.toast-message { 
    font-family: Helvetica,
    color: red
}

Or, if you look at the docs (http://ionicframework.com/docs/v2/api/components/toast/Toast/) there is a cssClass property you can use to assign your toast a specific class and then style that.

Pat Murray
  • 4,125
  • 7
  • 28
  • 33
  • Yes I can see how to change the font etc. But I need to change the layout of the message with paragraphs and indents etc. – Bill Noble May 09 '16 at 15:00
  • 2
    Ahh, I don't think there is a clean way to do that as of right now because the message takes a string over html (as you pointed out above). You could potentially edit the DOM directly after the component was created but this would be messy. Maybe let Ionic know that this feature would be useful – Pat Murray May 09 '16 at 15:12
  • Thanks Pat that is what I feared. – Bill Noble May 09 '16 at 17:45
1

Change toast background color and opacity:

let toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom',
      cssClass: 'changeToast'
    });

and add app.scss:

.changeToast{.toast-wrapper {opacity: 0.6; border-radius: 5px !important; text-align: center; background: color($colors, primary);}};

It's used with .toast-message

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Rajnikant Lodhi
  • 530
  • 4
  • 13
1

I tried all above, still didn't work, therefore I come across a new solution, you need cssClass outside of page css declaration:

let toast = this.toastCtrl.create({
          message: msg,
          duration: 3000,
          position: 'bottom',
          cssClass: 'toastcolor'
        });

post-list.scss like this

page-post-list {

}
.toastcolor .toast-message {
    background-color:skyblue;
}      
justnajm
  • 4,422
  • 6
  • 36
  • 56
0

Not sure about old Ionic versions, but in Ionic 5 you can't directly change inner CSS since it's encapsulated in the shadow

<ion-select>
  #shadow-root
    <div class="toast-container" part="container">
       ...
    </div>
</ion-select>

so, to change .toast-container (for example) in your cssClass you should use:

.my-custom-class::part(container) {
    flex-direction: column;
}

.my-custom-class {
    .toast-container {
        flex-direction: column; // will not work
    }
}
Ihor
  • 571
  • 6
  • 10
0

I'm using ionic v5 with angular and according to: https://ionicframework.com/docs/api/toast#css-shadow-parts

you can do something like this:

::ng-deep{
    ion-toast::part(container) {
        ...
    }
    ion-toast::part(message) {
        ...
    }
} 
Adán Escobar
  • 1,729
  • 9
  • 15