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

Share the block style selector code #14383

Merged
merged 7 commits into from
Jan 30, 2020
Merged

Conversation

scruffian
Copy link
Member

@scruffian scruffian commented Jan 17, 2020

Fixes #14466

Changes proposed in this Pull Request:

  • OpenTable and Calendly both have a block style selector which does essentially the same thing. Let's extract that to a new shared component.

Testing instructions:

  • Add a Calendly block and check that you can still change the styles in the sidebar
  • Add an OpenTable block and check that you can still change the styles in the sidebar

Screenshot 2020-01-17 at 13 48 58

Screenshot 2020-01-17 at 13 48 51

Proposed changelog entry for your changes:

  • no changelog

@scruffian scruffian added [Status] In Progress [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack [Block] OpenTable [Block] Calendly labels Jan 17, 2020
@scruffian scruffian requested a review from a team January 17, 2020 13:49
@scruffian scruffian self-assigned this Jan 17, 2020
@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello scruffian! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer, review, and approve D37842-code before merging this PR. Thank you!

@jetpackbot
Copy link

jetpackbot commented Jan 17, 2020

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: February 11, 2020.
Scheduled code freeze: February 4, 2020

Generated by 🚫 dangerJS against 65f08b7

@matticbot
Copy link
Contributor

scruffian, Your synced wpcom patch D37842-code has been updated.

@scruffian scruffian added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Jan 20, 2020
@simison
Copy link
Member

simison commented Jan 21, 2020

I'm sure this has been discussed somewhere and I'm not questioning this, but I'm curious why not just use Gutenberg's built-in styles feature? It also gives similar looking style picker in the sidebar.

@scruffian
Copy link
Member Author

Gutenberg's built in style switcher only deals with changing the class name for the block, not for changing attributes, which is how this works.

@creativecoder
Copy link
Contributor

@scruffian

We're using a similar approach in the Eventbrite block, but using an image rather than a block preview for the "in page" embed option, since that option takes a while to load (during which the preview is blank). https://github.com/Automattic/jetpack/pull/14075/files#diff-67c4a00b3bd3c01b617670a5fd26e417R148

What do you think about making this more generic by using a prop for rendering the preview?

@scruffian
Copy link
Member Author

We could make it more generic by passing in the preview mechanism as a function. However I'd rather keep this component as close to the core component as possible, with the hope of eventually getting it merged in there. We could look into how to integrate with Eventbrite once this (or that) PR merges...

@creativecoder
Copy link
Contributor

However I'd rather keep this component as close to the core component as possible, with the hope of eventually getting it merged in there.

I'd make the same argument in the Core context! The pattern is more broadly usable if it doesn't lock you into a very specific way of rendering a block preview, like it does now. But do what you think is best--what I have now for the Eventbrite block works just fine.

@simison
Copy link
Member

simison commented Jan 22, 2020

Gutenberg's built in style switcher only deals with changing the class name for the block, not for changing attributes, which is how this works.

Oh yea, that's annoying limitation indeed. We used style picker in Tiled gallery block but needed the style information in JS as well. There's a helper for that in core and in Jetpack:

const layoutStyle = getActiveStyleName( LAYOUT_STYLES, attributes.className );

/**
* Returns the active style from the given className.
*
* From @link https://github.com/WordPress/gutenberg/blob/ddac4f3cf8fd311169c7e125411343a437bdbb5a/packages/editor/src/components/block-styles/index.js#L20-L42
*
* @param {Array} styles Block style variations.
* @param {string} className Class name
*
* @return {Object?} The active style.
*/
function getActiveStyle( styles, className ) {
for ( const style of new TokenList( className ).values() ) {
if ( style.indexOf( 'is-style-' ) === -1 ) {
continue;
}
const potentialStyleName = style.substring( 9 );
const activeStyle = find( styles, { name: potentialStyleName } );
if ( activeStyle ) {
return activeStyle;
}
}
return find( styles, 'isDefault' );
}
export function getActiveStyleName( styles, className ) {
const style = getActiveStyle( styles, className );
return style ? style.name : null;
}

By defining styles semantically in the block, you give Gutenberg UI a chance to decide best how to surface that; it might change over time or be surfaced differently in various editor instances (think mobile app version, mobile web version, widgets screen, full site editing mode, writing mode, and generally things we haven't seen yet.)

@scruffian
Copy link
Member Author

@simison I agree, this needs more thought. I think this PR is still valuable in removing the duplicate code, but we should think more about how we interact with block styles - in a different PR!

@simison
Copy link
Member

simison commented Jan 23, 2020

I think this PR is still valuable in removing the duplicate code, but we should think more about how we interact with block styles - in a different PR!

Absolutely! 👍 :-) Just wanted to highlight other mechanisms in case.

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This will need a rebase now, and tests are failing because of the React import.

* External Dependencies
*/
import classnames from 'classnames';
import { memo } from 'React';
Copy link
Member

Choose a reason for hiding this comment

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

This is breaking things:

Module not found: Error: Can't resolve 'React' in '/home/travis/build/Automattic/jetpack/extensions/shared/components/block-styles-selector'

Can we do without all of React here? Can we get it from @wordpress/element instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a useMemo HOC in @wordpress/element, but when I use this I get an error from React. Looking into it some more...

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok I added a commit to take care of this too.

Copy link
Contributor

Choose a reason for hiding this comment

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

We might be able to change this to useMemo but we need useSelect which (as you've seen) also isn't available. It's a shame, but treating it like we have for the changes to BlockPreview makes sense to me.

@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 27, 2020
@jeherve jeherve added this to the 8.3 milestone Jan 27, 2020
@scruffian scruffian force-pushed the update/share-block-style-selector branch from 4ee07e7 to 98a0b3e Compare January 27, 2020 12:40
@matticbot
Copy link
Contributor

scruffian, Your synced wpcom patch D37842-code has been updated.

@scruffian
Copy link
Member Author

I rebased this and its working fine. I added a commit to just hide the preview if the version of WordPress doesn't support it:
Screenshot 2020-01-27 at 15 16 09

This seems like the simplest solution for now.

@scruffian scruffian added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Jan 27, 2020
@scruffian
Copy link
Member Author

I added a fix for #14466 too.

@pablinos
Copy link
Contributor

Looks good, and works as expected. It's a shame about the hooks, but never mind.

@jeherve jeherve modified the milestones: 8.3, 8.2 Jan 29, 2020
@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 29, 2020
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This seems to work well on my end. I'll merge this now, and take care of the matching wpcom reference since it introduces a new file.

Should we now update the Eventbrite block to use this too?

@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Jan 30, 2020
@jeherve jeherve merged commit 36c4883 into master Jan 30, 2020
@jeherve jeherve deleted the update/share-block-style-selector branch January 30, 2020 12:04
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Jan 30, 2020
jeherve pushed a commit that referenced this pull request Jan 30, 2020
* share the block style selector code

* Memoized the style previews to prevent them from rerendering

* remove unused CSS

* Remove unused class

* Dont show block previews unless they are supported

* use the internal memo if it exists

* don't try to render the style selector if there's no valid block

Co-authored-by: Paul Bunkham <[email protected]>
@jeherve
Copy link
Member

jeherve commented Jan 30, 2020

Cherry-picked to branch-8.2 in b878a81

@jeherve
Copy link
Member

jeherve commented Jan 30, 2020

r202313-wpcom

@jeherve
Copy link
Member

jeherve commented Jan 30, 2020

Should we now update the Eventbrite block to use this too?

Logged in #14527

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Calendly [Block] OpenTable [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack Touches WP.com Files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calendly / EventBrite blocks: failing with WordPress 5.2
7 participants