Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
getBlockTypes,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useEffect, useState, useContext } from '@wordpress/element';
import { useContext, useMemo } from '@wordpress/element';
import { getCSSRules } from '@wordpress/style-engine';
import {
__unstablePresetDuotoneFilter as PresetDuotoneFilter,
Expand Down Expand Up @@ -789,9 +789,6 @@ export const getBlockSelectors = ( blockTypes ) => {
};

export function useGlobalStylesOutput() {
const [ stylesheets, setStylesheets ] = useState( [] );
const [ settings, setSettings ] = useState( {} );
const [ svgFilters, setSvgFilters ] = useState( {} );
const { merged: mergedConfig } = useContext( GlobalStylesContext );
const [ blockGap ] = useSetting( 'spacing.blockGap' );
const hasBlockGapSupport = blockGap !== null;
Expand All @@ -801,9 +798,9 @@ export function useGlobalStylesOutput() {
return !! getSettings().disableLayoutStyles;
} );

useEffect( () => {
return useMemo( () => {
if ( ! mergedConfig?.styles || ! mergedConfig?.settings ) {
return;
return [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this return statement mean that the whole function could early return an empty array?

It looks like prior to this PR, the function will always return an array with four elements. From looking at the useState calls, an equivalent to the default values in that case might be something like:

return [ [], {}, {}, true ] ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this return statement mean that the whole function could early return an empty array?

Correct. Initially, I was planning to return the array with the same signature, but then I checked the usage, and returning undefined for values won't be an issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then I checked the usage, and returning undefined for values won't be an issue.

Very interesting! Particularly because of hasBlockGapSupport having a default truthy value. Given that the function doesn't appear to be exported outside of the edit-site package, this sounds okay to me in principle.

One thing to keep in mind with making changes to the output, is that from the documentation it sounds like there might have been (or be) an intention of potentially extracting the global styles APIs to their own package (this line). I think I remember @jorgefilipecosta working on making global styles data available in the post editor a while back, but I'm not sure if that's related.

I see that including hasBlockGapSupport was added to the output of the function in #40185 in April. From the looks of it, that value was only needed in toStyles and doesn't appear to be used by any consumers of useGlobalStylesOutput. It might be worth double-checking with @oandregal to see if there was a reason it was added to the output or whether it's still needed? It looks like we might be able to remove it altogether from the return value 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the best time to change return types if we want 😄

I was wondering about the hasBlockGapSupport return as well. So let'ss see if we can remove it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that including hasBlockGapSupport was added to the output of the function in #40185 in April. From the looks of it, that value was only needed in toStyles and doesn't appear to be used by any consumers of useGlobalStylesOutput.

Yeah, I don't see it in use anywhere either and don't remember why it was returned in the first place :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the confirmation, @oandregal. I removed hasBlockGapSupport from the return value.

}

const blockSelectors = getBlockSelectors( getBlockTypes() );
Expand All @@ -819,7 +816,7 @@ export function useGlobalStylesOutput() {
disableLayoutStyles
);
const filters = toSvgFilters( mergedConfig, blockSelectors );
setStylesheets( [
const stylesheets = [
{
css: customProperties,
isGlobalStyles: true,
Expand All @@ -828,15 +825,13 @@ export function useGlobalStylesOutput() {
css: globalStyles,
isGlobalStyles: true,
},
] );
setSettings( mergedConfig.settings );
setSvgFilters( filters );
];

return [ stylesheets, mergedConfig.settings, filters ];
}, [
hasBlockGapSupport,
hasFallbackGapSupport,
mergedConfig,
disableLayoutStyles,
] );

return [ stylesheets, settings, svgFilters, hasBlockGapSupport ];
}