UNPKG

@mui/x-charts

Version:

The community edition of the charts components (MUI X).

91 lines (90 loc) 2.44 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import * as React from 'react'; import { SVGContext } from '../context/DrawingProvider'; export function generateVirtualElement(mousePosition) { if (mousePosition === null) { return { getBoundingClientRect: function getBoundingClientRect() { return { width: 0, height: 0, x: 0, y: 0, top: 0, right: 0, bottom: 0, left: 0, toJSON: function toJSON() { return ''; } }; } }; } var x = mousePosition.x, y = mousePosition.y; return { getBoundingClientRect: function getBoundingClientRect() { return { width: 0, height: 0, x: x, y: y, top: y, right: x, bottom: y, left: x, toJSON: function toJSON() { return JSON.stringify({ width: 0, height: 0, x: x, y: y, top: y, right: x, bottom: y, left: x }); } }; } }; } export function useMouseTracker() { var svgRef = React.useContext(SVGContext); // Use a ref to avoid rerendering on every mousemove event. var _React$useState = React.useState(null), _React$useState2 = _slicedToArray(_React$useState, 2), mousePosition = _React$useState2[0], setMousePosition = _React$useState2[1]; React.useEffect(function () { var element = svgRef.current; if (element === null) { return function () {}; } var handleMouseOut = function handleMouseOut() { setMousePosition(null); }; var handleMouseMove = function handleMouseMove(event) { setMousePosition({ x: event.clientX, y: event.clientY }); }; element.addEventListener('mouseout', handleMouseOut); element.addEventListener('mousemove', handleMouseMove); return function () { element.removeEventListener('mouseout', handleMouseOut); element.removeEventListener('mousemove', handleMouseMove); }; }, [svgRef]); return mousePosition; } export function getTooltipHasData(trigger, displayedData) { if (trigger === 'item') { return displayedData !== null; } var hasAxisXData = displayedData.x !== null; var hasAxisYData = displayedData.y !== null; return hasAxisXData || hasAxisYData; }