@ibragim-dev-org/ui-forge-library 1.4.75 → 1.4.77

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.
@@ -1,11 +1,10 @@
1
1
  import * as i2 from '@angular/router';
2
2
  import { NavigationEnd, NavigationStart, ActivationEnd } from '@angular/router';
3
- import { BehaviorSubject, filter, take, scheduled, fromEvent, asyncScheduler, mergeAll, tap, debounceTime, Subject, Subscription, timer, switchMap, interval, takeWhile, finalize, ReplaySubject, takeUntil as takeUntil$1 } from 'rxjs';
3
+ import { BehaviorSubject, filter, take, scheduled, fromEvent, asyncScheduler, mergeAll, tap, debounceTime, Subject, takeUntil, throttleTime, Subscription, timer, switchMap, interval, takeWhile, finalize, ReplaySubject } from 'rxjs';
4
4
  import * as i0 from '@angular/core';
5
- import { model, effect, Directive, Injectable, output, Input, input, PLATFORM_ID, Inject, EventEmitter, Output, signal, inject, Pipe, Attribute, ChangeDetectionStrategy, Component, 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, signal, inject, ElementRef, NgZone, Pipe, Attribute, ChangeDetectionStrategy, Component, 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
- import { throttleTime, tap as tap$1, takeUntil, debounceTime as debounceTime$1 } from 'rxjs/operators';
9
8
  import { ulid } from 'ulid';
10
9
  import * as i1 from '@angular/cdk/overlay';
11
10
  import { OverlayConfig, FlexibleConnectedPositionStrategy } from '@angular/cdk/overlay';
@@ -869,32 +868,30 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.1", ngImpor
869
868
  args: [PLATFORM_ID]
870
869
  }] }], propDecorators: { sizeChange: [{ type: i0.Output, args: ["sizeChange"] }] } });
871
870
 
871
+ const ANCHOR_CLASS = 'ui-forge-anchor';
872
+ const EDGE_PIN_PX = 2;
873
+ // Drop-in replacement for ui-forge-library's StableScrollDirective (same inputs, outputs and API).
874
+ // Keeps the topmost visible `.ui-forge-anchor` child in place while content above it is added, removed or resized,
875
+ // like native scroll anchoring - which is switched off on the host so the browser doesn't correct a second time.
872
876
  class StableScrollDirective {
873
- el;
874
- platformId = inject(PLATFORM_ID);
875
- isBrowser = isPlatformBrowser(this.platformId);
876
- destroy$ = new Subject();
877
- scrolling$ = new Subject();
878
- _isScrolling = false;
879
- mutationObserver;
880
- restoreFrameId = null;
881
- restoreMicrotaskPending = false;
882
- // Restoration only runs after captureBeforeChange(); otherwise the element scrolls natively.
883
- restoreArmed = false;
884
- // ---- thresholds ----
885
877
  $topThreshold = input(0, ...(ngDevMode ? [{ debugName: "$topThreshold" }] : []));
886
878
  $bottomThreshold = input(0, ...(ngDevMode ? [{ debugName: "$bottomThreshold" }] : []));
887
879
  reachedTop = output();
888
880
  reachedBottom = output();
889
- // ---- anchor ----
890
- anchorSnapshot = null;
891
- // ---- direction tracking ----
881
+ el = inject(ElementRef).nativeElement;
882
+ zone = inject(NgZone);
883
+ isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
884
+ destroy$ = new Subject();
885
+ scrolling$ = new Subject();
886
+ mutationObserver;
887
+ resizeObserver;
888
+ anchor = null;
892
889
  lastScrollTop = 0;
893
- scrollingUp = false;
894
- scrollingDown = false;
895
- constructor(elRef) {
896
- this.el = elRef.nativeElement;
897
- }
890
+ wasPinnedToTop = false;
891
+ wasPinnedToBottom = false;
892
+ restoreArmed = false;
893
+ disarmFrameId = null;
894
+ _isScrolling = false;
898
895
  get scrolling() {
899
896
  return this._isScrolling;
900
897
  }
@@ -903,29 +900,31 @@ class StableScrollDirective {
903
900
  return;
904
901
  }
905
902
  this.lastScrollTop = this.el.scrollTop;
906
- this.refreshAnchorSnapshot();
907
- this.observeContentChanges();
908
- fromEvent(this.el, 'scroll')
909
- .pipe(throttleTime(50), tap$1(() => {
910
- const { scrollTop } = this.el;
911
- // detect direction
912
- this.scrollingUp = scrollTop < this.lastScrollTop;
913
- this.scrollingDown = scrollTop > this.lastScrollTop;
914
- this.lastScrollTop = scrollTop;
915
- this._isScrolling = true;
916
- this.scrolling$.next();
917
- this.checkThresholds();
918
- this.refreshAnchorSnapshot();
919
- }), takeUntil(this.destroy$))
920
- .subscribe();
921
- this.scrolling$
922
- .pipe(debounceTime$1(120), tap$1(() => (this._isScrolling = false)), takeUntil(this.destroy$))
923
- .subscribe();
903
+ this.refreshAnchor();
904
+ // Scroll handling stays outside Angular; only the threshold outputs re-enter the zone
905
+ this.zone.runOutsideAngular(() => {
906
+ this.observeContent();
907
+ const scroll$ = fromEvent(this.el, 'scroll', { passive: true });
908
+ // Every scroll event, so the anchor is always the element actually on screen
909
+ scroll$.pipe(takeUntil(this.destroy$)).subscribe(() => {
910
+ this._isScrolling = true;
911
+ this.scrolling$.next();
912
+ this.refreshAnchor();
913
+ });
914
+ // Trailing edge too, otherwise the final position of a scroll is never checked
915
+ scroll$
916
+ .pipe(throttleTime(50, asyncScheduler, { leading: true, trailing: true }), tap(() => this.checkThresholds()), takeUntil(this.destroy$))
917
+ .subscribe();
918
+ this.scrolling$
919
+ .pipe(debounceTime(120), tap(() => (this._isScrolling = false)), takeUntil(this.destroy$))
920
+ .subscribe();
921
+ });
924
922
  }
