Skip to content

Commit

Permalink
Add gap support to gallery block
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Davies committed Jan 12, 2022
1 parent 3a99ec3 commit a691007
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Display multiple images in a rich gallery. ([Source](https://github.com/WordPres

- **Name:** core/gallery
- **Category:** media
- **Supports:** align, anchor
- **Supports:** align, anchor, spacing (blockGap), units (em, px, rem, vh, vw)
- **Attributes:** allowResize, caption, columns, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, shortCodeTransforms, sizeSlug

## Group
Expand Down
17 changes: 16 additions & 1 deletion packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,22 @@
},
"supports": {
"anchor": true,
"align": true
"align": true,
"units": [ "px", "em", "rem", "vh", "vw" ],
"spacing": {
"blockGap": true,
"__experimentalDefaultControls": {
"blockGap": true
}
},
"__experimentalLayout": {
"allowSwitching": false,
"allowInheriting": false,
"allowEditing": false,
"default": {
"type": "flex"
}
}
},
"editorStyle": "wp-block-gallery-editor",
"style": "wp-block-gallery"
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/gallery/gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ export const Gallery = ( props ) => {
blockProps,
} = props;

const { align, columns, caption, imageCrop } = attributes;
const {
align,
columns,
caption,
imageCrop,
style: {
spacing: { blockGap },
},
} = attributes;

const { children, ...innerBlocksProps } = useInnerBlocksProps( blockProps, {
allowedBlocks,
Expand Down Expand Up @@ -67,6 +75,7 @@ export const Gallery = ( props ) => {
'is-cropped': imageCrop,
}
) }
style={ { '--gallery-block--gutter-size': blockGap } }
>
{ children }

Expand Down
31 changes: 1 addition & 30 deletions packages/block-library/src/gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,10 @@

// Styles for current version of gallery block.
.wp-block-gallery.has-nested-images {
display: flex;
flex-wrap: wrap;
// Need bogus :not(#individual-image) to override long :not()
// 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 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% - (var(--gallery-block--gutter-size, #{$grid-unit-20}) / 2));

&:nth-of-type(even) {
margin-right: 0;
}
}

figure.wp-block-image {
Expand Down Expand Up @@ -125,39 +112,23 @@
}

&.columns-1 figure.wp-block-image:not(#individual-image) {
margin-right: 0;
width: 100%;
}

// Beyond mobile viewports, we allow up to 8 columns.
@include break-small {
@for $i from 3 through 8 {
&.columns-#{ $i } figure.wp-block-image:not(#individual-image) {
margin-right: var(--gallery-block--gutter-size, #{$grid-unit-20});
width: calc(#{math.div(100%, $i)} - (var(--gallery-block--gutter-size, #{$grid-unit-20}) * #{math.div($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: 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) {
margin-right: 0;
}
}
// If number of columns not explicitly set default to 3 columns if 3 or more images.
&.columns-default {
figure.wp-block-image:not(#individual-image) {
margin-right: var(--gallery-block--gutter-size, #{$grid-unit-20});

width: calc(33.33% - (var(--gallery-block--gutter-size, 16px) * #{math.div(2, 3)}));
}
figure.wp-block-image:not(#individual-image):nth-of-type(3n+3) {
margin-right: 0;
}
// If only 2 child images use 2 columns.
figure.wp-block-image:not(#individual-image):first-child:nth-last-child(2),
figure.wp-block-image:not(#individual-image):first-child:nth-last-child(2) ~ figure.wp-block-image:not(#individual-image) {
Expand Down

0 comments on commit a691007

Please sign in to comment.