recharts
Version:
React charts
67 lines (64 loc) • 3.42 kB
JavaScript
import { useCallback } from 'react';
import { useAppSelector } from '../state/hooks';
import { selectAllEffectiveGraphicalItemDataKeys } from '../state/selectors/effectiveDataKeySelectors';
import { graphicalItemIdentity } from './graphicalItemIdentity';
import { useRechartsTheme } from './RechartsThemeContext';
import { hasOwnColors, withoutColors } from './dataEntryStyles';
import { getThemeFillObject, withFillColor } from './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 = useRechartsTheme();
var graphicalItemDataKeys = (_useAppSelector = useAppSelector(activeTheme == null ? selectEmptyGraphicalItemDataKeys : selectAllEffectiveGraphicalItemDataKeys)) !== null && _useAppSelector !== void 0 ? _useAppSelector : STABLE_EMPTY_GRAPHICAL_ITEM_DATA_KEYS;
return useCallback(theme => {
if (dataKey == null) {
return undefined;
}
return theme.graphicalItems[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
*/
export function useGraphicalItemIdentity(dataKey, explicitProps) {
var selectEntry = useGraphicalItemThemeEntry(dataKey);
var hasExplicitColors = hasOwnColors(explicitProps);
return useCallback(theme => {
var entry = withFillColor(selectEntry(theme));
return hasExplicitColors ? 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
*/
export function useGraphicalItemThemeFill(dataKey, explicitProps) {
var _selectEntry;
var theme = useRechartsTheme();
var selectEntry = useGraphicalItemThemeEntry(dataKey);
if (theme == null || hasOwnColors(explicitProps)) {
return undefined;
}
return getThemeFillObject((_selectEntry = selectEntry(theme)) === null || _selectEntry === void 0 ? void 0 : _selectEntry.fill);
}