question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Tooltip] Allow placement to be below cursor

See original GitHub issue

Traditional title attributes make the popup display under the mouse cursor - it would be great if we could replicate this behaviour using the Tooltip component, maybe with <Tooltip placement="cursor" />.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:18
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

8reactions
Morcatkocommented, Sep 14, 2020

This works for me

  const [position, setPosition] = React.useState({ x: undefined, y: undefined });

  return <Tooltip
      ...
      onMouseMove={e => setPosition({ x: e.pageX, y: e.pageY })}
      PopperProps={{
        anchorEl: {
          clientHeight: 0,
          clientWidth: 0,
          getBoundingClientRect: () => ({
            top: position.y,
            left: position.x,
            right: position.x,
            bottom: position.y,
            width: 0,
            height: 0,
          })
        }
      }}
    >
    {...}
    </Tooltip>
4reactions
oliviertassinaricommented, Sep 20, 2020

@ahtcx Here is a working implementation:

--- a/packages/material-ui/src/Tooltip/Tooltip.js
+++ b/packages/material-ui/src/Tooltip/Tooltip.js
@@ -145,6 +145,9 @@ class Tooltip extends React.Component {
     const { children, enterDelay } = this.props;
     const childrenProps = children.props;

+    this.pageX = event.pageX
+    this.pageY = event.pageY
+
     if (event.type === 'mouseover' && childrenProps.onMouseOver) {
       childrenProps.onMouseOver(event);
     }
@@ -340,7 +343,21 @@ class Tooltip extends React.Component {
             [classes.popperInteractive]: interactive,
           })}
           placement={placement}
-          anchorEl={this.childrenRef}
+          anchorEl={this.childrenRef ? {
+            clientWidth: this.childrenRef.clientWidth,
+            clientHeight: this.childrenRef.clientHeight,
+            getBoundingClientRect: () => {
+              const rect = this.childrenRef.getBoundingClientRect()
+              return {
+                left: this.pageX,
+                bottom: rect.bottom,
+                height: rect.height,
+                right: rect.right,
+                width: rect.width,
+                top: this.pageY,
+              }
+            },
+          } : null}
           open={open}
           id={childrenProps['aria-describedby']}
           transition

This is just a quick hacking. If you want to take the time to consolidate the implementation, we can talk about it. Alternatively, you can use the existing API to build the feature on top of it.

capture d ecran 2019-01-22 a 21 27 17

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Tooltip] Allow placement to be below cursor #14270 - GitHub
mouse overing, A tooltip should desplay with the time under the cursor.
Read more >
HTML-Tooltip position relative to mouse pointer - Stack Overflow
One solution for multiple elements is to update all tooltip span 's and setting them under the cursor on mouse move. jsFiddle var...
Read more >
How to Show, Position, and Style a Tooltip in HTML and CSS?
A tooltip is referred to as an element that provides extra information about an element every time a mouse cursor is brought over...
Read more >
How to: Position a ToolTip - WPF .NET Framework
You can position a tooltip by using a set of five properties that are defined in both the ToolTip and ToolTipService classes.
Read more >
Tooltips - Bootstrap
Tooltips rely on the 3rd party library Popper.js for positioning. ... Hover over the buttons below to see the four tooltips directions: top,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found