Skip to content

Commit 3f8bcf6

Browse files
committed
fix: use offsetWidth/offsetHeight instead of getComputedStyle for clone dimensions
getComputedStyle().width/height can return empty strings during the initial render cycle (before styles are fully computed), causing the measurement clone to have 0x0 dimensions. offsetWidth/offsetHeight are more reliable as they return the layout dimensions immediately.
1 parent d7767a0 commit 3f8bcf6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/hooks/useAlign.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,12 @@ export default function useAlign(
192192
// (transform: scale()), causing getBoundingClientRect() to return
193193
// incorrect values and producing wildly wrong popup positions.
194194
const measureEl = popupElement.cloneNode(false) as HTMLElement;
195-
// Copy computed dimensions to the clone since cloneNode(false) produces
195+
// Copy layout dimensions to the clone since cloneNode(false) produces
196196
// an empty element whose size would otherwise collapse to 0x0.
197-
const { height, width } = win.getComputedStyle(popupElement);
198-
measureEl.style.width = width;
199-
measureEl.style.height = height;
197+
// Use offsetWidth/offsetHeight instead of getComputedStyle because
198+
// computed styles may return empty during the initial render cycle.
199+
measureEl.style.width = `${popupElement.offsetWidth}px`;
200+
measureEl.style.height = `${popupElement.offsetHeight}px`;
200201
measureEl.style.left = '0';
201202
measureEl.style.top = '0';
202203
measureEl.style.right = 'auto';
@@ -231,6 +232,7 @@ export default function useAlign(
231232
// Measure from the temporary element (not affected by CSS transforms
232233
// or transitions on the popup).
233234
const popupRect = measureEl.getBoundingClientRect();
235+
const { height, width } = win.getComputedStyle(popupElement);
234236
popupRect.x = popupRect.x ?? popupRect.left;
235237
popupRect.y = popupRect.y ?? popupRect.top;
236238
const {

0 commit comments

Comments
 (0)