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): add strategy option to support edge use cases #83

Merged
merged 2 commits into from
Jan 3, 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
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 TooltipPortalProps {
children: React.ReactNode;
}

export const TooltipPortal = ({ children }: TooltipPortalProps) => {
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 === "fixed" ? "fixed" : "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>
);
};
Loading