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

Gallery block: add gap support #34608

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
8 changes: 7 additions & 1 deletion packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@
},
"supports": {
"anchor": true,
"align": true
"align": true,
"spacing": {
"blockGap": true,
"__experimentalDefaultControls": {
"blockGap": true
}
}
},
"editorStyle": "wp-block-gallery-editor",
"style": "wp-block-gallery"
Expand Down
17 changes: 9 additions & 8 deletions packages/block-library/src/gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@

// Styles for current version of gallery block.
.wp-block-gallery.has-nested-images {
--gallery-block--gutter-size: var(--wp--style--block-gap, $grid-unit-20);
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;
margin: 0 var(--gallery-block--gutter-size) var(--gallery-block--gutter-size) 0;

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

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

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

figcaption {
bottom: var(--gallery-block--gutter-size, #{$grid-unit-20});
bottom: var(--gallery-block--gutter-size);
}
}
}
Expand Down Expand Up @@ -129,14 +130,14 @@
@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)}));
margin-right: var(--gallery-block--gutter-size);
width: calc(#{math.div(100%, $i)} - (var(--gallery-block--gutter-size) * #{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});
margin-right: var(--gallery-block--gutter-size);
}
}
// Unset the right margin on every rightmost gallery item to ensure center balance.
Expand All @@ -148,7 +149,7 @@
// 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});
margin-right: var(--gallery-block--gutter-size);
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) {
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/gallery/v1/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ function GalleryEdit( props ) {
blockEditorStore
);

// Remove the tools panel for v1 Gallery so we only have to support
// any new dimension settings, etc. for the new gallery format.
const toolsPanel = document.getElementsByClassName(
Copy link
Contributor

Choose a reason for hiding this comment

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

The CSS rule is working nicely to hide the ToolsPanel, and I like it as a pragmatic workaround for not being able to conditionally update the block's supports settings / alter whether or not the panel is displayed.

This might be overkill (and I'm maybe being overly cautious), but I was wondering if it's worth making the check a little more specific to the block inspector? What do you think about something like the following:

const toolsPanel = document.querySelector( '.block-editor-block-inspector .components-tools-panel' );

Copy link
Contributor

Choose a reason for hiding this comment

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

Another case that we probably can't really do much about is whether or not the gap control is displayed in global styles for this block 🤔

'components-tools-panel'
)[ 0 ];

useEffect( () => {
if ( toolsPanel && isSelected ) {
toolsPanel.style.display = 'none';
}
}, [ toolsPanel ] );

const {
imageSizes,
mediaUpload,
Expand Down