nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - Tooltip positioning breaks when Tooltip.Content contains interactive elements (e.g Button, Link)

Open J4v4Scr1pt opened this issue 7 months ago • 3 comments

HeroUI Version

3.0.2

Describe the bug

When Tooltip.Content contains any interactive element (e.g. Button or Link), the Tooltip renders at the top-left corner of the viewport instead of being anchored to its trigger. Removing the interactive element from the content makes positioning work correctly.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

  'use client';

  import { Button, Tooltip } from '@heroui/react';

  export default function Repro() {
    return (
      <Tooltip delay={0}>
        <Tooltip.Trigger>
          <span>HOVER ME</span>
        </Tooltip.Trigger>
        <Tooltip.Content>
          <div>test <Button>click here</Button></div>
        </Tooltip.Content>
      </Tooltip>
    );
  }

Same issue occurs with Link instead of Button.

Workaround Replace the HeroUI Button/Link inside Tooltip.Content with a plain

Hello @J4v4Scr1pt 👋

I tracked down the issue.

HeroUI's Tooltip shares core logic with react-aria-components. Tooltip(which is TooltipTrigger in react-aria-component) wraps all of its children — including the overlay — with a FocusableProvider, which injects the trigger's ref into FocusableContext. Because React portals inherit the context tree, that context is still accessible inside Tooltip.Content even though it's rendered in a portal.

When a HeroUI Button or Link (both built on ButtonPrimitive from react-aria-components) mounts inside the overlay, it calls useFocusable → useSyncRef internally, which writes its own DOM ref to context.ref and ends up overwriting the original triggerRef.

A plain <button> or <a> doesn't cause this issue because native elements don't use useFocusable and therefore don't interact with FocusableContext.


Also, from what I understand, placing interactive elements like Button or Link inside a Tooltip isn't generally considered a standard pattern. Tooltips are typically meant for short, non-interactive text shown on hover or focus, and this pattern can also introduce accessibility concerns.

Could you share a bit more about your intended use case? That would help me think of a more appropriate solution.

Wonchang0314 avatar Apr 13 '26 14:04 Wonchang0314

Hello @J4v4Scr1pt 👋

I tracked down the issue.

HeroUI's Tooltip shares core logic with react-aria-components. Tooltip(which is TooltipTrigger in react-aria-component) wraps all of its children — including the overlay — with a FocusableProvider, which injects the trigger's ref into FocusableContext. Because React portals inherit the context tree, that context is still accessible inside Tooltip.Content even though it's rendered in a portal.

When a HeroUI Button or Link (both built on ButtonPrimitive from react-aria-components) mounts inside the overlay, it calls useFocusable → useSyncRef internally, which writes its own DOM ref to context.ref and ends up overwriting the original triggerRef.

A plain <button> or <a> doesn't cause this issue because native elements don't use useFocusable and therefore don't interact with FocusableContext.

Also, from what I understand, placing interactive elements like Button or Link inside a Tooltip isn't generally considered a standard pattern. Tooltips are typically meant for short, non-interactive text shown on hover or focus, and this pattern can also introduce accessibility concerns.

Could you share a bit more about your intended use case? That would help me think of a more appropriate solution.

Thanks for the quick response!

I'm responsible for the whole migration of V2 -> V3 in our product (I'm FE-Lead). During the migration I stumbled upon this use-case. After my post I also talked about this with our UX in charge and as you are touching upon we don't want this behavior in our tooltips and are changing some things by moving interactive content outside and some we switch to Popover.

If you are interested about the use-case it was a card with some bars in different colors etc. and below we had a "Trend/Accuracy-Icon" when hovering this we showed a rich tooltip that explained it all. To get more understanding there was a Link in the bottom to open our help center in a new tab"

For my case you can could close this as I understand that this is not intended use of the tooltip.

J4v4Scr1pt avatar Apr 13 '26 15:04 J4v4Scr1pt

I ran into the same problem after migrating to v3. Instead of using <a> and <button>, I applied a ref to Tooltip.Content and a div inside Tooltip.Trigger. Maybe it will make someone's life easier.

'use client';

import { Button, Chip, Tooltip } from '@heroui/react';
import Link from 'next/link';
import { useRef } from 'react';

export default function Test() {
  const chipRef = useRef<HTMLDivElement>(null);
  return (
    <Tooltip delay={0}>
      <Tooltip.Trigger>
        <div ref={chipRef}>
          <Chip>Some text</Chip>
        </div>
      </Tooltip.Trigger>
      <Tooltip.Content triggerRef={chipRef}>
        <Link href='some-href'>Some URL</Link>
        <Button>Copy URL</Button>
      </Tooltip.Content>
    </Tooltip>
  );
}

PiotrZacharskiThey avatar May 12 '26 13:05 PiotrZacharskiThey