# All files / components AnnotationTooltip.tsx

14.28% Statements 5/35
0% Branches 0/16
0% Functions 0/2
17.24% Lines 5/29

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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    1441x

    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,
      ANNOTATION_DEFAULT_MAX_WIDTH,
    } from '../constants'
    import {useAnnotationTooltipElement} from '../utils/legend/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, stopValue} = data || {}

      // move this 15 pixels up to get out of the way of the annotation click target
      const yAxisTooltipOffset = -15

      // setting position to be the average between start and stop;
      // if it is a ranged annotation then it will appear in the middle,
      // if it is a point annotation then the math is a no-op.
      const position = {
        x: dimension === 'x' ? (startValue + stopValue) / 2 : width,
        y: dimension === 'y' ? (startValue + stopValue) / 2 : yAxisTooltipOffset,
      } as TooltipPosition

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

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

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

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

      Iif (dimension === 'x') {
        tooltipCaretStyle = {
          ...tooltipCaretStyle,
          borderTopColor: data.color,
          left: `${clampedXOffset + position.x}px`,
          top: `${clampedYOffset + position.y}px`,
          transform: 'translateX(-50%)',
        }

        tooltipCaretFillStyle = {
          ...tooltipCaretStyle,
          borderTopColor: backgroundColor,
          top: `calc(${clampedYOffset + position.y}px - 4px)`,
          zIndex: 3,
        }
      }

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

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

      const textContainerStyle: CSSProperties = {
        overflowWrap: 'break-word',
      }

      const multiLines = data.title.split('\n')

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

          {multiLines.map((line, index) => {
            Iif (!line || line === '' || line.trim() === '') {
              return <br />
            }
            return (
              <div key={`annoLine-${index}`} style={textContainerStyle}>
                {line}
              </div>
            )
          })}
          <div style={textContainerStyle}>{data.description}</div>
        </div>,
        annotationTooltipElement
      )
    }