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 a Row control to grid layout in manual mode. #60652

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
'selector' => $selector,
'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
);
if ( ! empty( $layout['rowCount'] ) ) {
$layout_styles[] = array(
'selector' => $selector,
'declarations' => array( 'grid-template-rows' => 'repeat(' . $layout['rowCount'] . ', minmax(0, 1fr))' ),
);
}
} else {
$minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';

Expand Down
150 changes: 104 additions & 46 deletions packages/block-editor/src/layouts/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export default {
layout = {},
onChange,
clientId,
layoutBlockSupport = {},
} ) {
const { allowSizingOnChildren = false } = layoutBlockSupport;
return (
<>
<GridLayoutTypeControl
Expand All @@ -80,6 +82,7 @@ export default {
<GridLayoutColumnsControl
layout={ layout }
onChange={ onChange }
allowSizingOnChildren={ allowSizingOnChildren }
/>
) : (
<GridLayoutMinimumWidthControl
Expand All @@ -104,7 +107,11 @@ export default {
hasBlockGapSupport,
layoutDefinitions = LAYOUT_DEFINITIONS,
} ) {
const { minimumColumnWidth = '12rem', columnCount = null } = layout;
const {
minimumColumnWidth = '12rem',
columnCount = null,
rowCount = null,
} = layout;

// If a block's block.json skips serialization for spacing or spacing.blockGap,
// don't apply the user-defined value to the styles.
Expand All @@ -121,6 +128,11 @@ export default {
rules.push(
`grid-template-columns: repeat(${ columnCount }, minmax(0, 1fr))`
);
if ( rowCount ) {
rules.push(
`grid-template-rows: repeat(${ rowCount }, minmax(0, 1fr))`
);
}
} else if ( minimumColumnWidth ) {
rules.push(
`grid-template-columns: repeat(auto-fill, minmax(min(${ minimumColumnWidth }, 100%), 1fr))`,
Expand Down Expand Up @@ -227,53 +239,99 @@ function GridLayoutMinimumWidthControl( { layout, onChange } ) {
}

// Enables setting number of grid columns
function GridLayoutColumnsControl( { layout, onChange } ) {
const { columnCount = 3 } = layout;
function GridLayoutColumnsControl( {
Copy link
Member

Choose a reason for hiding this comment

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

Can we rename this? It's no longer just columns.

layout,
onChange,
allowSizingOnChildren,
} ) {
const { columnCount = 3, rowCount } = layout;

return (
<fieldset>
<BaseControl.VisualLabel as="legend">
{ __( 'Columns' ) }
</BaseControl.VisualLabel>
<Flex gap={ 4 }>
<FlexItem isBlock>
<NumberControl
size={ '__unstable-large' }
onChange={ ( value ) => {
/**
* If the input is cleared, avoid switching
* back to "Auto" by setting a value of "1".
*/
const validValue = value !== '' ? value : '1';
onChange( {
...layout,
columnCount: validValue,
} );
} }
value={ columnCount }
min={ 1 }
label={ __( 'Columns' ) }
hideLabelFromVision
/>
</FlexItem>
<FlexItem isBlock>
<RangeControl
value={ parseInt( columnCount, 10 ) } // RangeControl can't deal with strings.
onChange={ ( value ) =>
onChange( {
...layout,
columnCount: value,
} )
}
min={ 1 }
max={ 16 }
withInputField={ false }
label={ __( 'Columns' ) }
hideLabelFromVision
/>
</FlexItem>
</Flex>
</fieldset>
<>
<fieldset>
<BaseControl.VisualLabel as="legend">
{ __( 'Columns' ) }
</BaseControl.VisualLabel>
<Flex gap={ 4 }>
<FlexItem isBlock>
<NumberControl
size={ '__unstable-large' }
onChange={ ( value ) => {
/**
* If the input is cleared, avoid switching
* back to "Auto" by setting a value of "1".
*/
const validValue = value !== '' ? value : '1';
onChange( {
...layout,
columnCount: validValue,
} );
} }
value={ columnCount }
min={ 1 }
label={ __( 'Columns' ) }
hideLabelFromVision
/>
</FlexItem>
<FlexItem isBlock>
<RangeControl
value={ parseInt( columnCount, 10 ) } // RangeControl can't deal with strings.
onChange={ ( value ) =>
onChange( {
...layout,
columnCount: value,
} )
}
min={ 1 }
max={ 16 }
withInputField={ false }
label={ __( 'Columns' ) }
hideLabelFromVision
/>
</FlexItem>
</Flex>
</fieldset>
{ allowSizingOnChildren && (
<fieldset>
<BaseControl.VisualLabel as="legend">
{ __( 'Rows' ) }
</BaseControl.VisualLabel>
<Flex gap={ 4 }>
<FlexItem isBlock>
<NumberControl
size={ '__unstable-large' }
onChange={ ( value ) => {
onChange( {
...layout,
rowCount: value,
} );
} }
value={ rowCount }
min={ 1 }
label={ __( 'Rows' ) }
hideLabelFromVision
/>
</FlexItem>
<FlexItem isBlock>
<RangeControl
value={ parseInt( rowCount, 10 ) } // RangeControl can't deal with strings.
onChange={ ( value ) =>
onChange( {
...layout,
rowCount: value,
} )
}
min={ 1 }
max={ 16 }
withInputField={ false }
label={ __( 'Rows' ) }
hideLabelFromVision
/>
</FlexItem>
</Flex>
</fieldset>
) }
</>
);
}

Expand Down
Loading