File size: 3,791 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as React from 'react';
import { CSSProperties, PureComponent, ReactNode } from 'react';
import { AllowInDimension, AnimationDuration, AnimationTiming, CartesianViewBox, Coordinate } from '../util/types';
import { getTooltipTranslate } from '../util/tooltip/translate';
import { ElementOffset, SetElementOffset } from '../util/useElementOffset';

export type TooltipBoundingBoxProps = {
  active: boolean;
  allowEscapeViewBox: AllowInDimension;
  animationDuration: AnimationDuration;
  animationEasing: AnimationTiming;
  children: ReactNode;
  coordinate: Coordinate;
  hasPayload: boolean;
  isAnimationActive: boolean;
  offset: number;
  position: Partial<Coordinate>;
  reverseDirection: AllowInDimension;
  useTranslate3d: boolean;
  viewBox: CartesianViewBox;
  wrapperStyle: CSSProperties;
  lastBoundingBox: ElementOffset;
  innerRef: SetElementOffset;
  hasPortalFromProps: boolean;
};

type State = {
  dismissed: boolean;
  dismissedAtCoordinate: Coordinate;
};

export class TooltipBoundingBox extends PureComponent<TooltipBoundingBoxProps, State> {
  state = {
    dismissed: false,
    dismissedAtCoordinate: { x: 0, y: 0 },
  };

  componentDidMount() {
    document.addEventListener('keydown', this.handleKeyDown);
  }

  componentWillUnmount() {
    document.removeEventListener('keydown', this.handleKeyDown);
  }

  componentDidUpdate() {
    if (!this.state.dismissed) {
      return;
    }

    if (
      this.props.coordinate?.x !== this.state.dismissedAtCoordinate.x ||
      this.props.coordinate?.y !== this.state.dismissedAtCoordinate.y
    ) {
      this.state.dismissed = false;
    }
  }

  handleKeyDown = (event: KeyboardEvent) => {
    if (event.key === 'Escape') {
      this.setState({
        dismissed: true,
        dismissedAtCoordinate: {
          x: this.props.coordinate?.x ?? 0,
          y: this.props.coordinate?.y ?? 0,
        },
      });
    }
  };

  render() {
    const {
      active,
      allowEscapeViewBox,
      animationDuration,
      animationEasing,
      children,
      coordinate,
      hasPayload,
      isAnimationActive,
      offset,
      position,
      reverseDirection,
      useTranslate3d,
      viewBox,
      wrapperStyle,
      lastBoundingBox,
      innerRef,
      hasPortalFromProps,
    } = this.props;

    const { cssClasses, cssProperties } = getTooltipTranslate({
      allowEscapeViewBox,
      coordinate,
      offsetTopLeft: offset,
      position,
      reverseDirection,
      tooltipBox: {
        height: lastBoundingBox.height,
        width: lastBoundingBox.width,
      },
      useTranslate3d,
      viewBox,
    });

    // do not use absolute styles if the user has passed a custom portal prop
    const positionStyles: CSSProperties = hasPortalFromProps
      ? {}
      : {
          transition: isAnimationActive && active ? `transform ${animationDuration}ms ${animationEasing}` : undefined,
          ...cssProperties,
          pointerEvents: 'none',
          visibility: !this.state.dismissed && active && hasPayload ? 'visible' : 'hidden',
          position: 'absolute',
          top: 0,
          left: 0,
        };

    const outerStyle: CSSProperties = {
      ...positionStyles,
      visibility: !this.state.dismissed && active && hasPayload ? 'visible' : 'hidden',
      ...wrapperStyle,
    };

    return (
      // This element allow listening to the `Escape` key. See https://github.com/recharts/recharts/pull/2925
      <div
        // @ts-expect-error typescript library does not recognize xmlns attribute, but it's required for an HTML chunk inside SVG.
        xmlns="http://www.w3.org/1999/xhtml"
        tabIndex={-1}
        className={cssClasses}
        style={outerStyle}
        ref={innerRef}
      >
        {children}
      </div>
    );
  }
}