Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tooltip, assert children is valid element #96

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
);
};
Loading