{"version":3,"file":"ChartLegend.cjs","names":["factory","useProps","useStyles","getSeriesLabels","ColorSwatch","Box","classes"],"sources":["../../src/ChartLegend/ChartLegend.tsx"],"sourcesContent":["import {\n  Box,\n  BoxProps,\n  ColorSwatch,\n  ElementProps,\n  factory,\n  Factory,\n  StylesApiProps,\n  useProps,\n  useStyles,\n} from '@mantine/core';\nimport { ChartSeries } from '../types';\nimport { getSeriesLabels } from '../utils';\nimport classes from './ChartLegend.module.css';\n\nfunction updateChartLegendPayload(\n  payload: Record<string, any>[],\n  splitNestedKeys: boolean\n): Record<string, any>[] {\n  return payload.map((item) => {\n    const newDataKey = splitNestedKeys ? item.dataKey?.split('.').pop() : item.dataKey;\n    return {\n      ...item,\n      dataKey: newDataKey,\n      payload: {\n        ...item.payload,\n        name: newDataKey,\n        dataKey: newDataKey,\n      },\n    };\n  });\n}\n\nexport function getFilteredChartLegendPayload(\n  payload: readonly Record<string, any>[],\n  splitNestedKeys = true\n) {\n  return updateChartLegendPayload(\n    payload.filter((item) => item.color !== 'none'),\n    splitNestedKeys\n  );\n}\n\nexport type ChartLegendStylesNames = 'legendItem' | 'legendItemColor' | 'legendItemName' | 'legend';\n\nexport interface ChartLegendProps\n  extends BoxProps, StylesApiProps<ChartLegendFactory>, ElementProps<'div'> {\n  /** Chart data provided by recharts */\n  payload: readonly Record<string, any>[] | undefined;\n\n  /** Function called when mouse enters/leaves one of the legend items */\n  onHighlight: (area: string | number | null) => void;\n\n  /** Position of the legend relative to the chart, used to apply margin on the corresponding side */\n  legendPosition: 'top' | 'bottom' | 'middle';\n\n  /** Data used for labels, only applicable for area charts: AreaChart, LineChart, BarChart */\n  series?: ChartSeries[];\n\n  /** Determines whether color swatch should be shown next to the label @default true */\n  showColor?: boolean;\n\n  /** Determines whether the legend should be centered @default false */\n  centered?: boolean;\n}\n\nexport type ChartLegendFactory = Factory<{\n  props: ChartLegendProps;\n  ref: HTMLDivElement;\n  stylesNames: ChartLegendStylesNames;\n}>;\n\nexport const ChartLegend = factory<ChartLegendFactory>((_props) => {\n  const props = useProps('ChartLegend', null, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    payload,\n    onHighlight,\n    legendPosition,\n    mod,\n    series,\n    showColor,\n    centered,\n    attributes,\n    ...others\n  } = props;\n\n  const getStyles = useStyles<ChartLegendFactory>({\n    name: 'ChartLegend',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    rootSelector: 'legend',\n  });\n\n  if (!payload) {\n    return null;\n  }\n\n  const filteredPayload = getFilteredChartLegendPayload(payload, series != null);\n  const labels = getSeriesLabels(series);\n\n  const items = filteredPayload.map((item, index) => (\n    <div\n      key={index}\n      {...getStyles('legendItem')}\n      onMouseEnter={() => onHighlight(item.highlightKey ?? item.dataKey)}\n      data-without-color={showColor === false || undefined}\n    >\n      <ColorSwatch\n        color={item.color}\n        size={12}\n        {...getStyles('legendItemColor')}\n        withShadow={false}\n      />\n      <p {...getStyles('legendItemName')}>{labels[item.dataKey] || item.dataKey}</p>\n    </div>\n  ));\n\n  return (\n    <Box\n      mod={[{ position: legendPosition, centered }, mod]}\n      {...getStyles('legend')}\n      {...others}\n      onMouseLeave={(event) => {\n        others.onMouseLeave?.(event);\n        onHighlight(null);\n      }}\n    >\n      {items}\n    </Box>\n  );\n});\n\nChartLegend.displayName = '@mantine/charts/ChartLegend';\nChartLegend.classes = classes;\n"],"mappings":";;;;;;AAeA,SAAS,yBACP,SACA,iBACuB;CACvB,OAAO,QAAQ,KAAK,SAAS;EAC3B,MAAM,aAAa,kBAAkB,KAAK,SAAS,MAAM,GAAG,CAAC,CAAC,IAAI,IAAI,KAAK;EAC3E,OAAO;GACL,GAAG;GACH,SAAS;GACT,SAAS;IACP,GAAG,KAAK;IACR,MAAM;IACN,SAAS;GACX;EACF;CACF,CAAC;AACH;AAEA,SAAgB,8BACd,SACA,kBAAkB,MAClB;CACA,OAAO,yBACL,QAAQ,QAAQ,SAAS,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