Skip to content

Media Replace Flow: Add custom toggle support and fix button height#68084

Merged
mirka merged 14 commits into
WordPress:trunkfrom
yogeshbhutkar:68028/fix-spacing-issue
Jan 2, 2025
Merged

Media Replace Flow: Add custom toggle support and fix button height#68084
mirka merged 14 commits into
WordPress:trunkfrom
yogeshbhutkar:68028/fix-spacing-issue

Conversation

@yogeshbhutkar

Copy link
Copy Markdown
Contributor

Fixes: #68029

What?

This PR contains a patch for fixing spacing inconsistency in the background image selector button.

How?

As the issue description mentions, the default size of Toolbar Button was updated to compact, whose styles are applied using the is-compact classname.

The is-compact classname contains a height of 32px as mentioned here:

height: $button-size-compact;

$button-size-compact: 32px;

Whereas, the heights described in .components-dropdown applied to its parent, is actually 36px leading to a mismatch in the parent container and child's heights.

To fix the issue, the heights are updated inside .components-dropdown to now use fit-content instead of using the previously hardcoded value 36px.

As an alternative, we can also hardcode the height to 32px.

File Changed:

> packages/block-editor/src/components/background-image-control/style.scss

Testing Instructions

  1. Go to the site editor.
  2. Click on "Styles"
  3. Click on "Background"
  4. Confirm that the spacing for the button is now correct.

Screenshots

Before After
Screenshot 2024-12-17 at 10 38 17 AM Screenshot 2024-12-17 at 10 37 24 AM

@yogeshbhutkar yogeshbhutkar marked this pull request as ready for review December 18, 2024 09:47
@github-actions

github-actions Bot commented Dec 18, 2024

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@yogeshbhutkar

yogeshbhutkar commented Dec 18, 2024

Copy link
Copy Markdown
Contributor Author

Requesting a review from @mirka, as this is being carried forward from the previous PR: #68038

Apologies for any inconvenience this may have caused.

@juanfra juanfra added [Type] Bug An existing feature does not function as intended [Package] Block editor /packages/block-editor labels Dec 18, 2024
@juanfra juanfra requested a review from mirka December 18, 2024 10:28

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My feedback from #68038 has been addressed, so I'll let @mirka review individually when she gets a chance.

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, and I think we can future-proof it even further by exposing the entire renderToggle as an optional prop, rather than try to abstract it with buttonVariant. My concern with an abstract approach like buttonVariant is that we'll need to alter the API every time we need something slightly different, like an icon button or another Button variant.

So for example in BackgroundImageControls I want to be able to do something like:

<MediaReplaceFlow
  renderToggle={ ( props ) => (
    <Button
      { ...props }
      __next40pxDefaultSize
    />
  ) }

What do you think? (cc @WordPress/gutenberg-components)

