From a33e38d561b2c71adad609120678da677e32a2ff Mon Sep 17 00:00:00 2001 From: Dmitry <98899785+mdqst@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:47:01 +0300 Subject: [PATCH] Update useDynamicFontSizing.ts These changes make the code easier to read and slightly more efficient. --- packages/ui/src/hooks/useDynamicFontSizing.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/ui/src/hooks/useDynamicFontSizing.ts b/packages/ui/src/hooks/useDynamicFontSizing.ts index 03606d60eef..51458fc837c 100644 --- a/packages/ui/src/hooks/useDynamicFontSizing.ts +++ b/packages/ui/src/hooks/useDynamicFontSizing.ts @@ -11,24 +11,22 @@ export function useDynamicFontSizing( onSetFontSize: (amount: string) => void } { const [fontSize, setFontSize] = useState(maxFontSize) - const textInputElementWidthRef = useRef(0) + const textInputElementWidthRef = useRef(null) const onLayout = useCallback((event: LayoutChangeEvent) => { - if (textInputElementWidthRef.current) { - return - } - const width = event.nativeEvent.layout.width - textInputElementWidthRef.current = width + if (textInputElementWidthRef.current !== width) { + textInputElementWidthRef.current = width + } }, []) const onSetFontSize = useCallback( (amount: string) => { + if (!textInputElementWidthRef.current) return + const stringWidth = getStringWidth(amount, maxCharWidthAtMaxFontSize, fontSize, maxFontSize) const scaledSize = fontSize * (textInputElementWidthRef.current / stringWidth) - // If scaledSize = 0 then onLayout hasn't triggered yet and we should default to the largest size - const scaledSizeWithMin = scaledSize ? Math.max(scaledSize, minFontSize) : maxFontSize - const newFontSize = Math.round(Math.min(maxFontSize, scaledSizeWithMin)) + const newFontSize = Math.max(minFontSize, Math.min(maxFontSize, Math.round(scaledSize))) setFontSize(newFontSize) }, [fontSize, maxFontSize, minFontSize, maxCharWidthAtMaxFontSize], @@ -43,6 +41,5 @@ const getStringWidth = ( currentFontSize: number, maxFontSize: number, ): number => { - const widthAtMaxFontSize = value.length * maxCharWidthAtMaxFontSize - return widthAtMaxFontSize * (currentFontSize / maxFontSize) + return (value.length * maxCharWidthAtMaxFontSize * currentFontSize) / maxFontSize }