[BUG] Tooltip opens by itself when tabbing back in to the page
Describe the bug When I leave the tab with the tooltip hidden and come back to the tab, the tooltip reappears even though my mouse isn't over the tooltip anchor.
Version of Package v5.14.0
To Reproduce
Below's a stripped down version of the code I'm using that I've confirmed reproduces the issue. I've tried using 2 separate tooltips and using props on the tooltips rather than data- attributes just in case there was some weirdness there, but haven't had any luck.
import React, { useState } from 'react';
import { Tooltip } from 'react-tooltip';
export default function Root() {
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
return (
<>
<button
id='openSidebar'
className={'flat' + (isSidebarOpen ? '' : ' visible')}
onClick={() => setIsSidebarOpen(true)}
data-tooltip-id='tooltip'
data-tooltip-content='Show sidebar'
data-tooltip-delay-show={250}
data-tooltip-place='right'
data-tooltip-position-strategy='fixed'
>
Open
</button>
<Tooltip id='tooltip'/>
<div id="sidebar" className={isSidebarOpen ? '' : 'closed'}>
<div id='sidebarActions'>
<button type="submit" onClick={ /* Do something */}>
<span>
New Object
</span>
</button>
<button
id='closeSidebar'
onClick={() => setIsSidebarOpen(false)}
data-tooltip-id='tooltip'
data-tooltip-content='Close sidebar'
data-tooltip-delay-show={250}
data-tooltip-place='right'
data-tooltip-position-strategy='fixed'
>
Close
</button>
</div>
</div>
</>
);
}
Expected behavior The tooltip should remain hidden.
Screenshots
Desktop (please complete the following information if possible or delete this section):
- OS: MacOS
- Browser Chrome
- Version 114.0.5735.133 (arm64)
- Frameworks React 18.2.0
By default, the tooltip component listens for mouseenter/mouseleave and focus/blur events. What is happening here is that when you tab back in, your out-of-screen "close sidebar" button is getting focused, so the tooltip shows up.
You can try setting tabIndex={-1} on your "close sidebar" button when the sidebar is closed, to make it unfocusable. This should fix this issue, and actually also improve your application, since (IMO) that button shouldn't be interactable when the sidebar is closed. You could also just unmount the button (or the whole sidebar), but that seems excessive.
As an afterthought, maybe we should also add an ignoreFocus prop such that only mouseenter/mouseleave events are listened to.
Ah thank you! The solution didn't exactly work as clicking the button gives it focus regardless of tabindex, but your diagnosis is 100% correct and I've fixed it in another way. +1 to the ignoreFocus prop