Skip to content

Commit f06d089

Browse files
jorgefilipecostat-hamanosaarnilauri
authored
Fix: Fit text may not update when being grouped in a smaller container. (WordPress#73021)
Co-authored-by: jorgefilipecosta <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: saarnilauri <[email protected]>
1 parent 59a4d5b commit f06d089

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
__experimentalToolsPanelItem as ToolsPanelItem,
1212
} from '@wordpress/components';
1313

14+
const EMPTY_OBJECT = {};
15+
1416
/**
1517
* Internal dependencies
1618
*/
@@ -62,14 +64,19 @@ function useFitText( { fitText, name, clientId } ) {
6264
const hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY );
6365
const blockElement = useBlockElement( clientId );
6466

65-
// Monitor block attribute changes
66-
// Any attribute may change the available space.
67-
const blockAttributes = useSelect(
67+
// Monitor block attribute changes, and parent changes.
68+
// Any attribute or parent change may change the available space.
69+
const { blockAttributes, parentId } = useSelect(
6870
( select ) => {
6971
if ( ! clientId || ! hasFitTextSupport || ! fitText ) {
70-
return;
72+
return EMPTY_OBJECT;
7173
}
72-
return select( blockEditorStore ).getBlockAttributes( clientId );
74+
return {
75+
blockAttributes:
76+
select( blockEditorStore ).getBlockAttributes( clientId ),
77+
parentId:
78+
select( blockEditorStore ).getBlockRootClientId( clientId ),
79+
};
7380
},
7481
[ clientId, hasFitTextSupport, fitText ]
7582
);
@@ -143,6 +150,7 @@ function useFitText( { fitText, name, clientId } ) {
143150
if ( window.ResizeObserver && currentElement.parentElement ) {
144151
resizeObserver = new window.ResizeObserver( applyFitText );
145152
resizeObserver.observe( currentElement.parentElement );
153+
resizeObserver.observe( currentElement );
146154
}
147155

148156
// Cleanup function
@@ -169,7 +177,14 @@ function useFitText( { fitText, name, clientId } ) {
169177
styleElement.remove();
170178
}
171179
};
172-
}, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] );
180+
}, [
181+
fitText,
182+
clientId,
183+
parentId,
184+
applyFitText,
185+
blockElement,
186+
hasFitTextSupport,
187+
] );
173188

174189
// Trigger fit text recalculation when content changes
175190
useEffect( () => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ store( 'core/fit-text', {
3838
context.fontSize = optimizeFitText( ref, applyFontSize );
3939
} );
4040
resizeObserver.observe( ref.parentElement );
41+
resizeObserver.observe( ref );
4142

4243
// Return cleanup function to be called when element is removed.
4344
return () => {

0 commit comments

Comments
 (0)