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

Query block grid view #27067

Merged
merged 8 commits into from
Nov 20, 2020
Merged
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
4 changes: 4 additions & 0 deletions packages/block-library/src/post-featured-image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
a {
display: inline-block;
}
img {
max-width: 100%;
height: auto;
}
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion packages/block-library/src/query-loop/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"usesContext": [
"queryId",
"query",
"queryContext"
"queryContext",
"layout"
],
"supports": {
"reusable": false,
Expand Down
36 changes: 25 additions & 11 deletions packages/block-library/src/query-loop/edit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -38,6 +43,7 @@ export default function QueryLoopEdit( {
sticky,
} = {},
queryContext,
layout: { type: layoutType = 'flex', columns = 1 } = {},
},
} ) {
const [ { page } ] = useQueryContext() || queryContext || [ {} ];
Expand Down Expand Up @@ -102,7 +108,13 @@ export default function QueryLoopEdit( {
} ) ),
[ posts ]
);
const blockProps = useBlockProps();
const hasLayoutFlex = layoutType === 'flex' && columns > 1;
const blockProps = useBlockProps( {
className: classnames( {
'is-flex-container': hasLayoutFlex,
[ `columns-${ columns }` ]: hasLayoutFlex,
} ),
} );
const innerBlocksProps = useInnerBlocksProps( {}, { template: TEMPLATE } );

