import { arrow, flip, FloatingPortal, offset, Placement, shift, useFloating, useHover, useInteractions, useRole, } from "@floating-ui/react-dom-interactions"; import clsx from "clsx"; import { AnimatePresence, motion } from "framer-motion"; import * as React from "react"; import { preventWidows } from "utils/prevent-widows"; export interface TooltipProps { placement?: Placement; children?: React.ReactNode; content?: React.ReactNode; disabled?: boolean; className?: string; } const Tooltip: React.VoidFunctionComponent = ({ placement: preferredPlacement = "bottom", className, children, disabled, content, }) => { const arrowRef = React.useRef(null); const [open, setOpen] = React.useState(false); const { reference, floating, x, y, strategy, context, middlewareData, placement, } = useFloating({ strategy: "fixed", open, onOpenChange: setOpen, placement: preferredPlacement, middleware: [ offset(10), flip(), shift({ padding: 5 }), arrow({ element: arrowRef }), ], }); const placementGroup = placement.split("-")[0] as | "top" | "right" | "bottom" | "left"; const staticSide = { top: "bottom", right: "left", bottom: "top", left: "right", }[placementGroup]; const { getReferenceProps, getFloatingProps } = useInteractions([ useHover(context, { enabled: !disabled, restMs: 150, }), useRole(context, { role: "tooltip", }), ]); return ( <>
{children}
{open ? (
{typeof content === "string" ? preventWidows(content) : content} ) : null} ); }; export default Tooltip;