{ "name": "tooltip", "displayName": "Tooltip", "type": "component", "files": [ { "name": "Tooltip.tsx", "content": "import {\n Children,\n cloneElement,\n isValidElement,\n useCallback,\n useEffect,\n useId,\n useLayoutEffect,\n useRef,\n useState,\n type ReactElement,\n type ReactNode,\n} from 'react';\nimport { createPortal } from 'react-dom';\nimport { cn, vc } from '../../utils/cn';\nimport styles from './Tooltip.module.css';\n\nexport type TooltipPosition = 'top' | 'bottom' | 'left' | 'right';\n\nexport interface TooltipProps {\n content: ReactNode;\n position?: TooltipPosition;\n delay?: number;\n children: ReactElement;\n className?: string;\n}\n\nexport function Tooltip({\n content,\n position = 'top',\n delay = 200,\n children,\n className,\n}: TooltipProps) {\n const id = useId();\n const [open, setOpen] = useState(false);\n const [coords, setCoords] = useState<{\n top: number;\n left: number;\n pos: TooltipPosition;\n }>({\n top: 0,\n left: 0,\n pos: position,\n });\n const timer = useRef | null>(null);\n const triggerRef = useRef(null);\n const tipRef = useRef(null);\n\n const clear = () => {\n if (timer.current) {\n clearTimeout(timer.current);\n timer.current = null;\n }\n };\n\n const show = useCallback(() => {\n clear();\n timer.current = setTimeout(() => setOpen(true), delay);\n }, [delay]);\n\n const hide = useCallback(() => {\n clear();\n setOpen(false);\n }, []);\n\n useEffect(() => () => clear(), []);\n\n const compute = useCallback(() => {\n const trigger = triggerRef.current;\n const tip = tipRef.current;\n if (!trigger || !tip) return;\n const rect = trigger.getBoundingClientRect();\n const tipRect = tip.getBoundingClientRect();\n const gap = 8;\n let pos: TooltipPosition = position;\n let top = 0;\n let left = 0;\n const place = (p: TooltipPosition) => {\n switch (p) {\n case 'top':\n return {\n top: rect.top - tipRect.height - gap,\n left: rect.left + rect.width / 2 - tipRect.width / 2,\n };\n case 'bottom':\n return {\n top: rect.bottom + gap,\n left: rect.left + rect.width / 2 - tipRect.width / 2,\n };\n case 'left':\n return {\n top: rect.top + rect.height / 2 - tipRect.height / 2,\n left: rect.left - tipRect.width - gap,\n };\n case 'right':\n return {\n top: rect.top + rect.height / 2 - tipRect.height / 2,\n left: rect.right + gap,\n };\n }\n };\n let p = place(pos);\n // Flip if off-screen.\n if (pos === 'top' && p.top < 4) pos = 'bottom';\n else if (\n pos === 'bottom' &&\n p.top + tipRect.height > window.innerHeight - 4\n )\n pos = 'top';\n else if (pos === 'left' && p.left < 4) pos = 'right';\n else if (pos === 'right' && p.left + tipRect.width > window.innerWidth - 4)\n pos = 'left';\n p = place(pos);\n top = p.top + window.scrollY;\n left = p.left + window.scrollX;\n setCoords({ top, left, pos });\n }, [position]);\n\n useLayoutEffect(() => {\n if (open) compute();\n }, [open, content, compute]);\n\n const child = Children.only(children);\n if (!isValidElement(child)) return children;\n\n const merged = cloneElement(\n child as ReactElement>,\n {\n ref: (node: HTMLElement | null) => {\n triggerRef.current = node;\n const originalRef = (\n child as unknown as { ref?: React.Ref }\n ).ref;\n if (typeof originalRef === 'function') originalRef(node);\n else if (originalRef && 'current' in originalRef)\n (originalRef as { current: HTMLElement | null }).current = node;\n },\n 'aria-describedby': open\n ? id\n : (child.props as { 'aria-describedby'?: string })['aria-describedby'],\n onMouseEnter: (e: React.MouseEvent) => {\n show();\n (\n child.props as { onMouseEnter?: (e: React.MouseEvent) => void }\n ).onMouseEnter?.(e);\n },\n onMouseLeave: (e: React.MouseEvent) => {\n hide();\n (\n child.props as { onMouseLeave?: (e: React.MouseEvent) => void }\n ).onMouseLeave?.(e);\n },\n onFocus: (e: React.FocusEvent) => {\n show();\n (child.props as { onFocus?: (e: React.FocusEvent) => void }).onFocus?.(\n e,\n );\n },\n onBlur: (e: React.FocusEvent) => {\n hide();\n (child.props as { onBlur?: (e: React.FocusEvent) => void }).onBlur?.(e);\n },\n } as never,\n );\n\n return (\n <>\n {merged}\n {open &&\n typeof document !== 'undefined' &&\n createPortal(\n \n {content}\n \n ,\n document.body,\n )}\n \n );\n}\n" }, { "name": "Tooltip.module.css", "content": ".tip {\n z-index: var(--tatva-z-tooltip);\n background-color: var(--tatva-color-neutral-900);\n color: #ffffff;\n padding: var(--tatva-space-1) var(--tatva-space-2);\n border-radius: var(--tatva-radius-sm);\n font-family: var(--tatva-font-family);\n font-size: var(--tatva-font-size-xs);\n line-height: var(--tatva-line-height-tight);\n max-width: 240px;\n box-shadow: var(--tatva-shadow-md);\n opacity: 0;\n animation: tatva-tooltip-in 150ms ease forwards;\n pointer-events: none;\n}\n\n@keyframes tatva-tooltip-in {\n to {\n opacity: 1;\n }\n}\n\n.arrow {\n position: absolute;\n width: 8px;\n height: 8px;\n background-color: var(--tatva-color-neutral-900);\n transform: rotate(45deg);\n}\n\n.posTop .arrow {\n bottom: -4px;\n left: 50%;\n margin-left: -4px;\n}\n.posBottom .arrow {\n top: -4px;\n left: 50%;\n margin-left: -4px;\n}\n.posLeft .arrow {\n right: -4px;\n top: 50%;\n margin-top: -4px;\n}\n.posRight .arrow {\n left: -4px;\n top: 50%;\n margin-top: -4px;\n}\n" }, { "name": "index.ts", "content": "export { Tooltip } from './Tooltip';\nexport type { TooltipProps, TooltipPosition } from './Tooltip';\n" } ], "registryDependencies": [], "dependencies": [], "devDependencies": [] }