Skip to content

Commit

Permalink
fix: tooltip, assert children is valid element (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony authored Mar 27, 2024
1 parent a6f6467 commit a145651
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/Tooltip/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { isValidElement } from "react";
import * as RadixTooltip from "@radix-ui/react-tooltip";
import { useState } from "react";
import { cn } from "../utils";
Expand All @@ -12,11 +12,13 @@ export default function Tooltip({
sideOffset = 2,
hideOnClick = true,
className = "",
asChild = false,
asChild,
...restProps
}) {
const [open, setOpen] = useState(defaultOpen);

const shouldAsChild = asChild ?? isValidElement(children);

const tooltipContent = content && (
<RadixTooltip.Portal>
<RadixTooltip.Content
Expand All @@ -43,7 +45,7 @@ export default function Tooltip({
{...restProps}
>
<RadixTooltip.Trigger
asChild={asChild}
asChild={shouldAsChild}
onMouseEnter={() => setOpen(true)}
onMouseLeave={() => setOpen(false)}
className={className}
Expand Down
23 changes: 23 additions & 0 deletions src/stories/Tooltip.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";

import Tooltip from "../../lib/Tooltip";

export default {
title: "Tooltip",
component: Tooltip,
};
export const Primary = () => (
<div>
<Tooltip content={"tooltip content"}>{"hover me"}</Tooltip>
</div>
);

export const elementOrComponent = () => {
return (
<div>
<Tooltip content={"element/component"}>
<span>hover span</span>
</Tooltip>
</div>
);
};

0 comments on commit a145651

Please sign in to comment.