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

Refactored Gallery: Add custom gutter sizing #28377

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
3 changes: 3 additions & 0 deletions packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"imageCount": {
"type": "number",
"default": 0
},
"gutterSize": {
"type": "number"
}
},
"providesContext": {
Expand Down
30 changes: 26 additions & 4 deletions packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const MOBILE_CONTROL_PROPS_RANGE_CONTROL = Platform.select( {
native: { type: 'stepper' },
} );

const DEFAULT_GUTTER_SIZE = 16;
const MAX_GUTTER_SIZE = 100;
const MIN_GUTTER_SIZE = 0;

function GalleryEdit( props ) {
const {
setAttributes,
Expand All @@ -79,14 +83,15 @@ function GalleryEdit( props ) {
} = props;

const {
columns = defaultColumnsNumber( imageCount ),
gutterSize,
imageCount,
imageCrop,
imageUploads,
linkTarget,
linkTo,
columns = defaultColumnsNumber( imageCount ),
sizeSlug,
imageUploads,
shortCodeTransforms,
imageCrop,
sizeSlug,
} = attributes;

const {
Expand Down Expand Up @@ -338,6 +343,10 @@ function GalleryEdit( props ) {

const blockProps = useBlockProps( {
className: classnames( className, 'has-nested-images' ),
style: {
'--gallery-block--gutter-size':
gutterSize !== undefined && `${ gutterSize }px`,
},
} );

if ( ! hasImages ) {
Expand All @@ -362,6 +371,19 @@ function GalleryEdit( props ) {
required
/>
) }
<RangeControl
label={ __( 'Gutter size' ) }
value={ gutterSize }
onChange={ ( newGutterSize ) =>
setAttributes( { gutterSize: newGutterSize } )
}
initialPosition={ DEFAULT_GUTTER_SIZE }
min={ MIN_GUTTER_SIZE }
max={ MAX_GUTTER_SIZE }
{ ...MOBILE_CONTROL_PROPS_RANGE_CONTROL }
resetFallbackValue={ DEFAULT_GUTTER_SIZE }
allowReset
/>
<ToggleControl
label={ __( 'Crop images' ) }
checked={ !! imageCrop }
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/gallery/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,24 @@ import { defaultColumnsNumber } from './shared';

export default function save( { attributes } ) {
const {
imageCount,
caption,
columns = defaultColumnsNumber( imageCount ),
gutterSize,
imageCount,
imageCrop,
caption,
} = attributes;

const className = `blocks-gallery-grid has-nested-images columns-${ columns } ${
imageCrop ? 'is-cropped' : ''
}`;

const style = {
'--gallery-block--gutter-size':
gutterSize !== undefined && `${ gutterSize }px`,
};

return (
<figure { ...useBlockProps.save( { className } ) }>
<figure { ...useBlockProps.save( { className, style } ) }>
<InnerBlocks.Content />
{ ! RichText.isEmpty( caption ) && (
<RichText.Content
Expand Down
16 changes: 8 additions & 8 deletions packages/block-library/src/gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// specificity chain on default image block on front end.
figure.wp-block-image:not(#individual-image) {
// Add space between thumbnails, and unset right most thumbnails later.
margin: 0 $grid-unit-20 $grid-unit-20 0;
margin: 0 var(--gallery-block--gutter-size, #{$grid-unit-20}) var(--gallery-block--gutter-size, #{$grid-unit-20}) 0;

&:last-of-type:not(#individual-image) {
margin-right: 0;
}

width: calc(50% - #{$grid-unit-20});
width: calc(50% - var(--gallery-block--gutter-size, #{$grid-unit-20}));

&:nth-of-type(even) {
margin-right: 0;
Expand Down Expand Up @@ -112,11 +112,11 @@
margin-top: auto;
margin-bottom: auto;
img {
margin-bottom: $grid-unit-20;
margin-bottom: var(--gallery-block--gutter-size, #{$grid-unit-20});
}

figcaption {
bottom: $grid-unit-20;
bottom: var(--gallery-block--gutter-size, #{$grid-unit-20});
}
}
}
Expand Down Expand Up @@ -155,19 +155,19 @@
@include break-small {
@for $i from 3 through 8 {
&.columns-#{ $i } figure.wp-block-image:not(#individual-image) {
margin-right: $grid-unit-20;
width: calc(#{100% / $i} - #{$grid-unit-20 * ( $i - 1 ) / $i});
margin-right: var(--gallery-block--gutter-size, #{$grid-unit-20});
width: calc(#{100% / $i} - calc(var(--gallery-block--gutter-size, #{$grid-unit-20}) * calc(#{$i - 1}) / #{$i}));
}

// Prevent collapsing margin while sibling is being dragged.
&.columns-#{$i} figure.wp-block-image:not(#individual-image).is-dragging ~ figure.wp-block-image:not(#individual-image) {
margin-right: $grid-unit-20;
margin-right: var(--gallery-block--gutter-size, #{$grid-unit-20});
}
}

// Unset the right margin on every rightmost gallery item to ensure center balance.
@for $column-count from 1 through 8 {
&.columns-#{$column-count} figure.wp-block-image:not(#individual-image):nth-of-type( #{ $column-count }n ) {
&.columns-#{$column-count} figure.wp-block-image:not(#individual-image):nth-of-type(#{ $column-count }n) {
margin-right: 0;
}
}
Expand Down