925
923
  ngOnDestroy() {
926
924
  this.mutationObserver?.disconnect();
927
- if (this.restoreFrameId !== null && this.isBrowser) {
928
- cancelAnimationFrame(this.restoreFrameId);
925
+ this.resizeObserver?.disconnect();
926
+ if (this.disarmFrameId !== null && this.isBrowser) {
927
+ cancelAnimationFrame(this.disarmFrameId);
929
928
  }
930
929
  this.destroy$.next();
931
930
  this.destroy$.complete();
@@ -933,134 +932,146 @@ class StableScrollDirective {
933
932
  /** Call BEFORE data changes */
934
933
  captureBeforeChange() {
935
934
  this.restoreArmed = true;
936
- this.refreshAnchorSnapshot();
935
+ this.refreshAnchor();
937
936
  }
938
937
  /** Call AFTER render */
939
938
  restoreScrollPosition() {
940
- this.scheduleRestore();
939
+ this.compensate();
941
940
  }
942
- // ---- threshold logic ----
943
941
  checkThresholds() {
944
942
  const { scrollTop, scrollHeight, clientHeight } = this.el;
943
+ const movedUp = scrollTop < this.lastScrollTop;
944
+ const movedDown = scrollTop > this.lastScrollTop;
945
+ this.lastScrollTop = scrollTop;
945
946
  const topThreshold = this.$topThreshold();
946
947
  const topPx = topThreshold <= 1 ? scrollHeight * topThreshold : topThreshold;
947
948
  const bottomThreshold = this.$bottomThreshold();
948
949
  const bottomPx = bottomThreshold <= 1 ? scrollHeight * bottomThreshold : bottomThreshold;
949
- // emit top ONLY when scrolling UP
950
- if (this.scrollingUp && scrollTop <= topPx) {
951
- this.reachedTop.emit();
950
+ if (movedUp && scrollTop <= topPx) {
951
+ this.zone.run(() => this.reachedTop.emit());
952
952
  }
953
- // emit bottom ONLY when scrolling DOWN
954
- if (this.scrollingDown && scrollTop + clientHeight >= scrollHeight - bottomPx) {
955
- this.reachedBottom.emit();
953
+ if (movedDown && scrollTop + clientHeight >= scrollHeight - bottomPx) {
954
+ this.zone.run(() => this.reachedBottom.emit());
956
955
  }
957
956
  }
958
- observeContentChanges() {
957
+ observeContent() {
958
+ if (typeof ResizeObserver !== 'undefined') {
959
+ // Media loading or a message re-rendering above the viewport changes heights without adding nodes
960
+ this.resizeObserver = new ResizeObserver(() => this.compensate());
961
+ Array.from(this.el.children).forEach((child) => this.resizeObserver.observe(child));
962
+ }
959
963
  if (typeof MutationObserver === 'undefined') {
960
964
  return;
961
965
  }
962
966
  this.mutationObserver = new MutationObserver((mutations) => {
963
- if (!mutations.some((mutation) => mutation.addedNodes.length || mutation.removedNodes.length)) {
967
+ let hasListChange = false;
968
+ for (const mutation of mutations) {
969
+ mutation.addedNodes.forEach((node) => {
970
+ if (node instanceof Element) {
971
+ hasListChange = true;
972
+ this.resizeObserver?.observe(node);
973
+ }
974
+ });
975
+ mutation.removedNodes.forEach((node) => {
976
+ if (node instanceof Element) {
977
+ hasListChange = true;
978
+ this.resizeObserver?.unobserve(node);
979
+ }
980
+ });
981
+ }
982
+ if (!hasListChange) {
964
983
  return;
965
984
  }
966
- if (!this.restoreArmed) {
967
- this.refreshAnchorSnapshot();
985
+ // Unrequested changes at an edge show up rather than being held off-screen: a new message at the bottom
986
+ // (the host scrolls to it) or a chat moving to the top of a list
987
+ if (!this.restoreArmed && (this.wasPinnedToTop || this.wasPinnedToBottom)) {
988
+ this.refreshAnchor();
968
989
  return;
969
990
  }
970
- this.scheduleRestore();
971
- });
972
- this.mutationObserver.observe(this.el, {
973
- childList: true,
974
- });
975
- }
976
- scheduleRestore() {
977
- if (!this.isBrowser || this.restoreMicrotaskPending) {
978
- return;
979
- }
980
- this.restoreMicrotaskPending = true;
981
- queueMicrotask(() => {
982
- this.restoreMicrotaskPending = false;
983
- this.restoreFromSnapshot();
984
- this.scheduleAnimationFrameRestore();
991
+ this.compensate();
992
+ this.scheduleDisarm();
985
993
  });
994
+ this.mutationObserver.observe(this.el, { childList: true });
986
995
  }
987
- restoreFromSnapshot() {
988
- if (!this.anchorSnapshot) {
996
+ // Scrolls by exactly how far the anchor moved within the content, then re-anchors
997
+ compensate() {
998
+ const anchor = this.anchor;
999
+ if (!anchor || !anchor.element.isConnected || anchor.element.parentElement !== this.el) {
1000
+ this.refreshAnchor();
989
1001
  return;
990
1002
  }
991
- const { element, topOffset } = this.anchorSnapshot;
992
- if (!element.isConnected || element.parentElement !== this.el) {
993
- this.anchorSnapshot = null;
994
- return;
995
- }
996
- const containerTop = this.el.getBoundingClientRect().top;
997
- const nextTopOffset = element.getBoundingClientRect().top - containerTop;
998
- const delta = nextTopOffset - topOffset;
999
- if (delta !== 0) {
1000
- this.el.scrollTop += delta;
1003
+ const shift = this.contentTopOf(anchor.element) - anchor.contentTop;
1004
+ if (Math.abs(shift) >= 0.5) {
1005
+ this.el.scrollTop += shift;
1006
+ // The browser coalesces this into the next scroll event; shifting the baseline keeps it from reading as
1007
+ // the user scrolling the other way, which would fire the opposite threshold
1008
+ this.lastScrollTop += shift;
1001
1009
  }
1002
- this.lastScrollTop = this.el.scrollTop;
1003
- this.refreshAnchorSnapshot();
1010
+ this.refreshAnchor();
1004
1011
  }
1005
- scheduleAnimationFrameRestore() {
1006
- if (!this.isBrowser) {
1012
+ scheduleDisarm() {
1013
+ if (!this.restoreArmed) {
1007
1014
  return;
1008
1015
  }
1009
- if (this.restoreFrameId !== null) {
1010
- cancelAnimationFrame(this.restoreFrameId);
1016
+ if (this.disarmFrameId !== null) {
1017
+ cancelAnimationFrame(this.disarmFrameId);
1011
1018
  }
1012
- this.restoreFrameId = requestAnimationFrame(() => {
1013
- this.restoreFrameId = null;
1014
- this.restoreFromSnapshot();
1019
+ this.disarmFrameId = requestAnimationFrame(() => {
1020
+ this.disarmFrameId = null;
1021
+ this.compensate();
1015
1022
  this.restoreArmed = false;
1016
1023
  });
1017
1024
  }
1018
- refreshAnchorSnapshot() {
1019
- const anchor = this.resolveAnchorElement();
1020
- if (!anchor) {
1021
- this.anchorSnapshot = null;
1022
- return;
1023
- }
1024
- const containerTop = this.el.getBoundingClientRect().top;
1025
- const topOffset = anchor.getBoundingClientRect().top - containerTop;
1026
- this.anchorSnapshot = {
1027
- element: anchor,
1028
- topOffset,
1029
- };
1030
- }
1031
- resolveAnchorElement() {
1032
- const containerRect = this.el.getBoundingClientRect();
1033
- const candidates = this.resolveAnchorCandidates();
1025
+ refreshAnchor() {
1026
+ const { scrollTop, scrollHeight, clientHeight } = this.el;
1027
+ this.wasPinnedToTop = scrollTop <= EDGE_PIN_PX;
1028
+ this.wasPinnedToBottom = scrollHeight - clientHeight - scrollTop <= EDGE_PIN_PX;
1029
+ const element = this.findTopVisibleCandidate();
1030
+ this.anchor = element ? { element, contentTop: this.contentTopOf(element) } : null;
1031
+ }
1032
+ // Candidates are in document order, so the first one reaching into the viewport is found by binary search
1033
+ findTopVisibleCandidate() {
1034
+ const candidates = this.getCandidates();
1034
1035
  if (!candidates.length) {
1035
1036
  return null;
1036
1037
  }
1037
- const visibleCandidate = candidates.find((candidate) => {
1038
- const rect = candidate.getBoundingClientRect();
1039
- return rect.bottom > containerRect.top && rect.top < containerRect.bottom;
1040
- });
1041
- if (visibleCandidate) {
1042
- return visibleCandidate;
1038
+ const viewportTop = this.el.getBoundingClientRect().top;
1039
+ let low = 0;
1040
+ let high = candidates.length - 1;
1041
+ let found = candidates.length - 1;
1042
+ while (low <= high) {
1043
+ const middle = (low + high) >> 1;
1044
+ if (candidates[middle].getBoundingClientRect().bottom > viewportTop) {
1045
+ found = middle;
1046
+ high = middle - 1;
1047
+ }
1048
+ else {
1049
+ low = middle + 1;
1050
+ }
1043
1051
  }
1044
- return candidates.reduce((closest, candidate) => {
1045
- const closestDistance = Math.abs(closest.getBoundingClientRect().top - containerRect.top);
1046
- const candidateDistance = Math.abs(candidate.getBoundingClientRect().top - containerRect.top);
1047
- return candidateDistance < closestDistance ? candidate : closest;
1048
- });
1052
+ return candidates[found];
1049
1053
  }
1050
- resolveAnchorCandidates() {
1054
+ getCandidates() {
1051
1055
  const children = Array.from(this.el.children);
1052
- const anchor = children.filter((child) => child.classList.contains('ui-forge-anchor'));
1053
- return anchor.length ? anchor : children;
1056
+ const anchors = children.filter((child) => child.classList.contains(ANCHOR_CLASS));
1057
+ return anchors.length ? anchors : children;
1058
+ }
1059
+ contentTopOf(element) {
1060
+ return element.getBoundingClientRect().top - this.el.getBoundingClientRect().top + this.el.scrollTop;
1054
1061
  }
1055
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: StableScrollDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
1056
- 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 });
1062
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: StableScrollDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1063
+ 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" }, host: { properties: { "style.overflow-anchor": "\"none\"" } }, ngImport: i0 });
1057
1064
  }
1058
1065
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.1.1", ngImport: i0, type: StableScrollDirective, decorators: [{
1059
1066
  type: Directive,
1060
1067
  args: [{
1061
1068
  selector: '[stableScroll]',
1069
+ standalone: true,
1070
+ host: {
1071
+ '[style.overflow-anchor]': '"none"',
1072
+ },
1062
1073
  }]
1063
- }], ctorParameters: () => [{ type: i0.ElementRef }], propDecorators: { $topThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "$topThreshold", required: false }] }], $bottomThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "$bottomThreshold", required: false }] }], reachedTop: [{ type: i0.Output, args: ["reachedTop"] }], reachedBottom: [{ type: i0.Output, args: ["reachedBottom"] }] } });
1074
+ }], propDecorators: { $topThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "$topThreshold", required: false }] }], $bottomThreshold: [{ type: i0.Input, args: [{ isSignal: true, alias: "$bottomThreshold", required: false }] }], reachedTop: [{ type: i0.Output, args: ["reachedTop"] }], reachedBottom: [{ type: i0.Output, args: ["reachedBottom"] }] } });
1064
1075
 
1065
1076
  class CenterEllipsisPipe {
1066
1077
  transform(value, visibleStart = 4, visibleEnd = 4) {
@@ -2493,11 +2504,11 @@ class UiModal {
2493
2504
  this.destroyOutsideClickSubscription$.next();
2494
2505
  if (isBrowser()) {
2495
2506
  fromEvent(window, 'scroll')
2496
- .pipe(tap(() => this.closeAll()), takeUntil$1(this.destroyOutsideClickSubscription$))
2507
+ .pipe(tap(() => this.closeAll()), takeUntil(this.destroyOutsideClickSubscription$))
2497
2508
  .subscribe();
2498
2509
  }
2499
2510
  this.router.events
2500
- .pipe(debounceTime(100), tap(() => this.closeAll()), takeUntil$1(this.destroyOutsideClickSubscription$))
2511
+ .pipe(debounceTime(100), tap(() => this.closeAll()), takeUntil(this.destroyOutsideClickSubscription$))
2501
2512
  .subscribe();
2502
2513
  }
2503
2514
  destroy() {
@@ -2645,7 +2656,7 @@ class UiPopupContainerDirective {
2645
2656
  if (!this.popupInstance.isClosing) {
2646
2657
  this.$isShow.set(true);
2647
2658
  }
2648
- }), takeUntil$1(this.destroyAnimationOrigin$), takeUntilDestroyed(this.destroyRef))
2659
+ }), takeUntil(this.destroyAnimationOrigin$), takeUntilDestroyed(this.destroyRef))
2649
2660
  .subscribe();
