-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Try fixing captions on resized images #6496
Conversation
This is an alternative, and probably better approach, than the one explored in #6304. Both branches try and fix the same issue — ensuring that the caption inside a `figure` is only ever as wide as the adjecent image. This is only ah issue when that image is resized. This branch fixes that, in a fairly clean way. See the details in https://css-tricks.com/almanac/properties/w/width/
@@ -165,6 +170,10 @@ export const settings = { | |||
save( { attributes } ) { | |||
const { url, alt, caption, align, href, width, height, id } = attributes; | |||
|
|||
const classes = classnames( align ? `align${ align }` : null, { | |||
'is-resized': !! width || !! height, |
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.
Just curious, could we remove the need for this class with the following?
img[height],
img[width] {
...
}
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.
And if caption needs targeting.
img[height] + figcaption,
img[width] + figcaption {
...
}
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.
Sadly, we cannot, because width: min-content
has to be applied to the parent container, in this case the figure
. I suppose we could it as a data-is-resized attribute instead, and target that, if we want to get rid of classes. What do you think?
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.
This change needs a deprecated
version though :)
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 this is why I was wondering if we could avoid introducing new attributes, but ok if really needed! :)
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.
Added a deprecated version in 68b40e5. Did I do that right?
To clarify what happens — if the figure is as wide as the main content, it needs fit-content
. If the figure is smaller than the main content, it needs min-content
so that a long caption doesn't make the figure wider than its smallest element (which will always be the image).
Should the class be called "is-resized" for that, or can we think of something better?
Apparently, |
Bummer about that browser support. However I may have found a solution. This codepen works for me in all the good browsers, and also Edge and IE: https://codepen.io/joen/pen/pVevQo?editors=1100 Key is that the image height is set explicitly, or it'll look weird. But it seems like this should work for us? Haven't tried floating it yet, but it should work. Screenshots: |
core-blocks/image/style.scss
Outdated
display: -ms-inline-grid; | ||
-ms-grid-columns: min-content; | ||
|
||
> :nth-child(2) { |
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.
What does this target?
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.
I'm actually not 100% sure, it's part of the edge trick. But I'm wondering if maybe it's supposed to target the figcaption, in which case that would be better to write. I'll do some testing, see if we can get away with something cleaner.
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.
Great catch — this was supposed to target everything after the first image, but since there's only ever a figcaption, it's much cleaner to do just that, and the inline-block was unnecessary also.
Visually 👍 on this. I think it's a good fix for this issue. |
@jasmussen Code-wise, this still need to be done as the HTML output is different: https://github.com/WordPress/gutenberg/pull/6496/files#r185195135. |
core-blocks/image/index.js
Outdated
@@ -190,6 +199,9 @@ export const settings = { | |||
const { url, alt, caption, align, href, width, height } = attributes; | |||
const extraImageProps = width || height ? { width, height } : {}; | |||
const image = <img src={ url } alt={ alt } { ...extraImageProps } />; | |||
const classes = classnames( align ? `align${ align }` : null, { | |||
'is-resized': !! width || !! height, | |||
} ); |
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.
I don't understand this change? I think the deprecated versions shouldn't be updated once they land. I think what we need to do here is to add a new deprecated
version with the save
function without the is-resized
className. That way the blocks using that version would be considered as valid.
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.
Sounds good to me. Can you help me with this? Feel free to push directly
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.
Sounds good to me. Can you help me with this? Feel free to push directly
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.
LGTM 👍
Yay, thanks! |
@@ -165,6 +170,10 @@ export const settings = { | |||
save( { attributes } ) { | |||
const { url, alt, caption, align, href, width, height, id } = attributes; | |||
|
|||
const classes = classnames( align ? `align${ align }` : null, { |
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.
Minor: Good use case for classnames
object format is to inverse this, so you don't need to assign a fallback null
value to be omitted:
const classes = classnames( {
[ `align${ align }` ]: align,
// ...
} );
@@ -165,6 +170,10 @@ export const settings = { | |||
save( { attributes } ) { | |||
const { url, alt, caption, align, href, width, height, id } = attributes; | |||
|
|||
const classes = classnames( align ? `align${ align }` : null, { | |||
'is-resized': !! width || !! height, |
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.
Minor: classnames
doesn't require the value to be explicitly a boolean, so this could have been just width || height
. Or, if a boolean is desired, coerce after the fact: Boolean( width || height )
This is an alternative, and probably better approach, than the one explored in #6304.
Both branches try and fix the same issue — ensuring that the caption inside a
figure
is only ever as wide as the adjecent image. This is only ah issue when that image is resized.This branch fixes that, in a fairly clean way. See the details in https://css-tricks.com/almanac/properties/w/width/
The figure now uses both
fit-content
, andmin-content
, both rather magical. Fit-content uses the maximum width available, and is applied to images that are not resized. Min-content uses the minimum width available, and is applied when images are resized. In effect this means that the image in a figure with a caption will define the width of the caption itself.