From 7ed3ae0ca2201a3760d57ca91cdb836333cbc8e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lembit=20L=C3=B5pp?= Date: Thu, 24 Oct 2024 14:58:05 +0300 Subject: [PATCH] feat(bar): Add getColor to props provided to custom layers (#2651) * Add colors and colorBy to common props to enable access to colors in a custom layer * Pass getColor instead * Fix type --- packages/bar/src/Bar.tsx | 3 +++ packages/bar/src/BarCanvas.tsx | 3 +++ packages/bar/src/types.ts | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/bar/src/Bar.tsx b/packages/bar/src/Bar.tsx index 797b559e4c..0513bd8bcb 100644 --- a/packages/bar/src/Bar.tsx +++ b/packages/bar/src/Bar.tsx @@ -133,6 +133,7 @@ const InnerBar = ({ toggleSerie, legendsWithData, barTotals, + getColor, } = useBar({ indexBy, label, @@ -409,6 +410,7 @@ const InnerBar = ({ onClick, onMouseEnter, onMouseLeave, + getColor, }), [ commonProps, @@ -427,6 +429,7 @@ const InnerBar = ({ onClick, onMouseEnter, onMouseLeave, + getColor, ] ) diff --git a/packages/bar/src/BarCanvas.tsx b/packages/bar/src/BarCanvas.tsx index 27459641e9..38e93a6644 100644 --- a/packages/bar/src/BarCanvas.tsx +++ b/packages/bar/src/BarCanvas.tsx @@ -218,6 +218,7 @@ const InnerBarCanvas = ({ shouldRenderBarLabel, legendsWithData, barTotals, + getColor, } = useBar({ indexBy, label, @@ -292,6 +293,7 @@ const InnerBarCanvas = ({ onClick, onMouseEnter, onMouseLeave, + getColor, }), [ borderRadius, @@ -314,6 +316,7 @@ const InnerBarCanvas = ({ onClick, onMouseEnter, onMouseLeave, + getColor, ] ) diff --git a/packages/bar/src/types.ts b/packages/bar/src/types.ts index 22a4ab5756..c5bfcefc4c 100644 --- a/packages/bar/src/types.ts +++ b/packages/bar/src/types.ts @@ -12,7 +12,7 @@ import { Theme, ValueFormat, } from '@nivo/core' -import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors' +import { InheritedColorConfig, OrdinalColorScale, OrdinalColorScaleConfig } from '@nivo/colors' import { LegendProps } from '@nivo/legends' import { AnyScale, ScaleSpec, ScaleBandSpec } from '@nivo/scales' import { SpringValues } from '@react-spring/web' @@ -124,6 +124,7 @@ interface BarCustomLayerBaseProps xScale: AnyScale yScale: AnyScale + getColor: OrdinalColorScale> } export interface BarCustomLayerProps