@mui/x-charts
Version:
The community edition of the charts components (MUI X).
80 lines • 2.23 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { jsx as _jsx } from "react/jsx-runtime";
export var InteractionContext = /*#__PURE__*/React.createContext({
item: null,
axis: {
x: null,
y: null
},
useVoronoiInteraction: false,
dispatch: function dispatch() {
return null;
}
});
var dataReducer = function dataReducer(prevState, action) {
switch (action.type) {
case 'enterItem':
return _extends({}, prevState, {
item: action.data
});
case 'exitChart':
if (prevState.item === null && prevState.axis.x === null && prevState.axis.y === null) {
return prevState;
}
return _extends({}, prevState, {
axis: {
x: null,
y: null
},
item: null
});
case 'updateVoronoiUsage':
return _extends({}, prevState, {
useVoronoiInteraction: action.useVoronoiInteraction
});
case 'leaveItem':
if (prevState.item === null || Object.keys(action.data).some(function (key) {
return action.data[key] !== prevState.item[key];
})) {
// The item is already something else
return prevState;
}
return _extends({}, prevState, {
item: null
});
case 'updateAxis':
if (action.data.x === prevState.axis.x && action.data.y === prevState.axis.y) {
return prevState;
}
return _extends({}, prevState, {
axis: action.data
});
default:
return prevState;
}
};
export function InteractionProvider(_ref) {
var children = _ref.children;
var _React$useReducer = React.useReducer(dataReducer, {
item: null,
axis: {
x: null,
y: null
},
useVoronoiInteraction: false
}),
_React$useReducer2 = _slicedToArray(_React$useReducer, 2),
data = _React$useReducer2[0],
dispatch = _React$useReducer2[1];
var value = React.useMemo(function () {
return _extends({}, data, {
dispatch: dispatch
});
}, [data]);
return /*#__PURE__*/_jsx(InteractionContext.Provider, {
value: value,
children: children
});
}