Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Product Title block: add support global style #5515

Merged
merged 8 commits into from
Jan 12, 2022
Merged

Conversation

gigitux
Copy link
Contributor

@gigitux gigitux commented Jan 4, 2022

This PR adds support for global style for the Product Title block.

Now, the user can edit the style for:

  • line-height
  • font family
  • font-weight
  • text transform
  • background-color
  • margin
  • font-size

Fixes #4965

Screenshots

image
without global style

image
with global style

Testing

Manual Testing

Check out this branch:

  1. Install and enable the Gutenberg plugin on WordPress 5.9 site.
  2. Install and enable the Twenty Twenty-Two theme.
  3. Add the Single Product block (this block contains Product Title Block) to a post.
  4. Get the focus on Product Title block.
  5. On the right sidebar, personalize the styles of the block.
  6. Go on the page and check if there are changes.
  7. Reset to default using the Reset button from the different sections.
  8. Go to Dashboard and select Appearance > Editor (beta).
  9. On the Editor page click on the Styles icon on the right-top corner.
  10. Verify that the Product Title block is shown under the Blocks section. Personalize again the block.
  11. Save your changes.
  12. Go on the page created earlier and check if all styles are applied correctly.
  13. Edit your previous post/page again.
  14. Change again the styles.
  15. Save your changes.
  16. Check if these styles have priority over the styles from the Site Editor.

Changelog

Add support for the global style for the Product Title block.

@gigitux gigitux self-assigned this Jan 4, 2022
@rubikuserbot rubikuserbot requested review from a team and removed request for a team January 4, 2022 14:22
@gigitux gigitux added the focus: global styles Issues that involve styles/css/layout structure. label Jan 4, 2022
@gigitux gigitux added focus: FSE Work related to prepare WooCommerce for FSE. block: single product Issues related to the single product block. type: enhancement The issue is a request for an enhancement. and removed type: enhancement The issue is a request for an enhancement. labels Jan 4, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2022

Size Change: +445 B (0%)

Total Size: 815 kB

