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

Image: Add caption positioning #31814

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"align": {
"type": "string"
},
"alignCaption": {
"type": "string"
},
"url": {
"type": "string",
"source": "attribute",
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function ImageEdit( {
alt,
caption,
align,
alignCaption,
id,
width,
height,
Expand Down Expand Up @@ -279,6 +280,7 @@ export function ImageEdit( {
'is-transient': temporaryURL,
'is-resized': !! width || !! height,
[ `size-${ sizeSlug }` ]: sizeSlug,
[ `align-caption-${ alignCaption }` ]: alignCaption,
} );

const blockProps = useBlockProps( {
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { get, filter, map, last, pick, includes } from 'lodash';
import { isBlobURL } from '@wordpress/blob';
import {
ExternalLink,
Fill,
PanelBody,
ResizableBox,
Spinner,
Expand All @@ -19,6 +20,7 @@ import {
import { useViewportMatch, usePrevious } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import {
AlignmentControl,
BlockControls,
InspectorControls,
InspectorAdvancedControls,
Expand Down Expand Up @@ -68,6 +70,7 @@ export default function Image( {
alt,
caption,
align,
alignCaption,
id,
href,
rel,
Expand Down Expand Up @@ -394,6 +397,17 @@ export default function Image( {
</>
);

const captionControls = (
<Fill name="RichText.ToolbarControls.text-color">
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm using a reserved slot for text-color, but we should probably add a custom one for text-alignment.

<AlignmentControl
value={ alignCaption }
onChange={ ( newAlignCaption ) =>
setAttributes( { alignCaption: newAlignCaption } )
}
/>
</Fill>
);

const filename = getFilename( url );
let defaultedAlt;

Expand Down Expand Up @@ -557,6 +571,7 @@ export default function Image( {
which causes duplicated image upload. */ }
{ ! temporaryURL && controls }
{ img }
{ isSelected && captionControls }
Copy link
Member Author

Choose a reason for hiding this comment

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

The fill is only added when the image block is selected to avoid duplicated controls. When the "Styles" panel is open, or there are other images in the editor.

Probably there's a better way to solve this. 🙇

{ ( ! RichText.isEmpty( caption ) || isSelected ) && (
<RichText
ref={ captionRef }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function save( { attributes } ) {
alt,
caption,
align,
alignCaption,
href,
rel,
linkClass,
Expand All @@ -30,6 +31,7 @@ export default function save( { attributes } ) {

const classes = classnames( {
[ `align${ align }` ]: align,
[ `align-caption-${ alignCaption }` ]: alignCaption,
[ `size-${ sizeSlug }` ]: sizeSlug,
'is-resized': width || height,
} );
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/image/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
margin-right: auto;
}

&.align-caption-left > figcaption {
text-align: left;
}

&.align-caption-right > figcaption {
text-align: right;
}

&.align-caption-center > figcaption {
text-align: center;
}

// Supply caption styles to images, even if the theme hasn't opted in.
// Reason being: the new markup, <figcaptions>, are not likely to be styled in the majority of existing themes,
// so we supply the styles so as to not appear broken or unstyled in those themes.
Expand Down