UNPKG

recharts

Version:
105 lines (101 loc) • 6.25 kB
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; } 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; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } 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); } /** * @fileOverview * Graphical items highlight the active data point with an active representation: * `activeDot` in Line, Area, and Radar, `activeBar` in Bar, * and `activeShape` in Pie, Scatter, RadialBar, and Funnel. * This file decides when the `active` styles of a theme apply to it. * All graphical items follow the same rules. * * The theme designs the `active` colors to go with its own series colors: for example a background fill * with a series-color stroke. They do not fit a shape that has colors of its own. * So the theme active `fill` and `stroke` only apply to shapes that take their colors from the theme. * If the graphical item props, or the data entry, set their own `fill` or `stroke`, * the active shape drops the theme active `fill` and `stroke` together, and keeps its own colors. * The same goes for an active representation object (such as an `activeDot` object) with its own colors. * * Other active style properties (opacities, `strokeWidth`, `strokeDasharray`) do not bring a color, * so they always apply in the active state. Only the active representation object overrides them, field by field. * * On top of that, the theme only styles what Recharts renders by default. * A custom shape (a function or a React element) has all the control, and it receives no theme active styles. */ import { isValidElement } from 'react'; import { useRechartsTheme } from './RechartsThemeContext'; import { hasOwnColors, withoutColors } from './dataEntryStyles'; /** * Merges the `active` theme styles with the styles of the active representation. * * @param activeStyle `active` slice of the graphical item theme * @param ownStyles styles of the active representation, such as an `activeDot` object * @returns theme styles to apply to the active representation, without the ones that `ownStyles` override. * Undefined if there is nothing to apply. */ export function resolveThemedActiveStyles(activeStyle, ownStyles) { if (activeStyle == null || ownStyles == null || typeof ownStyles !== 'object') { return activeStyle; } var fillOpacity = ownStyles.fillOpacity, strokeOpacity = ownStyles.strokeOpacity, strokeWidth = ownStyles.strokeWidth, strokeDasharray = ownStyles.strokeDasharray; var themeStyles = hasOwnColors(ownStyles) ? withoutColors(activeStyle) : activeStyle; if (fillOpacity == null && strokeOpacity == null && strokeWidth == null && strokeDasharray == null) { return Object.keys(themeStyles).length > 0 ? themeStyles : undefined; } var result = _objectSpread({}, themeStyles); if (fillOpacity != null) { delete result.fillOpacity; } if (strokeOpacity != null) { delete result.strokeOpacity; } if (strokeWidth != null) { delete result.strokeWidth; } if (strokeDasharray != null) { delete result.strokeDasharray; } return Object.keys(result).length > 0 ? result : undefined; } /** * Returns the `active` theme styles of a graphical item, * or undefined if there is no active theme, or the theme does not define `active` styles. * If the graphical item sets its own `fill` or `stroke`, the result has no theme colors. * * @param themeSelector selects the theme slice of this graphical item * @param explicitProps props as provided by the user, before merging with theme or defaults */ export function useThemedActiveStyles(themeSelector, explicitProps) { var _themeSelector; var theme = useRechartsTheme(); var activeStyle = theme == null ? undefined : (_themeSelector = themeSelector(theme)) === null || _themeSelector === void 0 ? void 0 : _themeSelector.active; return hasOwnColors(explicitProps) ? withoutColors(activeStyle) : activeStyle; } /** * Returns the theme styles for a single active representation, or undefined if the theme does not apply to it. * * The theme applies if Recharts renders the default shape: the option is `true`, a predefined symbol name * (such as `activeDot="diamond"` or `activeShape="star"`), or an object. * A data entry or an object that sets its own `fill` or `stroke` drops the theme colors, * and keeps the other theme styles. * A custom shape (function or React element) ignores the theme. * * @param activeOption the `activeDot`, `activeShape`, or `activeBar` prop * @param themedActiveStyles result of {@link useThemedActiveStyles}, or the `active` slice of the graphical item theme * @param entry data entry, if this graphical item renders one shape per data entry */ export function getActiveStyleOverrides(activeOption, themedActiveStyles, entry) { var themeStyles = hasOwnColors(entry) ? withoutColors(themedActiveStyles) : themedActiveStyles; if (activeOption === true || typeof activeOption === 'string') { return themeStyles; } if (activeOption != null && typeof activeOption === 'object' && ! /*#__PURE__*/isValidElement(activeOption)) { return resolveThemedActiveStyles(themeStyles, activeOption); } return undefined; }