-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Provide your feedback here.
There are a couple of issues where people (myself included) were confused to find that tooltip position is off when focusable content is used inside of it:
- Tooltip with Link breaks placement · Issue #7707 · adobe/react-spectrum
- Incorrect positioning of the tooltip when rendering a button inside it · Issue #8547 · adobe/react-spectrum
- Tooltip positioning incorrect when containing a focusable element · Issue #5665 · adobe/react-spectrum
The reason I was doing it was that I needed to display some content on hover, and Tooltip seemed like the most straightforward option.
There was some discussion about wiring up a controlled popover with hover detection, but it’s not trivial to set up.
In my understanding, the reason why the tooltip appears incorrectly positioned is roughly as follows:
TooltipTrigger
providesref
to theFocusableProvider
, which is consumed by all elements that use theuseFocusable
hook here.- That ref is then used in
useOverlayPosition
for calculating the tooltip position - After the tooltip opens, given there’s some focusable content within it, that content replaces the correct ref of the trigger, and eventually the tooltip tries to reposition based on the newfound ref. It happens fast, so most of the time one sees only the end result, even though it can go through multiple repositionings based on how many interactive elements are within it & how they render.
What I had to do to "fix" it, is implement a context barrier, to prevent nested focusable elements from replacing the tooltip trigger ref. It works by setting the ref to null for all its children, effectively making the original trigger ref inaccessible down the tree.
import { FocusableProvider } from '@react-aria/interactions';
<TooltipTrigger>
<Button>
Hover me
</Button>
<Tooltip>
// The context barrier
<FocusableProvider ref={null}>
<Button>Click me</Button>
</FocusableProvider>
</Tooltip>
</TooltipTrigger>
Now I get that it’s inaccessible, but the tooltip is already inaccessible for touch devices, so it serves just as a UX improvement for mouse users anyway.
In any way, I think it could be made more robust (prevent ref leaking to its content for instance), or — better yet — there could be a new component for such behavior (hover popover — an overlay element positioned relative to a trigger triggered by hover). Aria APG recommends using non-modal dialogs for that, so I guess it can be made accessible.
Based on previous discussions, there seems to be a real demand for it:
- How to control the Popover component by hovering? · adobe/react-spectrum · Discussion #4705
- Control popover by hovering and outside interactions · adobe/react-spectrum · Discussion #6563
- Creating a hover activated navigation menu with the Popover component · adobe/react-spectrum · Discussion #7192
🔦 Context
No response
💻 Code Sample
No response
Version
No response
What browsers are you seeing the problem on?
No response
If other, please specify
No response
What operating system are you using?
No response