import * as RadixTooltip from '@radix-ui/react-tooltip' import styled, { type CSSObject } from 'styled-components' import { isValidElement } from 'react' import { type TooltipProps, TooltipAlign, TooltipSide, TooltipSize, TooltipSlotDirection, } from './types' import type { TooltipTokensType } from './tooltip.tokens' import Block from '../Primitives/Block/Block' import PrimitiveText from '../Primitives/PrimitiveText/PrimitiveText' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' import { tooltipContentAnimations } from './tooltip.animations' import { formatTextWithLineBreaks } from '../../global-utils/GlobalUtils' const Arrow = styled(RadixTooltip.Arrow)<{ $color: CSSObject['backgroundColor'] }>` fill: ${({ $color }) => $color}; ` const AnimatedTooltipContent = styled(RadixTooltip.Content)` ${tooltipContentAnimations} ` export const Tooltip = ({ children: trigger, content, side = TooltipSide.TOP, align = TooltipAlign.CENTER, showArrow = true, size = TooltipSize.SMALL, slot, slotDirection = TooltipSlotDirection.LEFT, delayDuration = 300, offset = 5, open, maxWidth, fullWidth = false, disableInteractive = false, }: TooltipProps) => { const tooltipTokens = useResponsiveTokens('TOOLTIP') const isNativeElement = isValidElement(trigger) && typeof trigger.type === 'string' const shouldWrapTrigger = !isNativeElement const wrappedTrigger = shouldWrapTrigger ? ( {trigger} ) : ( trigger ) return ( {wrappedTrigger} {content && ( {slot && slotDirection === TooltipSlotDirection.LEFT && ( )} {formatTextWithLineBreaks(content)} {slot && slotDirection === TooltipSlotDirection.RIGHT && ( )} {showArrow && ( )} ) } export default Tooltip