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 border block support for color, width, and style #31366

Merged
merged 21 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
23b36e4
Add border color, style and width support to image block
aaronrobertshaw Apr 30, 2021
3214b95
Pass image border props to image cropper
aaronrobertshaw Apr 30, 2021
0761a4e
Provide baseline border styles for improved visual feedback
aaronrobertshaw Aug 9, 2021
7221041
Remove unneeded style now block support applied to img
aaronrobertshaw Aug 31, 2021
1b92e84
Adjust caption styles for images in a gallery
aaronrobertshaw Nov 4, 2021
de4b702
Fix typo
aaronrobertshaw Dec 14, 2021
a738de9
Remove unneeded fallback border styles
aaronrobertshaw Dec 14, 2021
35143eb
Try improving application of custom border styles
aaronrobertshaw Apr 1, 2022
0f519b9
Remove unneeded resizablebox style
aaronrobertshaw Apr 4, 2022
678d024
Tweak application of border-box sizing
aaronrobertshaw Apr 4, 2022
ddb227f
Add deprecation for moving border radius and new classname
aaronrobertshaw Apr 30, 2021
c888bf5
Remove border radius from align wrapper deprecation fixture
aaronrobertshaw May 4, 2022
105edd2
Limit the damage done by theme.json and global styles borders
aaronrobertshaw May 24, 2022
bf70f60
Add img to Elements API and leverage for image block
aaronrobertshaw May 25, 2022
6195058
Add img element to api constants
aaronrobertshaw Jun 6, 2022
c145112
Disable border UI in Global Styles when skipping serialization of sup…
aaronrobertshaw Jun 6, 2022
ba1daad
Revert "Disable border UI in Global Styles when skipping serializatio…
aaronrobertshaw Jun 7, 2022
386c479
Revert "Add img to Elements API and leverage for image block"
aaronrobertshaw Jun 7, 2022
1b0d8c0
Update image block to leverage feature level selectors for border styles
aaronrobertshaw Jun 28, 2022
c189f98
Fix rebase issue after revert of img element
aaronrobertshaw Jul 1, 2022
24f4d0e
Override clearing of img borders in wp-admin by core
aaronrobertshaw Jul 5, 2022
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
12 changes: 9 additions & 3 deletions packages/block-editor/src/components/image-editor/cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function ImageCropper( {
clientWidth,
naturalHeight,
naturalWidth,
borderProps,
} ) {
const {
isInProgress,
Expand All @@ -44,10 +45,15 @@ export default function ImageCropper( {

return (
<div
className={ classnames( 'wp-block-image__crop-area', {
'is-applying': isInProgress,
} ) }
className={ classnames(
'wp-block-image__crop-area',
borderProps?.className,
{
'is-applying': isInProgress,
}
) }
style={ {
...borderProps?.style,
width: width || clientWidth,
height: editedHeight,
} }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/image-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export default function ImageEditor( {
clientWidth,
naturalHeight,
naturalWidth,
borderProps,
} ) {
return (
<>
<Cropper
borderProps={ borderProps }
url={ url }
width={ width }
height={ height }
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ figure.wp-block-gallery.has-nested-images {
position: relative;
flex-direction: column;
max-width: 100%;
// Prevents theme.json and global styles borders that apply to the outer
// wrapper from incorrectly enlarging gallery images to a point they
// result in a single column gallery.
box-sizing: border-box;

> div,
> a {
Expand Down Expand Up @@ -63,7 +67,12 @@ figure.wp-block-gallery.has-nested-images {
}
}

&.is-style-rounded {
&.has-custom-border img {
box-sizing: border-box;
}

&.is-style-rounded,
&.has-custom-border {
> div,
> a {
flex: 1 1 auto;
Expand Down
8 changes: 7 additions & 1 deletion packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@
"background": false
},
"__experimentalBorder": {
"color": true,
"radius": true,
"width": true,
"__experimentalSelector": "img, .wp-block-image__crop-area",
Copy link
Member

Choose a reason for hiding this comment

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

🍺

"__experimentalSkipSerialization": true,
"__experimentalDefaultControls": {
"radius": true
"color": true,
"radius": true,
"width": true
}
},
"__experimentalStyle": {
Expand Down
86 changes: 86 additions & 0 deletions packages/block-library/src/image/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const blockAttributes = {
source: 'html',
selector: 'figcaption',
},
title: {
type: 'string',
source: 'attribute',
selector: 'img',
attribute: 'title',
},
href: {
type: 'string',
source: 'attribute',
Expand Down Expand Up @@ -58,6 +64,9 @@ const blockAttributes = {
height: {
type: 'number',
},
sizeSlug: {
type: 'string',
},
linkDestination: {
type: 'string',
},
Expand Down Expand Up @@ -85,6 +94,83 @@ const blockSupports = {
};

const deprecated = [
// The following deprecation moves existing border radius styles onto the
// inner img element where new border block support styles must be applied.
// It will also add a new `.has-custom-border` class for existing blocks
// with border radii set. This class is required to improve caption position
// and styling when an image within a gallery has a custom border or
// rounded corners.
//
// See: https://github.com/WordPress/gutenberg/pull/31366/
{
attributes: blockAttributes,
supports: blockSupports,
save( { attributes } ) {
const {
url,
alt,
caption,
align,
href,
rel,
linkClass,
width,
height,
id,
linkTarget,
sizeSlug,
title,
} = attributes;

const newRel = isEmpty( rel ) ? undefined : rel;

const classes = classnames( {
[ `align${ align }` ]: align,
[ `size-${ sizeSlug }` ]: sizeSlug,
'is-resized': width || height,
} );

const image = (
<img
src={ url }
alt={ alt }
className={ id ? `wp-image-${ id }` : null }
width={ width }
height={ height }
title={ title }
/>
);

const figure = (
<>
{ href ? (
<a
className={ linkClass }
href={ href }
target={ linkTarget }
rel={ newRel }
>
{ image }
</a>
) : (
image
) }
{ ! RichText.isEmpty( caption ) && (
<RichText.Content
tagName="figcaption"
value={ caption }
/>
) }
</>
);

return (
<figure { ...useBlockProps.save( { className: classes } ) }>
{ figure }
</figure>
);
},
},
{
attributes: {
...blockAttributes,
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { get, has, omit, pick } from 'lodash';
import { get, has, isEmpty, omit, pick } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -17,6 +17,7 @@ import {
MediaPlaceholder,
useBlockProps,
store as blockEditorStore,
__experimentalUseBorderProps as useBorderProps,
} from '@wordpress/block-editor';
import { useEffect, useRef, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -296,10 +297,14 @@ export function ImageEdit( {
/>
);

const borderProps = useBorderProps( attributes );

const classes = classnames( className, {
'is-transient': temporaryURL,
'is-resized': !! width || !! height,
[ `size-${ sizeSlug }` ]: sizeSlug,
'has-custom-border':
!! borderProps.className || ! isEmpty( borderProps.style ),
} );

const blockProps = useBlockProps( {
Expand Down
13 changes: 9 additions & 4 deletions packages/block-library/src/image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ figure.wp-block-image:not(.wp-block) {
margin-top: -9px;
margin-left: -9px;
}

&:not(.is-style-rounded) > div:not(.components-placeholder) {
border-radius: inherit;
}
}

// This is necessary for the editor resize handles to accurately work on a non-floated, non-resized, small image.
Expand Down Expand Up @@ -94,6 +90,15 @@ figure.wp-block-image:not(.wp-block) {
position: relative;
max-width: 100%;
width: 100%;
overflow: hidden;

// This removes the border from the img within the image cropper so it
// can be applied to the cropper itself. This then allows the image to be
// cropped within the visual border providing more accurate editing and
// smoother UX.
.reactEasyCrop_Container .reactEasyCrop_Image {
border: none;
}
}

.wp-block-image__crop-icon {
Expand Down
42 changes: 26 additions & 16 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, filter, map, pick, includes } from 'lodash';
import { get, filter, isEmpty, map, pick, includes } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -30,6 +30,7 @@ import {
__experimentalImageEditor as ImageEditor,
__experimentalImageEditingProvider as ImageEditingProvider,
__experimentalGetElementClassName,
__experimentalUseBorderProps as useBorderProps,
} from '@wordpress/block-editor';
import { useEffect, useMemo, useState, useRef } from '@wordpress/element';
import { __, sprintf, isRTL } from '@wordpress/i18n';
Expand Down Expand Up @@ -57,7 +58,19 @@ import { MIN_SIZE, ALLOWED_MEDIA_TYPES } from './constants';

export default function Image( {
temporaryURL,
attributes: {
attributes,
setAttributes,
isSelected,
insertBlocksAfter,
onReplace,
onSelectImage,
onSelectURL,
onUploadError,
containerRef,
context,
clientId,
} ) {
const {
url = '',
alt,
caption,
Expand All @@ -72,18 +85,7 @@ export default function Image( {
height,
linkTarget,
sizeSlug,
},
setAttributes,
isSelected,
insertBlocksAfter,
onReplace,
onSelectImage,
onSelectURL,
onUploadError,
containerRef,
context,
clientId,
} ) {
} = attributes;
const imageRef = useRef();
const captionRef = useRef();
const prevUrl = usePrevious( url );
Expand Down Expand Up @@ -186,7 +188,7 @@ export default function Image( {

// Get naturalWidth and naturalHeight from image ref, and fall back to loaded natural
// width and height. This resolves an issue in Safari where the loaded natural
// witdth and height is otherwise lost when switching between alignments.
// width and height is otherwise lost when switching between alignments.
// See: https://github.com/WordPress/gutenberg/pull/37210.
const { naturalWidth, naturalHeight } = useMemo( () => {
return {
Expand Down Expand Up @@ -429,6 +431,11 @@ export default function Image( {
defaultedAlt = __( 'This image has an empty alt attribute' );
}

const borderProps = useBorderProps( attributes );
const isRounded = attributes.className?.includes( 'is-style-rounded' );
const hasCustomBorder =
!! borderProps.className || ! isEmpty( borderProps.style );

let img = (
// Disable reason: Image itself is not meant to be interactive, but
// should direct focus to block.
Expand All @@ -445,6 +452,8 @@ export default function Image( {
} );
} }
ref={ imageRef }
className={ borderProps.className }
style={ borderProps.style }
/>
{ temporaryURL && <Spinner /> }
</>
Expand All @@ -466,6 +475,7 @@ export default function Image( {
if ( canEditImage && isEditingImage ) {
img = (
<ImageEditor
borderProps={ isRounded ? undefined : borderProps }
url={ url }
width={ width }
height={ height }
Expand Down Expand Up @@ -530,7 +540,7 @@ export default function Image( {
<ResizableBox
size={ {
width: width ?? 'auto',
height: height ?? 'auto',
height: height && ! hasCustomBorder ? height : 'auto',
} }
showHandle={ isSelected }
minWidth={ minWidth }
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
RichText,
useBlockProps,
__experimentalGetElementClassName,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
} from '@wordpress/block-editor';

export default function save( { attributes } ) {
Expand All @@ -31,18 +32,26 @@ export default function save( { attributes } ) {
} = attributes;

const newRel = isEmpty( rel ) ? undefined : rel;
const borderProps = getBorderClassesAndStyles( attributes );

const classes = classnames( {
[ `align${ align }` ]: align,
[ `size-${ sizeSlug }` ]: sizeSlug,
'is-resized': width || height,
'has-custom-border':
!! borderProps.className || ! isEmpty( borderProps.style ),
Comment on lines +41 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This new has-custom-border class was required to be able to style image captions appropriately when in a Gallery block given border support now needs to be applied directly on the internal img element.

} );

const imageClasses = classnames( borderProps.className, {
[ `wp-image-${ id }` ]: !! id,
} );

const image = (
<img
src={ url }
alt={ alt }
className={ id ? `wp-image-${ id }` : null }
className={ imageClasses || undefined }
style={ borderProps.style }
width={ width }
height={ height }
title={ title }
Expand Down
Loading