Skip to content

Commit 15cac88

Browse files
committed
refactor(modal): add early guard for MutationObserver
1 parent 27299f0 commit 15cac88

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

core/src/components/modal/modal.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,10 @@ export class Modal implements ComponentInterface, OverlayInterface {
11661166
}
11671167

11681168
private initParentRemovalObserver() {
1169+
if (typeof MutationObserver === 'undefined') {
1170+
return;
1171+
}
1172+
11691173
// Only observe if we have a cached parent and are in browser environment
11701174
if (typeof window === 'undefined' || !this.cachedOriginalParent) {
11711175
return;
@@ -1184,35 +1188,33 @@ export class Modal implements ComponentInterface, OverlayInterface {
11841188
return;
11851189
}
11861190

1187-
if (typeof MutationObserver !== 'undefined') {
1188-
this.parentRemovalObserver = new MutationObserver((mutations) => {
1189-
mutations.forEach((mutation) => {
1190-
if (mutation.type === 'childList' && mutation.removedNodes.length > 0) {
1191-
// Check if our cached original parent was removed
1192-
const cachedParentWasRemoved = Array.from(mutation.removedNodes).some((node) => {
1193-
const isDirectMatch = node === this.cachedOriginalParent;
1194-
const isContainedMatch = this.cachedOriginalParent
1195-
? (node as HTMLElement).contains?.(this.cachedOriginalParent)
1196-
: false;
1197-
return isDirectMatch || isContainedMatch;
1198-
});
1199-
1200-
// Also check if parent is no longer connected to DOM
1201-
const cachedParentDisconnected = this.cachedOriginalParent && !this.cachedOriginalParent.isConnected;
1202-
1203-
if (cachedParentWasRemoved || cachedParentDisconnected) {
1204-
this.dismiss(undefined, 'parent-removed');
1205-
}
1191+
this.parentRemovalObserver = new MutationObserver((mutations) => {
1192+
mutations.forEach((mutation) => {
1193+
if (mutation.type === 'childList' && mutation.removedNodes.length > 0) {
1194+
// Check if our cached original parent was removed
1195+
const cachedParentWasRemoved = Array.from(mutation.removedNodes).some((node) => {
1196+
const isDirectMatch = node === this.cachedOriginalParent;
1197+
const isContainedMatch = this.cachedOriginalParent
1198+
? (node as HTMLElement).contains?.(this.cachedOriginalParent)
1199+
: false;
1200+
return isDirectMatch || isContainedMatch;
1201+
});
1202+
1203+
// Also check if parent is no longer connected to DOM
1204+
const cachedParentDisconnected = this.cachedOriginalParent && !this.cachedOriginalParent.isConnected;
1205+
1206+
if (cachedParentWasRemoved || cachedParentDisconnected) {
1207+
this.dismiss(undefined, 'parent-removed');
12061208
}
1207-
});
1209+
}
12081210
});
1211+
});
12091212

1210-
// Observe with subtree to catch nested removals
1211-
this.parentRemovalObserver.observe(grandParent, {
1212-
childList: true,
1213-
subtree: true,
1214-
});
1215-
}
1213+
// Observe with subtree to catch nested removals
1214+
this.parentRemovalObserver.observe(grandParent, {
1215+
childList: true,
1216+
subtree: true,
1217+
});
12161218
}
12171219

12181220
private cleanupParentRemovalObserver() {

0 commit comments

Comments
 (0)