@ibragim-dev-org/ui-forge-library 1.4.35 → 1.4.36

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.
@@ -2,7 +2,7 @@ import * as i2$1 from '@angular/router';
2
2
  import { NavigationEnd, NavigationStart, ActivationEnd } from '@angular/router';
3
3
  import { BehaviorSubject, filter, take, scheduled, fromEvent, asyncScheduler, mergeAll, tap, debounceTime, Subject, Subscription, timer, switchMap, interval, takeWhile, finalize, takeUntil as takeUntil$1 } from 'rxjs';
4
4
  import * as i0 from '@angular/core';
5
- import { model, effect, Directive, Injectable, output, Input, input, PLATFORM_ID, Inject, EventEmitter, Output, Pipe, Attribute, ChangeDetectionStrategy, Component, signal, computed, viewChild, untracked, Injector, afterRenderEffect, contentChildren, contentChild, ViewChildren } from '@angular/core';
5
+ import { model, effect, Directive, Injectable, output, Input, input, PLATFORM_ID, Inject, EventEmitter, Output, inject, Pipe, Attribute, ChangeDetectionStrategy, Component, signal, computed, viewChild, untracked, Injector, afterRenderEffect, contentChildren, contentChild, ViewChildren } from '@angular/core';
6
6
  import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
7
7
  import { isPlatformBrowser, DOCUMENT, NgStyle } from '@angular/common';
8
8
  import { throttleTime, tap as tap$1, takeUntil, debounceTime as debounceTime$1 } from 'rxjs/operators';
@@ -787,18 +787,25 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.1", ngImpor
787
787
 
