-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Try adding a grid layout type #49018
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
Changes from 1 commit
7c3ec88
439499e
afa4124
99a52f5
b338b73
7bb10f9
52bfcc7
04778c8
f85a8b5
470fcf3
099db1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1341,8 +1341,7 @@ protected function get_layout_styles( $block_metadata ) { | |
| $base_style_rules = _wp_array_get( $layout_definition, array( 'baseStyles' ), array() ); | ||
|
|
||
| if ( | ||
| ! empty( $class_name ) && | ||
| ! empty( $base_style_rules ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this change is to allow the block of code to be reachable if |
||
| ! empty( $class_name ) | ||
| ) { | ||
| // Output display mode. This requires special handling as `display` is not exposed in `safe_style_css_filter`. | ||
| if ( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -352,6 +352,21 @@ | |
| } | ||
| } | ||
| ] | ||
| }, | ||
| "grid": { | ||
| "name": "grid", | ||
| "slug": "grid", | ||
| "className": "is-layout-grid", | ||
| "displayMode": "grid", | ||
| "baseStyles": [], | ||
|
||
| "spacingStyles": [ | ||
| { | ||
| "selector": "", | ||
| "rules": { | ||
| "gap": null | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
|
|
||
| import { | ||
| Flex, | ||
| FlexItem, | ||
| __experimentalUnitControl as UnitControl, | ||
| } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { appendSelectors, getBlockGapCSS } from './utils'; | ||
| import { getGapCSSValue } from '../hooks/gap'; | ||
| import { shouldSkipSerialization } from '../hooks/utils'; | ||
|
|
||
| export default { | ||
| name: 'grid', | ||
| label: __( 'Grid' ), | ||
| inspectorControls: function GridLayoutInspectorControls( { | ||
| layout = {}, | ||
| onChange, | ||
| } ) { | ||
| return ( | ||
| <> | ||
| <Flex> | ||
| <FlexItem> | ||
| <GridLayoutMinimumWidthControl | ||
| layout={ layout } | ||
| onChange={ onChange } | ||
| /> | ||
| </FlexItem> | ||
| </Flex> | ||
| </> | ||
| ); | ||
| }, | ||
| toolBarControls: function DefaultLayoutToolbarControls() { | ||
| return null; | ||
| }, | ||
| getLayoutStyle: function getLayoutStyle( { | ||
| selector, | ||
| layout, | ||
| style, | ||
| blockName, | ||
| hasBlockGapSupport, | ||
| layoutDefinitions, | ||
| } ) { | ||
| const { minimumColumnWidth = '12rem' } = layout; | ||
|
|
||
| // If a block's block.json skips serialization for spacing or spacing.blockGap, | ||
| // don't apply the user-defined value to the styles. | ||
| const blockGapValue = | ||
| style?.spacing?.blockGap && | ||
| ! shouldSkipSerialization( blockName, 'spacing', 'blockGap' ) | ||
| ? getGapCSSValue( style?.spacing?.blockGap, '0.5em' ) | ||
| : undefined; | ||
|
|
||
| let output = ''; | ||
| const rules = []; | ||
|
|
||
| if ( minimumColumnWidth ) { | ||
| rules.push( | ||
| `grid-template-columns: repeat(auto-fill, minmax(${ minimumColumnWidth }, 1fr))` | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just playing around with this again, and I noticed that if we set a minimum column width that is greater than the viewport width, that the children will overflow the container: Is it worth trying to add a guardrail to the rule so that the minimum value can never be more than 100%? Would it be possible to nest
That seems to work for me locally:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooooh good catch! I'll have a play with it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It worked, thanks!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yay, glad it was a simple fix! |
||
| } | ||
|
|
||
| if ( rules.length ) { | ||
| output = `${ appendSelectors( selector ) } { | ||
| ${ rules.join( '; ' ) }; | ||
| }`; | ||
| } | ||
|
|
||
| // Output blockGap styles based on rules contained in layout definitions in theme.json. | ||
| if ( hasBlockGapSupport && blockGapValue ) { | ||
| output += getBlockGapCSS( | ||
| selector, | ||
| layoutDefinitions, | ||
| 'grid', | ||
| blockGapValue | ||
| ); | ||
| } | ||
| return output; | ||
| }, | ||
| getOrientation() { | ||
| return null; | ||
| }, | ||
| getAlignments() { | ||
| return []; | ||
| }, | ||
| }; | ||
|
|
||
| // Enables setting minimum width of grid items. | ||
| function GridLayoutMinimumWidthControl( { layout, onChange } ) { | ||
| const { minimumColumnWidth = '20rem' } = layout; | ||
|
|
||
| return ( | ||
| <UnitControl | ||
| size={ '__unstable-large' } | ||
|
||
| onChange={ ( value ) => { | ||
| onChange( { | ||
| ...layout, | ||
| minimumColumnWidth: value, | ||
| } ); | ||
| } } | ||
| value={ minimumColumnWidth } | ||
| /> | ||
| ); | ||
| } | ||



There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a future proofing question: if
minimumColumnWidthisn't set, this assumes a default of12rem. If we want to enable the control over individual columns again in follow-ups, how will we tell that we shouldn't be outputting12rem?By which I mean, what are the trade-offs between using
12remas an inferred value, versus explicitly defining12remwithin the Grid variation? For example, what if other blocks wished to use a different default minimum column width?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be pretty easy to introduce something like
isResponsiveagain, or any other property that indicates that the grid type is not based on column width (which would always be the default). We'll need a check before outputting the value because the whole template-columns declaration will be different for a static grid, and even if aminimumColumnWidthis set (e.g. if we don't reset it on switching to another grid type) we will only use the value if the grid is of the responsive type (or whatever we want to call it).I chose 12rem arbitrarily, it could be any other value but we do need to have some default value here or the
auto-fillwon't work.If other blocks want to use a different value they can set it when they define the layout type, e.g.
layout: { type: 'grid', minimumColumnWidth: '9rem' }.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So (just to make sure I'm following), we're essentially saying, a grid layout with absolutely no other values will default to
12remminimum column width. For future changes, we're adding additional attributes that will be set, so it'll be easy to determine when to switch off the current logic. And for changing the12remdefault on a block-by-block basis, we provide the default type type value as you mention.Sounds solid to me! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the idea is to have a sensible default so you can just add a grid and it'll look ok for the majority of cases? Then progressively add configurability.