DEV Community

Daniel Kreider
Daniel Kreider

Posted on • Originally published at danielk.tech

3 2

How to add a loading spinner to an Angular Material button (Step-by-step Guide)

The 2 minute complete guide to adding a loading spinner to the Angular Material Button

Alt Text

Err…

…wouldn't it be nice if Angular Material had a button with a loading spinner? Like the loading buttons that Semantic UI has.

Instead of ranting I decided to try to build my own solution  -  and succeeded!

With no further ado, I've included the code below.

Our spinner (CSS).

@keyframes spinner {
  to {transform: rotate(360deg);}
}

.spinner:before {
  content: '';
  box-sizing: border-box;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin-top: -10px;
  margin-left: -10px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  border-top-color: #000000;
  animation: spinner .8s linear infinite;
}
Enter fullscreen mode Exit fullscreen mode

Our button (HTML).

<div style="text-align:center">
  <button mat-raised-button color="primary" [class.spinner]="loading" [disabled]="loading" (click)="save()">Save</button>
</div>
Enter fullscreen mode Exit fullscreen mode

Our .ts file (the logic)

export class AppComponent{
  title = 'hello-world';
  loading = false;

  save(): void {
    this.loading = true;
  }
}
Enter fullscreen mode Exit fullscreen mode

Done!

I told you this was simple. You're welcome. Hope you enjoyed it!

Questions or comments? Don't hesitate to contact me.

Alt Text

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay