duck-dev-lib 0.0.76 → 0.0.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.
@@ -5956,6 +5956,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.1", ngImpor
5956
5956
 
5957
5957
  class DuckDevSegmentNeobrutal {
5958
5958
  cdr = inject(ChangeDetectorRef);
5959
+ destroyRef = inject(DestroyRef);
5960
+ host = inject(ElementRef);
5959
5961
  value = input(undefined, { ...(ngDevMode ? { debugName: "value" } : {}) });
5960
5962
  color = input(AccentEnumColor.White, { ...(ngDevMode ? { debugName: "color" } : {}) });
5961
5963
  valueChange = output();
@@ -5967,15 +5969,22 @@ class DuckDevSegmentNeobrutal {
5967
5969
  indicatorPosition = 0;
5968
5970
  indicatorWidth = 0;
5969
5971
  segmentStyle = computed(() => this.getSegmentStyle(this.color()), { ...(ngDevMode ? { debugName: "segmentStyle" } : {}) });
5972
+ resizeObserver = null;
5973
+ intersectionObserver = null;
5974
+ frameId = null;
5975
+ settleFrameId = null;
5970
5976
  constructor() {
5971
5977
  afterNextRender(() => {
5972
5978
  this.rebuildButtons();
5973
5979
  if (!this.selectedValue && this.buttons.length > 0) {
5974
5980
  this.selectedValue = this.buttons[0].value;
5975
5981
  }
5976
- this.updateIndicator(this.getSelectedIndex());
5982
+ this.watchContainerSize();
5983
+ this.watchHostVisibility();
5984
+ this.scheduleIndicatorUpdate();
5977
5985
  this.cdr.markForCheck();
5978
5986
  });
5987
+ this.destroyRef.onDestroy(() => this.destroyObservers());
5979
5988
  }
5980
5989
  ngAfterContentInit() {
5981
5990
  this.rebuildButtons();
@@ -5983,7 +5992,7 @@ class DuckDevSegmentNeobrutal {
5983
5992
  this.selectedValue = incoming ?? this.selectedValue;
5984
5993
  this.segmentButtons.changes.subscribe(() => {
5985
5994
  this.rebuildButtons();
5986
- setTimeout(() => this.updateIndicator(this.getSelectedIndex()));
5995
+ this.scheduleIndicatorUpdate();
5987
5996
  });
5988
5997
  }
5989
5998
  selectButton(value, index) {
@@ -5994,7 +6003,7 @@ class DuckDevSegmentNeobrutal {
5994
6003
  this.selectedValue = value;
5995
6004
  this.valueChange.emit(value);
5996
6005
  this.ionChange.emit({ detail: { value } });
5997
- this.updateIndicator(index);
6006
+ this.scheduleIndicatorUpdate(index);
5998
6007
  }
5999
6008
  rebuildButtons() {
6000
6009
  const btns = this.segmentButtons?.toArray() ?? [];
@@ -6014,6 +6023,63 @@ class DuckDevSegmentNeobrutal {
6014
6023
  const button = buttonElements[index];
6015
6024
  this.indicatorWidth = button.offsetWidth;
6016
6025
  this.indicatorPosition = button.offsetLeft;
6026
+ this.cdr.markForCheck();
6027
+ }
6028
+ }
6029
+ scheduleIndicatorUpdate(index = this.getSelectedIndex()) {
6030
+ if (this.frameId !== null) {
6031
+ cancelAnimationFrame(this.frameId);
6032
+ }
6033
+ this.frameId = requestAnimationFrame(() => {
6034
+ this.updateIndicator(index);
6035
+ this.frameId = requestAnimationFrame(() => {
6036
+ this.updateIndicator(index);
6037
+ this.frameId = null;
6038
+ });
6039
+ });
6040
+ }
6041
+ watchContainerSize() {
6042
+ const container = this.buttonsContainer().nativeElement;
6043
+ if (typeof ResizeObserver === 'undefined') {
6044
+ return;
6045
+ }
6046
+ this.resizeObserver = new ResizeObserver(() => {
6047
+ this.scheduleSettledIndicatorUpdate();
6048
+ });
6049
+ this.resizeObserver.observe(container);
6050
+ }
6051
+ watchHostVisibility() {
6052
+ if (typeof IntersectionObserver === 'undefined') {
6053
+ return;
6054
+ }
6055
+ this.intersectionObserver = new IntersectionObserver((entries) => {
6056
+ const entry = entries[0];
6057
+ if (entry?.isIntersecting) {
6058
+ this.scheduleSettledIndicatorUpdate();
6059
+ }
6060
+ });
6061
+ this.intersectionObserver.observe(this.host.nativeElement);
6062
+ }
6063
+ scheduleSettledIndicatorUpdate() {
6064
+ this.scheduleIndicatorUpdate();
6065
+ if (this.settleFrameId !== null) {
6066
+ cancelAnimationFrame(this.settleFrameId);
6067
+ }
6068
+ this.settleFrameId = requestAnimationFrame(() => {
6069
+ this.settleFrameId = requestAnimationFrame(() => {
6070
+ this.scheduleIndicatorUpdate();
6071
+ this.settleFrameId = null;
6072
+ });
6073
+ });
6074
+ }
6075
+ destroyObservers() {
6076
+ this.resizeObserver?.disconnect();
6077
+ this.intersectionObserver?.disconnect();
6078
+ if (this.frameId !== null) {
6079
+ cancelAnimationFrame(this.frameId);
6080
+ }
6081
+ if (this.settleFrameId !== null) {
6082
+ cancelAnimationFrame(this.settleFrameId);
6017
6083
  }
6018
6084
  }
6019
6085
  getSegmentStyle(color) {