import React, { createContext, useContext, useState, useRef } from 'react'; import { Slot } from './Slot'; const TooltipContext = createContext(null); export const TooltipProvider = ({ children }: any) => <>{children}; export const TooltipRoot = ({ children, delayDuration = 200 }: any) => { const [open, setOpen] = useState(false); const timeoutRef = useRef(null); const handleMouseEnter = () => { timeoutRef.current = setTimeout(() => setOpen(true), delayDuration); }; const handleMouseLeave = () => { clearTimeout(timeoutRef.current); setOpen(false); }; return (
{children}
); }; TooltipRoot.displayName = "Tooltip"; export const TooltipTrigger = React.forwardRef(({ asChild, children, ...props }, ref) => { const Comp = asChild ? Slot : "button"; return {children}; }); TooltipTrigger.displayName = "TooltipTrigger"; export const TooltipContent = React.forwardRef(({ className = '', children, sideOffset = 4, side = "top", ...props }, ref) => { const { open } = useContext(TooltipContext); if (!open) return null; let posClass = "bottom-full mb-2 left-1/2 -translate-x-1/2"; // default top if (side === "bottom") posClass = "top-full mt-2 left-1/2 -translate-x-1/2"; if (side === "left") posClass = "right-full mr-2 top-1/2 -translate-y-1/2"; if (side === "right") posClass = "left-full ml-2 top-1/2 -translate-y-1/2"; return (
{children}
); }); TooltipContent.displayName = "TooltipContent";