Skip to content

Commit b596683

Browse files
committed
fix: sync state when succeeding call to change styles
1 parent 7352dd2 commit b596683

File tree

2 files changed

+24
-29
lines changed
  • src

2 files changed

+24
-29
lines changed

src/components/font-size-control/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const FontSizeControl = props => {
3939
placeholder={ passedPlaceholder }
4040
onChangeUnit={ value => {
4141
// Change font-size so as not to surprise the user.
42-
if ( props.value !== '' ) {
42+
if ( props.value !== '' && ! isNaN( Number( value ) ) ) {
4343
if ( value === 'em' || value === 'rem' ) {
4444
props.onChange( String( pxToEm( props.value ) ) )
4545
} else if ( value === 'px' ) {

src/plugins/global-settings/typography/index.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -293,40 +293,35 @@ addFilter( 'stackable.global-settings.inspector', 'stackable/global-typography',
293293
}
294294

295295
const changeStyles = _typography => {
296-
const typography = cloneDeep( _typography )
297-
const newSettings = { ...typographySettings }
296+
setTypographySettings( prevTypographySettings => {
297+
const typography = cloneDeep( _typography )
298+
const newSettings = { ...prevTypographySettings }
298299

299-
Object.entries( typography ).forEach( ( [ selector, styles ] ) => {
300-
if ( ! selector || typeof styles !== 'object' ) {
301-
return
302-
}
300+
Object.entries( typography ).forEach( ( [ selector, styles ] ) => {
301+
if ( ! selector || typeof styles !== 'object' ) {
302+
return
303+
}
303304

304-
// Merge the new styles with the previous, while overwritting similar styles.
305-
// This allow adding styles without removing the previous ones.
306-
// Check if the object is empty, used for resetting the whole setting.
307-
if ( Object.keys( styles ).length !== 0 ) {
308-
styles = { ...newSettings[ selector ], ...styles }
309-
}
310-
/**
311-
* Delete the object keys with empty strings.
312-
* Otherwise, the API will throw an error code 400
313-
* because of incompatible schema type.
314-
*/
315-
Object.keys( styles ).forEach( key => {
316-
if ( styles[ key ] === '' ) {
317-
delete styles[ key ]
305+
if ( Object.keys( styles ).length !== 0 ) {
306+
styles = { ...newSettings[ selector ], ...styles }
318307
}
319-
} )
320308

321-
newSettings[ selector ] = styles
322-
} )
309+
Object.keys( styles ).forEach( key => {
310+
if ( styles[ key ] === '' ) {
311+
delete styles[ key ]
312+
}
313+
} )
323314

324-
// Update the global styles immediately when reset font size is triggered.
325-
if ( Object.values( typography ).some( styles => styles && ! styles.fontSize ) ) {
326-
doAction( 'stackable.global-settings.typography-update-global-styles', newSettings )
327-
}
315+
newSettings[ selector ] = styles
316+
} )
328317

329-
updateTypography( newSettings )
318+
// Update the global styles immediately when reset font size is triggered.
319+
if ( Object.values( typography ).some( styles => styles && ! styles.fontSize ) ) {
320+
doAction( 'stackable.global-settings.typography-update-global-styles', newSettings )
321+
}
322+
323+
return newSettings
324+
} )
330325
}
331326

332327
const resetStyles = selector => {

0 commit comments

Comments
 (0)