ui icon indicating copy to clipboard operation
ui copied to clipboard

Tooltip should utilize portal for content

Open khinshankhan opened this issue 2 years ago • 9 comments

Hello, superb library!

Because the tooltip utilizes divs, but divs aren't valid descendants of paragraphs (view this stackoverflow discussion), if someone where to use tooltip in a paragraph tag, they would get the error

Warning: validateDOMNesting(...): <div> cannot appear as a descendant of <p>.

Thanks to the radix discord, I suggest wrapping the content in the tooltip portal like so:

const TooltipContent = React.forwardRef<
  React.ElementRef<typeof TooltipPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
>(({ className, sideOffset = 4, ...props }, ref) => (
  <TooltipPrimitive.Portal>
    <TooltipPrimitive.Content
      ref={ref}
      sideOffset={sideOffset}
      className={cn(
        "z-50 overflow-hidden rounded-md border border-slate-100 bg-white px-3 py-1.5 text-sm text-slate-700 shadow-md animate-in fade-in-50 data-[side=bottom]:slide-in-from-top-1 data-[side=top]:slide-in-from-bottom-1 data-[side=left]:slide-in-from-right-1 data-[side=right]:slide-in-from-left-1 dark:border-slate-800 dark:bg-slate-800 dark:text-slate-400",
        className
      )}
      {...props}
    />
  </TooltipPrimitive.Portal>
))
TooltipContent.displayName = TooltipPrimitive.Content.displayName

This allows the content to be rendered in a different scope, avoiding the invalid descendants issue and retaining the styling and semantics as far as I can tell.

khinshankhan avatar Mar 18 '23 22:03 khinshankhan

This change also solved an issue I had where I wanted to display a tooltip within a Select, which caused the tooltip to be clipped by the bounds of the Select Content.

Dylan-Kentish avatar Oct 26 '23 09:10 Dylan-Kentish

@Dylan-Kentish Yes, I had the same issue.

dylanhu7 avatar Oct 31 '23 04:10 dylanhu7

This solved an issue for me where the tooltip content was being clipped by a parent with overflow-hidden. Portal should be added to the shadcn tooltip.

alivault avatar Apr 16 '24 20:04 alivault

So do I, I have same problem and @khinshankhan your solution is very good for me. tooltip's content need to be exist in different layer for accessibility and visibility.

daunJung-dev avatar May 31 '24 08:05 daunJung-dev

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jul 10 '24 23:07 shadcn

Well, I want to confirm if this is a bug or intentional. Other components like dropdown seem to use their respective portals.

I'm not familiar with the codebase, but if you agree it should be added, I could attempt a pr for this. Otherwise, just the existence of this issue is likely helpful for many as github has really good seo.

khinshankhan avatar Jul 11 '24 04:07 khinshankhan

I would like to see Portal support added for shadcn, it just seems like it should be there but isn't...

michaelgrewal avatar Aug 02 '24 22:08 michaelgrewal

Agreed. Not stale. Reopening.

shadcn avatar Aug 05 '24 07:08 shadcn

https://www.instagram.com/1900_officelle?igsh=MW5ueDI0b21qbW44Zg==

shadcn @.***> schrieb am Mo., 5. Aug. 2024, 09:14:

Reopened #129 https://github.com/shadcn-ui/ui/issues/129.

— Reply to this email directly, view it on GitHub https://github.com/shadcn-ui/ui/issues/129#event-13759369108, or unsubscribe https://github.com/notifications/unsubscribe-auth/BIO77CIC7L2S43GDMLPM67TZP4Q5BAVCNFSM6AAAAAAV7WZDAKVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJTG42TSMZWHEYTAOA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

nullnullsieben avatar Aug 05 '24 10:08 nullnullsieben