-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix tooltip not closing #34001
Fix tooltip not closing #34001
Conversation
@@ -49,6 +49,11 @@ export const TooltipPlugin: React.FC<TooltipPluginProps> = ({ | |||
|
|||
// Add uPlot hooks to the config, or re-add when the config changed | |||
useLayoutEffect(() => { | |||
if (plotCtx && plotCtx.plot) { | |||
plotCtx.plot.root.onmouseleave = (event) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you may instead consider binding to .u-over
, so it disappears when leaving the grid area instead of the entire chart.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, where can I find .u-over? I'm a bit lost here and this solution was quite hacky, any guidance appreciated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g.
grafana/public/app/plugins/panel/barchart/bars.ts
Lines 177 to 181 in cf39d5b
const init = (u: uPlot) => { | |
let over = u.root.querySelector('.u-over')! as HTMLElement; | |
over.style.overflow = 'hidden'; | |
over.appendChild(barMark); | |
}; |
(just make sure the querySelector only runs during init and not on every redraw)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i really should just expose this as u.over
in future uPlot versions, even in my own demos i have to do this querySelector dance pretty often.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I think I've cleaned this up a bit. Thanks for the help.
if (plotCtx && plotCtx.plot) { | ||
plotCtx.plot.root.querySelector('.u-over')!.addEventListener('mouseleave', plotMouseLeave); | ||
} | ||
|
||
return () => { | ||
if (plotCtx && plotCtx.plot) { | ||
plotCtx.plot.root.querySelector('.u-over')!.removeEventListener('mouseleave', plotMouseLeave); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel more comfortable now with the event listeners than with onmouseleave, good stuff!
Ah, and it works as expected! |
* Fix tooltip not closing * make this a bit less hacky * use a more specific element for the mouseleave event * Make sure we limit running of effect * Remove console log * Probably don't need useCallback
* Fix tooltip not closing * make this a bit less hacky * use a more specific element for the mouseleave event * Make sure we limit running of effect * Remove console log * Probably don't need useCallback
What this PR does / why we need it:
Fixes a bug where the tooltip on the bar graph stays open when the mouse leaves the plot.