@@ -5,31 +5,40 @@ const FontSizer = ({
55 Component = "div" ,
66 maxHeight = 100 ,
77 maxWidth = 100 ,
8- maxFontSize = 500 ,
8+ maxFontSize = 50 ,
99 minFontSize = 10 ,
1010 cacheKey,
1111 ...rest
1212} ) => {
1313 const [ ref , setRef ] = useState ( )
14+ const cancelRef = useRef ( false )
1415
1516 useEffect ( ( ) => {
1617 if ( ! ref ) return
1718
18- requestAnimationFrame ( ( ) => {
19+ const timeoutId = setTimeout ( ( ) => {
20+ cancelRef . current = false
1921 let fontSize = maxFontSize
2022
2123 ref . style . animation = "font-size 02s"
2224 ref . style . fontSize = fontSize + "px"
2325
24- const widthScale = maxWidth / ref . offsetWidth
25- const heightScale = maxHeight / ref . offsetHeight
26-
27- const scaleFactor = Math . min ( widthScale , heightScale )
28- fontSize = Math . floor ( maxFontSize * scaleFactor ) || 1
29-
30- ref . style . fontSize = fontSize + "px"
26+ while (
27+ ! cancelRef . current &&
28+ fontSize > minFontSize &&
29+ ( ref . offsetWidth > maxWidth || ref . offsetHeight > maxHeight )
30+ ) {
31+ const delta = Math . ceil ( fontSize / 100 )
32+ fontSize = fontSize - delta
33+ ref . style . fontSize = fontSize + "px"
34+ }
3135 } )
32- } , [ children , maxHeight , maxWidth , ref , cacheKey ] )
36+
37+ return ( ) => {
38+ cancelRef . current = true
39+ clearTimeout ( timeoutId )
40+ }
41+ } , [ children , maxHeight , maxWidth , ref ] )
3342
3443 return (
3544 < Component truncate ref = { setRef } { ...rest } >
0 commit comments