-
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
Add initial block support for width #26045
Conversation
c1411ca
to
8368408
Compare
aeedbf6
to
413d317
Compare
First pass attempt at creating a block support for adding a width selector with options for 25, 50, 75, 100 percent width. It is based off the work to add a block support for border radius. The intention is to extend this to include custom percentage widths and px support with a slider in the editor for custom sizing (see Search bar for an example).
When a custom width is selected apply the 'custom-width' className to the block. This is used to key off of for additional block-specific CSS; for example the Button block needs to set its inner component to 100% width for the styling to work correctly.
Nesting width into a dimension setting allows for the addition of related settings (height etc) in the future.
Adding a block support using CSS variables to the Button block causes the inline style to be added to the button's wrapper div as well as the inner anchor tag. This causes Block Invalidation errors for existing posts, as well as for any patterns that use the Button block. These errors are resolved with a deprecation that omits the block support. Once opened in the block editor they migrate cleanly.
Fix up an incorrectly applied rebase.
413d317
to
2b04a57
Compare
The use of `useBlockProps` in the Button block was causing all of the block's style attributes to be applied to the button's wrapper div, including color props that were previously applied only to the inner anchor tag. When a Button has both a custom background color and a border radius, this causes and issue where the background color (applied to the wrapper) exceeds the lines of the border (applied to the inner anchor tag). This commit removes style attributes related to the color props from the blockProps before they are applied to the wrapper. This also resolves the block invalidation errors that were previously addressed by a block deprecation, so the deprecation is no longer necessary and is also removed in this commit.
2b04a57
to
bcade7c
Compare
One consequence of implementing the block support with CSS variables in this way is that the style attributes end up getting applied to the outermost element of the block. This causes issues when any block support of this type* is added to the Button block, because the This causes block invalidation errors for Button, and also a bug when a user selects a custom background-color and also applies a border radius to a button. The border-radius is applied to the inner component, and the background-color to the wrapper, causing the color to exceed the lines of the border. I handled this in the Button block by stripping color props from
|
✨ Nice work @stacimc This is working as advertised for me. I have a few minor suggestions that I'll add as inline comments. I also have some thoughts on the stripping of color props before applying the block props to the wrapper but I'll write those up separately. |
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.
Appreciate all the hard work on this @stacimc it is coming along nicely! 🎉
After taking this for a test drive my suggestions fall under three banners:
- Trivial comments or wording tweaks
- Restructuring it a little to be consistent with the existing Typography tools
- Addressing the issue regarding stripping the color props before applying to the wrapper
One consequence of implementing the block support with CSS variables in this way is that the style attributes end up getting applied to the outermost element of the block.
I wonder if we would be better served by moving the application of the button styles and classes to the actual button block itself. This obviously might lead to some failed tests and needing a deprecation. I'm happy for others to weigh in on this one.
- At the moment, some of the block's props (border radius and colors) are retrieved and then applied to the inner link component.
- We could just apply these to the button component itself and use a couple of CSS styles to inherit these values as needed (overriding the default button link styles)
- Once the border radius block support PR is merged that could be opted into for the button block leading to its styles being applied to the button block itself for free.
- The button block could be updated to leverage the
useColors
hook and block support. This would again apply the colors to the button block. - Using the border radius and colors block support would mean we could remove all that code applying those styles manually from the button edit and save scripts.
I've played around with the suggestion above and it worked pretty well for me. It could do with some further exploration though. I'd be happy to help if we all decide to go down that path.
I do believe we use Width Settings percentage for only images. Here is an example with using width for columns: I am thinking the columns approach in relation to width might be better to use with buttons. Btw did you create a Gutenberg issue so that we can explore to figure out the best approach. It should also have feedback from design? Thanks. |
Some docstrings still referred to the block support as just width support, and needed to be updated after refactoring into a larger dimensions feature.
We also use width percentages on the search block. Also on the Jetpack form block there is a similar setup. I think it's useful for when you want to use a flexbox based arrangement to align buttons into columns using percentages. |
4032e7a
to
6f37465
Compare
Properly register the block support and apply the CSS variable inline styles for dynamic blocks that are rendered in PHP.
Because block supports implemented with CSS variables add styling to the outermost element of a block, this commit updates the Button block to expect styling to be applied at this level, rather than directly setting it on the inner component. This is accomplished by: 1. Updates the CSS so that the inner button will always inherit color, background-color, and border-radius styling that was applied inline to its wrapper. This only happens when a user has selected an option from a block support, so we can reliably override any other styling applied by defaults or the theme. 2. Adds a deprecation for the Button block since styling is now applied differently. 3. Removes the block-specific implementation of Color settings and instead applies the color block support. 4. Temporarily manually applies the border-radius to the wrapper. There is a PR for a border radius block support, which can be used to replace this.
Hi, thanks for your PR. However, implementing this feature as a wide-reaching block-supports feature is premature. Instead, I strongly suggest implementing width control directly in the Buttons block — which is a clear worthwhile use case — and seeing from there if more compelling use cases arise that would then have us generalise this feature. |
@mcsf We've been doing a lot of work on adding this kind of support to blocks, and have been directed towards making them "block support" features, see our other PRs: I'm wondering why width would be different in this case? I can see a number of initial block uses for this, the search block, image and gallery blocks (with expanded options), buttons, and separator blocks off the top of my head. |
@apeatling the typography tools in the examples above have a better defined shape (the typography object). Width and other dimension controls don't yet, and it's not clear how it might be apply to different blocks, which makes it a bit trickier. We should be using the same components for sure but we can wait a bit until |
Description
Issue #26368: This adds a dimensions block support providing a width selector with default options for 25%, 50%, 75%, and 100% width.
This is an alternative to implementing width as a selector directly on the Button block, implemented here: (#25999)
Changes
Button
component to use the new block supportNotes
width
options are nested underdimensions
settings and could be expanded to include additional block support options for heightsearch
blockButton
sets the width of the entire component -- no adjustments were made for margins in this iteration, so for example you will find that a 25% width button does not fit on the same line as a 75% width button.How has this been tested?
Manual testing using the Button block.
Test instructions
Buttons
block to a post, with one or moreButton
s.Button
var(--wp--preset--width--<the width you selected>)
.Screenshots
Types of changes
New feature (non-breaking change which adds functionality)
Next Steps
Checklist: