# All files / components AnnotationTooltip.tsx

17.86% Statements 5/28
0% Branches 0/14
0% Functions 0/1
21.74% Lines 5/23

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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    1151x

    1x

    1x
    1x

    1x

     import React, {CSSProperties} from 'react'
    import {FunctionComponent} from 'react'
    import {createPortal} from 'react-dom'

    import {Config, AnnotationMark, TooltipPosition} from '../types'
    import {ANNOTATION_TOOLTIP_CONTAINER_NAME} from '../constants'
    import {useTooltipElement} from '../utils/useTooltipElement'

    interface Props {
      boundingReference: DOMRect
      config: Config
      data: AnnotationMark
      width: number
    }

    export const AnnotationTooltip: FunctionComponent<Props> = props => {
      const {boundingReference, config, data, width} = props
      const {
        legendBackgroundColor: backgroundColor,
        legendBorder: border,
        legendFont: font,
        legendFontColor: fontColor,
      } = config
      const {dimension, startValue} = data || {}

      const position = {
        x: dimension === 'x' ? startValue : width,
        y: dimension === 'y' ? startValue : 0,
      } as TooltipPosition

      const clampedXOffset = Math.round(
        boundingReference ? boundingReference.x : position.x
      )

      const clampedYOffset = Math.round(
        boundingReference ? boundingReference.y : position.y
      )

      const annotationTooltipElement = useTooltipElement(
        ANNOTATION_TOOLTIP_CONTAINER_NAME,
        {
          xOffset: clampedXOffset,
          yOffset: clampedYOffset,
          position,
          dimension,
        }
      )

      let tooltipCaretStyle: CSSProperties = {
        position: 'absolute',
        borderWidth: '10px',
        borderStyle: 'solid',
        borderColor: 'transparent',
        zIndex: 2,
      }
      let tooltipCaretFillStyle

      if (dimension === 'x') {
        tooltipCaretStyle = {
          ...tooltipCaretStyle,
          borderTopColor: data.color,
          left: '50%',
          top: '100%',
          transform: 'translateX(-50%)',
        }

        tooltipCaretFillStyle = {
          ...tooltipCaretStyle,
          borderTopColor: backgroundColor,
          top: 'calc(100% - 4px)',
          zIndex: 3,
        }
      }

      if (dimension === 'y') {
        tooltipCaretStyle = {
          ...tooltipCaretStyle,
          borderRightColor: data.color,
          top: '50%',
          right: '100%',
          transform: 'translateY(-50%)',
        }

        tooltipCaretFillStyle = {
          ...tooltipCaretStyle,
          borderRightColor: backgroundColor,
          right: 'calc(100% - 4px)',
          zIndex: 3,
        }
      }

      return createPortal(
        <div
          className="giraffe-annotation-tooltip"
          data-testid="giraffe-anntation-tooltip"
          style={{
            border,
            borderColor: data.color,
            font,
            backgroundColor,
            color: fontColor,
            boxShadow: `0 0 4px 0px ${data.color}`,
            borderRadius: '3px',
            padding: '10px',
          }}
        >
          <div style={tooltipCaretStyle} />
          <div style={tooltipCaretFillStyle} />
          <div>{data.title}</div>
          <div>{data.description}</div>
        </div>,
        annotationTooltipElement
      )
    }