Skip to content

Commit fc2dd44

Browse files
committed
fix: Ensure font size attributes are correctly applied by using the replace method and clearing conflicting class
1 parent b2dca43 commit fc2dd44

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

src/font-size/inline-ui.js

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* WordPress dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { useAnchor } from '@wordpress/rich-text';
5+
import { useAnchor, removeFormat } from '@wordpress/rich-text';
66
import { Popover, Button, FontSizePicker } from '@wordpress/components';
77
import { useSelect } from '@wordpress/data';
8-
import { removeFormat } from '@wordpress/rich-text';
98

109
/**
1110
* Internal dependencies
@@ -24,76 +23,81 @@ import { createFormatHelpers } from '../utils';
2423
* @param {boolean} props.isActive - Indicates if the format is active.
2524
* @return {JSX.Element} - The rendered component.
2625
*/
27-
function InlineUI({
26+
function InlineUI( {
2827
value,
2928
onChange,
3029
onClose,
3130
activeAttributes,
3231
contentRef,
3332
isActive,
34-
}) {
35-
const { update } = createFormatHelpers({
33+
} ) {
34+
const { replace } = createFormatHelpers( {
3635
value,
3736
onChange,
3837
formatType: 'blablablocks/font-size',
3938
activeAttributes,
40-
});
39+
} );
4140

42-
const anchor = useAnchor({
41+
const anchor = useAnchor( {
4342
editableContentElement: contentRef,
4443
settings: { isActive },
45-
});
44+
} );
4645

4746
const fontSizes = useSelect(
48-
(select) => select('core/block-editor').getSettings().fontSizes,
47+
( select ) => select( 'core/block-editor' ).getSettings().fontSizes,
4948
[]
5049
);
5150

52-
const handleFontSizeChange = (newFontSize) => {
53-
if (newFontSize) {
51+
const handleFontSizeChange = ( newFontSize ) => {
52+
if ( newFontSize ) {
5453
// Find the font size object to get the slug
5554
const fontSizeObj = fontSizes?.find(
56-
(size) => size.size === newFontSize || size.slug === newFontSize
55+
( size ) =>
56+
size.size === newFontSize || size.slug === newFontSize
5757
);
5858

59-
if (fontSizeObj && fontSizeObj.slug) {
59+
if ( fontSizeObj && fontSizeObj.slug ) {
6060
// Use CSS class for theme-defined font sizes
61-
update({
62-
class: `has-${fontSizeObj.slug}-font-size`,
63-
});
61+
replace( {
62+
class: `has-${ fontSizeObj.slug }-font-size`,
63+
style: undefined,
64+
} );
6465
} else {
6566
// Use inline style for custom font sizes
66-
update({
67-
style: `font-size: ${newFontSize}`,
68-
});
67+
replace( {
68+
style: `font-size: ${ newFontSize }`,
69+
class: undefined,
70+
} );
6971
}
7072
}
7173
};
7274

7375
const handleClear = () => {
74-
onChange(removeFormat(value, 'blablablocks/font-size'));
76+
onChange( removeFormat( value, 'blablablocks/font-size' ) );
7577
onClose();
7678
};
7779

7880
// Extract current font size from class or style attribute
7981
const getCurrentFontSize = () => {
8082
// Check if there's a class attribute with has-*-font-size pattern
8183
const classAttr = activeAttributes.class;
82-
if (classAttr) {
83-
const match = classAttr.match(/has-([a-z0-9-]+)-font-size/);
84-
if (match) {
85-
const slug = match[1];
86-
const fontSizeObj = fontSizes?.find((size) => size.slug === slug);
84+
if ( classAttr ) {
85+
const match = classAttr.match( /has-([a-z0-9-]+)-font-size/ );
86+
if ( match ) {
87+
const slug = match[ 1 ];
88+
const fontSizeObj = fontSizes?.find(
89+
( size ) => size.slug === slug
90+
);
8791
return fontSizeObj ? fontSizeObj.size : null;
8892
}
8993
}
9094

9195
// Check if there's a style attribute with font-size
9296
const styleAttr = activeAttributes.style;
93-
if (styleAttr) {
94-
const match = styleAttr.match(/font-size:\s*([^;]+)/);
95-
if (match) {
96-
return match[1].trim();
97+
if ( styleAttr ) {
98+
const match = styleAttr.match( /font-size:\s*([^;]+)/ );
99+
if ( match ) {
100+
return match[ 1 ].trim();
97101
}
98102
}
99103

@@ -104,27 +108,27 @@ function InlineUI({
104108

105109
return (
106110
<Popover
107-
anchor={anchor}
111+
anchor={ anchor }
108112
className="block-editor-format-toolbar__blablablocks-font-size-popover"
109-
offset={20}
110-
onClose={onClose}
113+
offset={ 20 }
114+
onClose={ onClose }
111115
placement="bottom"
112116
shift
113117
>
114-
<div style={{ padding: '16px', minWidth: '220px' }}>
118+
<div style={ { padding: '16px', minWidth: '220px' } }>
115119
<FontSizePicker
116-
value={fontSizeValue}
117-
onChange={handleFontSizeChange}
118-
fontSizes={fontSizes}
120+
value={ fontSizeValue }
121+
onChange={ handleFontSizeChange }
122+
fontSizes={ fontSizes }
119123
/>
120124
<Button
121125
className="reset-button"
122-
disabled={!fontSizeValue}
123-
onClick={handleClear}
126+
disabled={ ! fontSizeValue }
127+
onClick={ handleClear }
124128
variant="tertiary"
125-
style={{ marginTop: '12px', width: '100%' }}
129+
style={ { marginTop: '12px', width: '100%' } }
126130
>
127-
{__('Clear', 'blablablocks-formats')}
131+
{ __( 'Clear', 'blablablocks-formats' ) }
128132
</Button>
129133
</div>
130134
</Popover>

0 commit comments

Comments
 (0)