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

Add global style for Product Categories List block #5133

Merged
merged 7 commits into from
Nov 30, 2021

Conversation

gigitux
Copy link
Contributor

@gigitux gigitux commented Nov 12, 2021

This is a part of #4965.

This PR:

  • Adds text color support
  • Adds link color support (WIP)
  • Adds option to change font size (works but there is a problem)
  • Adds option to change line-height

What missing

Link color support

The link color support doesn't work with theme colors. We are investigating this problem (internal discussion p1636652209321000-slack-C02FL3X7KR6)

With useBlockProps hook, I managed to make link color work 👍

Font size

When the user changes the font size, the updated block is not rendered. We are investigating this problem (internal discussion p1636643690311900-slack-C02FL3X7KR6)

When the user changes the font size from the post/page editor everything works correctly. It seems that it doesn't work when the user changes the font size from the site editor (the font size is not applied to any element on the page)

With the last version of Gutenberg, I'm not able to reproduce this problem 👍

image
before - editor
image
before - page

image
current implementation - new options in the editor
image
current implementation - new options in the editor

image
current implementation - block with custom styles

Testing

Manual Testing

How to test the changes in this PR:

Check out this branch:

  1. Install and enable Gutenberg plugin
  2. Install and enable TT1 Blocks theme
  3. Add the All Product Categories list block to a post
  4. Verify you can change the text color, font size, and line-height
  5. Save
  6. Go on the page and check if there are changes
  7. Reset to default
  8. Go to admin panel and click Site Editor
  9. Click on the Global styles icon
  10. Verify the Product Categories List block is shown and you can tweak its styles
  11. Save
  12. Go on the page created earlier and check if all styles are applied correctly
  13. Edit previous post/page again
  14. Change again text color, font size, and line-height
  15. Save
  16. Check if these styles have priority over the styles from the Site editor.

Changelog

Added global styles (text color, link color, line height, and font size) to the Product Title block.

