Skip to content

Commit 083b03c

Browse files
committed
chore(ssr): dont initialize MutationObserver if it does not exist (ssr env)
1 parent 36b8322 commit 083b03c

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

projects/igniteui-angular/src/lib/buttonGroup/buttonGroup.component.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
470470

471471
this.mutationObserver = this.setMutationsObserver();
472472

473-
this.mutationObserver.observe(this._el.nativeElement, this.observerConfig);
473+
this.mutationObserver?.observe(this._el.nativeElement, this.observerConfig);
474474
}
475475

476476
/**
@@ -483,7 +483,7 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
483483
this.queryListNotifier$.next();
484484
this.queryListNotifier$.complete();
485485

486-
this.mutationObserver.disconnect();
486+
this.mutationObserver?.disconnect();
487487
}
488488

489489
/**
@@ -513,28 +513,35 @@ export class IgxButtonGroupComponent extends DisplayDensityBase implements After
513513
}
514514
}
515515

516-
this.mutationObserver.observe(this._el.nativeElement, this.observerConfig);
516+
this.mutationObserver?.observe(this._el.nativeElement, this.observerConfig);
517517
}
518518

519519
private setMutationsObserver() {
520-
return new MutationObserver((records, observer) => {
521-
// Stop observing while handling changes
522-
observer.disconnect();
520+
if (typeof MutationObserver !== 'undefined') {
521+
return new MutationObserver((records, observer) => {
522+
// Stop observing while handling changes
523+
observer.disconnect();
523524

524-
const updatedButtons = this.getUpdatedButtons(records);
525+
const updatedButtons = this.getUpdatedButtons(records);
525526

526-
if (updatedButtons.length > 0) {
527-
updatedButtons.forEach((button) => {
528-
const index = this.buttons.map((b) => b.nativeElement).indexOf(button);
529-
const args: IButtonGroupEventArgs = { owner: this, button: this.buttons[index], index };
527+
if (updatedButtons.length > 0) {
528+
updatedButtons.forEach((button) => {
529+
const index = this.buttons.map((b) => b.nativeElement).indexOf(button);
530+
const args: IButtonGroupEventArgs = { owner: this, button: this.buttons[index], index };
530531

531-
this.updateButtonSelectionState(index, args);
532-
});
533-
}
532+
this.updateButtonSelectionState(index, args);
533+
});
534+
}
534535

535-
// Watch for changes again
536-
observer.observe(this._el.nativeElement, this.observerConfig);
537-
});
536+
// Watch for changes again
537+
observer.observe(this._el.nativeElement, this.observerConfig);
538+
539+
// Cleanup function
540+
this._renderer.listen(this._el.nativeElement, 'DOMNodeRemoved', () => {
541+
observer.disconnect();
542+
});
543+
});
544+
}
538545
}
539546

540547
private getUpdatedButtons(records: MutationRecord[]) {

0 commit comments

Comments
 (0)