UNPKG

recharts

Version:
74 lines (70 loc) • 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useGraphicalItemIdentity = useGraphicalItemIdentity; exports.useGraphicalItemThemeFill = useGraphicalItemThemeFill; var _react = require("react"); var _hooks = require("../state/hooks"); var _effectiveDataKeySelectors = require("../state/selectors/effectiveDataKeySelectors"); var _graphicalItemIdentity = require("./graphicalItemIdentity"); var _RechartsThemeContext = require("./RechartsThemeContext"); var _dataEntryStyles = require("./dataEntryStyles"); var _themeFill = require("./themeFill"); var STABLE_EMPTY_GRAPHICAL_ITEM_DATA_KEYS = []; var selectEmptyGraphicalItemDataKeys = () => STABLE_EMPTY_GRAPHICAL_ITEM_DATA_KEYS; /** * Creates a selector of the theme entry of a graphical item, using all dataKeys registered in the current chart. * @param dataKey effective dataKey of the graphical item, see {@link useEffectiveDataKey} */ function useGraphicalItemThemeEntry(dataKey) { var _useAppSelector; var activeTheme = (0, _RechartsThemeContext.useRechartsTheme)(); var graphicalItemDataKeys = (_useAppSelector = (0, _hooks.useAppSelector)(activeTheme == null ? selectEmptyGraphicalItemDataKeys : _effectiveDataKeySelectors.selectAllEffectiveGraphicalItemDataKeys)) !== null && _useAppSelector !== void 0 ? _useAppSelector : STABLE_EMPTY_GRAPHICAL_ITEM_DATA_KEYS; return (0, _react.useCallback)(theme => { if (dataKey == null) { return undefined; } return theme.graphicalItems[(0, _graphicalItemIdentity.graphicalItemIdentity)({ dataKey }, graphicalItemDataKeys, theme.graphicalItems.length)]; }, [dataKey, graphicalItemDataKeys]); } /** * Creates a theme selector for a graphical item using all dataKeys registered in the current chart. * * Theme colors never mix with user-provided colors. * If the graphical item sets its own fill or stroke, the selector returns the theme entry without its fill and stroke. * The item keeps the rest of its theme entry, including the `active` and `label` styles. * The `active` styles keep their colors here, `useThemedActiveStyles` removes them. * * A fill object in the theme entry counts by its `color`. Graphical items that draw the fill object * read it with {@link useGraphicalItemThemeFill}. * * @param dataKey effective dataKey of the graphical item, see {@link useEffectiveDataKey} * @param explicitProps props as provided by the user, before merging with theme or defaults */ function useGraphicalItemIdentity(dataKey, explicitProps) { var selectEntry = useGraphicalItemThemeEntry(dataKey); var hasExplicitColors = (0, _dataEntryStyles.hasOwnColors)(explicitProps); return (0, _react.useCallback)(theme => { var entry = (0, _themeFill.withFillColor)(selectEntry(theme)); return hasExplicitColors ? (0, _dataEntryStyles.withoutColors)(entry) : entry; }, [selectEntry, hasExplicitColors]); } /** * Returns the fill object of the theme entry of a graphical item, or undefined if the theme entry fills with a color. * A graphical item that sets its own fill or stroke gets undefined, the same as with {@link useGraphicalItemIdentity}. * * @param dataKey effective dataKey of the graphical item, see {@link useEffectiveDataKey} * @param explicitProps props as provided by the user, before merging with theme or defaults */ function useGraphicalItemThemeFill(dataKey, explicitProps) { var _selectEntry; var theme = (0, _RechartsThemeContext.useRechartsTheme)(); var selectEntry = useGraphicalItemThemeEntry(dataKey); if (theme == null || (0, _dataEntryStyles.hasOwnColors)(explicitProps)) { return undefined; } return (0, _themeFill.getThemeFillObject)((_selectEntry = selectEntry(theme)) === null || _selectEntry === void 0 ? void 0 : _selectEntry.fill); }