-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global styles: keep custom CSS when switching between style variations #56623
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
790d98d
Keep custom CSS when changing style variations
glendaviesnz 5b14962
Include block custom CSS
glendaviesnz ffd7b60
Fix merging of block styles
glendaviesnz 1355979
another fix of block styles merging
glendaviesnz 253ef56
Add toggle to specify if css should be preserved or not
glendaviesnz a61b824
only check for user block css if preserve toggled on
glendaviesnz 5e0be71
Fix issue with block styles not being removed
glendaviesnz fbdd11a
Move the setting of user styles into parent
glendaviesnz 8886181
Make toggle work update preview canvas
glendaviesnz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,12 @@ import classnames from 'classnames'; | |
*/ | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useMemo, useContext, useState } from '@wordpress/element'; | ||
import { useMemo, useContext, useState, useEffect } from '@wordpress/element'; | ||
import { ENTER } from '@wordpress/keycodes'; | ||
import { __experimentalGrid as Grid } from '@wordpress/components'; | ||
import { | ||
__experimentalGrid as Grid, | ||
ToggleControl, | ||
} from '@wordpress/components'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor'; | ||
|
||
|
@@ -25,9 +28,9 @@ const { GlobalStylesContext, areGlobalStyleConfigsEqual } = unlock( | |
blockEditorPrivateApis | ||
); | ||
|
||
function Variation( { variation } ) { | ||
function Variation( { variation, setCurrentVariation, variationId } ) { | ||
const [ isFocused, setIsFocused ] = useState( false ); | ||
const { base, user, setUserConfig } = useContext( GlobalStylesContext ); | ||
const { base, user } = useContext( GlobalStylesContext ); | ||
const context = useMemo( () => { | ||
return { | ||
user: { | ||
|
@@ -41,12 +44,7 @@ function Variation( { variation } ) { | |
}, [ variation, base ] ); | ||
|
||
const selectVariation = () => { | ||
setUserConfig( () => { | ||
return { | ||
settings: variation.settings, | ||
styles: variation.styles, | ||
}; | ||
} ); | ||
setCurrentVariation( variationId ); | ||
}; | ||
|
||
const selectOnEnter = ( event ) => { | ||
|
@@ -101,6 +99,13 @@ function Variation( { variation } ) { | |
} | ||
|
||
export default function StyleVariationsContainer() { | ||
const { user, setUserConfig } = useContext( GlobalStylesContext ); | ||
const [ currentUserStyles ] = useState( { ...user } ); | ||
const [ currentVariation, setCurrentVariation ] = useState(); | ||
|
||
const [ preserveAdditionalCSS, setPreserveAdditionalCSS ] = | ||
useState( true ); | ||
|
||
const variations = useSelect( ( select ) => { | ||
return select( | ||
coreStore | ||
|
@@ -114,22 +119,110 @@ export default function StyleVariationsContainer() { | |
settings: {}, | ||
styles: {}, | ||
}, | ||
...( variations ?? [] ).map( ( variation ) => ( { | ||
...variation, | ||
settings: variation.settings ?? {}, | ||
styles: variation.styles ?? {}, | ||
} ) ), | ||
...( variations ?? [] ).map( ( variation ) => { | ||
const blockStyles = { ...variation?.styles?.blocks } || {}; | ||
if ( | ||
currentUserStyles?.styles?.blocks && | ||
preserveAdditionalCSS | ||
) { | ||
Object.keys( currentUserStyles.styles.blocks ).forEach( | ||
( blockName ) => { | ||
if ( | ||
currentUserStyles.styles.blocks[ blockName ].css | ||
) { | ||
blockStyles[ blockName ] = { | ||
...( blockStyles[ blockName ] | ||
? blockStyles[ blockName ] | ||
: {} ), | ||
css: `${ | ||
blockStyles[ blockName ]?.css || '' | ||
} ${ | ||
currentUserStyles.styles.blocks[ | ||
blockName | ||
].css | ||
}`, | ||
}; | ||
} | ||
} | ||
); | ||
} | ||
|
||
const styles = preserveAdditionalCSS | ||
? { | ||
...variation.styles, | ||
...( currentUserStyles?.styles?.css || | ||
variation?.styles?.css | ||
? { | ||
css: `${ | ||
variation.styles?.css || '' | ||
} ${ currentUserStyles.styles.css }`, | ||
} | ||
: {} ), | ||
blocks: { | ||
...blockStyles, | ||
}, | ||
} | ||
: variation.styles; | ||
|
||
return { | ||
...variation, | ||
settings: variation.settings ?? {}, | ||
styles: styles ?? {}, | ||
}; | ||
} ), | ||
]; | ||
}, [ variations ] ); | ||
}, [ | ||
variations, | ||
currentUserStyles.styles.blocks, | ||
currentUserStyles.styles.css, | ||
preserveAdditionalCSS, | ||
] ); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be good to move the below to an event handler rather than useEffect, but need to refactor the update flows slightly to do this - will worry about that if we decide to go ahead with this refactor |
||
useEffect( () => { | ||
if ( currentVariation ) { | ||
setUserConfig( () => { | ||
return { | ||
settings: withEmptyVariation[ currentVariation ]?.settings, | ||
styles: withEmptyVariation[ currentVariation ]?.styles, | ||
}; | ||
} ); | ||
} | ||
}, [ | ||
currentVariation, | ||
preserveAdditionalCSS, | ||
setUserConfig, | ||
withEmptyVariation, | ||
] ); | ||
|
||
return ( | ||
<Grid | ||
columns={ 2 } | ||
className="edit-site-global-styles-style-variations-container" | ||
> | ||
{ withEmptyVariation.map( ( variation, index ) => ( | ||
<Variation key={ index } variation={ variation } /> | ||
) ) } | ||
</Grid> | ||
<> | ||
<Grid | ||
columns={ 2 } | ||
className="edit-site-global-styles-style-variations-container" | ||
> | ||
{ withEmptyVariation.map( ( variation, index ) => ( | ||
<Variation | ||
key={ index } | ||
variation={ variation } | ||
variationId={ index } | ||
setCurrentVariation={ setCurrentVariation } | ||
/> | ||
) ) } | ||
</Grid> | ||
|
||
<ToggleControl | ||
className="edit-site-global-styles-style-variations-preserve-css" | ||
label={ __( 'Keep additional CSS' ) } | ||
help={ __( | ||
'Preserve additional CSS when switching between variations.' | ||
) } | ||
checked={ preserveAdditionalCSS } | ||
onChange={ () => { | ||
setPreserveAdditionalCSS( | ||
preserveAdditionalCSS ? false : true | ||
); | ||
} } | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will extract the following into a method/hook, simplify the object spreading, and add some tests if there is some agreement to go with this approach