Skip to content

Commit

Permalink
Make floats work in the editor, same as frontend.
Browse files Browse the repository at this point in the history
This changes the editor float code for the image so it matches and works the same as the front-end.

That is, when an image is floated, the parent container (block container) retains its same max-width which usually allows a centered column to coexist with wide images. But it is zeroed out in height. The child element, the figure itself, is then floated, which allows the caption to share the same width as the figure, using a table-caption trick.

The CSS is the same, front and backend, but the markup is not the same, so some CSS references map to different things. With the ultimate goal of WYSIWYG, this might be worth revisiting in the future, but is way out of scope for this PR since there are a ton of React wrappers necessary for resizing and other things.

Note that this PR only touches images. If this is approved and merged in, we'll probably want to backport these changes to other blocks that can float, i.e. video, embed, cover image.
  • Loading branch information
jasmussen committed Aug 1, 2018
1 parent 23960d5 commit d36cce2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 21 additions & 0 deletions core-blocks/image/editor.scss
Expand Up @@ -103,3 +103,24 @@
margin: -1px;
}
}

// 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 figure on the frontend.
.editor-rich-text {
display: table-caption;
caption-side: bottom;
}
}
}
4 changes: 3 additions & 1 deletion packages/editor/src/components/block-list/style.scss
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 d36cce2

Please sign in to comment.