ion-toast
A Toast is a subtle notification commonly used in modern applications. It can be used to provide feedback about an operation or to display a system message. The toast appears on top of the app's content, and can be dismissed by the app to resume user interaction with the app.
Positioning
Toasts can be positioned at the top, bottom or middle of the viewport. The position can be passed upon creation. The possible values are top
, bottom
and middle
. If the position is not specified, the toast will be displayed at the bottom of the viewport.
Dismissing
The toast can be dismissed automatically after a specific amount of time by passing the number of milliseconds to display it in the duration
of the toast options. If a button with a role of "cancel"
is added, then that button will dismiss the toast. To dismiss the toast after creation, call the dismiss()
method on the instance.
Usage
- Angular
- Javascript
- React
- Stencil
- Vue
import { Component } from '@angular/core';
import { ToastController } from '@ionic/angular';
@Component({
selector: 'toast-example',
templateUrl: 'toast-example.html',
styleUrls: ['./toast-example.css'],
})
export class ToastExample {
constructor(public toastController: ToastController) {}
async presentToast() {
const toast = await this.toastController.create({
message: 'Your settings have been saved.',
duration: 2000,
});
toast.present();
}
async presentToastWithOptions() {
const toast = await this.toastController.create({
header: 'Toast header',
message: 'Click to Close',
position: 'top',
buttons: [
{
side: 'start',
icon: 'star',
text: 'Favorite',
handler: () => {
console.log('Favorite clicked');
},
},
{
text: 'Done',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
},
},
],
});
await toast.present();
const { role } = await toast.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
}
async function presentToast() {
const toast = document.createElement('ion-toast');
toast.message = 'Your settings have been saved.';
toast.duration = 2000;
document.body.appendChild(toast);
return toast.present();
}
async function presentToastWithOptions() {
const toast = document.createElement('ion-toast');
toast.header = 'Toast header';
toast.message = 'Click to Close';
toast.position = 'top';
toast.buttons = [
{
side: 'start',
icon: 'star',
text: 'Favorite',
handler: () => {
console.log('Favorite clicked');
},
},
{
text: 'Done',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
},
},
];
document.body.appendChild(toast);
await toast.present();
const { role } = await toast.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}