2650
2661
  if (isBrowser()) {
2651
2662
  fromEvent(window, 'keydown')
@@ -2780,7 +2791,7 @@ class UiPopupImageDirective {
2780
2791
  // -----------------------------
2781
2792
  // MOUSE WHEEL ZOOM
2782
2793
  // -----------------------------
2783
- fromEvent(window, 'wheel', { passive: false })
2794
+ fromEvent(container, 'wheel', { passive: false })
2784
2795
  .pipe(tap((event) => {
2785
2796
  event.preventDefault();
2786
2797
  const rect = container.getBoundingClientRect();
@@ -3329,7 +3340,7 @@ class UiDropdownService extends UiModal {
3329
3340
  positionStrategy.setOrigin({ x: positionAdjustment.left, y: positionAdjustment.top });
3330
3341
  overlayRef.updatePosition();
3331
3342
  }
3332
- }), takeUntil$1(this.destroyOutsideClickSubscription$))
3343
+ }), takeUntil(this.destroyOutsideClickSubscription$))
3333
3344
  .subscribe();
3334
3345
  }
3335
3346
  /**
@@ -3416,7 +3427,7 @@ class UiDropdownService extends UiModal {
3416
3427
  timer(150)
3417
3428
  .pipe(switchMap(() => overlayRef.outsidePointerEvents()), tap(() => {
3418
3429
  this.closeAll();
3419
- }), takeUntil$1(this.destroyOutsideClickSubscription$))
3430
+ }), takeUntil(this.destroyOutsideClickSubscription$))
3420
3431
  .subscribe();
3421
3432
  }
3422
3433
  /**
@@ -3820,7 +3831,7 @@ class UiCopyButtonDirective {
3820
3831
  timer(5000)
3821
3832
  .pipe(tap(() => {
3822
3833
  this.$isCopied.set(false);
3823
- }), takeUntil$1(this.destroyTimer$), takeUntilDestroyed(this.destroyRef))
3834
+ }), takeUntil(this.destroyTimer$), takeUntilDestroyed(this.destroyRef))
3824
3835
  .subscribe();
3825
3836
  })
3826
3837
  .catch((err) => {