if ( ! posts ) {
Expand All @@ -114,7 +126,7 @@ export default function QueryLoopEdit( {
}

return (
<div { ...blockProps }>
<ul { ...blockProps }>
{ blockContexts &&
blockContexts.map( ( blockContext ) => (
<BlockContextProvider
Expand All @@ -123,18 +135,20 @@ export default function QueryLoopEdit( {
>
{ blockContext ===
( activeBlockContext || blockContexts[ 0 ] ) ? (
<div { ...innerBlocksProps } />
<li { ...innerBlocksProps } />
) : (
<BlockPreview
blocks={ blocks }
__experimentalLive
__experimentalOnClick={ () =>
setActiveBlockContext( blockContext )
}
/>
<li>
<BlockPreview
blocks={ blocks }
__experimentalLive
__experimentalOnClick={ () =>
setActiveBlockContext( blockContext )
}
/>
</li>
) }
</BlockContextProvider>
) ) }
</div>
</ul>
);
}
4 changes: 3 additions & 1 deletion packages/block-library/src/query-loop/editor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.editor-styles-wrapper .wp-block.wp-block-query-loop {
.wp-block.wp-block-query-loop {
max-width: 100%;
padding-left: 0;
list-style: none;
}
18 changes: 16 additions & 2 deletions packages/block-library/src/query-loop/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,18 @@ function render_block_core_query_loop( $attributes, $content, $block ) {

$posts = get_posts( $query );

$classnames = '';
if ( isset( $block->context['layout'] ) && isset( $block->context['query'] ) ) {
if ( isset( $block->context['layout']['type'] ) && 'flex' === $block->context['layout']['type'] ) {
$classnames = "is-flex-container columns-{$block->context['layout']['columns']}";
}
}

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

$content = '';
foreach ( $posts as $post ) {
$content .= (
$block_content = (
new WP_Block(
$block->parsed_block,
array(
Expand All @@ -80,8 +89,13 @@ function render_block_core_query_loop( $attributes, $content, $block ) {
)
)
)->render( array( 'dynamic' => false ) );
$content .= "<li>{$block_content}</li>";
}
return $content;
return sprintf(
'<ul %1$s>%2$s</ul>',
$wrapper_attributes,
$content
);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions packages/block-library/src/query-loop/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.wp-block-query-loop {
max-width: 100%;
list-style: none;
padding: 0;

li {
clear: both;
}

&.is-flex-container {
flex-direction: row;
display: flex;
flex-wrap: wrap;

li {
margin: 0 1.25em 1.25em 0;
width: 100%;
}

@include break-small {
@for $i from 2 through 6 {
&.is-flex-container.columns-#{ $i } > li {
width: calc((100% / #{ $i }) - 1.25em + (1.25em / #{ $i }));

&:nth-child( #{ $i }n ) {
margin-right: 0;
}
}
}
}
}
}
9 changes: 8 additions & 1 deletion packages/block-library/src/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
"exclude": [],
"sticky": ""
}
},
"layout": {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the attempt for having a structure similar to __experimentalLayout. Check PR's description for more info.

Copy link
Member

Choose a reason for hiding this comment

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

Should it be experimental then?

Latest Posts block defines two attributes:

"postLayout": {
"type": "string",
"default": "list"
},
"columns": {
"type": "number",
"default": 3
},

We can always tweak providesContext to support some grouping if that makes it easier:

"providesContext": {
	"queryId": "queryId",
	"query": "query",
	"layout": {
		"columns": "columns",
		"type": "layout"
	}
}

"type": "object",
"default": {
"type": "list"
}
}
},
"providesContext": {
"queryId": "queryId",
"query": "query"
"query": "query",
"layout": "layout"
},
"usesContext": [
"postId"
Expand Down
17 changes: 14 additions & 3 deletions packages/block-library/src/query/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { DEFAULTS_POSTS_PER_PAGE } from '../constants';

const TEMPLATE = [ [ 'core/query-loop' ] ];
export function QueryContent( {
attributes: { queryId, query },
attributes,
context: { postId },
setAttributes,
} ) {
const { queryId, query, layout } = attributes;
const instanceId = useInstanceId( QueryContent );
const blockProps = useBlockProps();
const innerBlocksProps = useInnerBlocksProps( {}, { template: TEMPLATE } );
Expand Down Expand Up @@ -59,11 +60,21 @@ export function QueryContent( {
}, [ queryId, instanceId ] );
const updateQuery = ( newQuery ) =>
setAttributes( { query: { ...query, ...newQuery } } );
const updateLayout = ( newLayout ) =>
setAttributes( { layout: { ...layout, ...newLayout } } );
return (
<>
<QueryInspectorControls query={ query } setQuery={ updateQuery } />
<QueryInspectorControls
attributes={ attributes }
setQuery={ updateQuery }
setLayout={ updateLayout }
/>
<BlockControls>
<QueryToolbar query={ query } setQuery={ updateQuery } />
<QueryToolbar
attributes={ attributes }
setQuery={ updateQuery }
setLayout={ updateLayout }
/>
</BlockControls>
<div { ...blockProps }>
<QueryProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
TextControl,
FormTokenField,
SelectControl,
RangeControl,
Notice,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
Expand All @@ -31,7 +33,11 @@ const stickyOptions = [
{ label: __( 'Only' ), value: 'only' },
];

export default function QueryInspectorControls( { query, setQuery } ) {
export default function QueryInspectorControls( {
attributes: { query, layout },
setQuery,
setLayout,
} ) {
const {
order,
orderBy,
Expand Down Expand Up @@ -139,6 +145,26 @@ export default function QueryInspectorControls( { query, setQuery } ) {
label={ __( 'Post Type' ) }
onChange={ onPostTypeChange }
/>
{ layout?.type === 'flex' && (
<>
<RangeControl
label={ __( 'Columns' ) }
value={ layout.columns }
onChange={ ( value ) =>
setLayout( { columns: value } )
}
min={ 2 }
max={ Math.max( 6, layout.columns ) }
/>
{ layout.columns > 6 && (
<Notice status="warning" isDismissible={ false }>
{ __(
'This column count exceeds the recommended amount and may cause visual breakage.'
) }
</Notice>
) }
</>
) }
<QueryControls
{ ...{ order, orderBy } }
onOrderChange={ ( value ) => setQuery( { order: value } ) }
Expand Down
Loading