ion-loading
An overlay that can be used to indicate activity while blocking user interaction. The loading indicator appears on top of the app's content, and can be dismissed by the app to resume user interaction with the app. It includes an optional backdrop, which can be disabled by setting showBackdrop: false
upon creation.
Dismissing
The loading indicator can be dismissed automatically after a specific amount of time by passing the number of milliseconds to display it in the duration
of the loading options. To dismiss the loading indicator after creation, call the dismiss()
method on the loading instance. The onDidDismiss
function can be called to perform an action after the loading indicator is dismissed.
Customization
Loading uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector.
We recommend passing a custom class to cssClass
in the create
method and using that to add custom styles to the host and inner elements. This property can also accept multiple classes separated by spaces. View the Usage section for an example of how to pass a class using cssClass
.
/* DOES NOT WORK - not specific enough */
ion-loading {
color: green;
}
/* Works - pass "my-custom-class" in cssClass to increase specificity */
.my-custom-class {
color: green;
}
Any of the defined CSS Custom Properties can be used to style the Loading without needing to target individual elements:
.my-custom-class {
--background: #222;
--spinner-color: #fff;
color: #fff;
}
If you are building an Ionic Angular app, the styles need to be added to a global stylesheet file. Read Style Placement in the Angular section below for more information.
Usage
- Angular
- Javascript
- React
- Stencil
- Vue
import { Component } from '@angular/core';
import { LoadingController } from '@ionic/angular';
@Component({
selector: 'loading-example',
templateUrl: 'loading-example.html',
styleUrls: ['./loading-example.css'],
})
export class LoadingExample {
constructor(public loadingController: LoadingController) {}
async presentLoading() {
const loading = await this.loadingController.create({
cssClass: 'my-custom-class',
message: 'Please wait...',
duration: 2000,
});
await loading.present();
const { role, data } = await loading.onDidDismiss();
console.log('Loading dismissed!');
}
async presentLoadingWithOptions() {
const loading = await this.loadingController.create({
spinner: null,
duration: 5000,
message: 'Click the backdrop to dismiss early...',
translucent: true,
cssClass: 'custom-class custom-loading',
backdropDismiss: true,
});
await loading.present();
const { role, data } = await loading.onDidDismiss();
console.log('Loading dismissed with role:', role);
}
}