Skip to content

Commit dc3ebdb

Browse files
jorgefilipecostat-hamanoiamtakashi
authored
Fix: Fit Text not working on calculated line heights. (WordPress#74860)
Co-authored-by: jorgefilipecosta <jorgefilipecosta@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: iamtakashi <iamtakashi@git.wordpress.org>
1 parent d3466f4 commit dc3ebdb

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/block-editor/src/utils/fit-text-utils.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function findOptimalFontSize( textElement, applyFontSize ) {
2222
const paddingRight = parseFloat( computedStyle.paddingRight ) || 0;
2323
const range = document.createRange();
2424
range.selectNodeContents( textElement );
25-
25+
let maxclientHeight = textElement.clientHeight;
2626
while ( minSize <= maxSize ) {
2727
const midSize = Math.floor( ( minSize + maxSize ) / 2 );
2828
applyFontSize( midSize );
@@ -41,7 +41,17 @@ function findOptimalFontSize( textElement, applyFontSize ) {
4141
// Check if text fits within the element's height.
4242
const fitsHeight =
4343
alreadyHasScrollableHeight ||
44-
textElement.scrollHeight <= textElement.clientHeight;
44+
textElement.scrollHeight <= textElement.clientHeight ||
45+
textElement.scrollHeight <= maxclientHeight;
46+
47+
// When there are calculated line heights, text may jump in height
48+
// the available space may decrease while the font size decreases,
49+
// making text not fit.
50+
// We store a maximum reference height: the maximum reference element height that was observed
51+
// during the loop to avoid issues with such jumps.
52+
if ( textElement.clientHeight > maxclientHeight ) {
53+
maxclientHeight = textElement.clientHeight;
54+
}
4555

4656
if ( fitsWidth && fitsHeight ) {
4757
bestSize = midSize;

0 commit comments

Comments
 (0)