UNPKG

recharts

Version:
95 lines (87 loc) • 3.89 kB
var _excluded = ["fill", "active"]; function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import { useRechartsTheme } from './RechartsThemeContext'; /** * Theme styles with the `fill` as a plain color. */ /** * A theme entry of a graphical item with the `fill`, and the `fill` of its `active` styles, as plain colors. */ /** * Returns the fill object of a theme `fill`, or undefined if the `fill` is a color. * @param fill the `fill` of a theme style */ export function getThemeFillObject(fill) { return typeof fill === 'object' && fill != null ? fill : undefined; } function getFillColor(fill) { var _getThemeFillObject$c, _getThemeFillObject; return (_getThemeFillObject$c = (_getThemeFillObject = getThemeFillObject(fill)) === null || _getThemeFillObject === void 0 ? void 0 : _getThemeFillObject.color) !== null && _getThemeFillObject$c !== void 0 ? _getThemeFillObject$c : typeof fill === 'string' ? fill : undefined; } function hasColorFillsOnly(style) { var _style$active; return getThemeFillObject(style.fill) == null && getThemeFillObject((_style$active = style.active) === null || _style$active === void 0 ? void 0 : _style$active.fill) == null; } var withFillColorCache = new WeakMap(); /** * Returns the theme style with its fill objects replaced by their `color`, * for everything that paints with a plain color. * A fill object without `color` leaves no `fill`. * A style without fill objects comes back as it is. Otherwise the result is a copy, and it is cached, * so the same style always returns the same object. That keeps theme selectors stable. * @param style theme style, for example an entry of `graphicalItems`, or `grid` */ export function withFillColor(style) { if (style == null) { return undefined; } if (hasColorFillsOnly(style)) { return style; } var cached = withFillColorCache.get(style); if (cached != null) { return cached; } var fill = style.fill, active = style.active, rest = _objectWithoutProperties(style, _excluded); var result = rest; var color = getFillColor(fill); if (color != null) { result.fill = color; } if (active != null) { result.active = withFillColor(active); } withFillColorCache.set(style, result); return result; } var graphicalItemsCache = new WeakMap(); /** * Returns {@link withFillColor} of each theme entry. The result is cached, so the same entries always return * the same array. * @param graphicalItems the `graphicalItems` of a theme */ export function getColorGraphicalItems(graphicalItems) { if (graphicalItems == null) { return undefined; } var cached = graphicalItemsCache.get(graphicalItems); if (cached != null) { return cached; } var result = graphicalItems.map(style => withFillColor(style)); graphicalItemsCache.set(graphicalItems, result); return result; } /** * Returns the fill object of a theme slot, or undefined without a theme, or if the slot fills with a color. * Components that draw fill objects paint with it when they have no `fill` of their own. * @param themeSelector selects the theme slot, for example `theme => theme.grid` */ export function useThemeFillObject(themeSelector) { var _themeSelector; var theme = useRechartsTheme(); return theme == null ? undefined : getThemeFillObject((_themeSelector = themeSelector(theme)) === null || _themeSelector === void 0 ? void 0 : _themeSelector.fill); }