Skip to content

Commit c171c53

Browse files
committed
small adjustments & cleanup
1 parent 8bdb50f commit c171c53

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

packages/main/src/components/AnalyticalTable/hooks/useDynamicColumnWidths.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ interface IColumnMeta {
1616
width?: number | undefined;
1717
}
1818

19-
interface ComputedCSSVarValues {
19+
interface ComputedCSSValues {
2020
bodyFontFamily: string;
2121
bodyFontSize: string;
2222
headerFontFamily: string;
23+
rootFontSize: number;
2324
}
2425

26+
const FALLBACK_FONT_SIZE = 14;
2527
const ROW_SAMPLE_SIZE = 20;
2628
const MAX_WIDTH = 700;
2729
export const CELL_PADDING_PX = 18; /* padding left and right 0.5rem each (16px) + borders (1px) + buffer (1px) */
@@ -40,58 +42,57 @@ function getComputedCSSVarValue(variableName: string, fallback: string): string
4042
/**
4143
* Convert fontSize string to number. Only handles `rem` and `px` values as `em` is not by design.
4244
*/
43-
function toPx(fontSize: string): number {
45+
function toPx(fontSize: string, rootFontSize: number): number {
4446
if (fontSize.endsWith('rem')) {
4547
const rem = parseFloat(fontSize);
46-
const rootFont = parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
47-
return rem * rootFont;
48+
return rem * rootFontSize;
4849
}
4950
if (fontSize.endsWith('px')) {
5051
return parseFloat(fontSize);
5152
}
52-
return parseFloat(fontSize) || 16;
53+
54+
if (fontSize.endsWith('em')) {
55+
return FALLBACK_FONT_SIZE;
56+
}
57+
return parseFloat(fontSize) || FALLBACK_FONT_SIZE;
5358
}
5459

55-
function stringToPx(dataPoint: string, isHeader = false, computedCSSVarValues: ComputedCSSVarValues): number {
60+
function stringToPx(dataPoint: string, isHeader = false, computedCSSValues: ComputedCSSValues): number {
5661
if (!measurementCanvas) {
5762
measurementCanvas = document.createElement('canvas');
5863
measurementContext = measurementCanvas.getContext('2d');
5964
}
6065
if (!measurementContext) {
61-
return 14 * (isHeader ? 0.55 : 0.5) * dataPoint.length;
66+
return FALLBACK_FONT_SIZE * (isHeader ? 0.55 : 0.5) * dataPoint.length;
6267
}
6368

64-
const { bodyFontFamily, bodyFontSize, headerFontFamily } = computedCSSVarValues;
69+
const { bodyFontFamily, bodyFontSize, headerFontFamily, rootFontSize } = computedCSSValues;
6570
const fontFamily = isHeader ? headerFontFamily : bodyFontFamily;
66-
const fontSizePx = toPx(bodyFontSize);
71+
const fontSizePx = toPx(bodyFontSize, rootFontSize);
6772

6873
measurementContext.font = `${fontSizePx}px ${fontFamily}`;
6974
return Math.ceil(measurementContext.measureText(dataPoint).width);
7075
}
7176

72-
function getContentPxLongest(
73-
rowSample: RowType[],
74-
columnIdOrAccessor: string,
75-
computedCSSVarValues: ComputedCSSVarValues,
76-
) {
77+
function getContentPxLongest(rowSample: RowType[], columnIdOrAccessor: string, computedCSSValues: ComputedCSSValues) {
7778
return rowSample.reduce((max, item) => {
7879
const dataPoint = item.values?.[columnIdOrAccessor];
7980
if (dataPoint) {
80-
const val = stringToPx(dataPoint, false, computedCSSVarValues) + CELL_PADDING_PX;
81+
const val = stringToPx(dataPoint, false, computedCSSValues) + CELL_PADDING_PX;
8182
return Math.max(max, val);
8283
}
8384
return max;
8485
}, 0);
8586
}
8687

87-
function getContentPxAvg(rowSample: RowType[], columnIdOrAccessor: string, computedCSSVarValues: ComputedCSSVarValues) {
88+
function getContentPxAvg(rowSample: RowType[], columnIdOrAccessor: string, computedCSSValues: ComputedCSSValues) {
8889
return (
8990
rowSample.reduce((acc, item) => {
9091
const dataPoint = item.values?.[columnIdOrAccessor];
9192

9293
let val = 0;
9394
if (dataPoint) {
94-
val = stringToPx(dataPoint, false, computedCSSVarValues) + CELL_PADDING_PX;
95+
val = stringToPx(dataPoint, false, computedCSSValues) + CELL_PADDING_PX;
9596
}
9697
return acc + val;
9798
}, 0) / (rowSample.length || 1)
@@ -248,7 +249,7 @@ const calculateSmartAndGrowColumns = (
248249
columns: AnalyticalTableColumnDefinition[],
249250
instance: TableInstance,
250251
hiddenColumns: ColumnType,
251-
computedCSSVarValues: ComputedCSSVarValues,
252+
computedCSSValues: ComputedCSSValues,
252253
isGrow = false,
253254
) => {
254255
const { rows, state } = instance;
@@ -278,22 +279,22 @@ const calculateSmartAndGrowColumns = (
278279

279280
if (column.scaleWidthModeOptions?.cellString) {
280281
contentPxLength =
281-
stringToPx(column.scaleWidthModeOptions.cellString, false, computedCSSVarValues) + CELL_PADDING_PX;
282+
stringToPx(column.scaleWidthModeOptions.cellString, false, computedCSSValues) + CELL_PADDING_PX;
282283
} else {
283284
contentPxLength = isGrow
284-
? getContentPxLongest(rowSample, columnIdOrAccessor, computedCSSVarValues)
285-
: getContentPxAvg(rowSample, columnIdOrAccessor, computedCSSVarValues);
285+
? getContentPxLongest(rowSample, columnIdOrAccessor, computedCSSValues)
286+
: getContentPxAvg(rowSample, columnIdOrAccessor, computedCSSValues);
286287
}
287288

288289
if (column.scaleWidthModeOptions?.headerString) {
289290
headerPx = Math.max(
290-
stringToPx(column.scaleWidthModeOptions.headerString, true, computedCSSVarValues) + CELL_PADDING_PX,
291+
stringToPx(column.scaleWidthModeOptions.headerString, true, computedCSSValues) + CELL_PADDING_PX,
291292
60,
292293
);
293294
} else {
294295
headerPx =
295296
typeof column.Header === 'string'
296-
? Math.max(stringToPx(column.Header, true, computedCSSVarValues) + CELL_PADDING_PX, 60)
297+
? Math.max(stringToPx(column.Header, true, computedCSSValues) + CELL_PADDING_PX, 60)
297298
: 60;
298299
}
299300

@@ -529,21 +530,22 @@ const columns = (columns: TableInstance['columns'], { instance }: { instance: Ta
529530
});
530531
}
531532

532-
const computedCSSVarValues = {
533+
const computedCSSValues = {
533534
bodyFontFamily: getComputedCSSVarValue('--sapFontFamily', 'Arial, Helvetica, sans-serif'),
534535
bodyFontSize: getComputedCSSVarValue('--sapFontSize', '0.875rem'),
535536
headerFontFamily: getComputedCSSVarValue(
536537
'--_ui5wcr-AnalyticalTable-HeaderFontFamily',
537538
getComputedCSSVarValue('--sapFontFamily', 'Arial, Helvetica, sans-serif'),
538539
),
540+
rootFontSize: parseFloat(getComputedStyle(document.documentElement).fontSize) || FALLBACK_FONT_SIZE,
539541
};
540542

541543
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Smart) {
542-
return calculateSmartAndGrowColumns(columns, instance, hiddenColumns, computedCSSVarValues);
544+
return calculateSmartAndGrowColumns(columns, instance, hiddenColumns, computedCSSValues);
543545
}
544546

545547
if (scaleWidthMode === AnalyticalTableScaleWidthMode.Grow) {
546-
return calculateSmartAndGrowColumns(columns, instance, hiddenColumns, computedCSSVarValues, true);
548+
return calculateSmartAndGrowColumns(columns, instance, hiddenColumns, computedCSSValues, true);
547549
}
548550
};
549551

packages/main/src/components/AnalyticalTable/hooks/useFontsReady.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { useState, useEffect } from 'react';
1010
export function useFontsReady(): boolean {
1111
const [fontsReady, setFontsReady] = useState(document.fonts?.status === 'loaded');
1212
useEffect(() => {
13-
if (!document.fonts) return;
13+
if (!document.fonts) {
14+
return;
15+
}
1416

1517
if (document.fonts.status === 'loading') {
1618
let mounted = true;

0 commit comments

Comments
 (0)