@@ -16,6 +16,12 @@ interface IColumnMeta {
1616 width ?: number | undefined ;
1717}
1818
19+ interface ComputedCSSVarValues {
20+ bodyFontFamily : string ;
21+ bodyFontSize : string ;
22+ headerFontFamily : string ;
23+ }
24+
1925const ROW_SAMPLE_SIZE = 20 ;
2026const MAX_WIDTH = 700 ;
2127export const CELL_PADDING_PX = 18 ; /* padding left and right 0.5rem each (16px) + borders (1px) + buffer (1px) */
@@ -46,7 +52,7 @@ function toPx(fontSize: string): number {
4652 return parseFloat ( fontSize ) || 16 ;
4753}
4854
49- function stringToPx ( dataPoint : string , isHeader : boolean = false ) : number {
55+ function stringToPx ( dataPoint : string , isHeader = false , computedCSSVarValues : ComputedCSSVarValues ) : number {
5056 if ( ! measurementCanvas ) {
5157 measurementCanvas = document . createElement ( 'canvas' ) ;
5258 measurementContext = measurementCanvas . getContext ( '2d' ) ;
@@ -55,38 +61,37 @@ function stringToPx(dataPoint: string, isHeader: boolean = false): number {
5561 return 14 * ( isHeader ? 0.55 : 0.5 ) * dataPoint . length ;
5662 }
5763
58- const bodyFontFamily = getComputedCSSVarValue ( '--sapFontFamily' , 'Arial, Helvetica, sans-serif' ) ;
59- const bodyFontSize = getComputedCSSVarValue ( '--sapFontSize' , '0.875rem' ) ;
60- const headerFontFamily = getComputedCSSVarValue ( '--_ui5wcr-AnalyticalTable-HeaderFontFamily' , bodyFontFamily ) ;
61-
64+ const { bodyFontFamily, bodyFontSize, headerFontFamily } = computedCSSVarValues ;
6265 const fontFamily = isHeader ? headerFontFamily : bodyFontFamily ;
6366 const fontSizePx = toPx ( bodyFontSize ) ;
6467
6568 measurementContext . font = `${ fontSizePx } px ${ fontFamily } ` ;
6669 return Math . ceil ( measurementContext . measureText ( dataPoint ) . width ) ;
6770}
6871
69- function getContentPxLongest ( rowSample : RowType [ ] , columnIdOrAccessor : string ) {
72+ function getContentPxLongest (
73+ rowSample : RowType [ ] ,
74+ columnIdOrAccessor : string ,
75+ computedCSSVarValues : ComputedCSSVarValues ,
76+ ) {
7077 return rowSample . reduce ( ( max , item ) => {
7178 const dataPoint = item . values ?. [ columnIdOrAccessor ] ;
72-
7379 if ( dataPoint ) {
74- const val = stringToPx ( dataPoint ) + CELL_PADDING_PX ;
80+ const val = stringToPx ( dataPoint , false , computedCSSVarValues ) + CELL_PADDING_PX ;
7581 return Math . max ( max , val ) ;
7682 }
77-
7883 return max ;
7984 } , 0 ) ;
8085}
8186
82- function getContentPxAvg ( rowSample , columnIdOrAccessor ) {
87+ function getContentPxAvg ( rowSample : RowType [ ] , columnIdOrAccessor : string , computedCSSVarValues : ComputedCSSVarValues ) {
8388 return (
8489 rowSample . reduce ( ( acc , item ) => {
8590 const dataPoint = item . values ?. [ columnIdOrAccessor ] ;
8691
8792 let val = 0 ;
8893 if ( dataPoint ) {
89- val = stringToPx ( dataPoint ) + CELL_PADDING_PX ;
94+ val = stringToPx ( dataPoint , false , computedCSSVarValues ) + CELL_PADDING_PX ;
9095 }
9196 return acc + val ;
9297 } , 0 ) / ( rowSample . length || 1 )
@@ -243,6 +248,7 @@ const calculateSmartAndGrowColumns = (
243248 columns : AnalyticalTableColumnDefinition [ ] ,
244249 instance : TableInstance ,
245250 hiddenColumns : ColumnType ,
251+ computedCSSVarValues : ComputedCSSVarValues ,
246252 isGrow = false ,
247253) => {
248254 const { rows, state } = instance ;
@@ -271,18 +277,24 @@ const calculateSmartAndGrowColumns = (
271277 let headerPx : number , contentPxLength : number ;
272278
273279 if ( column . scaleWidthModeOptions ?. cellString ) {
274- contentPxLength = stringToPx ( column . scaleWidthModeOptions . cellString ) + CELL_PADDING_PX ;
280+ contentPxLength =
281+ stringToPx ( column . scaleWidthModeOptions . cellString , false , computedCSSVarValues ) + CELL_PADDING_PX ;
275282 } else {
276283 contentPxLength = isGrow
277- ? getContentPxLongest ( rowSample , columnIdOrAccessor )
278- : getContentPxAvg ( rowSample , columnIdOrAccessor ) ;
284+ ? getContentPxLongest ( rowSample , columnIdOrAccessor , computedCSSVarValues )
285+ : getContentPxAvg ( rowSample , columnIdOrAccessor , computedCSSVarValues ) ;
279286 }
280287
281288 if ( column . scaleWidthModeOptions ?. headerString ) {
282- headerPx = Math . max ( stringToPx ( column . scaleWidthModeOptions . headerString , true ) + CELL_PADDING_PX , 60 ) ;
289+ headerPx = Math . max (
290+ stringToPx ( column . scaleWidthModeOptions . headerString , true , computedCSSVarValues ) + CELL_PADDING_PX ,
291+ 60 ,
292+ ) ;
283293 } else {
284294 headerPx =
285- typeof column . Header === 'string' ? Math . max ( stringToPx ( column . Header , true ) + CELL_PADDING_PX , 60 ) : 60 ;
295+ typeof column . Header === 'string'
296+ ? Math . max ( stringToPx ( column . Header , true , computedCSSVarValues ) + CELL_PADDING_PX , 60 )
297+ : 60 ;
286298 }
287299
288300 metadata [ columnIdOrAccessor ] = {
@@ -498,13 +510,6 @@ const columns = (columns: TableInstance['columns'], { instance }: { instance: Ta
498510 } )
499511 . filter ( Boolean ) as TableInstance [ 'columns' ] ;
500512
501- if ( scaleWidthMode === AnalyticalTableScaleWidthMode . Smart ) {
502- return calculateSmartAndGrowColumns ( columns , instance , hiddenColumns ) ;
503- }
504-
505- if ( scaleWidthMode === AnalyticalTableScaleWidthMode . Grow ) {
506- return calculateSmartAndGrowColumns ( columns , instance , hiddenColumns , true ) ;
507- }
508513 const hasData = instance . data . length > 0 ;
509514
510515 if ( scaleWidthMode === AnalyticalTableScaleWidthMode . Default || ( ! hasData && loading ) ) {
@@ -520,6 +525,23 @@ const columns = (columns: TableInstance['columns'], { instance }: { instance: Ta
520525 return { ...column , width : calculatedWidth } ;
521526 } ) ;
522527 }
528+
529+ const computedCSSVarValues = {
530+ bodyFontFamily : getComputedCSSVarValue ( '--sapFontFamily' , 'Arial, Helvetica, sans-serif' ) ,
531+ bodyFontSize : getComputedCSSVarValue ( '--sapFontSize' , '0.875rem' ) ,
532+ headerFontFamily : getComputedCSSVarValue (
533+ '--_ui5wcr-AnalyticalTable-HeaderFontFamily' ,
534+ getComputedCSSVarValue ( '--sapFontFamily' , 'Arial, Helvetica, sans-serif' ) ,
535+ ) ,
536+ } ;
537+
538+ if ( scaleWidthMode === AnalyticalTableScaleWidthMode . Smart ) {
539+ return calculateSmartAndGrowColumns ( columns , instance , hiddenColumns , computedCSSVarValues ) ;
540+ }
541+
542+ if ( scaleWidthMode === AnalyticalTableScaleWidthMode . Grow ) {
543+ return calculateSmartAndGrowColumns ( columns , instance , hiddenColumns , computedCSSVarValues , true ) ;
544+ }
523545} ;
524546
525547export const useDynamicColumnWidths = ( hooks : ReactTableHooks ) => {
0 commit comments