Skip to content

Commit

Permalink
Merge branch 'pla-930-migrate-from-abusix-ui-to-hailstorm-tooltip' of h…
Browse files Browse the repository at this point in the history
  • Loading branch information
pallendes committed Dec 29, 2023
2 parents 06c46ba + d6f7997 commit 32c7a12
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 22 deletions.
20 changes: 20 additions & 0 deletions src/components/tooltip/tooltip-portal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect, useRef, useState } from "react";
import ReactDOM from "react-dom";

interface PortalProps {
children: React.ReactNode;
}

export const TooltipPortal = ({ children }: PortalProps) => {
const ref = useRef<Element | null>(null);
const [mounted, setMounted] = useState(false);

useEffect(() => {
if (document) {
ref.current = document.body;
setMounted(true);
}
}, []);

return mounted && ref.current ? ReactDOM.createPortal(children, ref.current) : null;
};
62 changes: 40 additions & 22 deletions src/components/tooltip/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Placement } from "@popperjs/core";
import React, { useEffect, useState } from "react";
import { usePopper } from "react-popper";
import { classNames } from "../../util/class-names";
import { TooltipPortal } from "./tooltip-portal";

interface TooltipProps {
children: React.ReactNode;
Expand All @@ -11,16 +12,25 @@ interface TooltipProps {
className?: string;
open?: boolean;
onClose?: () => void;
strategy?: "fixed" | "absolute" | "portal";
}

export const Tooltip = ({ children, title, position = "right", className, open }: TooltipProps) => {
export const Tooltip = ({
children,
title,
position = "right",
className,
open,
strategy = "absolute",
}: TooltipProps) => {
const [referenceElement, setReferenceElement] = useState<HTMLDivElement>();
const [popperElement, setPopperElement] = useState<HTMLDivElement>();
const [show, setShow] = useState(false);
const [isControlled, setIsControlled] = useState(false);

const { styles, attributes } = usePopper(referenceElement, popperElement, {
placement: position,
strategy: strategy !== "portal" ? strategy : "absolute",
modifiers: [{ name: "offset", options: { offset: [0, 8] } }],
});

Expand All @@ -31,6 +41,30 @@ export const Tooltip = ({ children, title, position = "right", className, open }
}
}, [open]);

const renderTooltipContent = () => (
<Transition
show={show}
enter="transition-opacity duration-75"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition-opacity duration-150"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div
ref={(el) => el && setPopperElement(el)}
className={classNames(
"rounded-lg bg-neutral-900 p-4 px-4 py-2 text-xs text-neutral-0 shadow",
className
)}
style={styles.popper}
{...attributes.popper}
>
{title}
</div>
</Transition>
);

return (
<div>
<div
Expand All @@ -40,27 +74,11 @@ export const Tooltip = ({ children, title, position = "right", className, open }
>
{children}
</div>
<Transition
show={show}
enter="transition-opacity duration-75"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition-opacity duration-150"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div
ref={(el) => el && setPopperElement(el)}
className={classNames(
"rounded-lg bg-neutral-900 p-4 px-4 py-2 text-xs text-neutral-0 shadow",
className
)}
style={styles.popper}
{...attributes.popper}
>
{title}
</div>
</Transition>
{strategy === "portal" ? (
<TooltipPortal>{renderTooltipContent()}</TooltipPortal>
) : (
renderTooltipContent()
)}
</div>
);
};

0 comments on commit 32c7a12

Please sign in to comment.