TooltipBoundingBox.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  2. function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
  3. function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  6. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  7. function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
  8. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  9. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  10. function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
  11. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  12. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  13. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  14. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  15. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  16. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
  17. import React, { PureComponent } from 'react';
  18. import { getTooltipTranslate } from '../util/tooltip/translate';
  19. var EPSILON = 1;
  20. export var TooltipBoundingBox = /*#__PURE__*/function (_PureComponent) {
  21. function TooltipBoundingBox() {
  22. var _this;
  23. _classCallCheck(this, TooltipBoundingBox);
  24. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  25. args[_key] = arguments[_key];
  26. }
  27. _this = _callSuper(this, TooltipBoundingBox, [].concat(args));
  28. _defineProperty(_this, "state", {
  29. dismissed: false,
  30. dismissedAtCoordinate: {
  31. x: 0,
  32. y: 0
  33. },
  34. lastBoundingBox: {
  35. width: -1,
  36. height: -1
  37. }
  38. });
  39. _defineProperty(_this, "handleKeyDown", function (event) {
  40. if (event.key === 'Escape') {
  41. var _this$props$coordinat, _this$props$coordinat2, _this$props$coordinat3, _this$props$coordinat4;
  42. _this.setState({
  43. dismissed: true,
  44. dismissedAtCoordinate: {
  45. x: (_this$props$coordinat = (_this$props$coordinat2 = _this.props.coordinate) === null || _this$props$coordinat2 === void 0 ? void 0 : _this$props$coordinat2.x) !== null && _this$props$coordinat !== void 0 ? _this$props$coordinat : 0,
  46. y: (_this$props$coordinat3 = (_this$props$coordinat4 = _this.props.coordinate) === null || _this$props$coordinat4 === void 0 ? void 0 : _this$props$coordinat4.y) !== null && _this$props$coordinat3 !== void 0 ? _this$props$coordinat3 : 0
  47. }
  48. });
  49. }
  50. });
  51. return _this;
  52. }
  53. _inherits(TooltipBoundingBox, _PureComponent);
  54. return _createClass(TooltipBoundingBox, [{
  55. key: "updateBBox",
  56. value: function updateBBox() {
  57. if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) {
  58. var box = this.wrapperNode.getBoundingClientRect();
  59. if (Math.abs(box.width - this.state.lastBoundingBox.width) > EPSILON || Math.abs(box.height - this.state.lastBoundingBox.height) > EPSILON) {
  60. this.setState({
  61. lastBoundingBox: {
  62. width: box.width,
  63. height: box.height
  64. }
  65. });
  66. }
  67. } else if (this.state.lastBoundingBox.width !== -1 || this.state.lastBoundingBox.height !== -1) {
  68. this.setState({
  69. lastBoundingBox: {
  70. width: -1,
  71. height: -1
  72. }
  73. });
  74. }
  75. }
  76. }, {
  77. key: "componentDidMount",
  78. value: function componentDidMount() {
  79. document.addEventListener('keydown', this.handleKeyDown);
  80. this.updateBBox();
  81. }
  82. }, {
  83. key: "componentWillUnmount",
  84. value: function componentWillUnmount() {
  85. document.removeEventListener('keydown', this.handleKeyDown);
  86. }
  87. }, {
  88. key: "componentDidUpdate",
  89. value: function componentDidUpdate() {
  90. var _this$props$coordinat5, _this$props$coordinat6;
  91. if (this.props.active) {
  92. this.updateBBox();
  93. }
  94. if (!this.state.dismissed) {
  95. return;
  96. }
  97. if (((_this$props$coordinat5 = this.props.coordinate) === null || _this$props$coordinat5 === void 0 ? void 0 : _this$props$coordinat5.x) !== this.state.dismissedAtCoordinate.x || ((_this$props$coordinat6 = this.props.coordinate) === null || _this$props$coordinat6 === void 0 ? void 0 : _this$props$coordinat6.y) !== this.state.dismissedAtCoordinate.y) {
  98. this.state.dismissed = false;
  99. }
  100. }
  101. }, {
  102. key: "render",
  103. value: function render() {
  104. var _this2 = this;
  105. var _this$props = this.props,
  106. active = _this$props.active,
  107. allowEscapeViewBox = _this$props.allowEscapeViewBox,
  108. animationDuration = _this$props.animationDuration,
  109. animationEasing = _this$props.animationEasing,
  110. children = _this$props.children,
  111. coordinate = _this$props.coordinate,
  112. hasPayload = _this$props.hasPayload,
  113. isAnimationActive = _this$props.isAnimationActive,
  114. offset = _this$props.offset,
  115. position = _this$props.position,
  116. reverseDirection = _this$props.reverseDirection,
  117. useTranslate3d = _this$props.useTranslate3d,
  118. viewBox = _this$props.viewBox,
  119. wrapperStyle = _this$props.wrapperStyle;
  120. var _getTooltipTranslate = getTooltipTranslate({
  121. allowEscapeViewBox: allowEscapeViewBox,
  122. coordinate: coordinate,
  123. offsetTopLeft: offset,
  124. position: position,
  125. reverseDirection: reverseDirection,
  126. tooltipBox: this.state.lastBoundingBox,
  127. useTranslate3d: useTranslate3d,
  128. viewBox: viewBox
  129. }),
  130. cssClasses = _getTooltipTranslate.cssClasses,
  131. cssProperties = _getTooltipTranslate.cssProperties;
  132. var outerStyle = _objectSpread(_objectSpread({
  133. transition: isAnimationActive && active ? "transform ".concat(animationDuration, "ms ").concat(animationEasing) : undefined
  134. }, cssProperties), {}, {
  135. pointerEvents: 'none',
  136. visibility: !this.state.dismissed && active && hasPayload ? 'visible' : 'hidden',
  137. position: 'absolute',
  138. top: 0,
  139. left: 0
  140. }, wrapperStyle);
  141. return (
  142. /*#__PURE__*/
  143. // This element allow listening to the `Escape` key.
  144. // See https://github.com/recharts/recharts/pull/2925
  145. React.createElement("div", {
  146. tabIndex: -1,
  147. className: cssClasses,
  148. style: outerStyle,
  149. ref: function ref(node) {
  150. _this2.wrapperNode = node;
  151. }
  152. }, children)
  153. );
  154. }
  155. }]);
  156. }(PureComponent);