feat: add persistOnHover option to keep tooltip visible when hovering over it#12255
Open
almahmudsarker wants to merge 1 commit into
Open
feat: add persistOnHover option to keep tooltip visible when hovering over it#12255almahmudsarker wants to merge 1 commit into
almahmudsarker wants to merge 1 commit into
Conversation
… tooltip.persistOnHover is true, the tooltip remains visible as the cursor moves from the data point into the tooltip bounding box. Closes chartjs#12080
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses issue #12080 by adding a
persistOnHoveroption to the tooltip plugin.The contribution focuses on a behavior improvement: when
tooltip.persistOnHoveris setto
true, the tooltip remains visible as the cursor moves from the data point into thetooltip bounding box, instead of disappearing immediately.
Changes Made
_isMouseOverTooltip(e)method tosrc/plugins/plugin.tooltip.js_getActiveElements()insrc/plugins/plugin.tooltip.jsto retain activeelements while the cursor is inside the tooltip bounding box
persistOnHover: falseto the tooltip defaults insrc/plugins/plugin.tooltip.jspersistOnHover: booleantoTooltipOptionsinsrc/types/index.d.tsImplementation Notes
The solution was implemented by intercepting the result of
_getActiveElements()afterthe normal interaction lookup returns empty. The tooltip's bounding box (
x,y,width,height) is already set from the previous render, so we check whether thecursor is inside it and return the previous active elements if so.
Alternative approaches considered:
point+tooltipinteraction mode — rejected because it would require calculatingtooltip bounds during hit testing (before rendering), creating a circular dependency.
The guard approach was chosen because it requires no changes to interaction modes or
controllers, has zero cost when the option is
false(the default), and is fullyself-contained inside the tooltip plugin.
Testing / Verification
pnpm run build— no errorshttp://localhost:8081/docs/master/samples/tooltip/interactions.htmlpersistOnHover: true: moving cursor from data point into tooltip keeps it visibleRelated Issue
Closes #12080