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

Add full and wide align controls to Query and Query Pagination blocks #29289

Closed
wants to merge 3 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
5 changes: 5 additions & 0 deletions packages/block-library/src/query-loop/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"apiVersion": 2,
"name": "core/query-loop",
"category": "design",
"attributes": {
"align": {
"type": "string"
}
},
"usesContext": [
"queryId",
"query",
Expand Down
10 changes: 8 additions & 2 deletions packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const TEMPLATE = [
];
export default function QueryLoopEdit( {
clientId,
attributes: { align },
context: {
query: {
perPage,
Expand Down Expand Up @@ -123,12 +124,17 @@ export default function QueryLoopEdit( {
[ posts ]
);
const hasLayoutFlex = layoutType === 'flex' && columns > 1;
const blockProps = useBlockProps( {
const extraBlockProps = {
className: classnames( {
'is-flex-container': hasLayoutFlex,
[ `columns-${ columns }` ]: hasLayoutFlex,
} ),
} );
};
// We haven't used the `supports.align` here because first we
// need to find a way to absorb toolbars for nested block
// wrappers (https://github.com/WordPress/gutenberg/issues/7694).
if ( align ) extraBlockProps[ 'data-align' ] = align;
Copy link
Contributor

Choose a reason for hiding this comment

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

why d we need this? Isn't it done automatically in the hooks?

const blockProps = useBlockProps( extraBlockProps );
const innerBlocksProps = useInnerBlocksProps( {}, { template: TEMPLATE } );

if ( ! posts ) {
Expand Down
1 change: 0 additions & 1 deletion packages/block-library/src/query-loop/editor.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.wp-block.wp-block-query-loop {
max-width: 100%;
padding-left: 0;
list-style: none;
}
7 changes: 7 additions & 0 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
}
}

// We haven't used the `supports.align` here because first we
// need to find a way to absorb toolbars for nested block
// wrappers (https://github.com/WordPress/gutenberg/issues/7694).
if ( isset( $attributes['align'] ) ) {
$classnames .= " align{$attributes['align']}";
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );

$content = '';
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/query-pagination/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"supports": {
"reusable": false,
"html": false
"html": false,
"align": [ "wide", "full" ]
},
"editorStyle": "wp-block-query-pagination-editor",
"style": "wp-block-query-pagination"
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function QueryContent( {
attributes,
context: { postId },
setAttributes,
clientId,
} ) {
const { queryId, query, layout } = attributes;
const instanceId = useInstanceId( QueryContent );
Expand Down Expand Up @@ -72,6 +73,7 @@ export function QueryContent( {
/>
<BlockControls>
<QueryToolbar
clientId={ clientId }
attributes={ attributes }
setQuery={ updateQuery }
setLayout={ updateLayout }
Expand Down
36 changes: 36 additions & 0 deletions packages/block-library/src/query/edit/query-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import {
BaseControl,
__experimentalNumberControl as NumberControl,
} from '@wordpress/components';
import {
BlockAlignmentToolbar,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useInstanceId } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { settings, list, grid } from '@wordpress/icons';

export default function QueryToolbar( {
clientId,
attributes: { query, layout },
setQuery,
setLayout,
Expand All @@ -21,6 +27,21 @@ export default function QueryToolbar( {
QueryToolbar,
'blocks-query-pagination-max-page-input'
);
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { queryLoops } = useSelect(
( select ) => {
const { getBlocks } = select( blockEditorStore );
const blocks = getBlocks( clientId );
const _queryLoops = blocks?.filter(
( { name } ) => name === 'core/query-loop'
);
return {
queryLoops: _queryLoops,
};
},
[ clientId ]
);

const layoutControls = [
{
icon: list,
Expand All @@ -36,6 +57,14 @@ export default function QueryToolbar( {
isActive: layout?.type === 'flex',
},
];
// Updates the `align` property in `QueryLoop` InnerBlocks all at once.
// This is usually just one instance though, as it doesn't make sense to
// have more than one `QueryLoop` in each `Query` block.
const updateAlignment = ( newValue ) => {
queryLoops.forEach( ( queryLoop ) => {
updateBlockAttributes( queryLoop.clientId, { align: newValue } );
} );
};
return (
<>
{ ! query.inherit && (
Expand Down Expand Up @@ -108,6 +137,13 @@ export default function QueryToolbar( {
/>
</ToolbarGroup>
) }
{ !! queryLoops?.length && (
<BlockAlignmentToolbar
value={ queryLoops[ 0 ].attributes.align }
onChange={ updateAlignment }
controls={ [ 'wide', 'full' ] }
/>
) }
<ToolbarGroup controls={ layoutControls } />
</>
);
Expand Down