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

Commit 030fb5a

Browse files
authored
Product rating: improve preview style when the product doesn't have rating (#9684)
* product rating: improve preview style when no rating is visible * fix class naming * fix cutted star * change variable * fix cut border
1 parent cb2ece0 commit 030fb5a

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

assets/js/atomic/blocks/product-elements/rating/block.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,35 @@ const getRatingCount = ( product: ProductResponseItem ) => {
4040
return Number.isFinite( count ) && count > 0 ? count : 0;
4141
};
4242

43+
const getStarStyle = ( rating: number ) => ( {
44+
width: ( rating / 5 ) * 100 + '%',
45+
} );
46+
47+
const NoRating = ( { parentClassName }: { parentClassName: string } ) => {
48+
const starStyle = getStarStyle( 0 );
49+
50+
return (
51+
<div
52+
className={ classnames(
53+
'wc-block-components-product-rating__norating-container',
54+
`${ parentClassName }-product-rating__norating-container`
55+
) }
56+
>
57+
<div
58+
className={ 'wc-block-components-product-rating__norating' }
59+
role="img"
60+
>
61+
<span style={ starStyle } />
62+
</div>
63+
<span>{ __( 'No Reviews', 'woo-gutenberg-products-block' ) }</span>
64+
</div>
65+
);
66+
};
67+
4368
const Rating = ( props: RatingProps ): JSX.Element => {
4469
const { rating, reviews, parentClassName } = props;
4570

46-
const starStyle = {
47-
width: ( rating / 5 ) * 100 + '%',
48-
};
71+
const starStyle = getStarStyle( rating );
4972

5073
const ratingText = sprintf(
5174
/* translators: %f is referring to the average rating value */
@@ -132,11 +155,7 @@ export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
132155
}
133156
);
134157
const mockedRatings = shouldDisplayMockedReviewsWhenProductHasNoReviews ? (
135-
<Rating
136-
rating={ 0 }
137-
reviews={ 0 }
138-
parentClassName={ parentClassName }
139-
/>
158+
<NoRating parentClassName={ parentClassName } />
140159
) : null;
141160

142161
const content = reviews ? (

assets/js/atomic/blocks/product-elements/rating/style.scss

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@
5656
margin-top: 0;
5757
margin-bottom: $gap-small;
5858
}
59+
60+
&__norating-container {
61+
display: inline-flex;
62+
flex-direction: row;
63+
align-items: center;
64+
gap: $gap-smaller;
65+
}
66+
67+
&__norating {
68+
display: inline-block;
69+
overflow: hidden;
70+
position: relative;
71+
width: 1.5em;
72+
height: 1.618em;
73+
line-height: 1.618;
74+
font-size: 1em;
75+
/* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
76+
font-family: star;
77+
font-weight: 400;
78+
-webkit-text-stroke: 2px var(--wp--preset--color--black, #000);
79+
&::before {
80+
content: "\53";
81+
top: 0;
82+
left: 0;
83+
right: 0;
84+
position: absolute;
85+
color: transparent;
86+
white-space: nowrap;
87+
text-align: center;
88+
}
89+
}
5990
}
6091

6192
.wp-block-woocommerce-single-product {

0 commit comments

Comments
 (0)