create a loader in typescript

In order to create a loader in TypeScript, you can follow these steps:

  1. Create a spinner using HTML and CSS. Here is an example:
<div class="spinner">
  <div class="dot1"></div>
  <div class="dot2"></div>
</div>
83 chars
5 lines
.spinner {
  margin: 100px auto;
  width: 40px;
  height: 40px;
  position: relative;
  text-align: center;
  -webkit-animation: sk-rotate 2s infinite linear;
  animation: sk-rotate 2s infinite linear;
}

.dot1, .dot2 {
  width: 60%;
  height: 60%;
  display: inline-block;
  position: absolute;
  top: 0;
  background-color: #333;
  border-radius: 100%;
  -webkit-animation: sk-bounce 2s infinite ease-in-out;
  animation: sk-bounce 2s infinite ease-in-out;
}

.dot2 {
  top: auto;
  bottom: 0;
  -webkit-animation-delay: -1s;
  animation-delay: -1s;
}

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

@-webkit-keyframes sk-bounce {
  0%, 100% { -webkit-transform: scale(0.0) }
  50% { -webkit-transform: scale(1.0) }
}

@keyframes sk-bounce {
  0%, 100% {
    transform: scale(0.0);
    -webkit-transform: scale(0.0);
  } 50% {
    transform: scale(1.0);
    -webkit-transform: scale(1.0);
  }
}
1020 chars
47 lines
  1. Create a TypeScript file and import the spinner HTML and CSS. Here is an example:
index.ts
import './spinner.css';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-loader',
  template: `
    <div class="spinner">
      <div class="dot1"></div>
      <div class="dot2"></div>
    </div>
  `,
  styleUrls: ['./spinner.css']
})
export class LoaderComponent implements OnInit {
  constructor() {}

  ngOnInit() {}
}
354 chars
19 lines
  1. Use the <app-loader> component in your HTML file wherever you need to display the loader.
<div class="content">
  <app-loader></app-loader>
  <!-- Your content goes here -->
</div>
91 chars
5 lines
  1. (Optional) You can also add some logic to show and hide the loader based on a boolean flag. Here is an example:
index.ts
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-loader',
  template: `
    <div *ngIf="isLoading" class="spinner">
      <div class="dot1"></div>
      <div class="dot2"></div>
    </div>
  `,
  styleUrls: ['./spinner.css']
})
export class LoaderComponent {
  @Input() isLoading = false;
}
323 chars
16 lines
<div class="content">
  <app-loader [isLoading]="isLoading"></app-loader>
  <!-- Your content goes here -->
</div>
115 chars
5 lines

gistlibby LogSnag