Skip to content

Commit 1684a53

Browse files
Improve select dropdown width matching with ResizeObserver
- Replace single useEffect with ResizeObserver for dynamic width tracking - Automatically handles responsive changes, content updates, and parent resizing - More reliable than previous approach that only measured width once on mount - Uses borderBoxSize for more accurate measurements with fallback to offsetWidth - Maintains portal benefits while ensuring dropdown width always matches trigger
1 parent 875bc53 commit 1684a53

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/components/src/ui/select.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,23 @@ export function Select({
5555
const [menuWidth, setMenuWidth] = React.useState<number | undefined>(undefined);
5656

5757
React.useEffect(() => {
58-
if (triggerRef.current) setMenuWidth(triggerRef.current.offsetWidth);
59-
}, []);
58+
if (!triggerRef.current) return;
59+
60+
const observer = new ResizeObserver((entries) => {
61+
for (const entry of entries) {
62+
// Use borderBoxSize for more accurate measurements
63+
const width = entry.borderBoxSize?.[0]?.inlineSize || entry.target.offsetWidth;
64+
setMenuWidth(width);
65+
}
66+
});
67+
68+
observer.observe(triggerRef.current);
69+
70+
// Set initial width immediately
71+
setMenuWidth(triggerRef.current.offsetWidth);
72+
73+
return () => observer.disconnect();
74+
}, []); // Only run once - observer handles all future changes
6075

6176
// Scroll to selected item when dropdown opens
6277
React.useEffect(() => {

0 commit comments

Comments
 (0)