Filename Size Change
build/active-filters-frontend.js 6.21 kB -1 B (0%)
build/active-filters.js 7.1 kB +2 B (0%)
build/all-products-frontend.js 18.6 kB -3 B (0%)
build/all-products.js 34.8 kB +207 B (+1%)
build/all-reviews.js 8.35 kB +2 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.76 kB -1 B (0%)
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.48 kB +1 B (0%)
build/atomic-block-components/button.js 853 B +1 B (0%)
build/atomic-block-components/image.js 1.05 kB +1 B (0%)
build/atomic-block-components/price-frontend.js 1.74 kB +5 B (0%)
build/atomic-block-components/sale-badge.js 620 B -2 B (0%)
build/atomic-block-components/sku.js 385 B -1 B (0%)
build/atomic-block-components/summary.js 873 B +1 B (0%)
build/atomic-block-components/tag-list.js 459 B +1 B (0%)
build/atomic-block-components/title-frontend.js 1.15 kB +41 B (+4%)
build/atomic-block-components/title.js 1.19 kB +81 B (+7%) 🔍
build/attribute-filter-frontend.js 16.3 kB +1 B (0%)
build/attribute-filter.js 12.6 kB -2 B (0%)
build/cart-blocks/express-payment-frontend.js 4.86 kB +2 B (0%)
build/cart-blocks/filled-cart-frontend.js 763 B -3 B (0%)
build/cart-blocks/line-items-frontend.js 5.48 kB +1 B (0%)
build/cart-blocks/order-summary-frontend.js 8.97 kB -3 B (0%)
build/cart-frontend.js 45.4 kB -7 B (0%)
build/cart.js 44.5 kB +4 B (0%)
build/checkout-blocks/billing-address-frontend.js 886 B +2 B (0%)
build/checkout-blocks/express-payment-frontend.js 5.15 kB +1 B (0%)
build/checkout-blocks/order-note-frontend.js 1.13 kB -1 B (0%)
build/checkout-blocks/order-summary-frontend.js 11.4 kB +3 B (0%)
build/checkout-blocks/payment-frontend.js 7.39 kB +3 B (0%)
build/checkout-blocks/shipping-address-frontend.js 972 B +1 B (0%)
build/checkout-blocks/shipping-methods-frontend.js 4.82 kB +2 B (0%)
build/checkout-frontend.js 47.5 kB -7 B (0%)
build/featured-category.js 8.55 kB -1 B (0%)
build/featured-product.js 9.91 kB -2 B (0%)
build/handpicked-products.js 7.33 kB +3 B (0%)
build/legacy-template.js 2.19 kB +3 B (0%)
build/mini-cart-contents.js 3.6 kB -4 B (0%)
build/mini-cart.js 6.46 kB -2 B (0%)
build/product-best-sellers.js 7.54 kB +1 B (0%)
build/product-category.js 9.11 kB +1 B (0%)
build/product-new.js 8.44 kB -2 B (0%)
build/product-search.js 2.47 kB -1 B (0%)
build/product-tag.js 8.5 kB +6 B (0%)
build/product-top-rated.js 8.41 kB +1 B (0%)
build/products-by-attribute.js 9.22 kB +1 B (0%)
build/reviews-by-category.js 11.8 kB -1 B (0%)
build/reviews-by-product.js 12.9 kB +2 B (0%)
build/single-product-frontend.js 22.1 kB -2 B (0%)
build/single-product.js 10.4 kB +11 B (0%)
build/stock-filter.js 6.83 kB -1 B (0%)
build/vendors--atomic-block-components/add-to-cart--cart-blocks/order-summary--checkout-blocks/billing-ad--c5eb4dcd-frontend.js 19 kB +2 B (0%)
build/vendors--atomic-block-components/add-to-cart-frontend.js 7.43 kB -2 B (0%)
build/vendors--cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/order-summary---eb4d2cec-frontend.js 4.75 kB +1 B (0%)
build/wc-blocks-editor-style-rtl.css 4.57 kB +13 B (0%)
build/wc-blocks-editor-style.css 4.57 kB +14 B (0%)
build/wc-blocks-style-rtl.css 21.7 kB +34 B (0%)
build/wc-blocks-style.css 21.7 kB +34 B (0%)
build/wc-blocks-vendors.js 65.6 kB +3 B (0%)
build/wc-blocks.js 2.96 kB +1 B (0%)
ℹ️ View Unchanged
Filename Size
build/atomic-block-components/add-to-cart-frontend.js 7.05 kB
build/atomic-block-components/add-to-cart.js 6.62 kB
build/atomic-block-components/button-frontend.js 1.48 kB
build/atomic-block-components/category-list-frontend.js 458 B
build/atomic-block-components/category-list.js 458 B
build/atomic-block-components/image-frontend.js 1.37 kB
build/atomic-block-components/price.js 1.7 kB
build/atomic-block-components/rating-frontend.js 553 B
build/atomic-block-components/rating.js 555 B
build/atomic-block-components/sale-badge-frontend.js 625 B
build/atomic-block-components/sku-frontend.js 386 B
build/atomic-block-components/stock-indicator-frontend.js 585 B
build/atomic-block-components/stock-indicator.js 585 B
build/atomic-block-components/summary-frontend.js 874 B
build/atomic-block-components/tag-list-frontend.js 460 B
build/blocks-checkout.js 17.6 kB
build/cart-blocks/accepted-payment-methods-frontend.js 1.15 kB
build/cart-blocks/checkout-button-frontend.js 1.14 kB
build/cart-blocks/empty-cart-frontend.js 345 B
build/cart-blocks/items-frontend.js 298 B
build/cart-blocks/totals-frontend.js 320 B
build/checkout-blocks/actions-frontend.js 1.44 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.22 kB
build/checkout-blocks/contact-information-frontend.js 2.94 kB
build/checkout-blocks/fields-frontend.js 343 B
build/checkout-blocks/terms-frontend.js 1.21 kB
build/checkout-blocks/totals-frontend.js 324 B
build/checkout.js 47.1 kB
build/mini-cart-component-frontend.js 14.2 kB
build/mini-cart-frontend.js 1.76 kB
build/price-filter-frontend.js 12.6 kB
build/price-filter.js 8.77 kB
build/price-format.js 1.18 kB
build/product-categories.js 3.48 kB
build/product-on-sale.js 8.8 kB
build/reviews-frontend.js 7.24 kB
build/stock-filter-frontend.js 6.81 kB
build/vendors--atomic-block-components/price--cart-blocks/line-items--cart-blocks/order-summary--checkout--8a3571de-frontend.js 5.71 kB
build/vendors--cart-blocks/line-items--checkout-blocks/order-summary-frontend.js 3.14 kB
build/wc-blocks-data.js 8.84 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 949 B
build/wc-blocks-registry.js 2.7 kB
build/wc-blocks-shared-context.js 1.52 kB
build/wc-blocks-shared-hocs.js 1.14 kB
build/wc-blocks-vendors-style-rtl.css 1.28 kB
build/wc-blocks-vendors-style.css 1.28 kB
build/wc-payment-method-bacs.js 816 B
build/wc-payment-method-cheque.js 811 B
build/wc-payment-method-cod.js 909 B
build/wc-payment-method-paypal.js 837 B
build/wc-settings.js 2.61 kB

