-
Notifications
You must be signed in to change notification settings - Fork 808
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
Jetpack button: fix width and alignment settings #41139
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available. Follow this PR Review Process:
Still unsure? Reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
@@ -90,6 +84,5 @@ export function ButtonEdit( props ) { | |||
} | |||
|
|||
export default compose( | |||
withColors( { backgroundColor: 'background-color' }, { textColor: 'color' } ), | |||
applyFallbackStyles |
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.
applyFallbackStyles
uses the withFallbackStyles
high order component from @wordpress/components
, which renders the unwanted extra div (at this line). The code suggests we could get rid of that extra div by specifying a node
prop, but it's unclear to me what node we could pass at that stage.
In the following discussion, not using applyFallbackStyles
was hinted by several persons: p1736773641027939/1736360651.812989-slack-C052XEUUBL4. I do think it's the way to go as well. As far as I can tell, what applyFallbackStyles
does is make the default background and text colors available to ButtonEdit
. Yet eventually, those two colors are passed to the ContrastChecker
component, which doesn't render a warning when the contrast ratio is too low to be deemed accessible.
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.
Yet eventually, those two colors are passed to the ContrastChecker component, which doesn't render a warning when the contrast ratio is too low to be deemed accessible.
It's because the applyFallbackStyles
code is wrong. It checks the block's outer element for the background color, but it's actually applied to the inner element (what it calls the textNode
).
That said, I think it's pretty trivial to rewrite withFallbackStyles/applyFallbackStyles in a more modern react way that won't result in an extra wrapper, so I agree with removing it, and I'd be happy to work on getting the contrast checker working again in a follow up PR.
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.
Here's a small PR that addresses that - #41294.
I'll keep it as a draft until this or another fix for the styles is merged.
@@ -52,6 +52,10 @@ | |||
flex: 0 0 100%; | |||
margin: 0; | |||
|
|||
&.wp-block-jetpack-button { | |||
flex-basis: auto; |
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.
Overwrite the 100%
value. We want to be able to set the width through the button styling options in the editor.
@@ -226,7 +220,7 @@ function get_button_wrapper_styles( $attributes ) { | |||
$has_width = array_key_exists( 'width', $attributes ); | |||
|
|||
if ( $has_width ) { | |||
$styles[] = 'max-width: 100%'; | |||
$styles[] = sprintf( 'width: %s;', $attributes['width'] ); |
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.
Apply the width to the root of the jetpack/button
, instead of its __link
element that's now 100% wide.
|
||
usePassthroughAttributes( { attributes, clientId, setAttributes } ); | ||
useWidth( { attributes, disableEffects: isWidthSetOnParentBlock, setAttributes } ); |
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.
Calling this hook results in resetting the button width when it is aligned left or right. I don't get the rationale behind that decision. In the current UI, from a user standpoint, this doesn't seem like something we'd want.
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.
I've been testing and I also couldn't see the use case that the hook caters to. I saw this was implemented in #25394 by @mmtr, maybe Miguel remembers.
If there's no apparent current use cases, I wonder if it can be removed completely from the codebase. It's also used by Payment Button, but that has no alignment tool on the toolbar, so perhaps it's not needed.
edit: it just occurred to me, a case where it might makes sense is when the button is 100% width and an alignment is set.
@@ -39,6 +34,7 @@ export function ButtonEdit( props ) { | |||
|
|||
const blockProps = useBlockProps( { | |||
className: clsx( 'wp-block-button', className ), | |||
style: { width }, |
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.
Set the width
to the root instead of the __link
element.
48fba64
to
27b3cb7
Compare
Thanks for the review @tellthemachines. Regarding the submit button vertical alignment issue, I intended to see how we wanted to handle that issue. And it's already broken (differently) on trunk: But yes, let's fix it now! I'll do it in a separate PR.
Totally. I don't have an answer, though. |
Thanks @aaronrobertshaw.
Initially, the plan was to fix Looking at the code, I suggest looking at the changes in
I haven't been able to reproduce this. Are you testing the branch locally, or with a live test site?
These are the caveats I mentioned in the PR description. It's not implemented on trunk either at the moment, and I believe it deserves a broader discussion UX-wise. How do we handle cases with multiple buttons? Should we only be able to align the group? Or buttons individually? |
Thanks for the feedback @lezama. I didn't see any errors in my compatibility testing but I'll give it another go to be sure. By any chance, would you know which branch you were running when you created the form? |
I think I was running trunk, maybe from a couple of days ago, not sure 😅 |
I'm also seeing this in my local docker env. It happens on trunk too for me. 🤔 It could also be my stack 😄 |
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.
Thanks for working on this!
I gave this a test and noticed different results with different themes so decided to focus on one in particular as I was confused by the results. I wasn't sure if it was environment based as well but I'll share what I found below.
I created a test post with a contact form, Eventbrite, Mailchimp and Calendly block on it using Jurassic Ninja, locally, and on a Simple site. I used the Twenty Twenty theme.
Local development env, and Simple:
Before applying the PR, the buttons were left-aligned in the editor by default, after applying now they are centre aligned (unless alignment settings had been added, in which case those alignments were respected).
Here's an example of how this affected the Register button, with screenshot showing before and after:
After, while the pre-existing widths have been respected, the display is different - for example the default with the Mailchimp button was full-width, now it's less than full-width.
Jurassic Ninja:
This is where I think there may have been some environmental differences or issues related to using the beta tester plugin. Before applying the changes, the width alignment settings all worked apart from the contact submit button (which I understand is expected). Left aligned by default.
After, alignment is unusual - it's central but off-centre depending on width in the editor, but still left aligned in preview.
Generally, should these differences be expected?
27b3cb7
to
96f6304
Compare
Code Coverage SummaryCoverage changed in 3 files.
|
Thanks for the review @coder-karen. Good catch, the issues you mention seem to be related to older themes, which use the I also had to increase the CSS specificity of some Jetpack button selectors so the styles are applied as expected on older and newer themes. It feels sketchy but I see no other way around. I've tested with the Twenty Twenty, Twenty Twenty-Four, and Twenty Twenty-Five themes. The former uses the Setting the width should work on all buttons (except for the Recipe block, which I excluded from the testing instructions). Setting the alignment doesn't work for buttons in the same row: in our case the Payments Buttons and Paid Content blocks. But it doesn't seem it ever worked properly. I don't want to tackle all issues in this PR, so as long as it doesn't make any blocks worse, I'd say let's ship it. The width/alignment should be the same in the editor and front end. |
@talldan and @lezama: I still haven't been able to reproduce the warning you're both seeing. Is there any other information you could think of that'd be relevant? Locally, I tried adding a new Contact form to a post while on trunk, then checking out this branch and rebuilding the forms package. Reloading the post didn't trigger any warning. I also tried switching from the latest plugin stable version to this branch with a JN site. Same results. |
It seems to have gone away now after a clean and rebuild. Sorry, I should've posted an update here. |
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.
In general, this seems to be working much better than trunk
, so I think I'd be inclined to merge it and if there are some small things that are still not working they can be fixed in follow up PRs. I went through the code pretty thoroughly and there are a lot of changes, but all explained fairly well.
There are still some oddities, but I think they're out of scope for this PR, and same behavior is mostly present in trunk:
- Width control - it was mentioned by others, but the way the width split button disappears when a left/right alignment is set feels like a bug.
- Payment Button(s) - The way Payment Button wraps Jetpack Button is quite confusing. The innermost Button has width and alignment options, but alignments don't work (in trunk or in this PR), and I don't see how they would given the Payment Button(s) blocks use a flex layout. Let me know if there's something I missed in my testing here.
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.
Thanks for the follow-up! I did a little more testing and I imagine the main thing that might generate some support requests is the change to default behavior (for Mailchimp, Calendly, Eventbrite buttons, also Contact Us depending on theme), which in my testing again just now were full-width in the editor and front-end before and now are not. That's based on the themes I looked at anyway. The new default behavior makes sense given the changes in this PR, I can't imagine there's any way around this though.
I did notice that with Twenty Twenty the Register / Contact Us alignment issue I'd mentioned before is not an issue anymore - they're side by side in the editor - but in preview they're where they should be (one right aligned, the other the line below but left aligned). It seems like this could be theme specific though.
So I agree with the previous approval, it looks like any possible iterations could be followed up with separately.
Resolves #38779
Setting the width and alignment of a
jetpack/button
block doesn't work consistently, depending on how it is integrated with its parent block and whether we're in the editor or site frontend.For instance, in a Form block, setting the button width to 50% in the editor shrinks it instead of making it half the container's width, which is what you'd expect from a user's perspective.
![Screenshot 2025-01-16 at 3 32 57 PM](https://private-user-images.githubusercontent.com/1620183/404031548-e1c052d8-72fa-4648-a417-f1a1320b5ccd.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwODQ2NTcsIm5iZiI6MTczOTA4NDM1NywicGF0aCI6Ii8xNjIwMTgzLzQwNDAzMTU0OC1lMWMwNTJkOC03MmZhLTQ2NDgtYTQxNy1mMWExMzIwYjVjY2QucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDlUMDY1OTE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9Nzk1ZGFlZGRkMTM5NGJmZGNkYTY4OWZlNTQ5OGJkZmViNDE1ZTNmZjU3MTY3MzExYjg0ZTc3NDMxYjgxYzNmMyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.3WGMJgBiIB_ykhE0mTDevPwKTC2_8kllX8HyN6LzzGA)
In a Mailchimp block, trying to align the button (to the right in the capture below) doesn't work.
![Screenshot 2025-01-16 at 3 33 08 PM](https://private-user-images.githubusercontent.com/1620183/404031605-abb60192-5ab0-4292-b545-dbc42f26840c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwODQ2NTcsIm5iZiI6MTczOTA4NDM1NywicGF0aCI6Ii8xNjIwMTgzLzQwNDAzMTYwNS1hYmI2MDE5Mi01YWIwLTQyOTItYjU0NS1kYmM0MmYyNjg0MGMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDlUMDY1OTE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OTQxODgyOTRmNTNkMjBiMjAzNzJhOGQ1ZDBlNzk0OTMwZmIxZmM1ODkxZmU5ZWRmMDM5NTdiOGJmZGUyOTA0NCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.IyN38qdfrA-YREutDI9BBz6Xd86WKWUPmJ6PhoPEobc)
The following blocks use the
jetpack/button
block and show one or both faulty behaviors we've highlighted:The AI Assistant might as well, though I haven't been able to experience it.
Proposed changes:
To address that, this PR does two things in the
jetpack/button
block:div
from the markup generated by the blockThis
div
prevents the flexbox layout applied to.block-editor-block-list__layout
from working as intended..wp-block-button
instead of.wp-block-button__link
.Indeed, this Gutenberg PR forces
.wp-block-button__link
to have a width of 100%, breaking the width settings behavior.Furthermore, the PR also updates the blocks consuming
jetpack/button
so the width and alignment settings work as expected. Here are some caveats:jetpack/button
in the Recipe block doesn't work as one would expect, given the width is relative to the grid items of the block. Solving this should be part of a broader discussion.jetpack/button
of a Form is confusing when it's not in its row. It deserves a separate discussion too.Other information:
Jetpack product discussion
n/a
Does this pull request change what data or activity we track or use?
No.
Testing instructions: