Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions packages/block-editor/src/hooks/fit-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,16 @@ function useFitText( { fitText, name, clientId } ) {
* @param {Function} props.setAttributes Function to set block attributes.
* @param {string} props.name Block name.
* @param {boolean} props.fitText Whether fit text is enabled.
* @param {string} props.fontSize Font size slug.
* @param {Object} props.style Block style object.
*/
export function FitTextControl( {
clientId,
fitText = false,
setAttributes,
name,
fontSize,
style,
} ) {
if ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) {
return null;
Expand All @@ -203,9 +207,28 @@ export function FitTextControl( {
__nextHasNoMarginBottom
label={ __( 'Fit text' ) }
checked={ fitText }
onChange={ () =>
setAttributes( { fitText: ! fitText || undefined } )
}
onChange={ () => {
const newFitText = ! fitText || undefined;
const updates = { fitText: newFitText };

// When enabling fit text, clear font size if it has a value
if ( newFitText ) {
if ( fontSize ) {
updates.fontSize = undefined;
}
if ( style?.typography?.fontSize ) {
updates.style = {
...style,
typography: {
...style?.typography,
fontSize: undefined,
},
};
}
}

setAttributes( updates );
} }
help={
fitText
? __( 'Text will resize to fit its container.' )
Expand Down Expand Up @@ -278,7 +301,7 @@ const hasFitTextSupport = ( blockNameOrType ) => {
export default {
useBlockProps,
addSaveProps,
attributeKeys: [ 'fitText' ],
attributeKeys: [ 'fitText', 'fontSize', 'style' ],
hasSupport: hasFitTextSupport,
edit: FitTextControl,
};
19 changes: 15 additions & 4 deletions packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,30 @@ function TypographyInspectorControl( { children, resetAllFilter } ) {

export function TypographyPanel( { clientId, name, setAttributes, settings } ) {
function selector( select ) {
const { style, fontFamily, fontSize } =
const { style, fontFamily, fontSize, fitText } =
select( blockEditorStore ).getBlockAttributes( clientId ) || {};
return { style, fontFamily, fontSize };
return { style, fontFamily, fontSize, fitText };
}
const { style, fontFamily, fontSize } = useSelect( selector, [ clientId ] );
const { style, fontFamily, fontSize, fitText } = useSelect( selector, [
clientId,
] );
const isEnabled = useHasTypographyPanel( settings );
const value = useMemo(
() => attributesToStyle( { style, fontFamily, fontSize } ),
[ style, fontSize, fontFamily ]
);

const onChange = ( newStyle ) => {
setAttributes( styleToAttributes( newStyle ) );
const newAttributes = styleToAttributes( newStyle );

// If setting a font size and fitText is currently enabled, disable it
const hasFontSize =
newAttributes.fontSize || newAttributes.style?.typography?.fontSize;
if ( hasFontSize && fitText ) {
newAttributes.fitText = undefined;
}

setAttributes( newAttributes );
};

if ( ! isEnabled ) {
Expand Down
Loading