From 23960d524763fa9aeac0c2f8df63ceefeac170e5 Mon Sep 17 00:00:00 2001 From: jasmussen Date: Thu, 5 Jul 2018 12:09:37 +0200 Subject: [PATCH] Revisit image floats. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/WordPress/gutenberg/pull/7624#issuecomment-402114247, 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. --- core-blocks/image/index.js | 49 ++++++++++++++++++++++++++++++++++-- core-blocks/image/style.scss | 28 ++++++++++++--------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/core-blocks/image/index.js b/core-blocks/image/index.js index 3cf3ff04e6c93f..1dae48c7095aee 100644 --- a/core-blocks/image/index.js +++ b/core-blocks/image/index.js @@ -6,6 +6,7 @@ import classnames from 'classnames'; /** * WordPress dependencies */ +import { Fragment } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { createBlock, @@ -217,15 +218,59 @@ export const settings = { /> ); - return ( -
+ const figure = ( + { href ? { image } : image } { caption && caption.length > 0 && } + + ); + + if ( 'left' === align || 'right' === align ) { + return ( +
+
+ { figure } +
+
+ ); + } + + return ( +
+ { 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 = ( + { + ); + + return ( +
+ { href ? { image } : image } + { caption && caption.length > 0 && } +
+ ); + }, + }, { attributes: blockAttributes, save( { attributes } ) { diff --git a/core-blocks/image/style.scss b/core-blocks/image/style.scss index 9797196434499d..17e65f0629fda7 100644 --- a/core-blocks/image/style.scss +++ b/core-blocks/image/style.scss @@ -1,6 +1,4 @@ .wp-block-image { - width: fit-content; - &.aligncenter { display: block; margin-left: auto; @@ -8,20 +6,26 @@ text-align: center; } - &.is-resized { - width: min-content; - - // 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. + .alignleft, + .alignright { + display: table; + margin: 0; figcaption { - -ms-grid-row: 2; + 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.