Skip to content

Commit 23ae259

Browse files
authored
fix: feature detection for IntersectionObserver (#4378)
1 parent 0f51b7f commit 23ae259

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

packages/components/src/components/custom-select/custom-select.lite.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -580,16 +580,18 @@ export default function DBCustomSelect(props: DBCustomSelectProps) {
580580
state._infoTextId = mId + '-info';
581581
state._invalidMessage = props.invalidMessage || DEFAULT_INVALID_MESSAGE;
582582

583-
state._observer = new IntersectionObserver((payload) => {
584-
if (detailsRef) {
585-
const entry = payload.find(
586-
({ target }) => target === detailsRef
587-
);
588-
if (entry && !entry.isIntersecting && detailsRef.open) {
589-
detailsRef.open = false;
583+
if (typeof window !== 'undefined' && 'IntersectionObserver' in window) {
584+
state._observer = new IntersectionObserver((payload) => {
585+
if (detailsRef) {
586+
const entry = payload.find(
587+
({ target }) => target === detailsRef
588+
);
589+
if (entry && !entry.isIntersecting && detailsRef.open) {
590+
detailsRef.open = false;
591+
}
590592
}
591-
}
592-
});
593+
});
594+
}
593595
});
594596

595597
onUpdate(() => {

packages/components/src/components/popover/popover.lite.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,19 @@ export default function DBPopover(props: DBPopoverProps) {
130130
_ref.addEventListener(event, () => state.handleLeave());
131131
});
132132

133-
state._observer = new IntersectionObserver((payload) => {
134-
const entry = payload.find(
135-
({ target }) => target === state.getTrigger()
136-
);
137-
if (entry && !entry.isIntersecting) {
138-
state.handleEscape(false);
139-
}
140-
});
133+
if (
134+
typeof window !== 'undefined' &&
135+
'IntersectionObserver' in window
136+
) {
137+
state._observer = new IntersectionObserver((payload) => {
138+
const entry = payload.find(
139+
({ target }) => target === state.getTrigger()
140+
);
141+
if (entry && !entry.isIntersecting) {
142+
state.handleEscape(false);
143+
}
144+
});
145+
}
141146
}
142147
}, [_ref, state.initialized]);
143148

packages/components/src/components/tooltip/tooltip.lite.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,19 @@ export default function DBTooltip(props: DBTooltipProps) {
117117
}
118118
}
119119

120-
state._observer = new IntersectionObserver((payload) => {
121-
const entry = payload.find(
122-
({ target }) => target === state.getParent()
123-
);
124-
if (entry && !entry.isIntersecting) {
125-
state.handleEscape(false);
126-
}
127-
});
120+
if (
121+
typeof window !== 'undefined' &&
122+
'IntersectionObserver' in window
123+
) {
124+
state._observer = new IntersectionObserver((payload) => {
125+
const entry = payload.find(
126+
({ target }) => target === state.getParent()
127+
);
128+
if (entry && !entry.isIntersecting) {
129+
state.handleEscape(false);
130+
}
131+
});
132+
}
128133

129134
state.initialized = false;
130135
}

0 commit comments

Comments
 (0)