import React, { useState, useEffect, useRef } from ' react ' ;
const [ isVisible , setIsVisible ] = useState ( false ) ;
const timeoutRef = useRef ( null ) ;
default: ' bg-gray-700 text-white ' ,
success: ' bg-green-500 text-white ' ,
error: ' bg-red-500 text-white ' ,
info: ' bg-blue-500 text-white ' ,
warning: ' bg-yellow-400 text-black ' ,
dark: ' bg-gray-900 text-white ' ,
light: ' bg-white text-black border border-gray-300 ' ,
base: ' p-2 text-sm ' , // Base size
const positionClasses = {
top: ' bottom-full mb-2 left-1/2 transform -translate-x-1/2 ' ,
bottom: ' top-full mt-2 left-1/2 transform -translate-x-1/2 ' ,
left: ' right-full mr-2 top-1/2 transform -translate-y-1/2 ' ,
right: ' left-full ml-2 top-1/2 transform -translate-y-1/2 ' ,
const showTooltip = () => {
if ( timeoutRef . current ) clearTimeout ( timeoutRef . current ) ;
timeoutRef . current = setTimeout ( () => setIsVisible ( true ) , delay ) ;
const hideTooltip = () => {
if ( timeoutRef . current ) clearTimeout ( timeoutRef . current ) ;
if ( timeoutRef . current ) clearTimeout ( timeoutRef . current ) ;
const handleMouseEnter = () => { if ( trigger === ' hover ' ) showTooltip () ; };
const handleMouseLeave = () => { if ( trigger === ' hover ' ) hideTooltip () ; };
const handleClick = () => {
if ( trigger === ' click ' ) {
setIsVisible ( ( prev ) => ! prev ) ;
className = " relative inline-block "
onMouseEnter = { handleMouseEnter }
onMouseLeave = { handleMouseLeave }
aria-describedby = " tooltip "
className = { ` absolute p-2 rounded shadow-lg ${ tooltipClasses [ variant ] } transition-opacity duration-300 ${ positionClasses [ position ] } ${ className } ${ sizeClasses [ size ] } ` }