Also as a separate issue, I noticed that the focus manipulation that editMediaButtonRef is being used to do is not relevant/appropriate anymore in these recent implementations. I'm looking at the original commit where that focus manipulation was added, and it seems like it was for focus restoration, which doesn't apply anymore (cc @draganescu in case I'm wrong). We could maybe clean that up as well.

.components-dropdown {
display: block;
height: 36px;
height: fit-content;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these height rules be removed entirely?

Suggested change
height: fit-content;

@yogeshbhutkar yogeshbhutkar Dec 20, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @mirka.

Yes, we can indeed remove the height rules. I've removed them in the latest commit. Here's a screenshot of the component after recent changes:

Screenshot 2024-12-20 at 8 56 42 AM

It's working as expected on my end.

This is good, and I think we can future-proof it even further by exposing the entire renderToggle as an optional prop, rather than try to abstract it with buttonVariant.

Great Point! I'll work on incorporating this, will update here once done.

@yogeshbhutkar

Copy link
Copy Markdown
Contributor Author

As per the suggestions provided by @mirka, the following are the changes made in the recent commit:

  1. Removed the code for buttonVariant abstract approach in favor of introducing the exposure of renderToggle as an optional prop.
  2. Cleaned up editMediaButtonRef as it was referred not relevant in the recent implementations here.
  3. Removed name prop from being passed inside BackgroundImageControls component when using the MediaReplaceFlow as when renderToggle is provided, the name is also provided within the Button. This name is anyways supposed to be used by the Button itself, so we can save up some redundancy by passing it within renderToggle itself.

Here's a screencast post recent changes:
Screen Recording Dec 20 2024

The changes seem to work as expected on my end.

@ciampo ciampo requested review from mirka and tyxla December 20, 2024 11:19
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ ( event ) => {
if ( event.keyCode === DOWN ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we've been doing it like that in other places, but event.keyCode is deprecated. You might want to use:

Suggested change
if ( event.keyCode === DOWN ) {
if ( event.key === 'ArrowDown' ) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tyxla, thanks for the review. I've incorporated the suggested changes in the latest commit.

Comment thread packages/block-editor/src/components/media-replace-flow/index.js
Comment on lines +141 to +144
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ openOnArrowDown }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should pass down these as default props for the media flow render toggle. This way, consumers don't have to re-implement a11y best practices.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, that is what I had in mind with this usage example. We can assume (and document) that the renderToggle function should accept and pass through button props to an eventual button element.

@yogeshbhutkar Let me know if you're unsure what to do here, I'd be happy to push up some proposed code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mirka, yeah, reread your example after I commented and noticed it 😅

Comment on lines +141 to +144
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ openOnArrowDown }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, that is what I had in mind with this usage example. We can assume (and document) that the renderToggle function should accept and pass through button props to an eventual button element.

@yogeshbhutkar Let me know if you're unsure what to do here, I'd be happy to push up some proposed code.

Comment on lines -372 to -378
name={
<InspectorImagePreviewItem
className="block-editor-global-styles-background-panel__image-preview"
imgUrl={ url }
filename={ title }
label={ imgLabel }
/>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because name is a standard prop of MediaReplaceFlow, I think it would be safer to continue using this prop rather than rendering it directly into our renderToggle.

One risk I'm thinking about is that the MediaReplaceFlow implementation could change some day to use the name prop in another place as well.

@yogeshbhutkar

Copy link
Copy Markdown
Contributor Author

While implementing the suggested changes, I found that entirely removing this CSS rule changes the height of the Image Preview component. Instead of completely removing it, the following rule will fix the issue:

.block-editor-global-styles-background-panel__dropdown-toggle {

Before After
Screenshot 2024-12-27 at 10 50 17 AM Screenshot 2024-12-27 at 10 37 59 AM

Also, it looks like there were some missed lints in the JSON file that previously got committed to the latest trunk

The pre-commit hook seems to have fixed those instances (#64425) in this PR while pushing the merged code to resolve conflicts.


Finally, this comment clarified the suggested approach further. So, I've updated the renderToggle implementation to accept button props, which must be passed to the button component. I've also documented this change.

If you feel this is not the best approach, I can refer to a small code example.


Here's a screencast after the suggested changes:

Recent Changes

Thanks a lot to the reviewers for these constant follow-ups ✨.

CC: @Mamaduka @mirka

Comment thread packages/block-library/src/post-template/block.json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are changes in this file still necessary?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @Mamaduka. I've tried to explain it above in this comment.

Thanks a lot for reviewing the PR 🚀

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API changes look great, thank you! I think we're good to go once the height value is fixed.

height: 36px;

.block-editor-global-styles-background-panel__dropdown-toggle {
height: 36px;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
height: 36px;
height: 40px;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @mirka. I've updated the height in the latest commit.

Screenshot 2024-12-30 at 11 48 30 AM

@yogeshbhutkar yogeshbhutkar changed the title Update background image control dropdown height to fit-content Media Replace Flow: Add custom toggle support and fix button height Dec 30, 2024
@yogeshbhutkar yogeshbhutkar requested a review from mirka December 30, 2024 06:55

@mirka mirka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the follow-ups! 🚀

@mirka mirka merged commit 9b35bc6 into WordPress:trunk Jan 2, 2025
@github-actions github-actions Bot added this to the Gutenberg 20.0 milestone Jan 2, 2025
bph pushed a commit to bph/gutenberg that referenced this pull request Jan 8, 2025
…ordPress#68084)

* Update background image control dropdown height to fit-content

* Replace ToolbarButton with Button in MediaReplaceFlow component

* Refactor MediaReplaceFlow to support dynamic button variants and update BackgroundImageControls to specify button variant

* Refactor MediaReplaceFlow to set default button variant to 'toolbar' and remove unused default variant

* Remove redundant height property from background image control dropdown styles

* Refactor BackgroundImageControls and MediaReplaceFlow to improve button rendering and support custom toggle rendering

* Remove unnecessary blank line in MediaReplaceFlow component

* Update BackgroundImageControls to use 'ArrowDown' key for dropdown navigation

* Media Replace Flow: Add custom toggle support and fix button height

* added `renderToggle` prop details to readme

* refactor: remove unused styles

* style: increase dropdown toggle height to 40px

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>
Gulamdastgir-Momin pushed a commit to Gulamdastgir-Momin/gutenberg that referenced this pull request Jan 23, 2025
…ordPress#68084)

* Update background image control dropdown height to fit-content

* Replace ToolbarButton with Button in MediaReplaceFlow component

* Refactor MediaReplaceFlow to support dynamic button variants and update BackgroundImageControls to specify button variant

* Refactor MediaReplaceFlow to set default button variant to 'toolbar' and remove unused default variant

* Remove redundant height property from background image control dropdown styles

* Refactor BackgroundImageControls and MediaReplaceFlow to improve button rendering and support custom toggle rendering

* Remove unnecessary blank line in MediaReplaceFlow component

* Update BackgroundImageControls to use 'ArrowDown' key for dropdown navigation

* Media Replace Flow: Add custom toggle support and fix button height

* added `renderToggle` prop details to readme

* refactor: remove unused styles

* style: increase dropdown toggle height to 40px

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: juanfra <juanfra@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Background image selector button spacing issue

5 participants