Skip to content

Commit 19542f0

Browse files
committed
fix(tooltip): fix tooltip position shift
1 parent f52f643 commit 19542f0

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

core/components/atoms/popperWrapper/PopperWrapper.tsx

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,35 @@ export const PopoverContent = React.forwardRef<HTMLDivElement, React.HTMLProps<H
234234

235235
const boundaryElement = appendToBody !== false && document ? document.body : portalRoot;
236236

237+
// Check if reference element exists and position is valid
238+
const hasReference = context.refs.reference.current != null;
237239
const isValidPosition = context.x !== 0 && context.y !== 0;
238-
const showVisibility =
239-
context.middlewareData.hide?.referenceHidden || context.middlewareData.hide?.escaped || !isValidPosition;
240240

241-
const content = isMounted && (
241+
// Check if transform is at (0,0) - floating-ui uses transform for positioning
242+
const floatingStyles = context.floatingStyles || {};
243+
const transform = floatingStyles.transform || '';
244+
// Check if transform is matrix(1, 0, 0, 1, 0, 0) or translate(0px, 0px) which means at origin
245+
const isAtOrigin =
246+
transform.includes('matrix(1, 0, 0, 1, 0, 0)') ||
247+
transform.includes('translate(0px, 0px)') ||
248+
transform.includes('translate(0, 0)');
249+
const hasCalculatedPosition = !isAtOrigin && transform !== '';
250+
251+
// Track if we've had a valid position at least once - use ref for synchronous updates
252+
const hasValidPositionRef = React.useRef(false);
253+
254+
// Update ref synchronously when position becomes valid
255+
if (context.open && isValidPosition && hasReference && hasCalculatedPosition) {
256+
hasValidPositionRef.current = true;
257+
} else if (!context.open) {
258+
hasValidPositionRef.current = false;
259+
}
260+
261+
// Determine if we should show the content
262+
const canShow = hasReference && ((isValidPosition && hasCalculatedPosition) || hasValidPositionRef.current);
263+
const shouldHide = context.middlewareData.hide?.referenceHidden || context.middlewareData.hide?.escaped || !canShow;
264+
265+
const content = isMounted && hasReference && (
242266
// <FloatingFocusManager context={floatingContext}
243267
// // modal={context.modal}
244268
// initialFocus={-1}>
@@ -247,7 +271,10 @@ export const PopoverContent = React.forwardRef<HTMLDivElement, React.HTMLProps<H
247271
style={{
248272
...context.floatingStyles,
249273
...style,
250-
visibility: showVisibility ? 'hidden' : 'visible',
274+
visibility: shouldHide ? 'hidden' : 'visible',
275+
// Completely hide when position is not ready to prevent (0,0) flash
276+
opacity: canShow ? undefined : 0,
277+
pointerEvents: canShow ? undefined : 'none',
251278
}}
252279
{...context.getFloatingProps(props)}
253280
>

0 commit comments

Comments
 (0)