|
| 1 | +<p align="center"> |
| 2 | + <img height="200px" width="200px" style="text-align: center;" src="https://cdn.rawgit.com/me-majidi/ng2-loading-spinner/e284608f/assets/logo.svg"> |
| 3 | +</p> |
| 4 | +<h1>Ng2-loading-spinner</h1> |
| 5 | +<p>Customisable loading spinner for angular4+ applications</p> |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +<img width="600px" src="https://cdn.rawgit.com/me-majidi/ng2-loading-spinner/b7d6877e/assets/demo_img1.gif"> |
| 12 | +<img width="400px" src="https://cdn.rawgit.com/me-majidi/ng2-loading-spinner/b7d6877e/assets/demo_img2.gif"> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Table of contents |
| 18 | + - [Online Demo](https://me-majidi.github.io/ng2-loading-spinner/) |
| 19 | + - [Installation](https://www.npmjs.com/package/ng2-loading-spinner#installation) |
| 20 | + - [Usage](https://www.npmjs.com/package/ng2-loading-spinner#usage) |
| 21 | + - [API](https://www.npmjs.com/package/ng2-loading-spinner#api) |
| 22 | + - [Examples](https://www.npmjs.com/package/ng2-loading-spinner#example) |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## Installation |
| 31 | +```shell |
| 32 | +npm install --save ng2-loading-spinner |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +## Usage |
| 37 | +Import `Ng2LoadingSpinnerModule` in your module |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { NgModule } from '@angular/core'; |
| 41 | +import { Ng2LoadingSpinnerModule } from 'ng2-loading-spinner' |
| 42 | + |
| 43 | +@NgModule({ |
| 44 | + imports: [ Ng2LoadingSpinnerModule ] |
| 45 | +}) |
| 46 | +export class TestModule { } |
| 47 | +``` |
| 48 | +then, use `ng2-loading` directive on element which you want spinner: |
| 49 | +```html |
| 50 | +<div class="card" |
| 51 | + [ng2-loading]="showSpinner"> |
| 52 | + ... |
| 53 | +</div> |
| 54 | +``` |
| 55 | +directive expects a boolean for showing and removing Loading spinner: |
| 56 | +``` typescript |
| 57 | +@Component({ |
| 58 | + selector : 'app.component', |
| 59 | + templateUrl: './app.component.html', |
| 60 | + styleUrls : [ './app.component.css' ] |
| 61 | +}) |
| 62 | +export class AppComponent { |
| 63 | + showSpinner: boolean = false; |
| 64 | + |
| 65 | + |
| 66 | + constructor() { |
| 67 | + setTimeOut(() => { |
| 68 | + this.showSpinner = false; |
| 69 | + }, 1500); |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +<br><br> |
| 75 | +<br><br> |
| 76 | +## API |
| 77 | + |
| 78 | +#### Input parameters |
| 79 | +Input | Type | Required | Description | |
| 80 | +------ | ----- | ----- | ----- | |
| 81 | +ng2-loading | boolean | Required | A boolean, which will determine when spinner is added to the DOM | |
| 82 | +config | INg2LoadingSpinnerConfig | Optional | Configuartion object for spinner. If no config options are set, the default config options will be used. | |
| 83 | +template | TemplateRef | Optional | If provided, the custom template will be shown in place of the default spinner animations. You can use this for rendering custom spinners instead of default spinner animations | |
| 84 | + |
| 85 | + |
| 86 | +#### Configurable options |
| 87 | +Option | Required | type | Default value | Description | Examples | |
| 88 | +--- | --- | --- | --- | --- | ---- | |
| 89 | +animationType | Optional | string | ANIMATION_TYPES.fadingCircle | The spinner animation to be used. import ANIMATION_TYPES constant to select valid options. | ANIMATION_TYPES.chasingDots | |
| 90 | +backdropColor | Optional | string | rgba(0, 0, 0, 0.3) | Background color of backdrop element | 'red', 'rgb(120, 0, 171)', '#434343' | |
| 91 | +backdropBorderRadius | Optional | string | 0 | The border-radius property to be aplied to the spinner | '10px', '1rem', '50%' | |
| 92 | +spinnerColor | Optional | string | white | Color of spinner | 'red', 'rgb(120, 0, 171)', '#434343' | |
| 93 | +spinnerPosition | Optional | string | 'center' | Position the spinner into the host view | 'top', 'right', 'bottom', 'left', 'top-right', 'bottom-left' | |
| 94 | +spinnerSize | Optional | string | 'md' | Option that indicates size of spinner | 'sm', 'md', 'lg' | |
| 95 | +spinnerFontSize | Optional | string | | Option for controlling size of spinner.If provided `spinnerSize` option will be ignored | '10px', '1rem' | |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +<br><br><br> |
| 101 | +#### Available spinner positions: |
| 102 | +Position | |
| 103 | +------ | |
| 104 | +center | |
| 105 | +top | |
| 106 | +right | |
| 107 | +bottom | |
| 108 | +left | |
| 109 | +top-right | |
| 110 | +left-right | |
| 111 | +top-left | |
| 112 | +bottom-left | |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +<br><br> |
| 117 | +#### Available spinner sizes: |
| 118 | +Size | |
| 119 | +------ | |
| 120 | +xs | |
| 121 | +sm | |
| 122 | +md | |
| 123 | +lg | |
| 124 | +xl | |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +## Examples |
| 138 | + |
| 139 | +#### Example 1 - with custom configuration options |
| 140 | + |
| 141 | +``` typescript |
| 142 | +import { Component } from '@angular/core'; |
| 143 | +import { ANIMATION_TYPES } from 'ng2-loading-spinner'; |
| 144 | +import { INg2LoadingSpinnerConfig } from 'ng2-loading-spinner'; |
| 145 | + |
| 146 | +@Component({ |
| 147 | + selector : 'app-example1', |
| 148 | + templateUrl: './example1.component.html', |
| 149 | + styleUrls : [ './example1.component.css' ] |
| 150 | +}) |
| 151 | + |
| 152 | +export class Example1Component { |
| 153 | + show = false; |
| 154 | + |
| 155 | + loadingConfig: INg2LoadingSpinnerConfig = { |
| 156 | + animationType : ANIMATION_TYPES.dualCircle, |
| 157 | + backdropColor : 'rgba(0, 0, 0, 0.3)', |
| 158 | + spinnerColor : '#fff', |
| 159 | + spinnerPosition: 'left', |
| 160 | + backdropBorderRadius: '15px', |
| 161 | + spinnerSize: 'md', |
| 162 | + spinnerFontSize: '2rem' |
| 163 | + }; |
| 164 | + |
| 165 | + |
| 166 | + constructor () { |
| 167 | + } |
| 168 | + |
| 169 | + showLoading() { |
| 170 | + this.show = true; |
| 171 | + setTimeout(() => { |
| 172 | + this.show = false; |
| 173 | + }, 1500); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +``` |
| 178 | + |
| 179 | +```html |
| 180 | +<button class="btn btn-dark" |
| 181 | + [ng2-loading]="show" |
| 182 | + [config]="loadingConfig" |
| 183 | + (click)="showLoading()"> |
| 184 | + Show Spinner |
| 185 | +</button> |
| 186 | +``` |
| 187 | + |
| 188 | +#### Example2 - using custom template |
| 189 | + |
| 190 | +``` typescript |
| 191 | +import { Component } from '@angular/core'; |
| 192 | +import { ANIMATION_TYPES } from 'ng2-loading-spinner'; |
| 193 | +import { INg2LoadingSpinnerConfig } from 'ng2-loading-spinner'; |
| 194 | +import { AuthService } from '../auth.service' |
| 195 | + |
| 196 | +@Component({ |
| 197 | + selector : 'app-example2', |
| 198 | + templateUrl: './example2.component.html', |
| 199 | + styleUrls : [ './example2.component.css' ] |
| 200 | +}) |
| 201 | + |
| 202 | +export class Example2Component { |
| 203 | + show = false; |
| 204 | + |
| 205 | + loadingConfig: INg2LoadingSpinnerConfig = { |
| 206 | + animationType : ANIMATION_TYPES.dualCircle, |
| 207 | + backdropColor : 'rgba(0, 0, 0, 0.3)', |
| 208 | + spinnerColor : '#fff', |
| 209 | + spinnerPosition: 'center', |
| 210 | + backdropBorderRadius: '15px', |
| 211 | + spinnerSize: 'md', |
| 212 | + spinnerFontSize: '2rem' |
| 213 | + }; |
| 214 | + |
| 215 | + |
| 216 | + constructor (private authService: AuthService) { |
| 217 | + } |
| 218 | + |
| 219 | + onLogin() { |
| 220 | + this.show = true; |
| 221 | + this.authService.login() |
| 222 | + .subscribe((res) => { |
| 223 | + this.show = false; |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
| 227 | +``` |
| 228 | + |
| 229 | + |
| 230 | +``` html |
| 231 | + |
| 232 | +<form [ng2-loading]="show" |
| 233 | + [config]="loadingConfig" |
| 234 | + [template]="customTemplate"> |
| 235 | + <input type="text" placeholder="username"> |
| 236 | + <input type="password" placeholder="password"> |
| 237 | + <button class="btn" (click)="onLogin()">Login</button> |
| 238 | +<form> |
| 239 | + |
| 240 | + |
| 241 | + |
| 242 | +<ng-template #customTemplate> |
| 243 | + <div class="align-items-center d-flex flex-column flex-direction"> |
| 244 | + <p>Please wait</p> |
| 245 | + <div class="custom-loader"></div> |
| 246 | + </div> |
| 247 | +</ng-template> |
| 248 | + |
| 249 | +``` |
| 250 | + |
| 251 | +and style this custom loader in example2.component.css: |
| 252 | + |
| 253 | +``` css |
| 254 | +.custom-loader { |
| 255 | + .... |
| 256 | +} |
| 257 | +``` |
| 258 | + |
0 commit comments