@@ -4,17 +4,16 @@ import { forwardRef, MouseEvent, MutableRefObject } from 'react';
44
55import { FlexBox } from '../Box' ;
66import { Text } from '../Typography' ;
7- import { useBarChartContext } from './BarChartProvider' ;
8- import { BarProps } from './types' ;
9- import { calculateBarWidth } from './utils' ;
107import {
118 BackgroundBar ,
129 BarWrapper ,
1310 ForegroundBar ,
1411 minBarWidth ,
1512} from './Bar/elements' ;
13+ import { useBarChartContext } from './BarChartProvider' ;
14+ import { BarProps } from './types' ;
15+ import { calculateBarWidth , getValuesSummary } from './utils' ;
1616
17- // Polymorphic row wrapper styles
1817const rowBaseStyles = css ( {
1918 display : 'flex' ,
2019 alignItems : 'center' ,
@@ -54,31 +53,9 @@ const RowAnchor = styled('a', styledOptions<'a'>())(
5453) ;
5554
5655export interface BarRowProps extends BarProps {
57- /** Index for animation staggering */
5856 index ?: number ;
5957}
6058
61- /**
62- * Generates an accessible summary of the bar values
63- */
64- function getValuesSummary ( {
65- yLabel,
66- seriesOneValue,
67- seriesTwoValue,
68- unit,
69- } : {
70- yLabel : string ;
71- seriesOneValue : number ;
72- seriesTwoValue ?: number ;
73- unit : string ;
74- } ) : string {
75- if ( seriesTwoValue !== undefined ) {
76- const gained = seriesOneValue ;
77- return `${ gained } ${ unit } gained - now at ${ seriesTwoValue } ${ unit } in ${ yLabel } category` ;
78- }
79- return `${ seriesOneValue } ${ unit } in ${ yLabel } category` ;
80- }
81-
8259export const BarRow = forwardRef <
8360 HTMLDivElement | HTMLButtonElement | HTMLAnchorElement ,
8461 BarRowProps
@@ -100,7 +77,6 @@ export const BarRow = forwardRef<
10077 const isStacked = seriesTwoValue !== undefined ;
10178 const displayValue = isStacked ? seriesTwoValue : seriesOneValue ;
10279
103- // Calculate bar widths as percentages
10480 const backgroundBarWidth = calculateBarWidth ( {
10581 value : displayValue ,
10682 maxRange,
@@ -123,78 +99,64 @@ export const BarRow = forwardRef<
12399 unit,
124100 } ) ;
125101
126- // Animation delay for staggered bar entrance
127102 const animationDelay = animate ? index * 0.1 : 0 ;
128103
129104 const rowContent = (
130105 < >
131- { /* Y-axis label with optional icon */ }
132106 < FlexBox
133107 alignItems = "center"
134- gap = { 8 }
135- minWidth = { { _ : 80 , sm : 120 } }
108+ color = { styleConfig . textColor }
136109 flexShrink = { 0 }
110+ gap = { 8 }
111+ minWidth = "200px"
137112 >
138- { Icon && < Icon size = { 16 } color = { styleConfig . textColor } /> }
139- < Text
140- variant = "p-small"
141- color = { styleConfig . textColor }
142- truncate = "ellipsis"
143- truncateLines = { 1 }
144- >
113+ { Icon && < Icon size = { 16 } /> }
114+ < Text fontWeight = 'bold' truncate = "ellipsis" truncateLines = { 1 } >
145115 { yLabel }
146116 </ Text >
147117 </ FlexBox >
148118
149- { /* Bar container */ }
150- < FlexBox flex = { 1 } alignItems = "center" position = "relative" >
151- < BarWrapper >
152- { /* Background bar (total value in stacked, or single value in non-stacked) */ }
153- < BackgroundBar
154- bg = { styleConfig . backgroundBarColor }
119+ < BarWrapper >
120+ < BackgroundBar
121+ animate = { animate ? { width : bgWidthStr } : undefined }
122+ bg = { styleConfig . backgroundBarColor }
123+ data-testid = "background-bar"
124+ initial = { animate ? { width : '0%' } : undefined }
125+ transition = { { duration : 0.5 , delay : animationDelay } }
126+ width = { ! animate ? bgWidthStr : undefined }
127+ />
128+ { isStacked && (
129+ < ForegroundBar
130+ animate = { animate ? { width : fgWidthStr } : undefined }
131+ bg = { styleConfig . foregroundBarColor }
132+ data-testid = "foreground-bar"
155133 initial = { animate ? { width : '0%' } : undefined }
156- animate = { animate ? { width : bgWidthStr } : undefined }
157- style = { ! animate ? { width : bgWidthStr } : undefined }
158- transition = { { duration : 0.5 , delay : animationDelay } }
159- data-testid = "background-bar"
134+ transition = { { duration : 0.5 , delay : animationDelay + 0.25 } }
135+ width = { ! animate ? fgWidthStr : undefined }
160136 />
137+ ) }
138+ </ BarWrapper >
161139
162- { /* Foreground bar (progress value in stacked mode only) */ }
163- { isStacked && (
164- < ForegroundBar
165- bg = { styleConfig . foregroundBarColor }
166- initial = { animate ? { width : '0%' } : undefined }
167- animate = { animate ? { width : fgWidthStr } : undefined }
168- style = { ! animate ? { width : fgWidthStr } : undefined }
169- transition = { { duration : 0.5 , delay : animationDelay + 0.25 } }
170- data-testid = "foreground-bar"
171- />
172- ) }
173- </ BarWrapper >
174- </ FlexBox >
175-
176- { /* Value display */ }
177140 < FlexBox
178141 alignItems = "center"
142+ flexShrink = { 0 }
179143 justifyContent = "flex-end"
180144 minWidth = { { _ : 40 , sm : 60 } }
181- flexShrink = { 0 }
182145 >
183- < Text variant = "p-small" color = { styleConfig . textColor } >
146+ < Text color = { styleConfig . textColor } variant = "p-small" >
184147 { displayValue . toLocaleString ( ) }
185148 { unit && ` ${ unit } ` }
186149 </ Text >
187150 </ FlexBox >
188151 </ >
189152 ) ;
190153
191- // Polymorphic rendering based on interactivity
192154 if ( href ) {
193155 return (
194156 < li >
195157 < RowAnchor
196- href = { href }
197158 aria-label = { valuesSummary }
159+ href = { href }
198160 ref = { ref as MutableRefObject < HTMLAnchorElement > }
199161 >
200162 { rowContent }
@@ -207,15 +169,15 @@ export const BarRow = forwardRef<
207169 return (
208170 < li >
209171 < RowButton
172+ aria-label = { valuesSummary }
173+ ref = { ref as MutableRefObject < HTMLButtonElement > }
210174 type = "button"
211175 onClick = { onClick }
212176 onKeyDown = { ( e ) => {
213177 if ( e . key === 'Enter' ) {
214178 onClick ( e as unknown as MouseEvent < HTMLButtonElement > ) ;
215179 }
216180 } }
217- aria-label = { valuesSummary }
218- ref = { ref as MutableRefObject < HTMLButtonElement > }
219181 >
220182 { rowContent }
221183 </ RowButton >
@@ -235,5 +197,3 @@ export const BarRow = forwardRef<
235197 ) ;
236198 }
237199) ;
238-
239- BarRow . displayName = 'BarRow' ;
0 commit comments