Tooltip abstraction using Slot doesn't work with components #660
-
I've created a Tooltip abstraction using import React, { ReactElement } from "react";
import { Root, Trigger, Content } from "@radix-ui/react-tooltip";
import { Slot } from "@radix-ui/react-slot";
function Tooltip(p): ReactElement {
return (
<Root delayDuration={0}>
<Trigger as={Slot}>{p.children}</Trigger>
{p.content && (
<Content
sideOffset={8}
side={p.side}
>
{p.content}
</Content>
)}
</Root>
);
} When I use it like this, it works: function Test () => (
<Tooltip content="test tooltip content">
<div className="w-[100px] h-[50px] border flex items-center justify-center text-body-lg">
Hover me!
</div>
</Tooltip>
); However, if I use a component instead, it doesn't show up on hover: function TestTrigger({ content }: { content: string }) {
return (
<div className="w-[100px] h-[50px] border flex items-center justify-center text-body-lg">
Hover me!
</div>
);
}
function Test () => (
<Tooltip content="test tooltip content">
<TestTrigger />
</Tooltip>
); What am I missing? Thanks in advance!
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
You need to Without that, a ref cannot be attached to the actual DOM node so it cannot be measured, and thus the tooltip cannot be positioned. Important to note also, the trigger should be an interactive element, most likely a |
Beta Was this translation helpful? Give feedback.
You need to
React.forwardRef
on yourTestTrigger
.Without that, a ref cannot be attached to the actual DOM node so it cannot be measured, and thus the tooltip cannot be positioned.
Important to note also, the trigger should be an interactive element, most likely a
button
. If you make it adiv
like this, you are left with handling all the accessibility yourself.