Skip to content

Commit 8f0ebc9

Browse files
Fix ResizeObserver timing issues causing test failures
- Set initial width immediately before setting up ResizeObserver - Ensure original behavior is preserved for compatibility - Prevent timing conflicts that were causing popover rendering issues
1 parent ebac38e commit 8f0ebc9

1 file changed

Lines changed: 16 additions & 21 deletions

File tree

packages/components/src/ui/select.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,24 @@ export function Select({
5757
React.useEffect(() => {
5858
if (!triggerRef.current) return;
5959

60-
// Check if ResizeObserver is available
61-
if (typeof ResizeObserver === 'undefined') {
62-
// Fallback to simple width measurement
63-
setMenuWidth(triggerRef.current.offsetWidth);
64-
return;
65-
}
66-
67-
const observer = new ResizeObserver((entries) => {
68-
for (const entry of entries) {
69-
// Use borderBoxSize for more accurate measurements with fallback
70-
const width = entry.borderBoxSize?.[0]?.inlineSize ??
71-
entry.contentBoxSize?.[0]?.inlineSize ??
72-
(entry.target as HTMLElement).offsetWidth;
73-
setMenuWidth(width);
74-
}
75-
});
76-
77-
observer.observe(triggerRef.current);
78-
79-
// Set initial width immediately
60+
// Set initial width immediately (original behavior)
8061
setMenuWidth(triggerRef.current.offsetWidth);
8162

82-
return () => observer.disconnect();
63+
// Add ResizeObserver for dynamic width tracking if available
64+
if (typeof ResizeObserver !== 'undefined') {
65+
const observer = new ResizeObserver((entries) => {
66+
for (const entry of entries) {
67+
// Use borderBoxSize for more accurate measurements with fallback
68+
const width = entry.borderBoxSize?.[0]?.inlineSize ??
69+
entry.contentBoxSize?.[0]?.inlineSize ??
70+
(entry.target as HTMLElement).offsetWidth;
71+
setMenuWidth(width);
72+
}
73+
});
74+
75+
observer.observe(triggerRef.current);
76+
return () => observer.disconnect();
77+
}
8378
}, []); // Only run once - observer handles all future changes
8479

8580
// Scroll to selected item when dropdown opens

0 commit comments

Comments
 (0)