Skip to content

Commit 87ca863

Browse files
committed
Image Block: Hide inline caption in editor when gallery toggle is off
Addresses testing feedback that `Show in gallery: off` only affected the published front end and left the caption visible in the block editor. - `image.js` now reads `showCaptionInGallery` from the provided block context and skips rendering the `<Caption>` component when it is explicitly false. - Added `"default": true` to the `showCaptionInGallery` and `showCaptionInLightbox` attributes in `gallery/block.json` so the context value is a stable boolean; otherwise the serialized block context would have coerced the missing attribute to `false`, which would have hidden the caption for every gallery in the editor.
1 parent 161d6ff commit 87ca863

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

packages/block-library/src/gallery/block.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@
115115
"default": "auto"
116116
},
117117
"showCaptionInGallery": {
118-
"type": "boolean"
118+
"type": "boolean",
119+
"default": true
119120
},
120121
"showCaptionInLightbox": {
121-
"type": "boolean"
122+
"type": "boolean",
123+
"default": true
122124
}
123125
},
124126
"providesContext": {

packages/block-library/src/image/image.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export default function Image( {
311311
setOffsetTop( imageElement?.offsetTop ?? 0 );
312312
}, [ imageElement ] );
313313
const setRefs = useMergeRefs( [ setImageElement, setResizeObserved ] );
314-
const { allowResize = true } = context;
314+
const { allowResize = true, showCaptionInGallery } = context;
315315

316316
const { image, canUserEdit, attachmentResolutionError } = useSelect(
317317
( select ) => {
@@ -1333,18 +1333,20 @@ export default function Image( {
13331333
{ img }
13341334
{ resizableBox }
13351335

1336-
<Caption
1337-
attributes={ attributes }
1338-
setAttributes={ setAttributes }
1339-
isSelected={ isSingleSelected }
1340-
insertBlocksAfter={ insertBlocksAfter }
1341-
label={ __( 'Image caption text' ) }
1342-
showToolbarButton={
1343-
isSingleSelected &&
1344-
( hasNonContentControls || isContentOnlyMode ) &&
1345-
! hideCaptionControls
1346-
}
1347-
/>
1336+
{ showCaptionInGallery !== false && (
1337+
<Caption
1338+
attributes={ attributes }
1339+
setAttributes={ setAttributes }
1340+
isSelected={ isSingleSelected }
1341+
insertBlocksAfter={ insertBlocksAfter }
1342+
label={ __( 'Image caption text' ) }
1343+
showToolbarButton={
1344+
isSingleSelected &&
1345+
( hasNonContentControls || isContentOnlyMode ) &&
1346+
! hideCaptionControls
1347+
}
1348+
/>
1349+
) }
13481350
</>
13491351
);
13501352
}

0 commit comments

Comments
 (0)