From 2e6bb6416d8abf61c3099229953c95650c7ef4f2 Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Mon, 18 Mar 2024 11:20:43 -0500 Subject: [PATCH] VizTooltips: Fix position during bottom or right edge initial hover (#84623) --- .../uPlot/plugins/TooltipPlugin2.tsx | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/packages/grafana-ui/src/components/uPlot/plugins/TooltipPlugin2.tsx b/packages/grafana-ui/src/components/uPlot/plugins/TooltipPlugin2.tsx index d1aaa71e698..0a54874a5d0 100644 --- a/packages/grafana-ui/src/components/uPlot/plugins/TooltipPlugin2.tsx +++ b/packages/grafana-ui/src/components/uPlot/plugins/TooltipPlugin2.tsx @@ -413,8 +413,12 @@ export const TooltipPlugin2 = ({ _someSeriesIdx = seriesIdxs.some((v, i) => i > 0 && v != null); viaSync = u.cursor.event == null; + let prevIsHovering = _isHovering; updateHovering(); - scheduleRender(); + + if (_isHovering || _isHovering !== prevIsHovering) { + scheduleRender(); + } }); const scrollbarWidth = 16; @@ -520,8 +524,32 @@ export const TooltipPlugin2 = ({ if (domRef.current != null) { size.observer.observe(domRef.current); + + // since the above observer is attached after container is in DOM, we need to manually update sizeRef + // and re-trigger a cursor move to do initial positioning math + const { width, height } = domRef.current.getBoundingClientRect(); + size.width = width; + size.height = height; + + const event = plot!.cursor.event; + + // if not viaSync, re-dispatch real event + if (event != null) { + plot!.over.dispatchEvent(event); + } else { + plot!.setCursor( + { + left: plot!.cursor.left!, + top: plot!.cursor.top!, + }, + true + ); + } + } else { + size.width = 0; + size.height = 0; } - }, [domRef.current]); + }, [isHovering]); if (plot && isHovering) { return createPortal(