{"version":3,"sources":["../src/use-bar-chart.tsx"],"sourcesContent":["import type {\n  CSSUIObject,\n  CSSUIProps,\n  PropGetter,\n  RequiredPropGetter,\n} from \"@yamada-ui/core\"\nimport type { Dict } from \"@yamada-ui/utils\"\nimport type { ComponentPropsWithoutRef, FC, ReactNode } from \"react\"\nimport type * as Recharts from \"recharts\"\nimport type {\n  BarChartProps,\n  BarChartType,\n  BarProps,\n  ChartLayoutType,\n  ReferenceLineProps,\n} from \"./chart.types\"\nimport { getVar, useTheme } from \"@yamada-ui/core\"\nimport { cx, runIfFunc } from \"@yamada-ui/utils\"\nimport { useCallback, useId, useMemo, useState } from \"react\"\nimport { Bar, Cell } from \"recharts\"\nimport { getComponentProps } from \"./chart-utils\"\nimport { barChartProperties, barProperties } from \"./rechart-properties\"\n\nexport interface UseBarChartOptions {\n  /**\n   * Chart data.\n   */\n  data: Dict[]\n  /**\n   * An array of objects with `name` and `color` keys. Determines which data should be consumed from the `data` array.\n   */\n  series: BarProps[]\n  /**\n   * Controls how chart bars are positioned relative to each other.\n   *\n   * @default `default`\n   */\n  type?: BarChartType\n  /**\n   * A function that returns a component that renders the bar cells.\n   */\n  cell?: FC<BarCellProps> | ReactNode\n  /**\n   * Controls fill opacity of all bars.\n   *\n   * @default 1\n   */\n  fillOpacity?: [number, number] | number\n  /**\n   * Chart orientation.\n   *\n   * @default 'horizontal'\n   */\n  layoutType?: ChartLayoutType\n  /**\n   * If any two categorical charts have the same syncId,\n   * these two charts can sync the position tooltip, and the startIndex, endIndex of Brush.\n   */\n  syncId?: number | string\n  /**\n   * A label to display below the X axis.\n   */\n  xAxisLabel?: string\n  /**\n   * A label to display below the Y axis.\n   */\n  yAxisLabel?: string\n  /**\n   * Props for the bars.\n   */\n  barProps?: Partial<BarProps>\n  /**\n   * Props passed down to recharts `BarChart` component.\n   */\n  chartProps?: BarChartProps\n  /**\n   * Reference lines that should be displayed on the chart.\n   */\n  referenceLineProps?: ReferenceLineProps[]\n}\n\nexport interface UseBarChartProps extends UseBarChartOptions {\n  styles: Dict<CSSUIObject | undefined>\n}\n\nexport const useBarChart = ({\n  type = \"default\",\n  cell = defaultBarCell,\n  data,\n  fillOpacity = 1,\n  layoutType = \"horizontal\",\n  series,\n  styles,\n  syncId,\n  xAxisLabel,\n  yAxisLabel,\n  referenceLineProps = [],\n  ...rest\n}: UseBarChartProps) => {\n  const uuid = useId()\n  const { theme } = useTheme()\n  const [highlightedArea, setHighlightedArea] = useState<null | string>(null)\n  const stacked = type === \"stacked\" || type === \"percent\"\n  const shouldHighlight = highlightedArea !== null\n  const {\n    activeBar = {},\n    background = {},\n    dimBar,\n    ...computedBarProps\n  } = rest.barProps ?? {}\n\n  const barColors: CSSUIProps[\"vars\"] = useMemo(\n    () =>\n      series.map(({ color }, index) => ({\n        name: `bar-${index}`,\n        token: \"colors\",\n        value: color ?? \"transparent\",\n      })),\n    [series],\n  )\n\n  const referenceLineColors: CSSUIProps[\"vars\"] = useMemo(\n    () =>\n      referenceLineProps.map(({ color }, index) => ({\n        name: `reference-line-${index}`,\n        token: \"colors\",\n        value: color ?? \"transparent\",\n      })),\n    [referenceLineProps],\n  )\n\n  const barVars: CSSUIProps[\"vars\"] = useMemo(() => {\n    return [\n      ...barColors,\n      ...referenceLineColors,\n      { name: \"fill-opacity\", value: fillOpacity },\n    ]\n  }, [barColors, fillOpacity, referenceLineColors])\n\n  const [chartProps, barChartClassName] = useMemo(() => {\n    const resolvedBarChartProps = { barGap: 8, ...rest.chartProps }\n\n    return getComponentProps<Dict, string>(\n      [resolvedBarChartProps, barChartProperties],\n      styles.chart,\n    )(theme)\n  }, [rest.chartProps, styles.chart, theme])\n\n  const [barProps, barClassName] = useMemo(() => {\n    const resolvedBarProps = {\n      fillOpacity: \"$fill-opacity\",\n      strokeOpacity: 1,\n      ...computedBarProps,\n    }\n\n    return getComponentProps<Dict, string>(\n      [resolvedBarProps, barProperties],\n      styles.bar,\n    )(theme)\n  }, [computedBarProps, styles.bar, theme])\n\n  const [dimBarProps, dimBarClassName] = useMemo(() => {\n    const resolvedDimBar = { fillOpacity: 0.3, strokeOpacity: 0, ...dimBar }\n\n    return getComponentProps<Dict, string>([resolvedDimBar, barProperties])(\n      theme,\n    )\n  }, [dimBar, theme])\n\n  const [activeBarProps, activeBarClassName] = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [activeBar, barProperties],\n        styles.activeBar,\n      )(theme),\n    [activeBar, styles.activeBar, theme],\n  )\n\n  const [backgroundProps, backgroundClassName] = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [background, barProperties],\n        styles.background,\n      )(theme),\n    [background, styles.background, theme],\n  )\n\n  const barPropsList = useMemo(\n    () =>\n      series.map((props, index) => {\n        const {\n          activeBar = {},\n          background = {},\n          dataKey,\n          dimBar = {},\n          ...computedProps\n        } = props\n        const id = `${uuid}-${dataKey}`\n        const color = getVar(`bar-${index}`)(theme)\n        const dimmed = shouldHighlight && highlightedArea !== dataKey\n        const computedDimBar = { ...dimBarProps, ...dimBar }\n        const resolvedProps = {\n          ...barProps,\n          ...computedProps,\n          ...(dimmed ? computedDimBar : {}),\n        }\n\n        const rest = getComponentProps<Dict, string>(\n          [resolvedProps, barProperties],\n          barClassName,\n          dimmed ? dimBarClassName : undefined,\n        )(theme, true)\n\n        const computedActiveBar = { ...activeBarProps, ...activeBar }\n\n        const resolvedActiveBar = getComponentProps<Dict, string>(\n          [computedActiveBar, barProperties],\n          activeBarClassName,\n        )(theme, true)\n\n        const computedBackground = { ...backgroundProps, ...background }\n\n        const resolvedBackground = getComponentProps<Dict, string>(\n          [computedBackground, barProperties],\n          backgroundClassName,\n        )(theme, true)\n\n        return {\n          ...rest,\n          id,\n          activeBar: resolvedActiveBar,\n          background: resolvedBackground,\n          color,\n          dataKey,\n        }\n      }),\n    [\n      activeBarClassName,\n      activeBarProps,\n      backgroundClassName,\n      backgroundProps,\n      barClassName,\n      barProps,\n      dimBarClassName,\n      dimBarProps,\n      highlightedArea,\n      series,\n      shouldHighlight,\n      theme,\n      uuid,\n    ],\n  )\n\n  const getBarProps: RequiredPropGetter<\n    { index: number } & Partial<Recharts.BarProps>,\n    Omit<Recharts.BarProps, \"ref\">\n  > = useCallback(\n    ({ className: classNameProp, index, ...props }, ref = null) => {\n      const {\n        id,\n        className,\n        activeBar,\n        background,\n        color,\n        dataKey = \"\",\n        ...rest\n      } = barPropsList[index] ?? {}\n\n      return {\n        id,\n        ref,\n        name: dataKey,\n        className: cx(classNameProp, className),\n        activeBar,\n        background,\n        dataKey,\n        fill: color,\n        isAnimationActive: false,\n        stackId: stacked ? \"stack\" : undefined,\n        stroke: color,\n        ...(props as Omit<Recharts.BarProps, \"dataKey\">),\n        ...rest,\n      } as Recharts.BarProps\n    },\n    [barPropsList, stacked],\n  )\n\n  const bars = useMemo(() => {\n    const hasStack = series.some((entry) => entry.stackId)\n\n    return series.map((series, index) => {\n      const { dataKey } = series\n\n      return (\n        <Bar\n          key={`bar-${dataKey}`}\n          {...getBarProps({ className: \"ui-bar-chart__bar\", index })}\n        >\n          {data.map((data, index) =>\n            runIfFunc(cell, { data, hasStack, index, series }),\n          )}\n        </Bar>\n      )\n    })\n  }, [series, getBarProps, cell, data])\n\n  const getBarChartProps: PropGetter<\n    ComponentPropsWithoutRef<typeof Recharts.BarChart>,\n    ComponentPropsWithoutRef<typeof Recharts.BarChart>\n  > = useCallback(\n    ({ className, ...props } = {}, ref = null) => ({\n      ref,\n      className: cx(className, barChartClassName),\n      data,\n      layout: layoutType,\n      margin: {\n        bottom: xAxisLabel ? 30 : undefined,\n        left: yAxisLabel ? 10 : undefined,\n        right: yAxisLabel ? 5 : undefined,\n      },\n      stackOffset: type === \"percent\" ? \"expand\" : undefined,\n      syncId,\n      ...props,\n      ...chartProps,\n    }),\n    [\n      barChartClassName,\n      data,\n      type,\n      layoutType,\n      syncId,\n      xAxisLabel,\n      yAxisLabel,\n      chartProps,\n    ],\n  )\n\n  return {\n    bars,\n    barVars,\n    setHighlightedArea,\n    getBarChartProps,\n    getBarProps,\n  }\n}\n\nexport type UseBarChartReturn = ReturnType<typeof useBarChart>\n\nexport interface BarCellProps {\n  data: Dict\n  hasStack: boolean\n  index: number\n  series: BarProps\n}\n\nconst defaultBarCell: FC<BarCellProps> = ({\n  data,\n  hasStack,\n  index,\n  series,\n}) => {\n  const { dataKey } = series\n  const key = `cell-${dataKey}-${index}`\n\n  if (!hasStack) return <Cell key={key} />\n\n  const keys = Object.keys(data)\n  const values = Object.values(data)\n\n  const currentIndex = keys.findIndex((key) => key === dataKey)\n  const lastIndex = values.findLastIndex((value) => value !== 0)\n\n  if (currentIndex === lastIndex) return <Cell key={key} />\n\n  return <Cell key={key} radius={0} />\n}\n"],"mappings":";;;;;;;;;;AAgBA,SAAS,QAAQ,gBAAgB;AACjC,SAAS,IAAI,iBAAiB;AAC9B,SAAS,aAAa,OAAO,SAAS,gBAAgB;AACtD,SAAS,KAAK,YAAY;AAmRlB;AAjND,IAAM,cAAc,CAAC;AAAA,EAC1B,OAAO;AAAA,EACP,OAAO;AAAA,EACP;AAAA,EACA,cAAc;AAAA,EACd,aAAa;AAAA,EACb;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,qBAAqB,CAAC;AAAA,EACtB,GAAG;AACL,MAAwB;AAlGxB;AAmGE,QAAM,OAAO,MAAM;AACnB,QAAM,EAAE,MAAM,IAAI,SAAS;AAC3B,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SAAwB,IAAI;AAC1E,QAAM,UAAU,SAAS,aAAa,SAAS;AAC/C,QAAM,kBAAkB,oBAAoB;AAC5C,QAAM;AAAA,IACJ,YAAY,CAAC;AAAA,IACb,aAAa,CAAC;AAAA,IACd;AAAA,IACA,GAAG;AAAA,EACL,KAAI,UAAK,aAAL,YAAiB,CAAC;AAEtB,QAAM,YAAgC;AAAA,IACpC,MACE,OAAO,IAAI,CAAC,EAAE,MAAM,GAAG,WAAW;AAAA,MAChC,MAAM,OAAO,KAAK;AAAA,MAClB,OAAO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