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

Automatically select group variation if there is only one available #61871

Merged

Conversation

costasovo
Copy link
Contributor

@costasovo costasovo commented May 22, 2024

What?

When a core/group block has only one variation available, let's skip the placeholder step and select the variation.

Why?

We are building an email editor, but many email clients don't support modern grids or flex layouts. Therefore, we've disabled most variations, leaving us only with one. It feels weird to display the placeholder step with only one option.

How?

I added an effect into the placeholder component in which I select the variation if there is only one available. I was thinking about adding this logic directly to the Edit component, but it would require calling select for variants for every Edit render so I rather added the change directly into the Placeholder. I'm happy to change it based on the feedback.

Testing Instructions

Default state (multiple variations)

  1. Insert a group block via inserter
  2. See placeholder with multiple variations

With only one variation

  1. Disable all group variations but one:
addFilter(
    'blocks.registerBlockType',
    'mailpoet-email-editor/disable-group-variations',
    ( settings, name ) => {
      if ( name === 'core/group' ) {
        return {
          ...settings,
          variations: settings.variations.filter(
            ( variation ) => variation.name === 'group',
          ),
        };
      }
      return settings;
    },
  );
  1. Insert the group block via the inserter
  2. See that the placeholder step was skipped

Testing Instructions for Keyboard

The appender in the empty group block is accessible using arrows in the canvas

Screenshots or screencast

Before

Screen Capture on 2024-05-22 at 16-50-25

After

Screen Capture on 2024-05-22 at 16-52-43

Closes #62239

@costasovo costasovo requested a review from ajitbohra as a code owner May 22, 2024 15:16
Copy link

github-actions bot commented May 22, 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: costasovo <costasovo@git.wordpress.org>
Co-authored-by: ockham <bernhard-reiter@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 added [Type] Enhancement A suggestion for improvement. [Block] Group Affects the Group Block labels Jun 8, 2024
@costasovo costasovo force-pushed the add/autoselect-single-group-variation branch 2 times, most recently from 53e517b to 961a1e9 Compare June 24, 2024 08:08
@ockham ockham added the [Feature] Block Variations Block variations, including introducing new variations and variations as a feature label Jun 24, 2024
@ockham ockham self-requested a review June 24, 2024 13:34
Copy link
Contributor

@ockham ockham left a comment

Choose a reason for hiding this comment

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

Thank you!

I added an effect into the placeholder component in which I select the variation if there is only one available. I was thinking about adding this logic directly to the Edit component, but it would require calling select for variants for every Edit render so I rather added the change directly into the Placeholder. I'm happy to change it based on the feedback.

Ah yes, this is an interesting question. Looking at the Group block's code, it seems to have been structured to contain the checks that determine if a placeholder should been shown in the useShouldShowPlaceHolder hook, whereas the Placeholder component doesn't perform any such checks.

Consequently, it's somewhat tempting to preserve this division -- i.e. move the variations check into useShouldShowPlaceHolder. However, that's unfortunately not the end of the story; we'll also need to add logic to the Edit component itself (probably another logical branch here). The Edit component will then also need to know the block variations, as will the useShouldShowPlaceHolder hook. I'm not too worried about the performance impact, but it would significantly balloon the code that's needed for this otherwise simple change. It's dubious if shoehorning this new check into the existing structure of the block will really make the code that much more readable if all these changes are needed.

So let's be pragmatic instead and go with your approach 👍

@ockham ockham force-pushed the add/autoselect-single-group-variation branch from 961a1e9 to 777bfe9 Compare June 26, 2024 10:39
@ockham
Copy link
Contributor

ockham commented Jun 26, 2024

Rebased (to get tests to pass 🤞)

@costasovo
Copy link
Contributor Author

@ockham Thank you for the review, rebasing and the feedback 🙇

Consequently, it's somewhat tempting to preserve this division -- i.e. move the variations check into useShouldShowPlaceHolder. However, that's unfortunately not the end of the story; we'll also need to add logic to the Edit component itself (probably another logical branch here). The Edit component will then also need to know the block variations, as will the useShouldShowPlaceHolder hook.

I started with this approach, and after I made a couple of these changes, I decided to go with the simple approach. My thoughts were pretty similar.

@ockham ockham merged commit 1998f1f into WordPress:trunk Jun 27, 2024
59 checks passed
@github-actions github-actions bot added this to the Gutenberg 18.8 milestone Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Group Affects the Group Block [Feature] Block Variations Block variations, including introducing new variations and variations as a feature [Type] Enhancement A suggestion for improvement.
3 participants