@mui/x-charts
Version:
The community edition of MUI X Charts components.
187 lines (186 loc) • 6.81 kB
JavaScript
'use client';
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import _extends from "@babel/runtime/helpers/esm/extends";
const _excluded = ["direction", "onItemClick", "className", "classes", "toggleVisibilityOnClick"];
import * as React from 'react';
import { styled } from '@mui/material/styles';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import useEventCallback from '@mui/utils/useEventCallback';
import { useLegend } from "../hooks/useLegend.mjs";
import { ChartsLabelMark } from "../ChartsLabel/ChartsLabelMark.mjs";
import { seriesContextBuilder } from "./onClickContextBuilder.mjs";
import { legendClasses, useUtilityClasses } from "./chartsLegendClasses.mjs";
import { consumeSlots } from "../internals/consumeSlots.mjs";
import { ChartsLabel } from "../ChartsLabel/ChartsLabel.mjs";
import { useChartsContext } from "../context/ChartsProvider/index.mjs";
import { selectorIsItemVisibleGetter } from "../internals/plugins/featurePlugins/useChartVisibilityManager/index.mjs";
import { useStore } from "../internals/store/useStore.mjs";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const RootElement = styled('ul', {
name: 'MuiChartsLegend',
slot: 'Root'
})(({
ownerState,
theme
}) => _extends({}, theme.typography.caption, {
color: (theme.vars || theme).palette.text.primary,
lineHeight: '100%',
display: 'flex',
flexDirection: ownerState.direction === 'vertical' ? 'column' : 'row',
alignItems: ownerState.direction === 'vertical' ? undefined : 'center',
flexShrink: 0,
gap: theme.spacing(2),
listStyleType: 'none',
paddingInlineStart: 0,
marginBlock: theme.spacing(1),
marginInline: theme.spacing(1),
flexWrap: 'wrap',
li: {
display: ownerState.direction === 'horizontal' ? 'inline-flex' : undefined
},
[`button.${legendClasses.series}`]: {
// Reset button styles
background: 'none',
border: 'none',
padding: 0,
fontFamily: 'inherit',
fontWeight: 'inherit',
fontSize: 'inherit',
letterSpacing: 'inherit',
color: 'inherit'
},
[`& .${legendClasses.series}`]: {
display: ownerState.direction === 'vertical' ? 'flex' : 'inline-flex',
alignItems: 'center',
gap: theme.spacing(1),
cursor: ownerState.onItemClick || ownerState.toggleVisibilityOnClick ? 'pointer' : 'default',
[`&.${legendClasses.hidden}`]: {
opacity: 0.5
}
},
gridArea: 'legend'
}));
const ChartsLegend = consumeSlots('MuiChartsLegend', 'legend', {
defaultProps: {
direction: 'horizontal'
},
// @ts-expect-error position is used only in the slots, but it is passed to the SVG wrapper.
// We omit it here to avoid passing to slots.
omitProps: ['position'],
classesResolver: useUtilityClasses
}, /*#__PURE__*/React.forwardRef(function ChartsLegend(props, ref) {
const data = useLegend();
const {
instance
} = useChartsContext();
const store = useStore();
const isItemVisible = store.use(selectorIsItemVisibleGetter);
const {
onItemClick,
className,
classes,
toggleVisibilityOnClick
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
const isButton = Boolean(onItemClick || toggleVisibilityOnClick);
const handleClick = useEventCallback((item, i) => event => {
if (onItemClick && item) {
onItemClick(event, seriesContextBuilder(item), i);
}
if (toggleVisibilityOnClick) {
instance.toggleItemVisibility({
type: item.type,
seriesId: item.seriesId,
dataIndex: item.dataIndex
});
}
});
if (data.items.length === 0) {
return null;
}
return /*#__PURE__*/_jsx(RootElement, _extends({
className: clsx(classes?.root, className),
ref: ref
}, other, {
ownerState: props,
children: data.items.map((item, i) => {
const isVisible = isItemVisible({
type: item.type,
seriesId: item.seriesId,
dataIndex: item.dataIndex
});
return /*#__PURE__*/_jsx("li", {
className: classes?.item,
"data-series": item.seriesId,
"data-index": item.dataIndex,
children: isButton ? /*#__PURE__*/_jsxs("button", {
className: clsx(classes?.series, !isVisible && classes?.hidden),
onClick: handleClick(item, i),
type: "button",
children: [/*#__PURE__*/_jsx(ChartsLabelMark, {
className: classes?.mark,
color: item.color,
type: item.markType,
markShape: item.markShape
}), /*#__PURE__*/_jsx(ChartsLabel, {
className: classes?.label,
children: item.label
})]
}) : /*#__PURE__*/_jsxs("div", {
className: clsx(classes?.series, !isVisible && classes?.hidden),
children: [/*#__PURE__*/_jsx(ChartsLabelMark, {
className: classes?.mark,
color: item.color,
type: item.markType,
markShape: item.markShape
}), /*#__PURE__*/_jsx(ChartsLabel, {
className: classes?.label,
children: item.label
})]
})
}, `${item.seriesId}-${item.dataIndex}`);
})
}));
}));
if (process.env.NODE_ENV !== "production") ChartsLegend.displayName = "ChartsLegend";
process.env.NODE_ENV !== "production" ? ChartsLegend.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
className: PropTypes.string,
/**
* The direction of the legend layout.
* The default depends on the chart.
*/
direction: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* Callback fired when a legend item is clicked.
* @param {React.MouseEvent<HTMLButtonElement, MouseEvent>} event The click event.
* @param {SeriesLegendItemContext} legendItem The legend item data.
* @param {number} index The index of the clicked legend item.
*/
onItemClick: PropTypes.func,
/**
* The props used for each component slot.
* @default {}
*/
slotProps: PropTypes.object,
/**
* Overridable component slots.
* @default {}
*/
slots: PropTypes.object,
sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
/**
* If `true`, clicking on a legend item will toggle the visibility of the corresponding series.
* @default false
*/
toggleVisibilityOnClick: PropTypes.bool
} : void 0;
export { ChartsLegend };