# All files / utils useTooltipStyle.ts

11.54% Statements 6/52
0% Branches 0/26
0% Functions 0/4
14.29% Lines 6/42

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
    1431x
    1x
    1x

    1x

    1x

    1x

     import {useLayoutStyle} from './useLayoutStyle'
    import {useRefMousePos} from './useMousePos'
    import {CLOCKFACE_Z_INDEX, LEAFLET_Z_INDEX} from '../constants'
    import {AnnotationTooltipOptions} from '../types'

    const MARGIN_X = 30

    export const useTooltipStyle = (el: HTMLDivElement) => {
      const {x, y} = useRefMousePos(document.body)

      // Position the tooltip next to the mouse cursor, like this:
      //
      //                   ┌─────────────┐
      //                   │             │
      //          (mouse)  │   tooltip   │
      //                   │             │
      //                   └─────────────┘
      //
      // The positioning is subject to the following restrictions:
      //
      // - If the tooltip overflows the right side of the screen, position it on
      //   the left side of the cursor instead
      //
      // - If the tooltip overflows the top or bottom of the screen (with a bit of
      //   margin), shift it just enough so that it is fully back inside the screen
      //
      useLayoutStyle(
        el,
        ({offsetWidth: tooltipWidth, offsetHeight: tooltipHeight}) => {
          if (x === null || y === null) {
            return {
              display: 'none',
            }
          }

          let dx = MARGIN_X
          let dy = 0 - tooltipHeight / 2

          if (x + dx + tooltipWidth > window.innerWidth) {
            dx = 0 - MARGIN_X - tooltipWidth
          }

          if (y + dy + tooltipHeight > window.innerHeight) {
            dy -= y + dy + tooltipHeight - window.innerHeight
          }

          if (y + dy < 0) {
            dy += 0 - (y + dy)
          }

          const clampedX = Math.max(x + dx, 8)
          const clampedY = Math.max(y + dy, 8)

          /* Geo widget maps are rendered with z-index: 399, we have to set it above
           that so that tooltips are not rendered/are hidden below the map, */
          return {
            display: 'inline',
            position: 'fixed',
            left: `${clampedX}px`,
            top: `${clampedY}px`,
            zIndex: CLOCKFACE_Z_INDEX + LEAFLET_Z_INDEX + 1,
          }
        }
      )
    }

    export const useAnnotationStyle = (
      el: HTMLDivElement,
      options: AnnotationTooltipOptions
    ) => {
      const {dimension, position, xOffset, yOffset} = options || {}
      const {x, y} = position || {x: null, y: null}

      // Position the tooltip above the annotation for vertical annotations, like this:
      //
      //          ┌─────────────┐
      //          │             │
      //          │   tooltip   │
      //          │             │
      //          └─────────────┘
      //                 |
      //                 |
      //                 |
      //                 |
      //
      // Position the tooltip to the right of the annotation for horizontal annotations, like this:
      //
      //             ┌─────────────┐
      //             │             │
      //    ---------│   tooltip   │
      //             │             │
      //             └─────────────┘
      //
      // The positioning is subject to the following restrictions:
      //
      // - If the tooltip does not fit above a vertical annotation due to screen size,
      //   shift it to overlay the top part of the annotation
      //
      // - If the tooltip does not fit to the right of a horizontal annotation due to screen size,
      //   shift it to overlay the right part of the annotation
      //
      useLayoutStyle(
        el,
        ({offsetWidth: tooltipWidth, offsetHeight: tooltipHeight}) => {
          if (x === null || y === null) {
            return {
              display: 'none',
            }
          }

          let dx = xOffset - tooltipWidth / 2
          if (dx + x + tooltipWidth > window.innerWidth) {
            dx = 0 - tooltipWidth / 2 + window.innerWidth - (x + xOffset)
          }
          let dy = Math.max(yOffset - tooltipHeight, 0)

          if (dimension === 'y') {
            dx = xOffset
            if (dx + x + tooltipWidth > window.innerWidth) {
              dx = 0 - tooltipWidth + window.innerWidth - (dx + x)
            }
            dy = yOffset - tooltipHeight / 2
            if (dy + y + tooltipHeight > window.innerHeight) {
              dy = 0 - tooltipHeight + window.innerHeight - (dy + y)
            }
          }

          const clampedX = Math.round(Math.max(dx + x, x))
          const clampedY = Math.round(dy + y)

          /* Geo widget maps are rendered with z-index: 399, we have to set it above
           that so that tooltips are not rendered/are hidden below the map, */
          return {
            display: 'inline',
            position: 'fixed',
            left: `${clampedX}px`,
            top: `${clampedY}px`,
            zIndex: CLOCKFACE_Z_INDEX + LEAFLET_Z_INDEX + 1,
          }
        }
      )
    }