Skip to content
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

Image Block: Conditionally Render Block Control Based on Component Presence #62132

Merged
merged 3 commits into from
May 30, 2024

Conversation

amitraj2203
Copy link
Contributor

What?

Fixes: #62040

Why?

There is a redundant separator in the block toolbar of Image block after setting an override in a synced pattern with an Image block.

How?

Conditionally render the block control based on the presence of internal components.

Testing Instructions

  1. Create a synced pattern.
  2. Add an image block.
  3. Under Advanced in Block Settings, enable an override for the Image block.
  4. Save and publish the synced pattern.
  5. Create a new page or post.
  6. Add the synced pattern with the override for the image block.
  7. Select the image block in the synced pattern.

Screenshots or screencast

image
Copy link

github-actions bot commented May 30, 2024

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: amitraj2203 <amitraj2203@git.wordpress.org>
Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>

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

@amitraj2203 amitraj2203 changed the title Refactor image block control rendering logic for clarity. May 30, 2024
@michalczaplinski michalczaplinski added the [Type] Bug An existing feature does not function as intended label May 30, 2024
Copy link
Contributor

@michalczaplinski michalczaplinski left a comment

Choose a reason for hiding this comment

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

Hi @amitraj2203! Thanks for the contribution, it fixes the issue perfectly fine!

Could we group the various conditionals together like:

const showUrlInput =
  isSingleSelected &&
  ! isEditingImage &&
  ! lockHrefControls &&
  ! lockUrlControls;

const showCoverControls = isSingleSelected && canInsertCover;

const showBlockControls =
  showUrlInput || allowCrop || showCoverControls;

and then use them to conditionally render the ImageURLInputUI and ToolbarButton.

This way, there's only one place to add additional conditions in the future so less risk that someone forgets to update both places

lightboxEnabled={ lightboxChecked }
onSetLightbox={ onSetLightbox }
resetLightbox={ resetLightbox }
{ shouldRenderOtherControls && (
Copy link
Contributor

Choose a reason for hiding this comment

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

A better name might be showBlockControls because they belong to the block group. The other group is below in the component tree.

Also, for consistency, we use show instead of render verb in variable names here to indicate that something is being shown (rendered).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @michalczaplinski, Thank you for reviewing the PR. I have made the changes as per your suggestions and will keep this approach in mind for future work.

Copy link
Contributor

@michalczaplinski michalczaplinski left a comment

Choose a reason for hiding this comment

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

Great work! 👍 Thanks for the quick response.

@michalczaplinski michalczaplinski enabled auto-merge (squash) May 30, 2024 11:12
@michalczaplinski michalczaplinski merged commit 3a95bcc into WordPress:trunk May 30, 2024
60 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.5 milestone May 30, 2024
carstingaxion pushed a commit to carstingaxion/gutenberg that referenced this pull request Jun 4, 2024
…esence (WordPress#62132)

* Refactor image block control rendering logic for clarity.

* Refactor conditional rendering logic for block controls.

Co-authored-by: amitraj2203 <amitraj2203@git.wordpress.org>
Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
patil-vipul pushed a commit to patil-vipul/gutenberg that referenced this pull request Jun 17, 2024
…esence (WordPress#62132)

* Refactor image block control rendering logic for clarity.

* Refactor conditional rendering logic for block controls.

Co-authored-by: amitraj2203 <amitraj2203@git.wordpress.org>
Co-authored-by: michalczaplinski <czapla@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
2 participants