788
788
  class StableScrollDirective {
789
789
  el;
790
- resizeObserver;
790
+ platformId = inject(PLATFORM_ID);
791
+ isBrowser = isPlatformBrowser(this.platformId);
791
792
  destroy$ = new Subject();
792
793
  scrolling$ = new Subject();
793
794
  _isScrolling = false;
795
+ mutationObserver;
796
+ restoreFrameId = null;
797
+ restoreMicrotaskPending = false;
794
798
  // ---- thresholds ----
795
799
  $topThreshold = input(0, ...(ngDevMode ? [{ debugName: "$topThreshold" }] : []));
796
800
  $bottomThreshold = input(0, ...(ngDevMode ? [{ debugName: "$bottomThreshold" }] : []));
797
801
  reachedTop = output();
798
802
  reachedBottom = output();
799
803
  // ---- anchor ----
800
- anchorElement = null;
801
- anchorOffset = 0;
804
+ anchorSnapshot = null;
805
+ // ---- direction tracking ----
806
+ lastScrollTop = 0;
807
+ scrollingUp = false;
808
+ scrollingDown = false;
802
809
  constructor(elRef) {
803
810
  this.el = elRef.nativeElement;
804
811
  }
@@ -806,92 +813,156 @@ class StableScrollDirective {
806
813
  return this._isScrolling;
807
814
  }
808
815
  ngAfterViewInit() {
809
- // scroll listener
816
+ if (!this.isBrowser) {
817
+ return;
818
+ }
819
+ this.lastScrollTop = this.el.scrollTop;
820
+ this.refreshAnchorSnapshot();
821
+ this.observeContentChanges();
810
822
  fromEvent(this.el, 'scroll')
811
823
  .pipe(throttleTime(50), tap$1(() => {
824
+ const { scrollTop } = this.el;
825
+ // detect direction
826
+ this.scrollingUp = scrollTop < this.lastScrollTop;
827
+ this.scrollingDown = scrollTop > this.lastScrollTop;
828
+ this.lastScrollTop = scrollTop;
812
829
  this._isScrolling = true;
813
830
  this.scrolling$.next();
814
831
  this.checkThresholds();
832
+ this.refreshAnchorSnapshot();
815
833
  }), takeUntil(this.destroy$))
816
834
  .subscribe();
817
- // scroll end detection
818
835
  this.scrolling$
819
836
  .pipe(debounceTime$1(120), tap$1(() => (this._isScrolling = false)), takeUntil(this.destroy$))
820
837
  .subscribe();
821
- // element resize
822
- this.resizeObserver = new ResizeObserver(() => {
823
- if (!this._isScrolling) {
824
- this.captureAnchor();
825
- requestAnimationFrame(() => this.restoreAnchor());
826
- }
827
- });
828
- this.resizeObserver.observe(this.el);
829
- // window resize
830
- fromEvent(window, 'resize')
831
- .pipe(throttleTime(50), tap$1(() => {
832
- if (!this._isScrolling) {
833
- this.captureAnchor();
834
- requestAnimationFrame(() => this.restoreAnchor());
835
- }
836
- }), takeUntil(this.destroy$))
837
- .subscribe();
838
838
  }
839
839
  ngOnDestroy() {
840
+ this.mutationObserver?.disconnect();
841
+ if (this.restoreFrameId !== null && this.isBrowser) {
842
+ cancelAnimationFrame(this.restoreFrameId);
843
+ }
840
844
  this.destroy$.next();
841
845
  this.destroy$.complete();
842
- if (this.resizeObserver) {
843
- this.resizeObserver.disconnect();
844
- }
845
846
  }
846
847
  /** Call BEFORE data changes */
847
848
  captureBeforeChange() {
848
- this.captureAnchor();
849
+ this.refreshAnchorSnapshot();
849
850
  }
850
851
  /** Call AFTER render */
851
852
  restoreScrollPosition() {
852
- this.restoreAnchor();
853
+ this.scheduleRestore();
853
854
  }
854
855
  // ---- threshold logic ----
855
856
  checkThresholds() {
856
857
  const { scrollTop, scrollHeight, clientHeight } = this.el;
857
- // top threshold
858
858
  const topThreshold = this.$topThreshold();
859
859
  const topPx = topThreshold <= 1 ? scrollHeight * topThreshold : topThreshold;
860
- if (scrollTop <= topPx) {
861
- this.reachedTop.emit();
862
- }
863
- // bottom threshold
864
860
  const bottomThreshold = this.$bottomThreshold();
865
861
  const bottomPx = bottomThreshold <= 1 ? scrollHeight * bottomThreshold : bottomThreshold;
866
- if (scrollTop + clientHeight >= scrollHeight - bottomPx) {
862
+ // emit top ONLY when scrolling UP
863
+ if (this.scrollingUp && scrollTop <= topPx) {
864
+ this.reachedTop.emit();
865
+ }
866
+ // emit bottom ONLY when scrolling DOWN
867
+ if (this.scrollingDown && scrollTop + clientHeight >= scrollHeight - bottomPx) {
867
868
  this.reachedBottom.emit();
868
869
  }
869
870
  }
870
- // ---- anchor logic ----
871
- captureAnchor() {
872
- const el = this.el;
873
- const midY = el.scrollTop + el.clientHeight * 0.5;
874
- const children = Array.from(el.children);
875
- const anchor = children.find((c) => {
876
- const top = c.offsetTop;
877
- const bottom = top + c.offsetHeight;
878
- return top <= midY && bottom >= midY;
871
+ observeContentChanges() {
872
+ if (typeof MutationObserver === 'undefined') {
873
+ return;
874
+ }
875
+ this.mutationObserver = new MutationObserver((mutations) => {
876
+ if (!mutations.some((mutation) => mutation.addedNodes.length || mutation.removedNodes.length)) {
877
+ return;
878
+ }
879
+ if (!this.anchorSnapshot) {
880
+ this.refreshAnchorSnapshot();
881
+ return;
882
+ }
883
+ this.scheduleRestore();
879
884
  });
880
- if (!anchor) {
881
- this.anchorElement = null;
885
+ this.mutationObserver.observe(this.el, {
886
+ childList: true,
887
+ });
888
+ }
889
+ scheduleRestore() {
890
+ if (!this.isBrowser || this.restoreMicrotaskPending) {
891
+ return;
892
+ }
893
+ this.restoreMicrotaskPending = true;
894
+ queueMicrotask(() => {
895
+ this.restoreMicrotaskPending = false;
896
+ this.restoreFromSnapshot();
897
+ this.scheduleAnimationFrameRestore();
898
+ });
899
+ }
900
+ restoreFromSnapshot() {
901
+ if (!this.anchorSnapshot) {
902
+ return;
903
+ }
904
+ const { element, topOffset } = this.anchorSnapshot;
905
+ if (!element.isConnected || element.parentElement !== this.el) {
906
+ this.anchorSnapshot = null;
907
+ return;
908
+ }
909
+ const containerTop = this.el.getBoundingClientRect().top;
910
+ const nextTopOffset = element.getBoundingClientRect().top - containerTop;
911
+ const delta = nextTopOffset - topOffset;
912
+ if (delta !== 0) {
913
+ this.el.scrollTop += delta;
914
+ }
915
+ this.lastScrollTop = this.el.scrollTop;
916
+ this.refreshAnchorSnapshot();
917
+ }
918
+ scheduleAnimationFrameRestore() {
919
+ if (!this.isBrowser) {
882
920
  return;
883
921
  }
884
- this.anchorElement = anchor;
885
- this.anchorOffset = midY - anchor.offsetTop;
922
+ if (this.restoreFrameId !== null) {
923
+ cancelAnimationFrame(this.restoreFrameId);
924
+ }
925
+ this.restoreFrameId = requestAnimationFrame(() => {
926
+ this.restoreFrameId = null;
927
+ this.restoreFromSnapshot();
928
+ });
886
929
  }
887
- restoreAnchor() {
888
- if (!this.anchorElement)
930
+ refreshAnchorSnapshot() {
931
+ const anchor = this.resolveAnchorElement();
932
+ if (!anchor) {
933
+ this.anchorSnapshot = null;
889
934
  return;
890
- const el = this.el;
891
- const anchor = this.anchorElement;
892
- const midY = anchor.offsetTop + this.anchorOffset;
893
- el.scrollTop = midY - el.clientHeight * 0.5;
894
- this.anchorElement = null;
935
+ }
936
+ const containerTop = this.el.getBoundingClientRect().top;
937
+ const topOffset = anchor.getBoundingClientRect().top - containerTop;
938
+ this.anchorSnapshot = {
939
+ element: anchor,
940
+ topOffset,
941
+ };
942
+ }
943
+ resolveAnchorElement() {
944
+ const containerRect = this.el.getBoundingClientRect();
945
+ const candidates = this.resolveAnchorCandidates();
946
+ if (!candidates.length) {
947
+ return null;
948
+ }
949
+ const visibleCandidate = candidates.find((candidate) => {
950
+ const rect = candidate.getBoundingClientRect();
951
+ return rect.bottom > containerRect.top && rect.top < containerRect.bottom;
952
+ });
953
+ if (visibleCandidate) {
954
+ return visibleCandidate;
955
+ }
956
+ return candidates.reduce((closest, candidate) => {
957
+ const closestDistance = Math.abs(closest.getBoundingClientRect().top - containerRect.top);
958
+ const candidateDistance = Math.abs(candidate.getBoundingClientRect().top - containerRect.top);
959
+ return candidateDistance < closestDistance ? candidate : closest;
960
+ });
961
+ }
962
+ resolveAnchorCandidates() {
963
+ const children = Array.from(this.el.children);
964
+ const messageChildren = children.filter((child) => child.classList.contains('message'));
965
+ return messageChildren.length ? messageChildren : children;
895
966
  }
896
967
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: StableScrollDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
897
968
  static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "21.1.1", type: StableScrollDirective, isStandalone: true, selector: "[stableScroll]", inputs: { $topThreshold: { classPropertyName: "$topThreshold", publicName: "$topThreshold", isSignal: true, isRequired: false, transformFunction: null }, $bottomThreshold: { classPropertyName: "$bottomThreshold", publicName: "$bottomThreshold", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { reachedTop: "reachedTop", reachedBottom: "reachedBottom" }, ngImport: i0 });