added loading skeleton directive for text data
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { Directive, OnChanges,Input,ElementRef,Renderer2 } from "@angular/core";
|
||||
import { SimpleChanges } from "@angular/core";
|
||||
@Directive({ selector: '[skText]', standalone: true })
|
||||
export class SkTextDirective implements OnChanges {
|
||||
@Input() skText = false;
|
||||
|
||||
private originalText = '';
|
||||
private originalColor = '';
|
||||
|
||||
constructor(private el: ElementRef, private renderer: Renderer2) {}
|
||||
|
||||
ngOnChanges(changes: SimpleChanges) {
|
||||
const change = changes['skText'];
|
||||
|
||||
// skip if value didn't actually change, or is undefined
|
||||
if (!change) return;
|
||||
|
||||
this.skText ? this.showSkeleton() : this.hideSkeleton();
|
||||
}
|
||||
|
||||
private showSkeleton() {
|
||||
const el: HTMLElement = this.el.nativeElement;
|
||||
this.originalText = el.textContent ?? '';
|
||||
this.originalColor = el.style.color;
|
||||
|
||||
// measure natural width before blanking text
|
||||
const width = Math.min(Math.max(this.originalText.length * 7.5, 48), 280);
|
||||
const height = 20;
|
||||
this.renderer.setStyle(el, 'color', 'transparent');
|
||||
this.renderer.setStyle(el, 'background', 'linear-gradient(90deg, #e2e8f0 25%, #f8fafc 50%, #e2e8f0 75%)');
|
||||
this.renderer.setStyle(el, 'background-size', '600px 100%');
|
||||
this.renderer.setStyle(el, 'animation', 'sk-shimmer 1.6s infinite linear');
|
||||
this.renderer.setStyle(el, 'border-radius', '4px');
|
||||
this.renderer.setStyle(el, 'min-width', `${width}px`);
|
||||
this.renderer.setStyle(el, 'max-height', `${height}px`);
|
||||
this.renderer.setStyle(el, 'display', 'inline-block');
|
||||
}
|
||||
|
||||
private hideSkeleton() {
|
||||
const el: HTMLElement = this.el.nativeElement;
|
||||
this.renderer.removeStyle(el, 'color');
|
||||
this.renderer.removeStyle(el, 'background');
|
||||
this.renderer.removeStyle(el, 'background-size');
|
||||
this.renderer.removeStyle(el, 'animation');
|
||||
this.renderer.removeStyle(el, 'border-radius');
|
||||
this.renderer.removeStyle(el, 'min-width');
|
||||
this.renderer.removeStyle(el, 'max-height');
|
||||
this.renderer.removeStyle(el, 'display');
|
||||
|
||||
}
|
||||
}
|
||||
@@ -186,3 +186,8 @@ h3 {
|
||||
--mat-icon-button-disabled-icon-color: rgba(255, 255, 255, 0.35);
|
||||
--mat-icon-button-icon-color: #ffffff;
|
||||
}
|
||||
|
||||
@keyframes sk-shimmer {
|
||||
0% { background-position: -600px 0; }
|
||||
100% { background-position: 600px 0; }
|
||||
}
|
||||
Reference in New Issue
Block a user