Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ Display multiple images in a rich gallery.
- **Name:** [core/gallery](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-gallery/)
- **Category:** [media](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/)
- **Allowed Blocks:** core/image
- **Supports:** align, anchor, color (background, gradients, ~~text~~), interactivity (clientNavigation), layout (default, ~~allowEditing~~, ~~allowInheriting~~, ~~allowSwitching~~), listView, spacing (blockGap, margin, padding), units (em, px, rem, vh, vw), ~~html~~
- **Supports:** align, anchor, color (background, gradients, ~~text~~), interactivity (clientNavigation), layout (allowEditing, allowSizingOnChildren, default, ~~allowInheriting~~, ~~allowJustification~~, ~~allowOrientation~~, ~~allowSwitching~~, ~~allowVerticalAlignment~~, ~~allowWrap~~), listView, spacing (blockGap, margin, padding), units (em, px, rem, vh, vw), ~~html~~
- **Attributes:** allowResize, aspectRatio, caption, columns, dynamicContent, fixedHeight, ids, imageCrop, images, linkTarget, linkTo, navigationButtonType, randomOrder, shortCodeTransforms, sizeSlug

## Group
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
### Enhancements

- Columns: Add transforms between Columns and the Row variation that preserve column widths through flex child sizing controls.
- Gallery: Add an opt-in Grid layout while preserving the existing Flex layout for current galleries.([#81909](https://github.com/WordPress/gutenberg/pull/81909)).

### Bug Fixes

Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/gallery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ _Defined via the [`supports`](https://developer.wordpress.org/block-editor/refer
- [`layout`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout):
- [`allowSwitching`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowswitching): `false`
- [`allowInheriting`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowinheriting): `false`
- [`allowEditing`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowediting): `false`
- [`allowEditing`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowediting): `true`
- [`allowOrientation`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-alloworientation): `false`
- [`allowJustification`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowjustification): `false`
- [`allowVerticalAlignment`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowverticalalignment): `false`
- [`allowWrap`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowwrap): `false`
- [`allowSizingOnChildren`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowsizingonchildren): `true`
- [`default`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-default): `{"type":"flex"}`
- [`interactivity`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#interactivity):
- `clientNavigation`: `true`
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@
"layout": {
"allowSwitching": false,
"allowInheriting": false,
"allowEditing": false,
"allowEditing": true,
"allowOrientation": false,
"allowJustification": false,
"allowVerticalAlignment": false,
"allowWrap": false,
"allowSizingOnChildren": true,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like this addition adds sizing controls for child image blocks in the flex layout, too. From memory I think we're largely hiding image size controls when the image appears in a Gallery block, so I wasn't sure if it's intentional to expose these controls for flex layouts or just for Grid layouts?

In a flex layout for the Gallery block, Fit and Grow do basically the same thing, so at first the controls mightn't seem all that useful. But it gets really interesting if you start playing around with giving individual images in a flex Gallery some percentage based widths. I.e. in this one I've given two blocks in a 3 column gallery a width of 5% and the blocks seems to rearrange pleasingly around it:

Image

So, whether or not this was intentional for the flex variation, I think this might just be a pretty cool feature on its own 😄

Are there any drawbacks in us exposing it here? It also allows users to effectively set an Image block in the flex gallery to span to full width of the container:

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok so this was accidental but it might actually be a good thing 😄

It could even be a workaround for #41781, though a bit dodgy because you'd have to calculate approximate percentages for each item. But flex-basis does override the width set in those pesky default responsive styles.

I guess the only drawback is that grow doesn't do anything, and for practical purposes there doesn't seem to be much difference between fixed and max either. Currently we don't have a way of providing only some of the options for this control. Perhaps that would be useful?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps that would be useful?

It could be, but TBH so long as the default "Fit" and at least one of the other controls (in this case Max and Fixed) feel useful, I wouldn't worry about conditionally showing some of the child layout options here.

To the casual passer by, if you click on an inner Image block and go to the styles tab, you see a collapsed Width control that defaults to Fit, and you can select either Max or Fixed and play around with giving it a value, so to me this feels pretty good for now.

So long as we're happy with this feature (and funnily enough, I'm really liking this unexpected feature 😄!) I think we could go with this and see if anyone notices or finds it confusing that grow doesn't do anything and that Max and Fixed are effectively the same in this case.

What do you reckon?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm happy to leave it as it is for now, and we can try hiding the non-working controls later if they're obviously causing confusion. This enables some neat layouts and doesn't break anything, so no reason to remove it.

"default": {
"type": "flex"
}
Expand Down
117 changes: 92 additions & 25 deletions packages/block-library/src/gallery/edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
MediaReplaceFlow,
useSettings,
} from '@wordpress/block-editor';
import { useEffect, useMemo } from '@wordpress/element';
import { useEffect, useMemo, useRef } from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
Expand All @@ -37,7 +37,12 @@ import {
fullscreen,
} from '@wordpress/icons';
import { sharedIcon } from './shared-icon';
import { defaultColumnsNumber, pickRelevantMediaFiles } from './shared';
import {
defaultColumnsNumber,
isGalleryFlexLayout,
isObject,
pickRelevantMediaFiles,
} from './shared';
import { getHrefAndDestination } from './utils';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import {
Expand Down Expand Up @@ -158,17 +163,72 @@ export default function GalleryEdit( props ) {
linkTo,
sizeSlug,
aspectRatio,
layout,
} = attributes;
const isFlexLayout = isGalleryFlexLayout( layout );
const previousLayoutRef = useRef( layout );

const {
__unstableMarkNextChangeAsNotPersistent,
__unstableMarkLastChangeAsPersistent,
replaceInnerBlocks,
updateBlockAttributes,
selectBlock,
} = useDispatch( blockEditorStore );
const { createSuccessNotice, createErrorNotice } =
useDispatch( noticesStore );

useEffect( () => {
// Variations replace the whole layout object. Carry Grid-only settings
// across a layout type change so switching back restores them.
const previousLayout = previousLayoutRef.current;
previousLayoutRef.current = layout;

if (
Comment on lines +180 to +186

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just wondering how important it is for us to preserve the grid layout settings when a user switches between layouts. With the Group block, I think we toss everything out when we toggle through, so the user might be expecting to start from a fresh slate when they change layouts? It's also possible for a user to go back to a previous state via undo, so wasn't sure how much we need this logic.

(This part seems to be working well, though, so this is just a question and not a blocker!)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, my reasoning was we preserve settings when going the other way, because the flex layout has a custom columns attribute that doesn't get wiped, so it felt asymmetrical to wipe out the grid config. I guess we could instead wipe the columns on switch?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, my reasoning was we preserve settings when going the other way, because the flex layout has a custom columns attribute that doesn't get wiped, so it felt asymmetrical to wipe out the grid config.

Good point! I really don't mind here, it feels good it retaining the values, too, and extra values in the block's attributes don't cause any issues as far as I can tell.

One argument in favour of keeping the conversion you've got here is that conceptually switching between flex and grid for a Gallery block (to a user) isn't switching blocks at all, whereas we're really treating Row and Group as separate blocks in the UI.

So if you're happy with how it's working I'd say we can keep it, and always remove it in a follow-up if anyone finds it causes them friction when switching between layouts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I sort of like it because it feels lower stakes to try the other layout if you already configured your gallery.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same. It's grown on me! 😄

! isObject( previousLayout ) ||
! isObject( layout ) ||
previousLayout.type === layout.type
) {
return;
}

const nextLayout = { ...layout };
let hasPreservedGridSetting = false;

if (
! Object.hasOwn( layout, 'columnCount' ) &&
Number.isInteger( previousLayout.columnCount ) &&
previousLayout.columnCount > 0
) {
nextLayout.columnCount = previousLayout.columnCount;
hasPreservedGridSetting = true;
}

if (
! Object.hasOwn( layout, 'minimumColumnWidth' ) &&
typeof previousLayout.minimumColumnWidth === 'string'
) {
nextLayout.minimumColumnWidth = previousLayout.minimumColumnWidth;
hasPreservedGridSetting = true;
}

if ( hasPreservedGridSetting ) {
previousLayoutRef.current = nextLayout;
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { layout: nextLayout } );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I notice that undo doesn't really work correctly when toggling between the layouts. To repro toggle back and forth between regular and grid gallery a few times. Then undo the same number of times. When I try there's not the same number of undo steps.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I also notice that sometimes when I adjust layout settings, the post isn't being dirtied, not sure if it's related to these changes or something else (RTC)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Then undo the same number of times

Undo doesn't seem to do anything if you toggle between layouts a few times and return to whichever one was initially set. if you're on the other one, undo reverts to the initially set one and then does nothing else. Is that what you see?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok I pushed a fix for this. the non-dirty layout settings changes I couldn't repro, so maybe RTC related?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like it still might be a bit buggy here for the undo steps of the first time a user makes a switch between layouts? For me, if I insert a Gallery block, switch it to Grid and then back to Flex, without having made any other changes, then the undo step doesn't seem to be working correctly:

2026-08-28.11.38.00.mp4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another option if this useEffect is hard to get right, could be to leave this part of the logic to a follow-up, potentially 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This seems to be how undo behaviour works for all block attribute changes: if you just toggle something on and off, that doesn't create undo levels. To test, tried:

  • toggling the drop cap on a Paragraph on and off
  • toggling the a text color on a Heading on and off

In both cases, though the undo button becomes active, clicking it does nothing. I'm not sure if this is a bug or a feature 😅 but in any case I'll remove the previous fix so that Gallery is consistent with everything else in not creating undo levels for just toggling one thing on and off.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like a bug 😅

}

// Every switch between layout types only touches the `layout`
// attribute, so without this the next switch would be merged into
// this one instead of creating its own undo level.
__unstableMarkLastChangeAsPersistent();
}, [
layout,
setAttributes,
__unstableMarkNextChangeAsNotPersistent,
__unstableMarkLastChangeAsPersistent,
] );

const { getBlock, getSettings, innerBlockImages, multiGallerySelection } =
useSelect(
( select ) => {
Expand Down Expand Up @@ -627,9 +687,10 @@ export default function GalleryEdit( props ) {
'blocks-gallery-grid',
{
[ `align${ align }` ]: align,
[ `columns-${ columns }` ]: columns !== undefined,
'columns-default': columns === undefined,
'is-cropped': imageCrop,
[ `columns-${ columns }` ]:
isFlexLayout && columns !== undefined,
'columns-default': isFlexLayout && columns === undefined,
'is-cropped': isFlexLayout && imageCrop,
},
]
),
Expand Down Expand Up @@ -711,9 +772,11 @@ export default function GalleryEdit( props ) {
resetAll={ () => {
setAttributes( {
navigationButtonType: 'icon',
columns: undefined,
imageCrop: true,
randomOrder: false,
...( isFlexLayout && {
columns: undefined,
imageCrop: true,
} ),
} );

setAspectRatio( 'auto' );
Expand All @@ -728,7 +791,7 @@ export default function GalleryEdit( props ) {
} }
dropdownMenuProps={ dropdownMenuProps }
>
{ displayedImageCount > 1 && (
{ isFlexLayout && displayedImageCount > 1 && (
<ToolsPanelItem
isShownByDefault
label={ __( 'Columns' ) }
Expand Down Expand Up @@ -779,20 +842,22 @@ export default function GalleryEdit( props ) {
/>
</ToolsPanelItem>
) }
<ToolsPanelItem
isShownByDefault
label={ __( 'Crop images to fit' ) }
hasValue={ () => ! imageCrop }
onDeselect={ () =>
setAttributes( { imageCrop: true } )
}
>
<ToggleControl
{ isFlexLayout && (
<ToolsPanelItem
isShownByDefault
label={ __( 'Crop images to fit' ) }
checked={ !! imageCrop }
onChange={ toggleImageCrop }
/>
</ToolsPanelItem>
hasValue={ () => ! imageCrop }
onDeselect={ () =>
setAttributes( { imageCrop: true } )
}
>
<ToggleControl
label={ __( 'Crop images to fit' ) }
checked={ !! imageCrop }
onChange={ toggleImageCrop }
/>
</ToolsPanelItem>
) }
<ToolsPanelItem
isShownByDefault
label={ __( 'Randomize order' ) }
Expand Down Expand Up @@ -930,10 +995,12 @@ export default function GalleryEdit( props ) {
/>
</BlockControls>
) }
<GalleryGapCustomProperties
style={ attributes.style }
clientId={ clientId }
/>
{ isFlexLayout && (
<GalleryGapCustomProperties
style={ attributes.style }
clientId={ clientId }
/>
) }
</>
{ isDynamic ? (
<GalleryDynamicView
Expand Down
12 changes: 11 additions & 1 deletion packages/block-library/src/gallery/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Adding `figure` to the selector increases the specificity above the global
// styles specificity, which is levelled at 0-1-0. We should figure out why
// `figure` is needed.
:root :where(figure.wp-block-gallery) {
:root :where(figure.wp-block-gallery.is-layout-flex) {
// Override the default list style type _only in the editor_
// to avoid :not() selector specificity issues.
// See https://github.com/WordPress/gutenberg/pull/10358
Expand All @@ -16,7 +16,17 @@
> .blocks-gallery-caption {
flex: 0 0 100%;
}
}

:root :where(.wp-block-gallery.is-layout-grid) {
> .blocks-gallery-caption,
> .components-placeholder,
> .block-editor-media-placeholder {
grid-column: 1 / -1;
}
}

:root :where(figure.wp-block-gallery) {
// @todo this deserves a refactor, by being moved to the toolbar.
.block-editor-media-placeholder.is-appender {
.components-placeholder__label {
Expand Down
12 changes: 8 additions & 4 deletions packages/block-library/src/gallery/gallery.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import clsx from 'clsx';
import { __ } from '@wordpress/i18n';
import { Caption } from '../utils/caption';
import { isGalleryFlexLayout } from './shared';

export default function Gallery( props ) {
const {
Expand All @@ -14,7 +15,8 @@ export default function Gallery( props ) {
multiGallerySelection,
} = props;

const { align, columns, imageCrop } = attributes;
const { align, columns, imageCrop, layout } = attributes;
const isFlexLayout = isGalleryFlexLayout( layout );

return (
<figure
Expand All @@ -25,9 +27,11 @@ export default function Gallery( props ) {
'blocks-gallery-grid',
{
[ `align${ align }` ]: align,
[ `columns-${ columns }` ]: columns !== undefined,
[ `columns-default` ]: columns === undefined,
'is-cropped': imageCrop,
[ `columns-${ columns }` ]:
isFlexLayout && columns !== undefined,
[ `columns-default` ]:
isFlexLayout && columns === undefined,
'is-cropped': isFlexLayout && imageCrop,
}
) }
>
Expand Down
Loading
Loading