@ibragim-dev-org/ui-forge-library 1.4.56 → 1.4.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -665,74 +665,141 @@ class SkeletonDirective {
665
665
  $isLoading = input.required({ ...(ngDevMode ? { debugName: "$isLoading" } : {}), alias: 'appSkeleton' });
666
666
  $lightColor = input.required(...(ngDevMode ? [{ debugName: "$lightColor" }] : []));
667
667
  $darkColor = input.required(...(ngDevMode ? [{ debugName: "$darkColor" }] : []));
668
+ skeletonizedChildren = new Set();
669
+ hiddenMediaNodes = new Set();
668
670
  constructor(el, renderer) {
669
671
  this.el = el;
670
672
  this.renderer = renderer;
671
673
  effect(() => {
672
674
  if (this.$isLoading()) {
673
- this.applyHostSkeletonStyles();
674
- this.applySkeletonRecursive(this.el.nativeElement, 0);
675
+ this.applySkeletonStyles();
675
676
  }
676
677
  else {
677
- this.removeSkeletonRecursive(this.el.nativeElement);
678
- this.removeHostSkeletonStyles();
678
+ this.removeSkeletonStyles();
679
679
  }
680
680
  });
681
681
  }
682
682
  ngAfterViewInit() {
683
683
  this.injectKeyframes();
684
+ this.applySkeletonStyles();
684
685
  }
685
- applyHostSkeletonStyles() {
686
- this.renderer.setStyle(this.el.nativeElement, 'position', 'relative');
687
- this.renderer.setStyle(this.el.nativeElement, 'overflow', 'hidden');
686
+ applySkeletonStyles() {
687
+ if (!this.$isLoading())
688
+ return;
689
+ const element = this.el.nativeElement;
690
+ this.applyHostSkeletonStyles(element);
691
+ this.applyChildSkeletonStyles(element);
692
+ }
693
+ applyHostSkeletonStyles(element) {
694
+ this.renderer.setStyle(element, 'position', 'relative');
695
+ this.renderer.setStyle(element, 'overflow', 'hidden');
696
+ this.renderer.setStyle(element, 'user-select', 'none');
697
+ }
698
+ applyChildSkeletonStyles(element) {
699
+ this.skeletonizedChildren.forEach((child) => this.removeChildSkeletonStyles(child));
700
+ this.skeletonizedChildren.clear();
701
+ this.hiddenMediaNodes.forEach((node) => this.renderer.removeStyle(node, 'display'));
702
+ this.hiddenMediaNodes.clear();
703
+ Array.from(element.children).forEach((child) => {
704
+ this.processSkeletonNode(child);
705
+ });
706
+ }
707
+ processSkeletonNode(node) {
708
+ if (node === this.el.nativeElement)
709
+ return;
710
+ if (this.shouldSkeletonizeNode(node)) {
711
+ this.applySkeletonToNode(node);
712
+ }
713
+ Array.from(node.children).forEach((child) => {
714
+ this.processSkeletonNode(child);
715
+ });
716
+ }
717
+ removeSkeletonStyles() {
718
+ const element = this.el.nativeElement;
719
+ this.renderer.removeStyle(element, 'position');
720
+ this.renderer.removeStyle(element, 'overflow');
721
+ this.renderer.removeStyle(element, 'user-select');
722
+ this.hiddenMediaNodes.forEach((node) => this.renderer.removeStyle(node, 'display'));
723
+ this.hiddenMediaNodes.clear();
724
+ this.skeletonizedChildren.forEach((child) => this.removeChildSkeletonStyles(child));
725
+ this.skeletonizedChildren.clear();
726
+ }
727
+ removeChildSkeletonStyles(child) {
728
+ this.renderer.removeStyle(child, 'position');
729
+ this.renderer.removeStyle(child, 'overflow');
730
+ this.renderer.removeStyle(child, 'color');
731
+ this.renderer.removeStyle(child, 'background-color');
732
+ this.renderer.removeStyle(child, 'background-image');
733
+ this.renderer.removeStyle(child, 'background-size');
734
+ this.renderer.removeStyle(child, 'background-repeat');
735
+ this.renderer.removeStyle(child, 'animation');
736
+ this.renderer.removeStyle(child, 'border-radius');
737
+ this.renderer.removeStyle(child, 'display');
738
+ this.renderer.removeStyle(child, 'user-select');
739
+ this.renderer.removeStyle(child, 'max-width');
740
+ }
741
+ shouldSkeletonizeNode(node) {
742
+ return this.hasDirectTextContent(node) || this.hasImageContent(node);
743
+ }
744
+ applySkeletonToNode(node) {
745
+ const firstChild = node.firstElementChild;
746
+ const shouldAnimateParent = Boolean(firstChild && this.isMediaNode(firstChild));
747
+ if (shouldAnimateParent && firstChild) {
748
+ this.renderer.setStyle(firstChild, 'display', 'none');
749
+ this.hiddenMediaNodes.add(firstChild);
750
+ }
751
+ if (shouldAnimateParent || this.hasDirectTextContent(node)) {
752
+ this.renderer.setStyle(node, 'position', 'relative');
753
+ this.renderer.setStyle(node, 'overflow', 'hidden');
754
+ this.renderer.setStyle(node, 'color', 'transparent');
755
+ this.renderer.setStyle(node, 'background-color', this.$lightColor());
756
+ this.renderer.setStyle(node, 'background-image', this.getSkeletonGradient());
757
+ this.renderer.setStyle(node, 'background-size', '200% 100%');
758
+ this.renderer.setStyle(node, 'background-repeat', 'no-repeat');
759
+ this.renderer.setStyle(node, 'animation', 'skeletonPulse 1.4s ease-in-out infinite');
760
+ const existingRadius = getComputedStyle(node).borderRadius;
761
+ if (!existingRadius || existingRadius === '0px') {
762
+ this.renderer.setStyle(node, 'border-radius', '4px');
763
+ }
764
+ this.renderer.setStyle(node, 'display', node.style.display || 'block');
765
+ this.renderer.setStyle(node, 'user-select', 'none');
766
+ this.renderer.setStyle(node, 'max-width', 'fit-content');
767
+ this.skeletonizedChildren.add(node);
768
+ }
769
+ }
770
+ hasDirectTextContent(element) {
771
+ const directText = Array.from(element.childNodes)
772
+ .filter((node) => node.nodeType === Node.TEXT_NODE)
773
+ .map((node) => node.textContent ?? '')
774
+ .join('')
775
+ .replace(/\s+/g, '')
776
+ .trim();
777
+ return Boolean(directText);
688
778
  }
689
- removeHostSkeletonStyles() {
690
- this.renderer.removeStyle(this.el.nativeElement, 'position');
691
- this.renderer.removeStyle(this.el.nativeElement, 'overflow');
779
+ hasImageContent(element) {
780
+ return Boolean(element.querySelector('img, svg, canvas, video, iframe, picture'));
781
+ }
782
+ isMediaNode(element) {
783
+ return ['img', 'svg', 'canvas', 'video', 'iframe', 'picture'].includes(element.tagName.toLowerCase());
784
+ }
785
+ getSkeletonGradient() {
786
+ return `linear-gradient(90deg, ${this.$lightColor()} 25%, ${this.$darkColor()} 50%, ${this.$lightColor()} 75%)`;
692
787
  }
693
788
  injectKeyframes() {
694
789
  if (!isBrowser())
695
790
  return;
696
- const styleSheet = document.styleSheets[0];
697
- if (!styleSheet)
791
+ const styleElement = document.getElementById('ui-forge-skeleton-keyframes');
792
+ if (styleElement)
698
793
  return;
699
- const parentPulse = `
700
- @keyframes skeletonPulseParent {
701
- 0% { background-color: ${this.$lightColor()}; }
702
- 50% { background-color: ${this.$darkColor()}; }
703
- 100% { background-color: ${this.$lightColor()}; }
704
- }
705
- `;
706
- const childPulse = `
707
- @keyframes skeletonPulseChild {
708
- 0% { background-color: ${this.$darkColor()}; }
709
- 50% { background-color: ${this.$lightColor()}; }
710
- 100% { background-color: ${this.$darkColor()}; }
794
+ const style = document.createElement('style');
795
+ style.id = 'ui-forge-skeleton-keyframes';
796
+ style.textContent = `
797
+ @keyframes skeletonPulse {
798
+ 0% { background-position: 200% 0; }
799
+ 100% { background-position: -200% 0; }
711
800
  }
712
801
  `;
713
- if (![...styleSheet.cssRules].some((r) => r.cssText.includes('skeletonPulseParent'))) {
714
- styleSheet.insertRule(parentPulse, styleSheet.cssRules.length);
715
- }
716
- if (![...styleSheet.cssRules].some((r) => r.cssText.includes('skeletonPulseChild'))) {
717
- styleSheet.insertRule(childPulse, styleSheet.cssRules.length);
718
- }
719
- }
720
- applySkeletonRecursive(element, depth) {
721
- const animationName = depth % 2 === 0 ? 'skeletonPulseParent' : 'skeletonPulseChild';
722
- this.renderer.setStyle(element, 'color', 'transparent'); // hide text
723
- this.renderer.setStyle(element, 'animation', `${animationName} 1.5s infinite`);
724
- this.renderer.setStyle(element, 'border-radius', getComputedStyle(element).borderRadius);
725
- Array.from(element.children).forEach((child) => {
726
- this.applySkeletonRecursive(child, depth + 1);
727
- });
728
- }
729
- removeSkeletonRecursive(element) {
730
- this.renderer.removeStyle(element, 'color');
731
- this.renderer.removeStyle(element, 'animation');
732
- this.renderer.removeStyle(element, 'background-color');
733
- Array.from(element.children).forEach((child) => {
734
- this.removeSkeletonRecursive(child);
735
- });
802
+ document.head.appendChild(style);
736
803
  }
737
804
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: SkeletonDirective, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive });
738
805
  static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.1.1", type: SkeletonDirective, isStandalone: true, selector: "[appSkeleton]", inputs: { $isLoading: { classPropertyName: "$isLoading", publicName: "appSkeleton", isSignal: true, isRequired: true, transformFunction: null }, $lightColor: { classPropertyName: "$lightColor", publicName: "$lightColor", isSignal: true, isRequired: true, transformFunction: null }, $darkColor: { classPropertyName: "$darkColor", publicName: "$darkColor", isSignal: true, isRequired: true, transformFunction: null } }, ngImport: i0 });