primitives icon indicating copy to clipboard operation
primitives copied to clipboard

Tooltip within popover opens automatically due to trigger receiving focus

Open SomeHats opened this issue 2 years ago • 8 comments

Bug report

Current Behavior

Hi radix-ui folks <3

Currently, if you create a popover with a tooltip inside, opening the popover automatically opens the tooltip. This is because the popover auto-focuses it's contents, and if the tooltip trigger is the first focusable element it receives that focus, which causes it to open.

This may not really be all that much of an issue - you can work around it by .preventDefault()ing the popover onOpenAutoFocus, but it tripped me up for a little while so I figured I'd let you know - feel free to close!

Kapture 2023-07-03 at 12 41 12

Expected behavior

When opening a popover containing a tooltip, the tooltip should remain closed by default until its trigger is hovered

Reproducible example

https://stackblitz.com/edit/stackblitz-starters-qgptek?file=src%2FApp.tsx

Suggested solution

Possibly nothing! Although I'd guess the .preventDefault() approach has it's tradeoffs for accessibility.

Your environment

Software Name(s) Version
Radix Package(s) react-popover, react-tooltip 1.0.6
React n/a 18.2
Browser Chrome 114.0.5735.133

SomeHats avatar Jul 04 '23 10:07 SomeHats

I spent the last hour of my life trying to figure out why this was happening. Thank you for posting this.

ryantbrown avatar Aug 11 '23 20:08 ryantbrown

Same issue when the trigger is in Dialog.

pureliumy avatar Dec 09 '23 14:12 pureliumy

Thanks for this. I'm using a tooltip with a dropdown trigger and the dropdown menu kept triggering the tooltip. By putting onCloseAutoFocus={e => e.preventDefault()} on DropdownMenu.Content the issue went away.

jacksonblankenship avatar Mar 12 '24 17:03 jacksonblankenship

Thanks for this. I'm using a tooltip with a dropdown trigger and the dropdown menu kept triggering the tooltip. By putting onCloseAutoFocus={e => e.preventDefault()} on DropdownMenu.Content the issue went away.

Omg this was the answer. thank you

rgorai-cc avatar Mar 25 '24 12:03 rgorai-cc

@jacksonblankenship The issue mentioned above is tooltip inside popover getting triggered when popover is clicked. Your solution is for when tooltip outside getting triggered when popover is closed.

This fixes this issue:

<Popover.Content
  onOpenAutoFocus={(event) => {
    event.preventDefault();
  }}
>
  ...
</Popover.Content>

RakeshPotnuru avatar Apr 04 '24 13:04 RakeshPotnuru

@jacksonblankenship The issue mentioned above is tooltip inside popover getting triggered when popover is clicked. Your solution is for when tooltip outside getting triggered when popover is closed.

This fixes this issue:

<Popover.Content
  onOpenAutoFocus={(event) => {
    event.preventDefault();
  }}
>
  ...
</Popover.Content>

This works well with me .. thanks

youneszahzouh avatar Apr 30 '24 18:04 youneszahzouh

In my case, I was mounting a non-popover scrolling div into the DOM and as soon as it mounted, the tooltip would get triggered.

I fixed this by preventing the onFocusCapture event from propagating on the div

<div
  onFocusCapture={(e) => {
    e.stopPropagation();
  }}
>
 // tooltip is somewhere in here
</div>

hallucinogenizer avatar Jun 04 '24 09:06 hallucinogenizer