Create Custom Loading Spinner in Lightning Web Component Salesforce | LWC Stack ☁️⚡️

 In this blog you will learn to create a Custom Loading Spinner for your Lightning Web Components.

To use a custom spinner you may search for JavaScript spinner and you will be getting tons of results containing custom spinner CSS.

In this blog I am using css from a very simple example at w3schools.com 

Checkout the code below : 

CSS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
.loader {
  border: 16px solid #f3f3f3;
  border-radius: 50%;
  border-top: 16px solid blue;
  border-right: 16px solid green;
  border-bottom: 16px solid red;
  border-left: 16px solid pink;
  width: 120px;
  height: 120px;
  -webkit-animation: spin 2s linear infinite;
  animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}


HTML

1
2
3
4
5
6
7
8
9
<template>
  <template if:true={isLoading}>
    <div class="loader"></div>
    Please Wait...
  </template>
  <br />
  <lightning-button label="Toggle Spinner" onclick={showSpinner}>
  </lightning-button>
</template>


JS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import { LightningElement } from "lwc";

export default class CustomSpinner extends LightningElement {
  isLoading = false;

  showSpinner() {
    if (!this.isLoading) {
      this.isLoading = true;
    } else {
      this.isLoading = false;
    }
  }
}


Output



Checkout complete video below

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Happy Coding :)

Post a Comment

0 Comments