{"version":3,"file":"BulletChart.mjs","names":["classes"],"sources":["../../src/BulletChart/BulletChart.tsx"],"sourcesContent":["import {\n  Box,\n  BoxProps,\n  createVarsResolver,\n  ElementProps,\n  factory,\n  Factory,\n  getThemeColor,\n  MantineColor,\n  rem,\n  StylesApiProps,\n  Tooltip,\n  useMantineTheme,\n  useProps,\n  useStyles,\n} from '@mantine/core';\nimport classes from './BulletChart.module.css';\n\nexport interface BulletChartRange {\n  /** Upper bound value for this range */\n  value: number;\n\n  /** Background color of the range band */\n  color: MantineColor;\n\n  /** Optional label for the range */\n  label?: string;\n}\n\nexport type BulletChartStylesNames =\n  | 'root'\n  | 'label'\n  | 'trackWrapper'\n  | 'track'\n  | 'range'\n  | 'bar'\n  | 'target'\n  | 'rangeLabel'\n  | 'barLabel'\n  | 'targetLabel';\n\nexport type BulletChartCssVariables = {\n  root: '--bullet-size' | '--bullet-bar-size';\n};\n\nexport interface BulletChartProps\n  extends BoxProps, StylesApiProps<BulletChartFactory>, ElementProps<'div'> {\n  /** Current actual value */\n  value: number;\n\n  /** Target value, displayed as a marker line */\n  target?: number;\n\n  /** Qualitative ranges displayed as background bands */\n  ranges: BulletChartRange[];\n\n  /** Orientation, @default 'horizontal' */\n  orientation?: 'horizontal' | 'vertical';\n\n  /** Label displayed next to the chart */\n  label?: React.ReactNode;\n\n  /** Value formatter for tooltip/display and labels */\n  valueFormatter?: (value: number) => string;\n\n  /** Color of the actual value bar, @default 'white' */\n  barColor?: MantineColor;\n\n  /** Color of the target marker, @default 'dark' for light, 'white' for dark */\n  targetColor?: MantineColor;\n\n  /** Height of the chart track area (ranges), @default 32 */\n  size?: number | string;\n\n  /** Height of the actual value bar, @default 16 */\n  barSize?: number | string;\n\n  /** Target marker size relative to chart size, @default 0.7 */\n  targetRatio?: number;\n\n  /** Target marker thickness, @default 2 */\n  targetSize?: number | string;\n\n  /** Whether to show tooltip on hover, @default false */\n  withTooltip?: boolean;\n\n  /** Custom tooltip content */\n  getTooltipLabel?: (data: { value: number; target?: number }) => React.ReactNode;\n}\n\nexport type BulletChartFactory = Factory<{\n  props: BulletChartProps;\n  ref: HTMLDivElement;\n  stylesNames: BulletChartStylesNames;\n  vars: BulletChartCssVariables;\n}>;\n\nconst defaultProps = {\n  orientation: 'horizontal',\n  size: 32,\n  barSize: 16,\n  targetRatio: 0.7,\n  targetSize: 2,\n  withTooltip: false,\n} satisfies Partial<BulletChartProps>;\n\nconst varsResolver = createVarsResolver<BulletChartFactory>((_theme, { size, barSize }) => ({\n  root: {\n    '--bullet-size': rem(size),\n    '--bullet-bar-size': rem(barSize),\n  },\n}));\n\nexport const BulletChart = factory<BulletChartFactory>((_props) => {\n  const props = useProps('BulletChart', defaultProps, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    attributes,\n    value,\n    target,\n    ranges,\n    orientation,\n    label,\n    valueFormatter,\n    barColor,\n    targetColor,\n    size,\n    barSize,\n    targetRatio,\n    targetSize,\n    withTooltip,\n    getTooltipLabel,\n    variant,\n    ...others\n  } = props;\n\n  const theme = useMantineTheme();\n\n  const getStyles = useStyles<BulletChartFactory>({\n    name: 'BulletChart',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    vars,\n    varsResolver,\n  });\n\n  const isVertical = orientation === 'vertical';\n  const maxRange = Math.max(...ranges.map((r) => r.value), 0);\n  const formatValue = (v: number) => (valueFormatter ? valueFormatter(v) : v.toString());\n\n  // Clamp positions/sizes to the track so out-of-range values and targets never overflow it\n  const toPercentage = (v: number) =>\n    maxRange > 0 ? Math.max(0, Math.min((v / maxRange) * 100, 100)) : 0;\n\n  const resolvedBarColor = barColor ? getThemeColor(barColor, theme) : 'var(--mantine-color-white)';\n\n  const resolvedTargetColor = targetColor ? getThemeColor(targetColor, theme) : undefined;\n\n  const sortedRanges = [...ranges].sort((a, b) => b.value - a.value);\n\n  const rangeBands = sortedRanges.map((range, index) => {\n    const percentage = toPercentage(range.value);\n    const sizeStyle = isVertical\n      ? { height: `${percentage}%`, bottom: 0 }\n      : { width: `${percentage}%` };\n\n    return (\n      <div\n        key={index}\n        {...getStyles('range', {\n          style: {\n            ...sizeStyle,\n            backgroundColor: getThemeColor(range.color, theme),\n          },\n        })}\n      />\n    );\n  });\n\n  const rangeLabels = sortedRanges.map((range, index) => {\n    const percentage = toPercentage(range.value);\n    const positionStyle = isVertical ? { bottom: `${percentage}%` } : { left: `${percentage}%` };\n\n    return (\n      <div key={index} {...getStyles('rangeLabel', { style: positionStyle })}>\n        {formatValue(range.value)}\n      </div>\n    );\n  });\n\n  const barPercentage = toPercentage(value);\n  const barSizeStyle = isVertical\n    ? { height: `${barPercentage}%`, bottom: 0 }\n    : { width: `${barPercentage}%` };\n\n  const barElement = (\n    <div\n      {...getStyles('bar', {\n        style: {\n          ...barSizeStyle,\n          backgroundColor: resolvedBarColor,\n        },\n      })}\n    />\n  );\n\n  const barLabelPosition = isVertical\n    ? { bottom: `${barPercentage}%` }\n    : { left: `${barPercentage}%` };\n\n  const barLabelElement = (\n    <div {...getStyles('barLabel', { style: barLabelPosition })}>{formatValue(value)}</div>\n  );\n\n  const targetPercentage = target != null ? toPercentage(target) : 0;\n\n  const targetElement =\n    target != null ? (\n      <div\n        {...getStyles('target', {\n          style: isVertical\n            ? {\n                bottom: `${targetPercentage}%`,\n                width: `${targetRatio! * 100}%`,\n                height: rem(targetSize),\n                backgroundColor: resolvedTargetColor,\n              }\n            : {\n                left: `${targetPercentage}%`,\n                height: `${targetRatio! * 100}%`,\n                width: rem(targetSize),\n                backgroundColor: resolvedTargetColor,\n              },\n        })}\n      />\n    ) : null;\n\n  const targetLabelElement =\n    target != null ? (\n      <div\n        {...getStyles('targetLabel', {\n          style: isVertical ? { bottom: `${targetPercentage}%` } : { left: `${targetPercentage}%` },\n        })}\n      >\n        {formatValue(target)}\n      </div>\n    ) : null;\n\n  const getDefaultTooltipLabel = () => {\n    const parts = [`Value: ${formatValue(value)}`];\n    if (target != null) {\n      parts.push(`Target: ${formatValue(target)}`);\n    }\n    return parts.join(' / ');\n  };\n\n  const tooltipLabel = getTooltipLabel\n    ? getTooltipLabel({ value, target })\n    : getDefaultTooltipLabel();\n\n  const trackWrapper = (\n    <div {...getStyles('trackWrapper')} data-orientation={orientation}>\n      {rangeLabels}\n      {targetLabelElement}\n      <div {...getStyles('track')} data-orientation={orientation}>\n        {rangeBands}\n        {barElement}\n        {targetElement}\n      </div>\n      {barLabelElement}\n    </div>\n  );\n\n  return (\n    <Box {...getStyles('root')} variant={variant} mod={{ orientation }} {...others}>\n      {label && <div {...getStyles('label')}>{label}</div>}\n      {withTooltip ? (\n        <Tooltip label={tooltipLabel} position={isVertical ? 'right' : 'top'}>\n          {trackWrapper}\n        </Tooltip>\n      ) : (\n        trackWrapper\n      )}\n    </Box>\n  );\n});\n\nBulletChart.displayName = '@mantine/charts/BulletChart';\nBulletChart.classes = classes;\nBulletChart.varsResolver = varsResolver;\n"],"mappings":";;;;;AAiGA,MAAM,eAAe;CACnB,aAAa;CACb,MAAM;CACN,SAAS;CACT,aAAa;CACb,YAAY;CACZ,aAAa;AACf;AAEA,MAAM,eAAe,oBAAwC,QAAQ,EAAE,MAAM,eAAe,EAC1F,MAAM;CACJ,iBAAiB,IAAI,IAAI;CACzB,qBAAqB,IAAI,OAAO;AAClC,EACF,EAAE;AAEF,MAAa,cAAc,SAA6B,WAAW;CACjE,MAAM,QAAQ,SAAS,eAAe,cAAc,MAAM;CAC1D,MAAM,EACJ,YACA,WACA,OACA,QACA,UACA,MACA,YACA,OACA,QACA,QACA,aACA,OACA,gBACA,UACA,aACA,MACA,SACA,aACA,YACA,aACA,iBACA,SACA,GAAG,WACD;CAEJ,MAAM,QAAQ,gBAAgB;CAE9B,MAAM,YAAY,UAA8B;EAC9C,MAAM;EACN,SAAA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAED,MAAM,aAAa,gBAAgB;CACnC,MAAM,WAAW,KAAK,IAAI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