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

Rating Product block: Add support for global style #5521

Merged
merged 14 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion assets/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
left: 0;
right: 0;
position: absolute;
color: #000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Removing this line changes the color of the stars in product grid blocks.

All Products block without styles applied:

Before After
imatge imatge

Top Rated Products block:

Before After
imatge imatge

Do you think there is any way to keep this declaration while getting global styles to work?

Copy link
Contributor Author

@gigitux gigitux Feb 8, 2022

Choose a reason for hiding this comment

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

Thanks for catching this!

I restored the previous CSS with 84f00de commit.

I forgot that we have some components that display product ratings on the PHP side (as ´Top Rated Products` block). Now the global style will be applied on PHP side blocks too (but as default the color will be black)

I'm not sure about the screenshot that you shared.
image

I checked the trunk and I see always black stars. Can you check again?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, not sure what I did to get that color. 😅 It was in Storefront but I checked it again and it's dark-gray as the other blocks.

white-space: nowrap;
}
}
Expand Down
7 changes: 6 additions & 1 deletion assets/js/atomic/blocks/product-elements/rating/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { withProductDataContext } from '@woocommerce/shared-hocs';
* Internal dependencies
*/
import './style.scss';
import { useColorProps } from '../../../../hooks/style-attributes';

/**
* Product Rating Block Component.
Expand All @@ -22,10 +23,12 @@ import './style.scss';
* @param {string} [props.className] CSS Class name for the component.
* @return {*} The component.
*/
const Block = ( { className } ) => {
const Block = ( props ) => {
const { className } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const rating = getAverageRating( product );
const colorProps = useColorProps( props );

if ( ! rating ) {
return null;
Expand Down Expand Up @@ -60,11 +63,13 @@ const Block = ( { className } ) => {
<div
className={ classnames(
className,
colorProps.className,
'wc-block-components-product-rating',
{
[ `${ parentClassName }__product-rating` ]: parentClassName,
}
) }
style={ colorProps.style }
>
<div
className={ classnames(
Expand Down
11 changes: 10 additions & 1 deletion assets/js/atomic/blocks/product-elements/rating/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -11,7 +12,15 @@ import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';

const Edit = ( { attributes } ) => {
return <Block { ...attributes } />;
const blockProps = useBlockProps( {
className: 'wp-block-woocommerce-product-rating',
} );

return (
<div { ...blockProps }>
<Block { ...attributes } />
</div>
);
};
export default withProductSelector( {
icon: BLOCK_ICON,
Expand Down
4 changes: 4 additions & 0 deletions assets/js/atomic/blocks/product-elements/rating/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import {
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import { supports } from './support';
import { Save } from './save';

const blockConfig = {
title,
description,
icon: { src: icon },
attributes,
supports,
edit,
save: Save,
};

registerBlockType( 'woocommerce/product-rating', {
Expand Down
21 changes: 21 additions & 0 deletions assets/js/atomic/blocks/product-elements/rating/save.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import classnames from 'classnames';

type Props = {
attributes: Record< string, unknown > & {
className?: string;
};
};

export const Save = ( { attributes }: Props ): JSX.Element => {
return (
<div
{ ...useBlockProps.save( {
className: classnames( 'is-loading', attributes.className ),
} ) }
/>
);
};
5 changes: 3 additions & 2 deletions assets/js/atomic/blocks/product-elements/rating/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
right: 0;
position: absolute;
opacity: 0.5;
color: #aaa;
color: inherit;
white-space: nowrap;
}
span {
Expand All @@ -32,6 +32,7 @@
left: 0;
right: 0;
position: absolute;
color: inherit;
padding-top: 1.5em;
}
span::before {
Expand All @@ -40,7 +41,7 @@
left: 0;
right: 0;
position: absolute;
color: #000;
color: inherit;
white-space: nowrap;
}
}
Expand Down
14 changes: 14 additions & 0 deletions assets/js/atomic/blocks/product-elements/rating/support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';

export const supports = {
...( isFeaturePluginBuild() && {
color: {
text: true,
background: false,
link: false,
},
} ),
};
56 changes: 56 additions & 0 deletions src/BlockTypes/ProductRating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

/**
* ProductRating class.
*/
class ProductRating extends AbstractBlock {

/**
* Block name.
*
* @var string
*/
protected $block_name = 'product-rating';

/**
* API version name.
*
* @var string
*/
protected $api_version = '2';

/**
* Get the editor script handle for this block type.
*
* @param string $key Data to get, or default to everything.
* @return array|string;
*/
protected function get_block_type_editor_script( $key = null ) {
$script = [
'handle' => 'wc-' . $this->block_name . '-block',
'path' => $this->asset_api->get_block_asset_build_path( 'atomic-block-components/rating' ),
'dependencies' => [ 'wc-blocks' ],
];
return $key ? $script[ $key ] : $script;
}

/**
* Get block supports. Shared with the frontend.
* IMPORTANT: If you change anything here, make sure to update the JS file too.
*
* @return array
*/
protected function get_block_type_supports() {
return array(
'color' =>
array(
'text' => true,
'background' => false,
'link' => false,
),
'__experimentalSelector' => '.wc-block-components-product-rating',
);
}

}
2 changes: 1 addition & 1 deletion src/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ protected function get_block_types() {
'ProductTitle',
'ProductSummary',
'ProductStockIndicator',
'ProductRating',
];

if ( Package::feature()->is_feature_plugin_build() ) {
Expand Down Expand Up @@ -222,7 +223,6 @@ protected function get_atomic_blocks() {
'product-button',
'product-image',
'product-price',
'product-rating',
'product-sale-badge',
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to remove this line, otherwise I was getting an error telling me product-sale-badge was registered twice:

( ! ) Notice: WP_Block_Type_Registry::register was called <strong>incorrectly</strong>. Block type "woocommerce/product-sale-badge" is already registered. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 5.0.0.) in .../Local Sites/latest/app/public/wp-includes/functions.php on line 5768

imatge

'product-sku',
'product-category-list',
Expand Down