@mui/x-charts
Version:
The community edition of MUI X Charts components.
140 lines (139 loc) • 5.04 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"];
import * as React from 'react';
import { styled } from '@mui/material/styles';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { useLegend } from "../hooks/useLegend.js";
import { ChartsLabelMark } from "../ChartsLabel/ChartsLabelMark.js";
import { seriesContextBuilder } from "./onClickContextBuilder.js";
import { legendClasses, useUtilityClasses } from "./chartsLegendClasses.js";
import { consumeSlots } from "../internals/consumeSlots.js";
import { ChartsLabel } from "../ChartsLabel/ChartsLabel.js";
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)
}
}));
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 {
onItemClick,
className,
classes
} = props,
other = _objectWithoutPropertiesLoose(props, _excluded);
if (data.items.length === 0) {
return null;
}
const Element = onItemClick ? 'button' : 'div';
return /*#__PURE__*/_jsx(RootElement, _extends({
className: clsx(classes?.root, className),
ref: ref
}, other, {
ownerState: props,
children: data.items.map((item, i) => {
return /*#__PURE__*/_jsx("li", {
className: classes?.item,
"data-series": item.id,
children: /*#__PURE__*/_jsxs(Element, {
className: classes?.series,
role: onItemClick ? 'button' : undefined,
type: onItemClick ? 'button' : undefined,
onClick: onItemClick ?
// @ts-ignore onClick is only attached to a button
event => onItemClick(event, seriesContextBuilder(item), i) : undefined,
children: [/*#__PURE__*/_jsx(ChartsLabelMark, {
className: classes?.mark,
color: item.color,
type: item.markType
}), /*#__PURE__*/_jsx(ChartsLabel, {
className: classes?.label,
children: item.label
})]
})
}, item.id);
})
}));
}));
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])
} : void 0;
export { ChartsLegend };