Skip to content

Commit dae37e7

Browse files
committed
feat(loading): add ability to hide spinner in the config or options
tweak css to add a max width to the loading indicator references #5426
1 parent c251649 commit dae37e7

File tree

6 files changed

+145
-30
lines changed

6 files changed

+145
-30
lines changed

ionic/components/loading/loading.ios.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// --------------------------------------------------
66

77
$loading-ios-padding: 24px 34px !default;
8+
$loading-ios-max-width: 270px !default;
89
$loading-ios-max-height: 90% !default;
910
$loading-ios-border-radius: 8px !default;
1011
$loading-ios-text-color: #000 !default;
@@ -24,6 +25,7 @@ $loading-ios-spinner-dots-color: $loading-ios-spinner-color !default;
2425
.loading-wrapper {
2526
padding: $loading-ios-padding;
2627

28+
max-width: $loading-ios-max-width;
2729
max-height: $loading-ios-max-height;
2830

2931
border-radius: $loading-ios-border-radius;

ionic/components/loading/loading.md.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// --------------------------------------------------
66

77
$loading-md-padding: 24px !default;
8+
$loading-md-max-width: 280px !default;
89
$loading-md-max-height: 90% !default;
910
$loading-md-border-radius: 2px !default;
1011
$loading-md-text-color: rgba(0, 0, 0, .5) !default;
@@ -24,6 +25,7 @@ $loading-md-spinner-dots-color: $loading-md-spinner-color !default;
2425
.loading-wrapper {
2526
padding: $loading-md-padding;
2627

28+
max-width: $loading-md-max-width;
2729
max-height: $loading-md-max-height;
2830

2931
border-radius: $loading-md-border-radius;

ionic/components/loading/loading.ts

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,99 @@ import {Animation} from '../../animations/animation';
55
import {Transition, TransitionOptions} from '../../transitions/transition';
66
import {Config} from '../../config/config';
77
import {Spinner} from '../spinner/spinner';
8-
import {isPresent} from '../../util/util';
8+
import {isPresent, isUndefined, isDefined} from '../../util/util';
99
import {NavParams} from '../nav/nav-params';
1010
import {ViewController} from '../nav/view-controller';
1111

1212

1313
/**
1414
* @name Loading
1515
* @description
16+
* An overlay that can be used to indicate activity while blocking user
17+
* interaction. The loading indicator appears on top of the app's content,
18+
* and can be automatically dismissed by the app or manually dismissed by
19+
* the user to resume interaction with the app. It includes an optional
20+
* backdrop, which can optionally be clicked to dismiss the loading
21+
* indicator.
22+
*
23+
* ### Creating
24+
* You can pass all of the loading options in the first argument of
25+
* the create method: `Loading.create(opts)`. The spinner name should be
26+
* passed in the `spinner` property, and any optional HTML can be passed
27+
* in the `content` property. If you do not pass a value to `spinner`
28+
* the loading indicator will use the spinner specified by the mode. To
29+
* set the spinner name across the app, set the value of `loadingSpinner`
30+
* in your app's config. To hide the spinner, you can set
31+
* `loadingSpinner: 'hide'` or pass `spinner: 'hide'` in the loading
32+
* options. See the create method below for all available options.
33+
*
34+
* ### Dismissing
35+
* The loading indicator can be dismissed automatically after a specific
36+
* amount of time by passing the number of milliseconds to display it in
37+
* the `duration` of the loading options. It can also be dismissed by
38+
* clicking on the backdrop or pressing the escape key if
39+
* `enableBackdropDismiss` is set to `true` in the loading options. By
40+
* default the loading indicator will show even during page changes,
41+
* but this can be disabled by setting `dismissOnPageChange` to `true`.
42+
* To dismiss the loading indicator after creation, call the `dismiss()`
43+
* method on the Loading instance.
44+
*
45+
* ### Limitations
46+
* The element is styled to appear on top of other content by setting its
47+
* `z-index` property. You must ensure no element has a stacking context with
48+
* a higher `z-index` than this element.
49+
*
50+
* @usage
51+
* ```ts
52+
* constructor(nav: NavController) {
53+
* this.nav = nav;
54+
* }
55+
*
56+
* presentLoadingDefault() {
57+
* let loading = Loading.create({
58+
* content: 'Please wait...'
59+
* });
60+
*
61+
* this.nav.present(loading);
62+
*
63+
* setTimeout(() => {
64+
* loading.dismiss();
65+
* }, 5000);
66+
* }
67+
*
68+
* presentLoadingCustom() {
69+
* let loading = Loading.create({
70+
* spinner: 'hide',
71+
* content: `
72+
* <div class="custom-spinner-container">
73+
* <div class="custom-spinner-box"></div>
74+
* </div>`,
75+
* duration: 5000
76+
* });
77+
*
78+
* this.nav.present(loading);
79+
* }
80+
*
81+
* presentLoadingText() {
82+
* let loading = Loading.create({
83+
* spinner: 'hide',
84+
* content: 'Loading Please Wait...'
85+
* });
86+
*
87+
* this.nav.present(loading);
88+
*
89+
* setTimeout(() => {
90+
* this.nav.push(Page2);
91+
* }, 1000);
92+
*
93+
* setTimeout(() => {
94+
* loading.dismiss();
95+
* }, 5000);
96+
* }
97+
* ```
98+
*
99+
* @demo /docs/v2/demos/loading/
100+
* @see {@link /docs/v2/api/components/spinner/Spinner Spinner API Docs}
16101
*/
17102
export class Loading extends ViewController {
18103

@@ -39,11 +124,11 @@ export class Loading extends ViewController {
39124
}
40125

41126
/**
42-
* Open a loading indicator with the following options
127+
* Create a loading indicator with the following options
43128
*
44129
* | Option | Type | Description |
45130
* |-----------------------|------------|------------------------------------------------------------------------------------------------------------------|
46-
* | icon |`string` | The spinner icon for the loading indicator. |
131+
* | spinner |`string` | The name of the SVG spinner for the loading indicator. |
47132
* | content |`string` | The html content for the loading indicator. |
48133
* | cssClass |`string` | An additional class for custom styles. |
49134
* | showBackdrop |`boolean` | Whether to show the backdrop. Default true. |
@@ -69,8 +154,8 @@ export class Loading extends ViewController {
69154
template:
70155
'<div (click)="bdClick()" tappable disable-activated class="backdrop" [class.hide-backdrop]="!d.showBackdrop" role="presentation"></div>' +
71156
'<div class="loading-wrapper">' +
72-
'<div *ngIf="d.icon" class="loading-spinner">' +
73-
'<ion-spinner [name]="d.icon == \'platform\' ? null : d.icon"></ion-spinner>' +
157+
'<div *ngIf="showSpinner" class="loading-spinner">' +
158+
'<ion-spinner [name]="d.spinner"></ion-spinner>' +
74159
'</div>' +
75160
'<div *ngIf="d.content" [innerHTML]="d.content" class="loading-content"></div>' +
76161
'</div>',
@@ -83,6 +168,7 @@ class LoadingCmp {
83168
private d: any;
84169
private id: number;
85170
private created: number;
171+
private showSpinner: boolean;
86172

87173
constructor(
88174
private _viewCtrl: ViewController,
@@ -101,6 +187,17 @@ class LoadingCmp {
101187
this.id = (++loadingIds);
102188
}
103189

190+
ngOnInit() {
191+
// If no spinner was passed in loading options we need to fall back
192+
// to the loadingSpinner in the app's config, then the mode spinner
193+
if (isUndefined(this.d.spinner)) {
194+
this.d.spinner = this._config.get('loadingSpinner', this._config.get('spinner', 'ios'));
195+
}
196+
197+
// If the user passed hide to the spinner we don't want to show it
198+
this.showSpinner = isDefined(this.d.spinner) && this.d.spinner !== 'hide';
199+
}
200+
104201
onPageDidEnter() {
105202
let activeElement: any = document.activeElement;
106203
if (document.activeElement) {
@@ -138,7 +235,7 @@ class LoadingCmp {
138235
}
139236

140237
export interface LoadingOptions {
141-
icon?: string;
238+
spinner?: string;
142239
content?: string;
143240
showBackdrop?: boolean;
144241
dismissOnPageChange?: boolean;

ionic/components/loading/loading.wp.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// --------------------------------------------------
66

77
$loading-wp-padding: 20px !default;
8+
$loading-wp-max-width: 280px !default;
89
$loading-wp-max-height: 90% !default;
910
$loading-wp-border-radius: 2px !default;
1011
$loading-wp-text-color: #fff !default;
@@ -22,6 +23,7 @@ $loading-wp-spinner-dots-color: $loading-wp-spinner-color !default;
2223
.loading-wrapper {
2324
padding: $loading-wp-padding;
2425

26+
max-width: $loading-wp-max-width;
2527
max-height: $loading-wp-max-height;
2628

2729
border-radius: $loading-wp-border-radius;

ionic/components/loading/test/basic/index.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,48 @@ import {App, Page, ActionSheet, Loading, NavController, ViewController, Platform
77
class E2EPage {
88
constructor(private nav: NavController, private platform: Platform) {}
99

10-
showLoadingIos() {
10+
presentLoadingIos() {
1111
let loading = Loading.create({
12-
icon: 'ios',
12+
spinner: 'ios',
1313
enableBackdropDismiss: true
1414
});
1515

1616
this.nav.present(loading);
1717
}
1818

19-
showLoadingDots() {
19+
presentLoadingDots() {
2020
let loading = Loading.create({
21-
icon: 'dots',
21+
spinner: 'dots',
2222
content: 'Loading...',
2323
enableBackdropDismiss: true
2424
});
2525

2626
this.nav.present(loading);
2727
}
2828

29-
showLoadingBubbles() {
29+
presentLoadingBubbles() {
3030
let loading = Loading.create({
31-
icon: 'bubbles',
31+
spinner: 'bubbles',
3232
content: 'Loading...',
3333
enableBackdropDismiss: true
3434
});
3535

3636
this.nav.present(loading);
3737
}
3838

39-
showLoadingCircles() {
39+
presentLoadingCircles() {
4040
let loading = Loading.create({
41-
icon: 'circles',
41+
spinner: 'circles',
4242
content: 'Loading...',
4343
enableBackdropDismiss: true
4444
});
4545

4646
this.nav.present(loading);
4747
}
4848

49-
showLoadingCrescent() {
49+
presentLoadingCrescent() {
5050
let loading = Loading.create({
51-
icon: 'crescent',
51+
spinner: 'crescent',
5252
content: 'Please wait...',
5353
enableBackdropDismiss: true,
5454
duration: 1500
@@ -57,18 +57,22 @@ class E2EPage {
5757
this.nav.present(loading);
5858
}
5959

60-
showLoadingDefault() {
60+
presentLoadingDefault() {
6161
let loading = Loading.create({
62-
icon: 'platform',
6362
content: 'Please wait...',
6463
enableBackdropDismiss: true,
6564
});
6665

6766
this.nav.present(loading);
67+
68+
setTimeout(() => {
69+
loading.dismiss();
70+
}, 5000);
6871
}
6972

70-
showLoadingCustom() {
73+
presentLoadingCustom() {
7174
let loading = Loading.create({
75+
spinner: 'hide',
7276
content: `
7377
<div class="custom-spinner-container">
7478
<div class="custom-spinner-box"></div>
@@ -79,13 +83,21 @@ class E2EPage {
7983
this.nav.present(loading);
8084
}
8185

82-
showLoadingText() {
86+
presentLoadingText() {
8387
let loading = Loading.create({
84-
content: 'Loading Please Wait...',
85-
enableBackdropDismiss: true
88+
spinner: 'hide',
89+
content: 'Loading Please Wait...'
8690
});
8791

8892
this.nav.present(loading);
93+
94+
setTimeout(() => {
95+
this.goToPage2();
96+
}, 1000);
97+
98+
setTimeout(() => {
99+
loading.dismiss();
100+
}, 5000);
89101
}
90102

91103
goToPage2() {

ionic/components/loading/test/basic/main.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
</ion-navbar>
55

66
<ion-content padding>
7-
<button block (click)="showLoadingIos()">iOS Spinner</button>
8-
<button block (click)="showLoadingDots()">Dots Spinner</button>
9-
<button block (click)="showLoadingBubbles()">Bubbles Spinner</button>
10-
<button block (click)="showLoadingCircles()">Circles Spinner</button>
11-
<button block (click)="showLoadingCrescent()">Crescent Spinner 1500 Duration</button>
12-
<button block (click)="showLoadingDefault()" secondary class="e2eLoadingDefaultSpinner">Default Spinner</button>
13-
<button block (click)="showLoadingCustom()" danger>Custom Spinner</button>
14-
<button block (click)="showLoadingText()" dark>Content Only</button>
7+
<button block (click)="presentLoadingIos()">iOS Spinner</button>
8+
<button block (click)="presentLoadingDots()">Dots Spinner</button>
9+
<button block (click)="presentLoadingBubbles()">Bubbles Spinner</button>
10+
<button block (click)="presentLoadingCircles()">Circles Spinner</button>
11+
<button block (click)="presentLoadingCrescent()">Crescent Spinner 1500 Duration</button>
12+
<button block (click)="presentLoadingDefault()" secondary class="e2eLoadingDefaultSpinner">Default Spinner</button>
13+
<button block (click)="presentLoadingCustom()" danger>Custom Spinner</button>
14+
<button block (click)="presentLoadingText()" dark>Content Only</button>
1515
</ion-content>
1616

1717
<ion-toolbar position="bottom">

0 commit comments

Comments
 (0)