Repository navigation
Try adding a Grid block variation to Gallery #81909
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
Changes from 12 commits
0de5034
7729f17
4148865
a52dc37
902df2e
77843ee
c51cc03
cad60ce
75089d9
51ae216
651d994
1d1c38b
e00b35d
d1d7834
74dc68d
435d5a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
|
@@ -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 { | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option if this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) => { | ||
|
|
@@ -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, | ||
| }, | ||
| ] | ||
| ), | ||
|
|
@@ -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' ); | ||
|
|
@@ -728,7 +791,7 @@ export default function GalleryEdit( props ) { | |
| } } | ||
| dropdownMenuProps={ dropdownMenuProps } | ||
| > | ||
| { displayedImageCount > 1 && ( | ||
| { isFlexLayout && displayedImageCount > 1 && ( | ||
| <ToolsPanelItem | ||
| isShownByDefault | ||
| label={ __( 'Columns' ) } | ||
|
|
@@ -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' ) } | ||
|
|
@@ -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 | ||
|
|
||
There was a problem hiding this comment.
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:
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:
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
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.