A local-first personal finance app
fix dynamic font size calculation in formula reports (#6445)
Matt Fiddaman0b80e5491e81
diff --git a/packages/desktop-client/src/components/reports/FormulaResult.tsx b/packages/desktop-client/src/components/reports/FormulaResult.tsxindex 4166f3f43a..7b4c6dfdbb 100644--- a/packages/desktop-client/src/components/reports/FormulaResult.tsx+++ b/packages/desktop-client/src/components/reports/FormulaResult.tsx@@ -1,5 +1,6 @@import React, {type Ref,+ type RefObject,useCallback,useEffect,useMemo,@@ -31,6 +32,7 @@ type FormulaResultProps = {fontSizeMode?: 'dynamic' | 'static';staticFontSize?: number;customColor?: string | null;+ containerRef?: RefObject<HTMLDivElement | null>;};export function FormulaResult({@@ -43,6 +45,7 @@ export function FormulaResult({fontSizeMode = 'dynamic',staticFontSize = 32,customColor = null,+ containerRef,}: FormulaResultProps) {const [fontSize, setFontSize] = useState<number>(initialFontSize);const refDiv = useRef<HTMLDivElement>(null);@@ -62,9 +65,14 @@ export function FormulaResult({const calculateFontSize = useCallback(() => {if (!refDiv.current) return;- const { clientWidth, clientHeight } = refDiv.current;- const width = clientWidth; // no margin required on left and right- const height = clientHeight - CONTAINER_MARGIN * 2; // account for margin top and bottom+ const elementToMeasure =+ containerRef?.current || refDiv.current.parentElement || refDiv.current;+ const { clientWidth, clientHeight } = elementToMeasure;++ const width = clientWidth;+ const height = clientHeight - CONTAINER_MARGIN * 2;++ if (width <= 0 || height <= 0) return;// Get the actual display value length at calculation timeconst valueLength = displayValue.length || 1; // Avoid division by zero@@ -84,7 +92,7 @@ export function FormulaResult({previousFontSizeRef.current = calculatedFontSize;fontSizeChanged(calculatedFontSize);}- }, [displayValue, fontSizeChanged]);+ }, [displayValue, fontSizeChanged, containerRef]);// Debounce the calculation to avoid too many recalculationsconst debouncedCalculateFontSize = useRef(diff --git a/packages/desktop-client/src/components/reports/reports/FormulaCard.tsx b/packages/desktop-client/src/components/reports/reports/FormulaCard.tsxindex 5dfe584a96..38e8b10bd5 100644--- a/packages/desktop-client/src/components/reports/reports/FormulaCard.tsx+++ b/packages/desktop-client/src/components/reports/reports/FormulaCard.tsx@@ -1,4 +1,4 @@-import { useState, useMemo } from 'react';+import { useState, useMemo, useRef } from 'react';import { useTranslation } from 'react-i18next';import { View } from '@actual-app/components/view';@@ -29,6 +29,7 @@ export function FormulaCard({const { t } = useTranslation();const [nameMenuOpen, setNameMenuOpen] = useState(false);const themeColors = useThemeColors();+ const containerRef = useRef<HTMLDivElement>(null);const formula = meta?.formula || '=SUM(1, 2, 3)';const fontSize = meta?.fontSize;@@ -110,6 +111,7 @@ export function FormulaCard({/></View><View+ ref={containerRef}style={{justifyContent: 'center',alignItems: 'center',@@ -132,6 +134,7 @@ export function FormulaCard({staticFontSize={staticFontSize}customColor={customColor}animate={isEditing ?? false}+ containerRef={containerRef}/></View></View>diff --git a/upcoming-release-notes/6445.md b/upcoming-release-notes/6445.mdnew file mode 100644index 0000000000..7624dbd011--- /dev/null+++ b/upcoming-release-notes/6445.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [matt-fidd]+---++Fix dynamic font size calculation in formula reports