From cf43330206d4f965986310be7df7ebe7a8a7bc7b Mon Sep 17 00:00:00 2001 From: genja Date: Mon, 8 Jun 2026 15:06:53 +0200 Subject: [PATCH] added loading skeleton directive for text data --- .../custom-directives/sk-text.directive.ts | 51 +++++++++++++++++++ webapp/src/styles.scss | 5 ++ 2 files changed, 56 insertions(+) create mode 100644 webapp/src/app/custom-directives/sk-text.directive.ts diff --git a/webapp/src/app/custom-directives/sk-text.directive.ts b/webapp/src/app/custom-directives/sk-text.directive.ts new file mode 100644 index 00000000..4884dde1 --- /dev/null +++ b/webapp/src/app/custom-directives/sk-text.directive.ts @@ -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'); + + } +} \ No newline at end of file diff --git a/webapp/src/styles.scss b/webapp/src/styles.scss index 271f28ac..0a3a5f8d 100644 --- a/webapp/src/styles.scss +++ b/webapp/src/styles.scss @@ -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; } +} \ No newline at end of file