compressed-size-action

@gigitux gigitux force-pushed the add/4965-product-title branch from 00b3803 to 5e477db Compare January 4, 2022 14:33
Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

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

The block style is working for me 🎉 , both color and typography are working. But for the global style, the background color didn't work in my test. Please check the screencast for me detail.

Screen.Recording.2022-01-05.at.16.49.06.mov

} ),
},
color: {
text: false,
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why text color is disabled here? Given that we allow changing the background, it makes more sense to me to allow changing text color too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By #4965, the color for the text should be disabled, but I agree with you.
I added the support for text color, too, now I will add a comment on the main issue.

Comment on lines 38 to 40
const getValueOrDefault = ( value: unknown ) => {
return isString( value ) && value.length > 0 ? value : '0';
};
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should return 0 as the default value here, it should be undefined instead. The element may have spacing rules defined in CSS, using 0 will override its styling.

Comment on lines 48 to 57
return {
...acc,
[ key ]: `${ getValueOrDefault(
spacingProperty.top
) } ${ getValueOrDefault(
spacingProperty.right
) } ${ getValueOrDefault(
spacingProperty.bottom
) } ${ getValueOrDefault( spacingProperty.left ) }`,
};
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, we should only use shorthand when all four sides are set, otherwise, we should use single side rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this! I updated the function!

@gigitux
Copy link
Contributor Author

gigitux commented Jan 5, 2022

The block style is working for me 🎉 , both color and typography are working. But for the global style, the background color didn't work in my test. Please check the screencast for me detail.

Screen.Recording.2022-01-05.at.16.49.06.mov

Mmm, it seems that it is a GB problem. What version are you using?

@dinhtungdu
Copy link
Member

Mmm, it seems that it is a GB problem. What version are you using?

@gigitux I used the latest trunk (I've just tested again).

@gigitux
Copy link
Contributor Author

gigitux commented Jan 5, 2022

Mmm, it seems that it is a GB problem. What version are you using?

@gigitux I used the latest trunk (I've just tested again).

Mmm, it is a very strange problem.
https://user-images.githubusercontent.com/4463174/148233284-f73cd082-0da4-4fc1-b5ee-64e74c1ddd7a.mp4
If you see my video, I don't have this problem. Same with TT1 theme.

I updated the PR with your feedback. I also modified the CSS, because before I hadn't edited the save function to be compliant with apiVersion 2: now everything should be ok 👍

@dinhtungdu
Copy link
Member

Mmm, it is a very strange problem.
user-images.githubusercontent.com/4463174/148233284-f73cd082-0da4-4fc1-b5ee-64e74c1ddd7a.mp4
If you see my video, I don't have this problem. Same with TT1 theme.

@gigitux Which WP version are you using?

@gigitux
Copy link
Contributor Author

gigitux commented Jan 6, 2022

@gigitux Which WP version are you using?

WordPress 5.9 - beta 4. What about you?

Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

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

@gigitux With a fresh installation, the global style works as expected now. I left some comments about the utilities, please take a look.

Comment on lines 71 to 76
...getSpacingStyleInline( key, {
top: getValueOrDefault( spacingProperty.top ),
right: getValueOrDefault( spacingProperty.right ),
bottom: getValueOrDefault( spacingProperty.bottom ),
left: getValueOrDefault( spacingProperty.left ),
} ),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
...getSpacingStyleInline( key, {
top: getValueOrDefault( spacingProperty.top ),
right: getValueOrDefault( spacingProperty.right ),
bottom: getValueOrDefault( spacingProperty.bottom ),
left: getValueOrDefault( spacingProperty.left ),
} ),
...getSpacingStyleInline( key, spacingProperty ),

Copy link
Member

Choose a reason for hiding this comment

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

I think we can pass spacingProperty as the second argument then handle sides there instead. It makes the callback look cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can pass spacingProperty as the second argument then handle sides there instead. It makes the callback look cleaner.

It can be an idea, but to be honest I prefer to create a more strict typed code.

image

With this code (so without type), we lose some information about how the function getSpacingStyleInline works.
I mean without an object with top, right, bottom, and left key, it doesn't make sense invoke getSpacingStyleInlinefunction. With the typeRecord<string, unknown>` we lose this information and the developer is forced to read the whole function to understand what kind of data to pass.

But this is just my opinion, if you think that it is better to change it, I will do it :D

Copy link
Member

Choose a reason for hiding this comment

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

@gigitux With your example, I don't see the need for getSpacingStyleInline, we can add the inline style directly like this. Note that we can use undefined properties just fine because they will get omitted during render.

		if ( keys.includes( key ) ) {
			return {
				...acc,
				[ `${ key }Left` ]: spacingProperty.left,
				[ `${ key }Right` ]: spacingProperty.right,
				[ `${ key }Top` ]: spacingProperty.top,
				[ `${ key }Bottom` ]: spacingProperty.bottom,
			};
		}

Comment on lines 43 to 50
...( isString( values.top ) && { [ `${ key }Top` ]: values.top } ),
...( isString( values.right ) && {
[ `${ key }Right` ]: values.right,
} ),
...( isString( values.bottom ) && {
[ `${ key }Bottom` ]: values.bottom,
} ),
...( isString( values.left ) && { [ `${ key }Left` ]: values.left } ),
Copy link
Member

Choose a reason for hiding this comment

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

Can we use key: value here then remove the null properties? We can even return style object with null properties because they will be omitted during render. Using spread here is quite hard to read IMO.

assets/js/utils/style-attributes-utils.ts Outdated Show resolved Hide resolved
@gigitux gigitux requested a review from dinhtungdu January 6, 2022 13:30
@dinhtungdu
Copy link
Member

dinhtungdu commented Jan 6, 2022

@gigitux Looking at https://github.com/WordPress/gutenberg/blob/0666dee9235aedd3d1fa51c733d0c1e146227452/packages/block-library/src/button/edit.js#L26, I'm wondering if we can reuse the spacing handler in Gutenberg instead of creating our own utilities? Like what we do with useColorProps.

Edit: By the way, we're creating custom hooks, so style-attributes-utils.ts may not be the best place for those hooks.

Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

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

@gigitux Great work on this PR 👏🏽 !

Update: Unfortunately the API __experimentalGetSpacingClassesAndStyles is not available on WP 5.8. What should we do? Should we maintain the previous code for WordPress 5.8 and use the new hook for WordPress 5.9?

As long as our custom hook is feature-gated and no fatal error throws on WP 5.8, I think we can just focus on 5.9, it's not worth the effort to support 5.8 given that most of the global style functionalities are available from 5.9. cc @Aljullu for more insights : ).

@github-actions github-actions bot added this to the 6.8.0 milestone Jan 7, 2022
@gigitux
Copy link
Contributor Author

gigitux commented Jan 7, 2022

As long as our custom hook is feature-gated and no fatal error throws on WP 5.8, I think we can just focus on 5.9, it's not worth the effort to support 5.8 given that most of the global style functionalities are available from 5.9. cc @Aljullu for more insights : ).

Mmm, we have got a fatal error on WP 5.8 (without GB)
image

The error:
image

This bug is caused by __experimentalGetSpacingClassesAndStyles

@Aljullu
Copy link
Contributor

Aljullu commented Jan 7, 2022

As long as our custom hook is feature-gated and no fatal error throws on WP 5.8, I think we can just focus on 5.9, it's not worth the effort to support 5.8 given that most of the global style functionalities are available from 5.9. cc @Aljullu for more insights : ).

Agree with this. 🙂

This bug is caused by __experimentalGetSpacingClassesAndStyles

Would it be possible to check if __experimentalGetSpacingClassesAndStyles exists at this point, and return early if it doesn't?

Then, we might probably need to do the same check here, so controls don't appear in the editor if the function doesn't exist.

@gigitux
Copy link
Contributor Author

gigitux commented Jan 7, 2022

Would it be possible to check if __experimentalGetSpacingClassesAndStyles exists at this point, and return early if it doesn't?

Then, we might probably need to do the same check here, so controls don't appear in the editor if the function doesn't exist.

Yes! Sure! I will add this check! Thanks for the feedback!

@gigitux gigitux force-pushed the add/4965-product-title branch 3 times, most recently from fa8c990 to 18642e6 Compare January 7, 2022 17:14
@gigitux gigitux requested a review from dinhtungdu January 7, 2022 17:15
@gigitux
Copy link
Contributor Author

gigitux commented Jan 7, 2022

@Aljullu @dinhtungdu I just added a utility function 18642e6 to check if there is support for the spacing style support.

Let me know, what do you think!

Comment on lines 31 to 55
...( isFeaturePluginBuild() && {
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontWeight: true,
__experimentalTextTransform: true,
__experimentalFontFamily: true,
},
} ),
...( isFeaturePluginBuild() && {
color: {
text: true,
background: true,
link: false,
gradients: true,
__experimentalSkipSerialization: true,
},
} ),
...( isFeaturePluginBuild() &&
hasSpacingStyleSupport() && {
spacing: {
margin: true,
__experimentalSkipSerialization: true,
},
typography: {
fontSize: true,
},
}
: sharedConfig.supports,
} ),
Copy link
Member

Choose a reason for hiding this comment

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

Did html: false get removed on purpose?

By the way, this part can be rewritten to:

		...sharedConfig.supports,
		...( isFeaturePluginBuild() && {
			typography: {
				fontSize: true,
				lineHeight: true,
				__experimentalFontWeight: true,
				__experimentalTextTransform: true,
				__experimentalFontFamily: true,
			},
			color: {
				text: true,
				background: true,
				link: false,
				gradients: true,
				__experimentalSkipSerialization: true,
			},
			...( hasSpacingStyleSupport() && {
				spacing: {
					margin: true,
					__experimentalSkipSerialization: true,
				},
			} ),
		} ),

Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

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

@gigitux In my test with the latest trunk of Gutenberg, WP 5.9 beta 4, and WC 6.1.0-dev, the text color works for block style but doesn't work for global style.

Screen.Recording.2022-01-11.at.22.45.42.mov

@gigitux
Copy link
Contributor Author

gigitux commented Jan 11, 2022

@gigitux In my test with the latest trunk of Gutenberg, WP 5.9 beta 4, and WC 6.1.0-dev, the text color works for block style but doesn't work for global style.

Screen.Recording.2022-01-11.at.22.45.42.mov

Thanks for your feedback. If you save the custom color and you check the frontend, you will be able to see the custom color.

The problem, in this case, is that Gutenberg injects custom style for all anchor elements on global style
image

We are not sure that this element is an anchor element because the user can enable or disable the link feature: if we want to fix the problem, we should enable both link and text color. To be honest, I think that it is not a great UX for the user, but from what I understand in this weeks there isn't an alternative.

@dinhtungdu
Copy link
Member

dinhtungdu commented Jan 12, 2022

The problem, in this case, is that Gutenberg injects custom style for all anchor elements on global style

@gigitux I opened #5548 to fix this issue by overriding the style for the anchor of the title in the editor. Please take a look!

We are not sure that this element is an anchor element because the user can enable or disable the link feature: if we want to fix the problem, we should enable both link and text color. To be honest, I think that it is not a great UX for the user, but from what I understand in this weeks there isn't an alternative.

I think the title should use the same color setting regardless it's a link or not. Enable link color makes the block more complex.

@gigitux
Copy link
Contributor Author

gigitux commented Jan 12, 2022

I opened #5548 to fix this issue by overriding the style for the anchor of the title in the editor. Please take a look!

Thanks for the help! I didn't know that the editor.scss is loaded on the FSE editor page too. Thanks for the help!

Let me know if I can merge this PR!

Copy link
Member

@dinhtungdu dinhtungdu left a comment

Choose a reason for hiding this comment

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

:shipit:

@gigitux gigitux merged commit 345bfcd into trunk Jan 12, 2022
@gigitux gigitux deleted the add/4965-product-title branch January 12, 2022 13:33
@gigitux gigitux added the type: enhancement The issue is a request for an enhancement. label Jan 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: single product Issues related to the single product block. focus: FSE Work related to prepare WooCommerce for FSE. focus: global styles Issues that involve styles/css/layout structure. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add global styles to all WC Blocks
3 participants