import React, { useEffect, useRef, useState } from 'react'; export interface Props { callback: () => void; focusCb?: () => void; blurCb?: () => void; content: React.ReactNode; id: string; position: 'bottom' | 'left' | 'right' | 'top'; showQuestion?: boolean; showInfo?: boolean; theme: string; themeTooltip?: string; themeWrapper?: string; title: string | React.ReactNode; width: string; showOnHover?: boolean; buttonComponent?: React.ReactNode; iconColor?: string; ariaControls?: string; } // automatically activated tooltip const Tooltip = ({ callback = () => null, focusCb = () => null, blurCb = () => null, content, id = 'tooltip', position = 'bottom', showQuestion = false, showInfo = false, showOnHover = true, theme = 'cursor-pointer px-0.5 transition-all', themeTooltip = `bg-white border text-black px-3 py-2 inline-block rounded-md text-xs text-center shadow-md`, themeWrapper = ``, title, width = 'w-80', buttonComponent, iconColor = 'text-_-misc-selectedDark', }: Props) => { const [show, showSetter] = useState(false); const [focus, focusSetter] = useState(false); const wrapperRef = useRef(null); const [positionClass, positionClassSetter] = useState( 'absolute translate-x-1/2 top-0 ml-2' ); useEffect(() => { switch (position) { case 'right': positionClassSetter('absolute left-full top-full'); break; case 'left': positionClassSetter('absolute top-0 mr-2 right-full'); break; case 'top': positionClassSetter('absolute bottom-full mb-2 -left-1/2'); break; default: positionClassSetter( 'absolute top-full mt-2 -ml-10 top-full translate-x-1/2' ); } document.addEventListener('click', handleClickOutside, false); return () => { document.removeEventListener('click', handleClickOutside, false); }; }, [position]); const handleButtonClick = () => { callback(); showSetter(!show); }; const handleClickOutside = (event: { target: any }) => { if (!wrapperRef?.current?.contains(event.target)) showSetter(false); }; return ( { blurCb(); setTimeout(() => { showSetter(false); }, 200); focusSetter(false); }} onFocus={() => { focusCb(); showSetter(true); focusSetter(true); }} onClick={() => { callback(); showSetter(!show); }} onMouseEnter={() => { showOnHover && showSetter(true); }} onMouseLeave={() => { !focus && showOnHover && showSetter(false); }} tabIndex={0} onKeyDown={(event) => { if (event.key === 'Enter' || event.key === ' ') { handleButtonClick(); } }} > {buttonComponent || {title}}{' '} {(showQuestion || showInfo) && ( {showQuestion && (