Skip to content

Commit f4439c3

Browse files
Replace ResizeObserver with Radix CSS custom property
- Remove complex JavaScript width measurement logic - Use --radix-popover-trigger-width CSS variable instead - Much cleaner approach that leverages Radix UI's built-in functionality - Automatically handles width matching without JavaScript overhead - This should provide perfect width matching with the trigger
1 parent 838e7fd commit f4439c3

1 file changed

Lines changed: 2 additions & 39 deletions

File tree

packages/components/src/ui/select.tsx

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,7 @@ export function Select({
5353
const triggerRef = React.useRef<HTMLButtonElement>(null);
5454
const popoverRef = React.useRef<HTMLDivElement>(null);
5555
const selectedItemRef = React.useRef<HTMLButtonElement>(null);
56-
const [menuWidth, setMenuWidth] = React.useState<number | undefined>(undefined);
57-
58-
React.useEffect(() => {
59-
if (!triggerRef.current) return;
60-
61-
const updateWidth = () => {
62-
if (triggerRef.current) {
63-
const width = triggerRef.current.offsetWidth;
64-
console.log('Setting menu width to:', width); // Debug log
65-
setMenuWidth(width);
66-
}
67-
};
68-
69-
// Set initial width immediately
70-
updateWidth();
71-
72-
// Add ResizeObserver for dynamic width tracking if available
73-
if (typeof ResizeObserver !== 'undefined') {
74-
const observer = new ResizeObserver((entries) => {
75-
for (const entry of entries) {
76-
// Use borderBoxSize for more accurate measurements with fallback
77-
const width = entry.borderBoxSize?.[0]?.inlineSize ??
78-
entry.contentBoxSize?.[0]?.inlineSize ??
79-
(entry.target as HTMLElement).offsetWidth;
80-
console.log('ResizeObserver detected width change:', width); // Debug log
81-
setMenuWidth(width);
82-
}
83-
});
84-
85-
observer.observe(triggerRef.current);
86-
return () => observer.disconnect();
87-
}
88-
}, []); // Only run once - observer handles all future changes
89-
90-
// Debug effect to log menuWidth changes
91-
React.useEffect(() => {
92-
console.log('menuWidth changed to:', menuWidth);
93-
}, [menuWidth]);
56+
// No need for JavaScript width measurement - Radix provides --radix-popover-trigger-width CSS variable
9457

9558
// Scroll to selected item when dropdown opens
9659
React.useEffect(() => {
@@ -181,7 +144,7 @@ export function Select({
181144
// biome-ignore lint/a11y/useSemanticElements: using <div> for PopoverContent to ensure keyboard accessibility and focus management
182145
role="listbox"
183146
id={listboxId}
184-
style={{ width: menuWidth ? `${menuWidth}px` : undefined }}
147+
style={{ width: 'var(--radix-popover-trigger-width)' }}
185148
data-slot="popover-content"
186149
>
187150
<div className="bg-white p-1.5 rounded-md focus:outline-none sm:text-sm w-full">

0 commit comments

Comments
 (0)