# All files / src/components Tooltip.jsx

50% Statements 6/12
0% Branches 0/12
0% Functions 0/1
50% Lines 6/12

    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

    2x

    2x

    2x

    2x

    2x
    2x

     import { css, StyleSheet } from 'aphrodite';
    import React from 'react';
    import PropTypes from 'prop-types';

    import { withBoundingRects, withBoundingRectsProps } from '@vx/bounds';

    const DEFAULT_WIDTH = 200;

    const styles = StyleSheet.create({
      tooltip: {
        pointerEvents: 'none',
        position: 'absolute',
        zIndex: 100,
        background: 'white',
        border: '1px solid #eaeaea',
        padding: 8,
        borderRadius: 4,
        fontFamily: 'BlinkMacSystemFont,Roboto,Helvetica Neue,sans-serif',
        fontSize: 12,
      },
    });

    const propTypes = {
      ...withBoundingRectsProps,
      left: PropTypes.number.isRequired,
      top: PropTypes.number.isRequired,
      detectOverflowX: PropTypes.bool,
      detectOverflowY: PropTypes.bool,
      width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
      children: PropTypes.node,
    };

    const defaultProps = {
      width: DEFAULT_WIDTH,
      detectOverflowX: true,
      detectOverflowY: true,
      children: null,
    };

    function Tooltip({
      left: initialLeft,
      top: initialTop,
      width,
      rect,
      parentRect,
      detectOverflowX,
      detectOverflowY,
      children,
    }) {
      let left = initialLeft;
      let top = initialTop;

      if (rect && parentRect) {
        left = detectOverflowX && rect.right > parentRect.right ? (left - rect.width) : left;
        top = detectOverflowY && rect.bottom > parentRect.bottom ? (top - rect.height) : top;
      }

      return (
        <div
          className={css(styles.tooltip)}
          style={{ top, left, width }}
        >
          {children}
        </div>
      );
    }

    Tooltip.propTypes = propTypes;
    Tooltip.defaultProps = defaultProps;

    export default withBoundingRects(Tooltip);