TooltipBoundingBox.js 11 KB

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