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

try adding max columns to grid #60941

Closed
wants to merge 1 commit into from
Closed
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
72 changes: 57 additions & 15 deletions packages/block-editor/src/layouts/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,23 @@ export default {
layout={ layout }
onChange={ onChange }
/>
{ layout?.columnCount ? (
{ layout?.minimumColumnWidth ? (
<>
<GridLayoutMinimumWidthControl
layout={ layout }
onChange={ onChange }
/>
<GridLayoutMaxColumnsControl
layout={ layout }
onChange={ onChange }
/>
</>
) : (
<GridLayoutColumnsAndRowsControl
layout={ layout }
onChange={ onChange }
allowSizingOnChildren={ allowSizingOnChildren }
/>
) : (
<GridLayoutMinimumWidthControl
layout={ layout }
onChange={ onChange }
/>
) }
{ window.__experimentalEnableGridInteractivity && (
<GridVisualizer clientId={ clientId } />
Expand Down Expand Up @@ -123,8 +129,17 @@ export default {

let output = '';
const rules = [];

if ( columnCount ) {
if ( minimumColumnWidth ) {
const maxValue = !! columnCount
? `max(${ minimumColumnWidth }, ( 100% - (${
blockGapValue || '1.2rem'
}*${ ( columnCount || 4 ) - 1 }) ) / ${ columnCount || 4 })`
: `min(${ minimumColumnWidth }, 100%)`;
rules.push(
`grid-template-columns: repeat(auto-fill, minmax(${ maxValue }, 1fr))`,
`container-type: inline-size`
);
} else if ( columnCount ) {
rules.push(
`grid-template-columns: repeat(${ columnCount }, minmax(0, 1fr))`
);
Expand All @@ -133,11 +148,6 @@ export default {
`grid-template-rows: repeat(${ rowCount }, minmax(0, 1fr))`
);
}
} else if ( minimumColumnWidth ) {
rules.push(
`grid-template-columns: repeat(auto-fill, minmax(min(${ minimumColumnWidth }, 100%), 1fr))`,
`container-type: inline-size`
);
}

if ( rules.length ) {
Expand Down Expand Up @@ -238,6 +248,38 @@ function GridLayoutMinimumWidthControl( { layout, onChange } ) {
);
}

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

return (
<>
<fieldset>
<BaseControl.VisualLabel as="legend">
{ __( 'Max Columns' ) }
</BaseControl.VisualLabel>
<Flex gap={ 4 }>
<FlexItem isBlock>
<NumberControl
size={ '__unstable-large' }
onChange={ ( value ) => {
onChange( {
...layout,
columnCount: value,
} );
} }
value={ columnCount }
min={ 1 }
label={ __( 'Columns' ) }
hideLabelFromVision
/>
</FlexItem>
</Flex>
</fieldset>
</>
);
}

// Enables setting number of grid columns
function GridLayoutColumnsAndRowsControl( {
layout,
Expand Down Expand Up @@ -351,7 +393,7 @@ function GridLayoutTypeControl( { layout, onChange } ) {
minimumColumnWidth || '12rem'
);

const isManual = !! columnCount ? 'manual' : 'auto';
const isManual = !! minimumColumnWidth ? 'auto' : 'manual';
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Since this isn't a boolean, isManual feels a bit misleading. Maybe something like columnMode? Names are hard 😄


const onChangeType = ( value ) => {
if ( value === 'manual' ) {
Expand All @@ -361,7 +403,7 @@ function GridLayoutTypeControl( { layout, onChange } ) {
}
onChange( {
...layout,
columnCount: value === 'manual' ? tempColumnCount : null,
columnCount: tempColumnCount,
minimumColumnWidth:
value === 'auto' ? tempMinimumColumnWidth : null,
} );
Expand Down
Loading