{"version":3,"file":"Sparkline.mjs","names":["ReChartsAreaChart","classes"],"sources":["../../src/Sparkline/Sparkline.tsx"],"sourcesContent":["import { useId, useMemo } from 'react';\nimport { Area, AreaProps, AreaChart as ReChartsAreaChart, ResponsiveContainer } from 'recharts';\nimport {\n  Box,\n  BoxProps,\n  createVarsResolver,\n  ElementProps,\n  factory,\n  Factory,\n  getThemeColor,\n  MantineColor,\n  StylesApiProps,\n  useProps,\n  useStyles,\n} from '@mantine/core';\nimport { AreaChartCurveType, AreaGradient } from '../AreaChart';\nimport classes from './Sparkline.module.css';\n\nexport interface SparklineTrendColors {\n  positive: MantineColor;\n  negative: MantineColor;\n  neutral?: MantineColor;\n}\n\nexport type SparklineStylesNames = 'root';\nexport type SparklineCssVariables = {\n  root: '--chart-color';\n};\n\nexport interface SparklineProps\n  extends BoxProps, StylesApiProps<SparklineFactory>, ElementProps<'div'> {\n  /** Data used to render the chart */\n  data: (number | null)[];\n\n  /** Key of `theme.colors` or any valid CSS color @default theme.primaryColor */\n  color?: MantineColor;\n\n  /** Determines whether the chart fill should be a gradient @default true */\n  withGradient?: boolean;\n\n  /** Controls fill opacity of the area @default 0.6 */\n  fillOpacity?: number;\n\n  /** Type of the curve @default 'linear' */\n  curveType?: AreaChartCurveType;\n\n  /** Area stroke width @default 2 */\n  strokeWidth?: number;\n\n  /** If set, `color` prop is ignored and chart color is determined by the difference between first and last value. */\n  trendColors?: SparklineTrendColors;\n\n  /** Determines whether null values should be connected with other values @default true */\n  connectNulls?: boolean;\n\n  /** Props passed down to the underlying recharts `Area` component */\n  areaProps?: Omit<AreaProps<any, any>, 'data' | 'dataKey' | 'ref'>;\n}\n\nexport type SparklineFactory = Factory<{\n  props: SparklineProps;\n  ref: HTMLDivElement;\n  stylesNames: SparklineStylesNames;\n  vars: SparklineCssVariables;\n}>;\n\nconst defaultProps = {\n  withGradient: true,\n  connectNulls: true,\n  fillOpacity: 0.6,\n  strokeWidth: 2,\n  curveType: 'linear',\n} satisfies Partial<SparklineProps>;\n\nfunction getTrendColor(data: (number | null)[], trendColors: SparklineTrendColors) {\n  const first = data[0];\n  const last = data[data.length - 1];\n\n  if (first === null || last === null) {\n    return trendColors.neutral || trendColors.positive;\n  }\n\n  if (first < last) {\n    return trendColors.positive;\n  }\n\n  if (first > last) {\n    return trendColors.negative;\n  }\n\n  return trendColors.neutral || trendColors.positive;\n}\n\nconst varsResolver = createVarsResolver<SparklineFactory>(\n  (theme, { color, data, trendColors }) => ({\n    root: {\n      '--chart-color': trendColors\n        ? getThemeColor(getTrendColor(data, trendColors), theme)\n        : color\n          ? getThemeColor(color, theme)\n          : undefined,\n    },\n  })\n);\n\nexport const Sparkline = factory<SparklineFactory>((_props) => {\n  const props = useProps('Sparkline', defaultProps, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    data,\n    withGradient,\n    fillOpacity,\n    curveType,\n    strokeWidth,\n    trendColors,\n    connectNulls,\n    areaProps,\n    attributes,\n    ...others\n  } = props;\n\n  const getStyles = useStyles<SparklineFactory>({\n    name: 'Sparkline',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    vars,\n    varsResolver,\n  });\n\n  const id = useId();\n  const mappedData = useMemo(() => data.map((value, index) => ({ value, index })), [data]);\n\n  return (\n    <Box {...getStyles('root')} {...others} dir=\"ltr\">\n      <ResponsiveContainer>\n        <ReChartsAreaChart data={mappedData}>\n          <Area\n            dataKey=\"value\"\n            type={curveType}\n            fill={`url(#${id})`}\n            stroke=\"var(--chart-color, var(--mantine-color-blue-filled))\"\n            isAnimationActive={false}\n            connectNulls={connectNulls}\n            strokeWidth={strokeWidth}\n            fillOpacity={1}\n            activeDot={false}\n            {...areaProps}\n          />\n\n          <defs>\n            <AreaGradient\n              id={id}\n              color=\"var(--chart-color, var(--mantine-color-blue-filled))\"\n              fillOpacity={fillOpacity}\n              withGradient={withGradient}\n            />\n          </defs>\n        </ReChartsAreaChart>\n      </ResponsiveContainer>\n    </Box>\n  );\n});\n\nSparkline.displayName = '@mantine/charts/Sparkline';\nSparkline.classes = classes;\nSparkline.varsResolver = varsResolver;\n\nexport namespace Sparkline {\n  export type Props = SparklineProps;\n  export type StylesNames = SparklineStylesNames;\n  export type CssVariables = SparklineCssVariables;\n  export type Factory = SparklineFactory;\n}\n"],"mappings":";;;;;;;;AAkEA,MAAM,eAAe;CACnB,cAAc;CACd,cAAc;CACd,aAAa;CACb,aAAa;CACb,WAAW;CACZ;AAED,SAAS,cAAc,MAAyB,aAAmC;CACjF,MAAM,QAAQ,KAAK;CACnB,MAAM,OAAO,KAAK,KAAK,SAAS;AAEhC,KAAI,UAAU,QAAQ,SAAS,KAC7B,QAAO,YAAY,WAAW,YAAY;AAG5C,KAAI,QAAQ,KACV,QAAO,YAAY;AAGrB,KAAI,QAAQ,KACV,QAAO,YAAY;AAGrB,QAAO,YAAY,WAAW,YAAY;;AAG5C,MAAM,eAAe,oBAClB,OAAO,EAAE,OAAO,MAAM,mBAAmB,EACxC,MAAM,EACJ,iBAAiB,cACb,cAAc,cAAc,MAAM,YAAY,EAAE,MAAM,GACtD,QACE,cAAc,OAAO,MAAM,GAC3B,KAAA,GACP,EACF,EACF;AAED,MAAa,YAAY,SAA2B,WAAW;CAC7D,MAAM,QAAQ,SAAS,aAAa,cAAc,OAAO;CACzD,MAAM,EACJ,YACA,WACA,OACA,QACA,UACA,MACA,MACA,cACA,aACA,WACA,aACA,aACA,cACA,WACA,YACA,GAAG,WACD;CAEJ,MAAM,YAAY,UAA4B;EAC5C,MAAM;EACN,SAAA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;CAEF,MAAM,KAAK,OAAO;CAClB,MAAM,aAAa,cAAc,KAAK,KAAK,OAAO,WAAW;EAAE;EAAO;EAAO,EAAE,EAAE,CAAC,KAAK,CAAC;AAExF,QACE,oBAAC,KAAD;EAAK,GAAI,UAAU,OAAO;EAAE,GAAI;EAAQ,KAAI;YAC1C,oBAAC,qBAAD,EAAA,UACE,qBAACA,WAAD;GAAmB,MAAM;aAAzB,CACE,oBAAC,MAAD;IACE,SAAQ;IACR,MAAM;IACN,MAAM,QAAQ,GAAG;IACjB,QAAO;IACP,mBAAmB;IACL;IACD;IACb,aAAa;IACb,WAAW;IACX,GAAI;IACJ,CAAA,EAEF,oBAAC,QAAD,EAAA,UACE,oBAAC,cAAD;IACM;IACJ,OAAM;IACO;IACC;IACd,CAAA,EACG,CAAA,CACW;MACA,CAAA;EAClB,CAAA;EAER;AAEF,UAAU,cAAc;AACxB,UAAU,UAAUC;AACpB,UAAU,eAAe"}