Skip to content

Commit

Permalink
Refactor useGlobalStylesOutputWithConfig and `useGlobalStylesOutput…
Browse files Browse the repository at this point in the history
…` to accept an options object, where previously it was a single boolean for disableRootPadding.

This enables global styles revisions to output variations CSS if the CPT contains variations styles.
  • Loading branch information
ramonjd committed Jun 23, 2024
1 parent 950f5c8 commit 7f7128e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,13 @@ export function processCSSNesting( css, blockSelector ) {
} );
return processedCSS;
}
/**
* An object containing options for useGlobalStylesOutput.
*
* @typedef {Object} useGlobalStylesOutputOptions
* @property {boolean} disableRootPadding Disable root padding styles. Default `false`.
* @property {boolean} variationStyles Include variation styles. Default `false`.
*/

/**
* Returns the global styles output using a global styles configuration.
Expand All @@ -1324,14 +1331,14 @@ export function processCSSNesting( css, blockSelector ) {
* The use case for a custom config is to generate bespoke styles
* and settings for previews, or other out-of-editor experiences.
*
* @param {Object} mergedConfig Global styles configuration.
* @param {boolean} disableRootPadding Disable root padding styles.
* @param {Object} mergedConfig Global styles configuration.
* @param {useGlobalStylesOutputOptions} options Options for generating global styles output.
*
* @return {Array} Array of stylesheets and settings.
*/
export function useGlobalStylesOutputWithConfig(
mergedConfig = {},
disableRootPadding
options = {}
) {
const [ blockGap ] = useGlobalSetting( 'spacing.blockGap' );
mergedConfig = setThemeFileUris(
Expand Down Expand Up @@ -1369,7 +1376,10 @@ export function useGlobalStylesOutputWithConfig(
hasBlockGapSupport,
hasFallbackGapSupport,
disableLayoutStyles,
disableRootPadding
options?.disableRootPadding,
{
variationStyles: options?.variationStyles,
}
);
const svgs = toSvgFilters( updatedConfig, blockSelectors );

Expand Down Expand Up @@ -1416,19 +1426,20 @@ export function useGlobalStylesOutputWithConfig(
hasFallbackGapSupport,
mergedConfig,
disableLayoutStyles,
disableRootPadding,
options?.disableRootPadding,
options?.variationStyles,
getBlockStyles,
] );
}

/**
* Returns the global styles output based on the current state of global styles config loaded in the editor context.
*
* @param {boolean} disableRootPadding Disable root padding styles.
* @param {useGlobalStylesOutputOptions} options Disable root padding styles.
*
* @return {Array} Array of stylesheets and settings.
*/
export function useGlobalStylesOutput( disableRootPadding = false ) {
export function useGlobalStylesOutput( options ) {
const { merged: mergedConfig } = useContext( GlobalStylesContext );
return useGlobalStylesOutputWithConfig( mergedConfig, disableRootPadding );
return useGlobalStylesOutputWithConfig( mergedConfig, options );
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ function useGlobalStylesRenderer() {
const postType = useSelect( ( select ) => {
return select( editSiteStore ).getEditedPostType();
} );
const [ styles, settings ] = useGlobalStylesOutput(
postType !== TEMPLATE_POST_TYPE
);
const [ styles, settings ] = useGlobalStylesOutput( {
disableRootPadding: postType !== TEMPLATE_POST_TYPE,
} );
const { getSettings } = useSelect( editSiteStore );
const { updateSettings } = useDispatch( editSiteStore );

Expand Down
4 changes: 3 additions & 1 deletion packages/edit-site/src/components/revisions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function Revisions( { userConfig, blocks } ) {
[ originalSettings ]
);

const [ globalStyles ] = useGlobalStylesOutputWithConfig( mergedConfig );
const [ globalStyles ] = useGlobalStylesOutputWithConfig( mergedConfig, {
variationStyles: true,
} );

const editorStyles =
! isObjectEmpty( globalStyles ) && ! isObjectEmpty( userConfig )
Expand Down

0 comments on commit 7f7128e

Please sign in to comment.