Skip to content

Commit

Permalink
More context providing to the block support rather than the Group block
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Apr 7, 2022
1 parent 15d4a55 commit 3ac8f3c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

/**
* Registers the layout block attribute for block types that support it.
* Registers the layout block attribute for block types that support it,
* and sets `provides_context` to allow children to access the layout settings.
*
* @param WP_Block_Type $block_type Block Type.
*/
Expand All @@ -22,6 +23,14 @@ function gutenberg_register_layout_support( $block_type ) {
'type' => 'object',
);
}

if ( ! isset( $block_type->provides_context ) ) {
$block_type->provides_context = array();
}

if ( ! array_key_exists( 'layout', $block_type->provides_context ) ) {
$block_type->provides_context['layout'] = 'layout';
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/block-editor/src/hooks/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ export function addAttribute( settings ) {
return settings;
}

/**
* Adds layout to `providesContext` so that the layout context can be accessed
* via child blocks.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
export function addProvidesContext( settings ) {
if ( settings.providesContext?.layout ) {
return settings;
}
if ( hasBlockSupport( settings, layoutBlockSupportKey ) ) {
const providesContext = settings.providesContext || {};
settings.providesContext = {
...providesContext,
layout: 'layout',
};
}

return settings;
}

/**
* Override the default edit UI to include layout controls
*
Expand Down Expand Up @@ -240,6 +263,11 @@ addFilter(
'core/layout/addAttribute',
addAttribute
);
addFilter(
'blocks.registerBlockType',
'core/layout/addProvidesContext',
addProvidesContext
);
addFilter(
'editor.BlockListBlock',
'core/editor/layout/with-layout-styles',
Expand Down
4 changes: 0 additions & 4 deletions packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
"enum": [ "all", "insert", false ]
}
},
"providesContext": {
"layout": "layout",
"orientation": "orientation"
},
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
Expand Down

0 comments on commit 3ac8f3c

Please sign in to comment.