Skip to content

Commit

Permalink
Revisit image floats.
Browse files Browse the repository at this point in the history
This adds a wrapping `aside` element to any image block that's floated left or right. From the spec, https://www.w3.org/TR/html52/grouping-content.html#the-figure-element:

> For content that is only tangentially related, or that serves a separate purpose than the surrounding flow, the aside element should be used (and can itself wrap a figure). For example, a pull quote that repeats content from an article would be more appropriate in an aside than in a figure, because it isn’t part of the content, it’s a repetition of the content for the purposes of enticing readers or highlighting key topics.

The above is the reasoning for using the `aside` element to wrap the figure. But why wrap the figure at all?

Because due to issues surfaced in #7624 (comment), it seems our current implmeentation isn't responsive.

The challenge is — what if you float a very small image to the left, and write a giant caption. Even if we apply `width: fit-content;` on the `figure`, the caption will expand the `figure` to accommodate as much text as the parent wrapping element will allow. `min-content` doesn't work either, because this will make the `figure` only as wide as the smallest word.

What we have in master works in most cases, through dark magic, but it also only works because we remove the `max-width` from the nested image. This means the image won't resize with the viewport, and is therefore not responsive.

I have explored so many many options for fixing this, and after all this time, what it boils down to is this:

- We can either set a fixed size on floated captions, say 33%, and hope it works for the image that's floated. Not ideal, and you can see the end result here: https://codepen.io/joen/pen/wXbqwN
- We can add a wrapping element around the `figure`, so that it and the `figcaption` can be sized using table rules, as in this PR.

Why not just set the width on the figure element and float that? Because then we can't accommodate wide images, which rely on an unbounded main column.

It's not ideal that we have to add an extra wrapping element, but it can be semantic, and it feels like the simplest to work with for themers implementing wide images coexisting with floats.
  • Loading branch information
jasmussen authored and Joen Asmussen committed Aug 13, 2018
1 parent fa3e58a commit 27c09d5
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class ImageEdit extends Component {
<Fragment>
{ getInspectorControls( imageWidth, imageHeight ) }
<ResizableBox
className="wp-block-image__resizer"
size={
width && height ? {
width,
Expand Down
61 changes: 52 additions & 9 deletions packages/block-library/src/image/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
position: relative;
margin: 0;

img {
display: block;
width: 100%;
}

&.is-resized img {
height: 100%;
}

&.is-transient img {
@include loading_fade;
}
Expand All @@ -20,6 +11,18 @@
}
}

// This is necessary for the editor resize handles to accurately work on a non floated small non resized image.
.wp-block-image {
.wp-block-image__resizer {
display: inline-block;

img {
display: block;
width: 100%;
}
}
}

.wp-block-image__resize-handler-right,
.wp-block-image__resize-handler-bottom,
.wp-block-image__resize-handler-left {
Expand Down Expand Up @@ -103,3 +106,43 @@
margin: -$border-width;
}
}

// Although the float markup is different in the editor compared to the frontend,
// this CSS uses the same technique to allow floats in a wide images context.
// That is, the block retains its centering and max-width, and a child inside
// is floated instead of the block itself.
[data-type="core/image"][data-align="center"],
[data-type="core/image"][data-align="left"],
[data-type="core/image"][data-align="right"] {
.editor-block-list__block-edit {
figure {
margin: 0;
display: table;
}

// This maps to the figcaption on the frontend.
.editor-rich-text {
display: table-caption;
caption-side: bottom;
}
}
}

[data-type="core/image"][data-align="wide"],
[data-type="core/image"][data-align="full"] {
figure img {
width: 100%;
}
}

// This is similar to above but for resized unfloated images only, where the markup is different.
[data-type="core/image"] .editor-block-list__block-edit figure:not(.alignfull):not(.alignwide) {
margin: 0;
display: table;

// This maps to the figcaption on the frontend.
.editor-rich-text {
display: table-caption;
caption-side: bottom;
}
}
49 changes: 47 additions & 2 deletions packages/block-library/src/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
createBlock,
Expand Down Expand Up @@ -216,15 +217,59 @@ export const settings = {
/>
);

return (
<figure className={ classes }>
const figure = (
<Fragment>
{ href ? <a href={ href }>{ image }</a> : image }
{ caption && caption.length > 0 && <RichText.Content tagName="figcaption" value={ caption } /> }
</Fragment>
);

if ( 'left' === align || 'right' === align || 'center' === align ) {
return (
<div>
<figure className={ classes }>
{ figure }
</figure>
</div>
);
}

return (
<figure className={ classes }>
{ figure }
</figure>
);
},

deprecated: [
{
attributes: blockAttributes,
save( { attributes } ) {
const { url, alt, caption, align, href, width, height, id } = attributes;

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

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

return (
<figure className={ classes }>
{ href ? <a href={ href }>{ image }</a> : image }
{ caption && caption.length > 0 && <RichText.Content tagName="figcaption" value={ caption } /> }
</figure>
);
},
},
{
attributes: blockAttributes,
save( { attributes } ) {
Expand Down
39 changes: 25 additions & 14 deletions packages/block-library/src/image/style.scss
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
.wp-block-image {
width: fit-content;
max-width: 100%;

img {
max-width: 100%;
}

&.aligncenter {
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
}

&.is-resized {
width: min-content;
&.alignfull img,
&.alignwide img {
width: 100%;
}

// Emulate min-content for Edge and IE11
display: -ms-inline-grid;
-ms-grid-columns: min-content;
// Floats get an extra wrapping aside element, so the figure becomes a child.
// Resized images that are not floated, need the caption trick too.
&:not(.alignfull):not(.alignwide),
.alignleft,
.alignright,
.aligncenter,
&.is-resized {
display: table;
margin: 0;

figcaption {
-ms-grid-row: 2;
> figcaption {
display: table-caption;
caption-side: bottom;
}
}

img {
max-width: none;
}
.alignleft {
float: left;
margin-right: 1em;
}

.alignright {
float: right;
margin-left: 1em;
}

// Supply caption styles to images, even if the theme hasn't opted in.
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/components/block-list/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,16 @@
width: 100%;

// When images are floated, the block itself should collapse to zero height.
margin-bottom: 0;
height: 0;

// Hide block outline when an image is floated.
.editor-block-list__block-edit {
&::before {
content: none;
}

// This margin won't collapse on its own, so zero it out.
margin-top: 0;
}

// Keep a 1px margin to compensate for the border/outline.
Expand Down

0 comments on commit 27c09d5

Please sign in to comment.