@gigitux gigitux self-assigned this Nov 12, 2021
@rubikuserbot rubikuserbot requested review from a team and dinhtungdu and removed request for a team November 12, 2021 11:48
@gigitux gigitux force-pushed the add/4965-global-style-product-categories-list branch from 530b89d to 7655afc Compare November 12, 2021 11:53
/**
* AttributesUtils class used for getting class and style from attributes.
*/
class AttributesUtils {
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'm not sure about this name. Maybe is it better GlobalStyleUtils?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, or maybe a mix between the two: StyleAttributesUtil? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

StyleAttributesUtils makes more sense to me, because the global part is automatically generated.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 12, 2021

Size Change: +881 B (0%)

Total Size: 1.04 MB

Filename Size Change
build/active-filters.js 7.09 kB -1 B (0%)
build/all-products.js 32.9 kB +2 B (0%)
build/atomic-block-components/price.js 1.69 kB +1 B (0%)
build/attribute-filter.js 10.8 kB +1 B (0%)
build/cart-blocks/line-items-frontend.js 5.12 kB +11 B (0%)
build/cart.js 44.8 kB +4 B (0%)
build/checkout-blocks/order-summary-frontend.js 11.4 kB +13 B (0%)
build/checkout.js 47.9 kB +5 B (0%)
build/featured-category.js 6.67 kB +1 B (0%)
build/mini-cart-component-frontend.js 40.9 kB +7 B (0%)
build/mini-cart-contents.js 1.81 kB -1 B (0%)
build/mini-cart.js 6.23 kB +288 B (+5%) 🔍
build/price-filter.js 8.62 kB +1 B (0%)
build/product-best-sellers.js 5.57 kB +1 B (0%)
build/product-categories.js 3.46 kB +550 B (+19%) ⚠️
build/product-new.js 5.73 kB +1 B (0%)
build/product-top-rated.js 5.7 kB +1 B (0%)
build/products-by-attribute.js 6.61 kB -2 B (0%)
build/reviews-by-category.js 9.96 kB -3 B (0%)
build/reviews-by-product.js 11 kB -1 B (0%)
build/wc-blocks-vendors.js 254 kB +2 B (0%)
ℹ️ View Unchanged
Filename Size
build/active-filters-frontend.js 6.23 kB
build/all-products-frontend.js 22.1 kB
build/all-reviews.js 8.31 kB
build/atomic-block-components/add-to-cart--atomic-block-components/button--atomic-block-components/image---a7e2bb9b.js 2.77 kB
build/atomic-block-components/add-to-cart--atomic-block-components/button.js 1.49 kB
build/atomic-block-components/add-to-cart-frontend.js 6.87 kB
build/atomic-block-components/add-to-cart.js 6.44 kB
build/atomic-block-components/button-frontend.js 1.48 kB
build/atomic-block-components/button.js 851 B
build/atomic-block-components/category-list-frontend.js 457 B
build/atomic-block-components/category-list.js 457 B
build/atomic-block-components/image-frontend.js 1.38 kB
build/atomic-block-components/image.js 1.05 kB
build/atomic-block-components/price-frontend.js 1.74 kB
build/atomic-block-components/rating-frontend.js 553 B
build/atomic-block-components/rating.js 554 B
build/atomic-block-components/sale-badge-frontend.js 626 B
build/atomic-block-components/sale-badge.js 624 B
build/atomic-block-components/sku-frontend.js 385 B
build/atomic-block-components/sku.js 385 B
build/atomic-block-components/stock-indicator-frontend.js 583 B
build/atomic-block-components/stock-indicator.js 586 B
build/atomic-block-components/summary-frontend.js 874 B
build/atomic-block-components/summary.js 874 B
build/atomic-block-components/tag-list-frontend.js 458 B
build/atomic-block-components/tag-list.js 457 B
build/atomic-block-components/title-frontend.js 1.11 kB
build/atomic-block-components/title.js 1.11 kB
build/attribute-filter-frontend.js 16.6 kB
build/blocks-checkout.js 20.8 kB
build/cart-blocks/accepted-payment-methods-frontend.js 1.14 kB
build/cart-blocks/checkout-button-frontend.js 1.14 kB
build/cart-blocks/empty-cart-frontend.js 346 B
build/cart-blocks/express-payment-frontend.js 4.82 kB
build/cart-blocks/filled-cart-frontend.js 768 B
build/cart-blocks/items-frontend.js 298 B
build/cart-blocks/order-summary-frontend.js 8.95 kB
build/cart-blocks/totals-frontend.js 320 B
build/cart-frontend.js 49.1 kB
build/checkout-blocks/actions-frontend.js 1.44 kB
build/checkout-blocks/billing-address--checkout-blocks/shipping-address-frontend.js 4.24 kB
build/checkout-blocks/billing-address-frontend.js 890 B
build/checkout-blocks/contact-information-frontend.js 2.94 kB
build/checkout-blocks/express-payment-frontend.js 5.11 kB
build/checkout-blocks/fields-frontend.js 343 B
build/checkout-blocks/order-note-frontend.js 1.13 kB
build/checkout-blocks/payment-frontend.js 7.49 kB
build/checkout-blocks/shipping-address-frontend.js 976 B
build/checkout-blocks/shipping-methods-frontend.js 4.89 kB
build/checkout-blocks/terms-frontend.js 1.22 kB
build/checkout-blocks/totals-frontend.js 323 B
build/checkout-frontend.js 51.3 kB
build/featured-product.js 8.02 kB
build/handpicked-products.js 5.37 kB
build/legacy-template.js 2.05 kB
build/mini-cart-frontend.js 1.74 kB
build/price-filter-frontend.js 12.4 kB
build/price-format.js 1.19 kB
build/product-category.js 6.44 kB
build/product-on-sale.js 6.11 kB
build/product-search.js 2.47 kB
build/product-tag.js 5.81 kB
build/reviews-frontend.js 7.21 kB
build/single-product-frontend.js 25.5 kB
build/single-product.js 8.5 kB
build/stock-filter-frontend.js 6.81 kB
build/stock-filter.js 6.81 kB
build/vendors--atomic-block-components/add-to-cart--cart-blocks/order-summary--checkout-blocks/billing-ad--c5eb4dcd-frontend.js 16.1 kB
build/vendors--atomic-block-components/add-to-cart-frontend.js 4.45 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/vendors--cart-blocks/order-summary--checkout-blocks/billing-address--checkout-blocks/order-summary---eb4d2cec-frontend.js 5.02 kB
build/wc-blocks-data.js 8.84 kB
build/wc-blocks-editor-style-rtl.css 15.8 kB
build/wc-blocks-editor-style.css 15.8 kB
build/wc-blocks-google-analytics.js 1.56 kB
build/wc-blocks-middleware.js 949 B
build/wc-blocks-registry.js 2.69 kB
build/wc-blocks-shared-context.js 1.51 kB
build/wc-blocks-shared-hocs.js 1.14 kB
build/wc-blocks-style-rtl.css 21.1 kB
build/wc-blocks-style.css 21.1 kB
build/wc-blocks-vendors-style-rtl.css 1.37 kB
build/wc-blocks-vendors-style.css 1.37 kB
build/wc-blocks.js 2.96 kB
build/wc-payment-method-bacs.js 820 B
build/wc-payment-method-cheque.js 816 B
build/wc-payment-method-cod.js 912 B
build/wc-payment-method-paypal.js 838 B
build/wc-payment-method-stripe.js 11.1 kB
build/wc-settings.js 2.58 kB

compressed-size-action

@gigitux gigitux requested a review from Aljullu November 12, 2021 11:58
@gigitux gigitux added block: product categories list Issues related to the Product Categories List 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. labels Nov 12, 2021
@gigitux gigitux mentioned this pull request Nov 12, 2021
18 tasks
Copy link
Contributor

@Aljullu Aljullu left a comment

Choose a reason for hiding this comment

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

Good job @gigitux adding global styles to the Product Categories List block. The utils class you created can be very useful for other PHP-rendered blocks. 👏

The link color support doesn't work with theme colors.

I think that's because we need to add this class to the block: wp-block-woocommerce-product-categories. By default, Gutenberg appends a class like this to all blocks, but because we are rendering it in PHP, we need to do it manually. I think once you add it, the link styles will apply correctly.

When the user changes the font size, the updated block is not rendered.

I'm not able to reproduce this issue (tested with Gutenberg trunk and theme Quadrat).

I added some other comments below, but overall the approach looks good to me. 👌

@@ -13,6 +13,7 @@ import './style.scss';
import Block from './block.js';

registerBlockType( 'woocommerce/product-categories', {
apiVersion: 2,
Copy link
Contributor

Choose a reason for hiding this comment

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

With apiVersion: 2, you will need to pass useBlockProps() to the edit component. Similarly to how it's done here:

https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/trunk/assets/js/blocks/reviews/editor-container-block.js#L58

(I think you will need to replace the fragment <> with a wrapping div)


$output = '<div class="' . esc_attr( $classes ) . '">';
$output = '<div class="' . esc_attr( $classes ) . '" style="' . $styles . '">';
Copy link
Contributor

Choose a reason for hiding this comment

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

We should escape every variable added to the output:

Suggested change
$output = '<div class="' . esc_attr( $classes ) . '" style="' . $styles . '">';
$output = '<div class="' . esc_attr( $classes ) . '" style="' . esc_attr( $styles ) . '">';

/**
* AttributesUtils class used for getting class and style from attributes.
*/
class AttributesUtils {
Copy link
Contributor

Choose a reason for hiding this comment

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

Agree, or maybe a mix between the two: StyleAttributesUtil? 🤔

public static function get_font_size_class_and_style( $attributes ) {

$font_size = $attributes['fontSize'];
$custom_font_size = $attributes['style']['typography']['fontSize'];
Copy link
Contributor

Choose a reason for hiding this comment

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

If you set define( 'WP_DEBUG', 'True' ); in your site's wp-config.php, you will probably see errors appear on the screen because of this line. That's because typography might be missing inside the style object.

I think you will need to use array_key_exists() (or something similar?) every time you try to access an attribute inside $attributes['style'].

Comment on lines 41 to 44
return array(
'class' => null,
'style' => null,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, should we just return null?

foreach ( $categories as $category ) {
$output .= '
<li class="wc-block-product-categories-list-item">
<a href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>
<a style="' . $link_color_class_and_style['style'] . '" class="' . $link_color_class_and_style['class'] . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too, we should escape all attributes:

Suggested change
<a style="' . $link_color_class_and_style['style'] . '" class="' . $link_color_class_and_style['class'] . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>
<a style="' . esc_attr( $link_color_class_and_style['style'] ) . '" class="' . esc_attr( $link_color_class_and_style['class'] ) . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>

$font_size_class_and_style = AttributesUtils::get_font_size_class_and_style( $attributes );

$classes = array_filter(
[ $line_height_class_and_style['class'], $text_color_class_and_style['class'], $font_size_class_and_style['class'] ],
Copy link
Contributor

Choose a reason for hiding this comment

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

$line_height_class_and_style might be null (according to this line), so here you will also need to check if class exists before using it.

Same for the other objects.

*
* @param array $attributes Block attributes.
*
* @return string
Copy link
Member

Choose a reason for hiding this comment

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

We should update this comment to match the actual return value types.

Comment on lines 135 to 140
return array_filter(
$classes_and_styles,
function ( $var ) {
return ! is_null( $var );
}
);
Copy link
Member

Choose a reason for hiding this comment

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

array_filter default callbackup already removes all falsy items, so we can use it without the callback.

Suggested change
return array_filter(
$classes_and_styles,
function ( $var ) {
return ! is_null( $var );
}
);
return array_filter( $classes_and_styles );

@gigitux gigitux force-pushed the add/4965-global-style-product-categories-list branch 2 times, most recently from 02dd6e6 to fe77684 Compare November 16, 2021 11:56
@gigitux gigitux marked this pull request as ready for review November 16, 2021 13:11
Add global style for product categories list block
@gigitux gigitux force-pushed the add/4965-global-style-product-categories-list branch from fe77684 to eeb0d39 Compare November 16, 2021 13:13
@gigitux
Copy link
Contributor Author

gigitux commented Nov 16, 2021

Thanks for your review. I fix the code.
I edited the description because:

  • With useBlockProps hook, I managed to make link color work 👍
  • When the user changes the font size from the post/page editor everything works correctly. It seems that it doesn't work when the user changes the font size from the site editor (the font size is not applied to any element on the page)

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! The block style is working on my end, both editor and the front end. But the global style only works in the editor. We have this issue because the wrapper element of the block on the frontend doesn't have wp-block-woocommerce-product-categories class.

foreach ( $categories as $category ) {
$output .= '
<li class="wc-block-product-categories-list-item">
<a href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>
<a style="' . esc_attr( $link_color_style ) . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here


$output = '<div class="' . esc_attr( $classes ) . '">';
$output = '<div class="wp-block-woocommerce-product-categories ' . esc_attr( $classes ) . '" style="' . esc_attr( $styles ) . '">';
Copy link
Contributor Author

@gigitux gigitux Nov 17, 2021

Choose a reason for hiding this comment

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

There is a possibility that the style property will be empty. Do you think that is it necessary to exclude style property when the variable $styles is empty?
The same concept for class property and variable $classes

Copy link
Member

Choose a reason for hiding this comment

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

@gigitux Empty style or class attribute is fine to me. But if you prefer a cleaner HTML output, go for it. I prefer empty attributes because of the readability. We will need some if condition checks to exclude the empty attributes, which makes the code harder to read IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We will need some if condition checks to exclude the empty attributes, which makes the code harder to read IMO.

I agree with you! In any case, I preferred to point it out to 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 This is working great on my end, both local and global block styles are working. I left some comments about coding standards and minor issues, please take a look.

'fontSize' => $this->get_schema_string(),
'lineHeight' => $this->get_schema_string(),
'style' => array( 'type' => 'object' ),

Copy link
Member

Choose a reason for hiding this comment

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

This empty line should be removed.

Comment on lines 150 to 151


Copy link
Member

Choose a reason for hiding this comment

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

Extra empty lines here should be removed too.

$string_classes = implode( ' ', $classes );
$string_styles = implode( ' ', $styles );

return array( $string_classes, $string_styles );
Copy link
Member

Choose a reason for hiding this comment

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

You're changing the return data type of this function, so you should update the comment docs block as well.

By the way, this method now returns an array of classes and styles, then the method name should be updated to reflect the return data.

$font_size_class = isset( $font_size_class_and_style['class'] ) ? $font_size_class_and_style['class'] : null;

$classes = array_filter(
[ $line_height_class, $text_color_class, $font_size_class ]
Copy link
Member

Choose a reason for hiding this comment

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

We should use only one array style. Same for line 141 below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean with use only one array style. Maybe do you mean that I should use array?

Suggested change
[ $line_height_class, $text_color_class, $font_size_class ]
array($line_height_class, $text_color_class, $font_size_class)

Copy link
Member

Choose a reason for hiding this comment

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

@gigitux Yeah, I mean the syntax, we should use one style to define arrays.

Comment on lines 348 to 349
<a style="' . esc_attr( $link_color_style ) . '" href="' . esc_attr( get_term_link( $category->term_id, 'product_cat' ) ) . '">' . $this->get_image_html( $category, $attributes ) . esc_html( $category->name ) . '</a>
' . $this->getCount( $category, $attributes ) . '
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why the indentation of these lines needs to change. They look correct.

@@ -1,11 +1,15 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;


Copy link
Member

Choose a reason for hiding this comment

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

Another extra line.

@@ -301,11 +338,15 @@ protected function renderList( $categories, $attributes, $uid, $depth = 0 ) {
protected function renderListItems( $categories, $attributes, $uid, $depth = 0 ) {
$output = '';

$link_color_class_and_style = StyleAttributesUtils::get_link_color_class_and_style( $attributes );

$link_color_style = $link_color_class_and_style['style'];
Copy link
Member

Choose a reason for hiding this comment

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

This throws a warning on my end: Warning: Trying to access array offset on value of type null in /wp-content/plugins/woocommerce-gutenberg-products-block/src/BlockTypes/ProductCategories.php on line 343.

@gigitux gigitux force-pushed the add/4965-global-style-product-categories-list branch from f2096f6 to bbd1bfa Compare November 24, 2021 10:50
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.

Hi @gigitux I'm not sure if you finished updating the PR but I left some more comments on the latest changes.

@@ -90,9 +99,10 @@ protected function render( $attributes, $content ) {
* Get the list of classes to apply to this block.
Copy link
Member

Choose a reason for hiding this comment

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

This comment needs to be updated as well.

Comment on lines 158 to 159


Copy link
Member

Choose a reason for hiding this comment

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

Consecutive empty lines here.

Comment on lines 147 to 150
line_height => self::get_line_height_class_and_style( $attributes ),
text_color => self::get_text_color_class_and_style( $attributes ),
font_size => self::get_font_size_class_and_style( $attributes ),
link_color => self::get_link_color_class_and_style( $attributes ),
Copy link
Member

Choose a reason for hiding this comment

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

The array keys need to be string

Comment on lines 113 to 114


Copy link
Member

Choose a reason for hiding this comment

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

Consecutive empty lines here.

Comment on lines 8 to 9


Copy link
Member

Choose a reason for hiding this comment

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

Consecutive empty lines here.

Comment on lines 122 to 140
$line_height_class_and_style = StyleAttributesUtils::get_line_height_class_and_style( $attributes );
$text_color_class_and_style = StyleAttributesUtils::get_text_color_class_and_style( $attributes );
$font_size_class_and_style = StyleAttributesUtils::get_font_size_class_and_style( $attributes );

$line_height_class = isset( $line_height_class_and_style['class'] ) ? $line_height_class_and_style['class'] : null;
$text_color_class = isset( $text_color_class_and_style['class'] ) ? $text_color_class_and_style['class'] : null;
$font_size_class = isset( $font_size_class_and_style['class'] ) ? $font_size_class_and_style['class'] : null;

$classes = array_filter(
array( $line_height_class, $text_color_class, $font_size_class )
);

$line_height_style = isset( $line_height_class_and_style['style'] ) ? $line_height_class_and_style['style'] : null;
$text_color_style = isset( $text_color_class_and_style['style'] ) ? $text_color_class_and_style['style'] : null;
$font_size_style = isset( $font_size_class_and_style['style'] ) ? $font_size_class_and_style['style'] : null;

$styles = array_filter(
array( $line_height_style, $text_color_style, $font_size_style )
);
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if we can use StyleAttributesUtils:get_classes_and_styles_by_attributes here? Something like this:

Suggested change
$line_height_class_and_style = StyleAttributesUtils::get_line_height_class_and_style( $attributes );
$text_color_class_and_style = StyleAttributesUtils::get_text_color_class_and_style( $attributes );
$font_size_class_and_style = StyleAttributesUtils::get_font_size_class_and_style( $attributes );
$line_height_class = isset( $line_height_class_and_style['class'] ) ? $line_height_class_and_style['class'] : null;
$text_color_class = isset( $text_color_class_and_style['class'] ) ? $text_color_class_and_style['class'] : null;
$font_size_class = isset( $font_size_class_and_style['class'] ) ? $font_size_class_and_style['class'] : null;
$classes = array_filter(
array( $line_height_class, $text_color_class, $font_size_class )
);
$line_height_style = isset( $line_height_class_and_style['style'] ) ? $line_height_class_and_style['style'] : null;
$text_color_style = isset( $text_color_class_and_style['style'] ) ? $text_color_class_and_style['style'] : null;
$font_size_style = isset( $font_size_class_and_style['style'] ) ? $font_size_class_and_style['style'] : null;
$styles = array_filter(
array( $line_height_style, $text_color_style, $font_size_style )
);
foreach( StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes ) as $key => $item ) {
// We skip link_color for wrapper element.
if ( 'link_color' === $key ) {
continue;
}
if ( ! empty( $item['class'] ) ) {
$classes[] = $item['class'];
}
if ( ! empty( $item['style'] ) ) {
$styles[] = $item['style'];
}
}

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'm wondering if we can use StyleAttributesUtils:get_classes_and_styles_by_attributes here? Something like this:

Maybe in the future, we could add more attributes and if this happens we should update this foreach. I would prefer to use an immutable approach. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@gigitux I agree with an immutable approach. But I think we still need too much code to calculate classes/styles string to use in markup. I have an idea to refactor the util in #5277, please take a look!

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 agree with an immutable approach. But I think we still need too much code to calculate classes/styles string to use in markup. I have an idea to refactor the util in #5277, please take a look!

Done and approved! Thanks for your great feedback!

@gigitux gigitux force-pushed the add/4965-global-style-product-categories-list branch from 4f67958 to 2b96117 Compare November 29, 2021 09:51
@gigitux gigitux requested a review from dinhtungdu November 29, 2021 10:27
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.

LGTM 🚢

@github-actions github-actions bot added this to the 6.5.0 milestone Nov 30, 2021
@gigitux gigitux merged commit d1306f2 into trunk Nov 30, 2021
@gigitux gigitux deleted the add/4965-global-style-product-categories-list branch November 30, 2021 15:28
jonny-bull pushed a commit to jonny-bull/woocommerce-gutenberg-products-block that referenced this pull request Dec 14, 2021
* Add global style for product categories list block woocommerce#4965

Add global style for product categories list block

* add support for link color

* add feature flag

* fix code style and PHP warning

* update doc comment

* remove empty space

* refactor StyleAttributesUtils (woocommerce#5277)

Co-authored-by: Tung Du <[email protected]>
jonny-bull pushed a commit to jonny-bull/woocommerce-gutenberg-products-block that referenced this pull request Dec 16, 2021
* Add global style for product categories list block woocommerce#4965

Add global style for product categories list block

* add support for link color

* add feature flag

* fix code style and PHP warning

* update doc comment

* remove empty space

* refactor StyleAttributesUtils (woocommerce#5277)

Co-authored-by: Tung Du <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
block: product categories list Issues related to the Product Categories List 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.

3 participants