Skip to content
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

Block editor: hooks: fix rules of hooks violations #48900

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,29 @@ const layoutBlockSupportKey = '__experimentalLayout';
/**
* Generates the utility classnames for the given block's layout attributes.
*
* @param { Object } blockAttributes Block attributes.
* @param { string } blockName Block name.
* @param {Object} blockAttributes Block attributes.
* @param {string} blockName Block name.
* @param {Object} globalLayoutSettings Global layout settings.
*
* @return { Array } Array of CSS classname strings.
*/
export function useLayoutClasses( blockAttributes = {}, blockName ) {
export function useLayoutClasses(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is just a local API right?

blockAttributes = {},
blockName,
globalLayoutSettings
) {
const rootPaddingAlignment = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings().__experimentalFeatures
?.useRootPaddingAwareAlignments;
}, [] );
const globalLayoutSettings = useSetting( 'layout' ) || {};

const { layout } = blockAttributes;

if ( ! hasBlockSupport( blockName, layoutBlockSupportKey ) ) {
return [];
}

const { default: defaultBlockLayout } =
getBlockSupport( blockName, layoutBlockSupportKey ) || {};
const usedLayout =
Expand Down Expand Up @@ -372,9 +380,11 @@ export const withLayoutStyles = createHigherOrderComponent(
layout?.inherit || layout?.contentSize || layout?.wideSize
? { ...layout, type: 'constrained' }
: layout || defaultBlockLayout || {};
const layoutClasses = hasLayoutBlockSupport
? useLayoutClasses( attributes, name )
: null;
const layoutClasses = useLayoutClasses(
attributes,
name,
defaultThemeLayout
);
// Higher specificity to override defaults from theme.json.
const selector = `.wp-container-${ id }.wp-container-${ id }`;
const blockGapSupport = useSetting( 'spacing.blockGap' );
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ export default function VisualEditor( { styles } ) {

const postContentLayoutClasses = useLayoutClasses(
newestPostContentAttributes,
'core/post-content'
'core/post-content',
globalLayoutSettings
);

const blockListLayoutClass = classnames(
Expand Down