-
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
Refactor block alignments and margins #20689
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom'; | |
import { BACKSPACE, DELETE, ENTER } from '@wordpress/keycodes'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -49,7 +50,9 @@ const BlockComponent = forwardRef( | |
mode, | ||
blockTitle, | ||
wrapperProps, | ||
attributes, | ||
} = useContext( BlockContext ); | ||
const supportAlignments = hasBlockSupport( name, 'align', false ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this accept an array of possible alignments? |
||
const { initialPosition } = useSelect( | ||
( select ) => { | ||
if ( ! isSelected ) { | ||
|
@@ -190,7 +193,7 @@ const BlockComponent = forwardRef( | |
const blockElementId = `block-${ clientId }${ htmlSuffix }`; | ||
const Animated = animated[ tagName ]; | ||
|
||
return ( | ||
const blockContentElement = ( | ||
<Animated | ||
// Overrideable props. | ||
aria-label={ blockLabel } | ||
|
@@ -199,7 +202,9 @@ const BlockComponent = forwardRef( | |
{ ...props } | ||
id={ blockElementId } | ||
ref={ wrapper } | ||
className={ classnames( className, props.className ) } | ||
className={ classnames( className, props.className, { | ||
'wp-block': ! supportAlignments, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this class have to be removed when there's alignment? Does it hurt to have .wp-block > .wp-block? |
||
} ) } | ||
data-block={ clientId } | ||
data-type={ name } | ||
data-title={ blockTitle } | ||
|
@@ -216,6 +221,21 @@ const BlockComponent = forwardRef( | |
{ children } | ||
</Animated> | ||
); | ||
|
||
if ( supportAlignments ) { | ||
return ( | ||
<div | ||
className={ classnames( 'wp-block wp-align-wrapper', { | ||
[ `wp-align-${ attributes.align }` ]: !! attributes.align, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restating my concerns in #20650 (comment), I think now is the prime time to revise the naming scheme for the alignment/position/width options. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you propose? for me we should stay consistent in terms of classNames, in terms of labels in the UI it can be different There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we were to put them all under a single naming scheme, I'd go with something like this:
I'm not sure whether But I'm not convinced that different options should use the same naming scheme if they're technically different things. If we were to use more diverse names depending on what exactly the position/alignment/whatever actually is, I would go with something like the following:
I prefer this naming scheme since it's more concise, and doesn't try to fit a redundant, possibly incorrect word like "position" into the class names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we can change the name of the alignments today Also cc @aduth as my go to naming person :P There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youknowriad There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I'm talking about the attribute values, not the classNames.We also need to keep. BC for existing themes with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we omit the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasmussen I'm fine with removing them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youknowriad Personally, I think that changing the attribute values, even if it means some somewhat complex back-compat, is well worth it to get class names that actually make sense. Changing how block alignments work is likely to break styles anyway, so we might as well take advantage of the opportunity so people don't have to update their styles twice. Also, do we even need |
||
} ) } | ||
data-align={ attributes.align } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we adding the data attributes here if we're going with classes? I think this will only make things more confusing as it's the old way of styling alignment in the editor. |
||
> | ||
{ blockContentElement } | ||
</div> | ||
); | ||
} | ||
|
||
return blockContentElement; | ||
} | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,65 @@ | |
@import "./components/editor-skeleton/style.scss"; | ||
@import "./components/inserter/style.scss"; | ||
|
||
|
||
// Default styles for alignments in case the theme doesn't provide them. | ||
.wp-block { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, this should be something that a theme author can copy/paste to its stylesheet and tweak the numbers.
When we'll be able to remove IE11 support, we could use CSS variables for the widths and margins. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we're refactoring now anyway, I really think this is something that we should do now. I see the alignment wrapper as context to a block or blocks, not as a block itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if this is the main reason for magically adding the alignment wrapper, and we're going to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My original ideal in #20650 (comment) was to have something like: <AlignmentWrapper align={align}>
<Block.figure />
</AlignmentWrapper> Both in the edit and save function. |
||
max-width: $content-width; | ||
margin-left: auto; | ||
margin-right: auto; | ||
margin-top: $default-block-margin; | ||
margin-bottom: $default-block-margin; | ||
|
||
// Floated blocks shouldn't have margins | ||
&[data-align="left"], | ||
&[data-align="right"] { | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
|
||
& > * { | ||
// z-index should be higher than regular blocks to allow selection on click | ||
z-index: z-index(".wp-block {aligned left or right}"); | ||
} | ||
} | ||
|
||
// Left. | ||
&[data-align="left"] > * { | ||
// This is in the editor only; the image should be floated on the frontend. | ||
/*!rtl:begin:ignore*/ | ||
float: left; | ||
margin-right: 2em; | ||
/*!rtl:end:ignore*/ | ||
} | ||
|
||
// Right. | ||
&[data-align="right"] > * { | ||
// Right: This is in the editor only; the image should be floated on the frontend. | ||
/*!rtl:begin:ignore*/ | ||
float: right; | ||
margin-left: 2em; | ||
/*!rtl:end:ignore*/ | ||
} | ||
|
||
&[data-align="full"] { | ||
max-width: none; | ||
clear: both; | ||
|
||
// Leave room for the UI | ||
margin-left: -$block-padding; | ||
margin-right: -$block-padding; | ||
@include break-small() { | ||
margin-left: -$block-padding - $block-padding - $block-side-ui-width - $border-width - $border-width; | ||
margin-right: -$block-padding - $block-padding - $block-side-ui-width - $border-width - $border-width; | ||
} | ||
} | ||
|
||
&[data-align="wide"] { | ||
max-width: 1100px; | ||
clear: both; | ||
} | ||
|
||
&[data-align="center"] { | ||
display: flex; | ||
justify-content: center; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,13 +58,6 @@ | |
* This allows us to create normalization styles that are easily overridden by editor styles. | ||
*/ | ||
|
||
// Provide every block with a default base margin. This margin provides a consistent spacing | ||
// between blocks in the editor. | ||
.editor-styles-wrapper .block-editor-block-list__block { | ||
margin-top: $default-block-margin; | ||
margin-bottom: $default-block-margin; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been wanting to remove this for ages, but there's always been some reason for keeping it. Can it safely be removed now? Cc @jasmussen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seeems like we can! From quick testing in a theme that doesn't style the editor, things look right. And it'd definitely a rule we want to remove. So it does seem now's the time! Note that these two rules seem to mostly take its place: and Good to be mindful of these removals, when we get to the DOM "lightening", can check on a per-block basis, because now it will be up to each individual block to ensure a margin. |
||
} | ||
|
||
// This tag marks the end of the styles that apply to editing canvas contents and need to be manipulated when we resize the editor. | ||
#end-resizable-editor-section { | ||
display: none; | ||
|
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.