All files / src/components/common Tooltip.tsx

0% Statements 0/30
0% Branches 0/1
0% Functions 0/1
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import React, { useState } from "react"
import styled from "styled-components"
import {
	getAsVar,
	VSC_DESCRIPTION_FOREGROUND,
	VSC_INPUT_BORDER,
	VSC_INPUT_PLACEHOLDER_FOREGROUND,
	VSC_SIDEBAR_BACKGROUND,
} from "@/utils/vscStyles"
 
interface TooltipProps {
	visible?: boolean
	hintText?: string
	tipText: string
	children: React.ReactNode
	style?: React.CSSProperties
}
 
// add styled component for tooltip
const TooltipBody = styled.div<Pick<TooltipProps, "style">>`
	position: absolute;
	background-color: ${getAsVar(VSC_SIDEBAR_BACKGROUND)};
	color: ${getAsVar(VSC_DESCRIPTION_FOREGROUND)};
	padding: 5px;
	border-radius: 5px;
	bottom: 100%;
	left: ${(props) => props.style?.left ?? -180}%;
	z-index: ${(props) => props.style?.zIndex ?? 1000};
	white-space: wrap;
	max-width: 200px;
	border: 1px solid ${getAsVar(VSC_INPUT_BORDER)};
	pointer-events: none;
	font-size: 0.9em;
`
 
const Hint = styled.div`
	font-size: 0.8em;
	color: ${getAsVar(VSC_INPUT_PLACEHOLDER_FOREGROUND)};
	opacity: 0.8;
	margin-top: 2px;
`
 
const Tooltip: React.FC<TooltipProps> = ({ visible, tipText, hintText, children, style }) => {
	const [isHovered, setIsHovered] = useState(false)
 
	// Determine final visibility based on prop or internal state
	const shouldShow = visible !== undefined ? visible : isHovered
 
	return (
		<div
			onMouseEnter={() => setIsHovered(true)}
			onMouseLeave={() => setIsHovered(false)}
			style={{ position: "relative", display: "inline-block" }}>
			{children}
			{shouldShow && (
				<TooltipBody style={style}>
					{tipText}
					{hintText && <Hint>{hintText}</Hint>}
				</TooltipBody>
			)}
		</div>
	)
}
 
export default Tooltip