React-tooltip: Feature: ReactTooltip.show()

Created on 2 Nov 2015  Â·  23Comments  Â·  Source: wwayne/react-tooltip

I have an important tooltip that I want to show programmatically. I could then set a timeout and ReactTooltip.hide() it .. Best if the hover would prevent it from being hidden.

Do you have an example for this? Should I try to capture an event and pass it to ReactTooltip.showTooltip (e) ?

Feature

Most helpful comment

This has been supported since 3.1.5, but you have to specific the target element, for example:

import {findDOMNode} from 'react-dom'
import ReactTooltip from 'react-tooltip'

<p ref='foo' data-tip='tooltip'></p>
<button onClick={() => { ReactTooltip.show(findDOMNode(this.refs.foo)) }}></button>
<ReactTooltip />

All 23 comments

Now the react-tooltip doesn't support show programmatically, let me do some develop next.

In addition to that, it would be nice if we could choose the way it's triggered - on hover, click or event.

Thanks @Sly777
v0.9.0 start to support customer event
<ReactTooltip event='click' /> or <a data-event='click' data-tip='tooltip'></a>

Did this fix the original issue with ReactTooltip.show()? It seems like the .show() method is still missing to me.

I didn't build the .show method, instead there have custom event to trigger tooltip to show up.

Thanks for confirming.

how can I trigger custom event?

For example:

<a data-event='specialhat' data-tip='tooltip'></a>

How to trigger such event?

Behind data-event='event' is document.addEventListener('event', () => {...}, false), so it only support those HTML DOM event like click, doubleClick, etc... so react tooltip can't trigger your custom event specialhat

ah I see. so in case I want to display error tool tip when user input invalid data, what's the best way to trigger it in this case?

It can not be 'click', because it's an input type text. users click it at the beginning.

@craigcosmo Looks there's no way to satisfy your requirement at the moment, you may need a static method like ReactTooltip.show('tooltipId'), I don't know when I have time to build such function..

@craigcosmo : Were you able to implement show method? Just wanted to check before i give it a try.
@wwayne : Is this something which is still in your radar?

Not yet, if you do, please notify once you got. Im also interested

On Thu, Jun 16, 2016 at 2:29 AM +0700, "Ranjith Nair" [email protected] wrote:

@craigcosmo : Were you able to implement show method? Just wanted to check before i give it a try.

@wwayne : Is this something which is still in your radar?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

No, I don't have any plan on this at the moment. It's not that easy to implement this feature, much less we should have to consider if the user want to show multi tooltip at the same time.

this show() feature would be very useful to highlight icons / locations on a google map. Please let me know how you get on.

This has been supported since 3.1.5, but you have to specific the target element, for example:

import {findDOMNode} from 'react-dom'
import ReactTooltip from 'react-tooltip'

<p ref='foo' data-tip='tooltip'></p>
<button onClick={() => { ReactTooltip.show(findDOMNode(this.refs.foo)) }}></button>
<ReactTooltip />

thank you so much for making this!

it shows _XflowTooltip2.default.show is not a function.
I called this function on image click.
My version is 3.2.3

@DeepanshuTaneja No idea why you got _XflowTooltip2, but I didn't find _XflowTooltip2 in this repo

thank you!

@wwayne Thanks for creating the library! I have a question about your example to use ReactTooltip.show(target). Why is the type of the input target string? The return type of findDOMNode is not string, so I wonder if it still works now:

<button onClick={() => { ReactTooltip.show(findDOMNode(this.refs.foo)) }}></button>

I tried to use React.useRef hook, but it usually refers to a DOM element instead of string.

I also looked at the source code, and it seems that the way it works is to create an event by (https://github.com/wwayne/react-tooltip/blob/master/src/decorators/staticMethods.js#L15):

event = document.createEvent("Event");
event.initEvent(eventName, false, true, { target });

However, I tried it out and the function initEvent only accepts 3 input parameters.

All I'm trying to figure out is how to use ReactTooltip.show(target) to manually show the tooltip. Could you provide another example with React.useRef, or add details on what target should be? Thank you very much!

@mymywang Just ran into this. You can use this for React.useRef:

ReactTooltip.show(tooltipTarget?.current);

Not sure if I did something wrong though, but I had to set data-event="fakeEvent" to stop the default hover behavior.

@ZDTaylor Thanks. I tried it but typescript complains that the input passed into ReactTooltip.show() should be a string. I didn't try to override the type error though. If it could work, there's probably just an issue in type definitions.

I had to do the following to stop from TS complaining:

const toolTipRef = useRef(null);
useEffect(() => {
  ReactTooltip.show((toolTipRef?.current as unknown) as Element);
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

abijames picture abijames  Â·  3Comments

tatsujb picture tatsujb  Â·  4Comments

jhlee4 picture jhlee4  Â·  3Comments

Joseppi83 picture Joseppi83  Â·  4Comments

tanykim picture tanykim  Â·  4Comments