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

Commit b0791c1

Browse files
authored
Update grid blocks preview to match width of products on the frontend (#489)
* Update grid blocks preview to match width of products on the front end * Remove max-width from CSS, set it on the preview itself so we can use the dynamic theme width * Override block thumbnail width for multiple column layouts Previously larger thumbnails would cause multiple column layouts to break and wrap early. This enforces the column width for those cases * Add the thumbnail size to images, to prevent larger images in multiple col layouts * Update snapshot to new preview markup
1 parent c7cc1d4 commit b0791c1

File tree

6 files changed

+68
-17
lines changed

6 files changed

+68
-17
lines changed

assets/css/editor.scss

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,31 @@
1919
flex-wrap: wrap;
2020
justify-content: flex-start;
2121

22+
&.components-placeholder {
23+
padding: 2em 1em;
24+
}
25+
2226
&.is-loading,
23-
&.is-not-found,
24-
&.cols-1 {
27+
&.is-not-found {
2528
display: block;
2629
}
2730

28-
.wc-product-preview {
29-
flex: 1;
30-
padding: $gap/2;
31+
&.cols-1 {
32+
display: block;
33+
34+
.wc-product-preview {
35+
margin-left: auto;
36+
margin-right: auto;
37+
}
3138
}
3239

3340
@for $i from 2 to 7 {
3441
&.cols-#{$i} .wc-product-preview {
35-
max-width: calc(#{ 100% / $i });
36-
min-width: calc(#{ 100% / $i });
37-
flex: 1;
42+
flex: 1 0 calc(#{ 100% / $i });
43+
max-width: 100% / $i !important;
3844
}
3945
}
4046

41-
&.components-placeholder {
42-
padding: 2em 1em;
43-
}
44-
4547
// Styles for "resuable block" preview.
4648
.editor-block-preview & {
4749
min-width: 5em;

assets/js/components/product-preview/index.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,29 @@ import './style.scss';
1313
* Display a preview for a given product.
1414
*/
1515
const ProductPreview = ( { product } ) => {
16-
const { placeholderImgSrc } = wc_product_block_data; /* eslint-disable-line camelcase */
16+
const {
17+
placeholderImgSrc,
18+
} = wc_product_block_data; /* eslint-disable-line camelcase */
1719

1820
let image = null;
1921
if ( product.images.length ) {
20-
image = <img src={ product.images[ 0 ].src } alt="" />;
22+
image = (
23+
<img
24+
className="wc-product-preview__image"
25+
src={ product.images[ 0 ].src }
26+
alt=""
27+
style={ { width: `${ wc_product_block_data.thumbnail_size }px` } }
28+
/>
29+
);
2130
} else {
22-
image = <img src={ placeholderImgSrc } alt="" />;
31+
image = (
32+
<img
33+
className="wc-product-preview__image"
34+
src={ placeholderImgSrc }
35+
alt=""
36+
style={ { width: `${ wc_product_block_data.thumbnail_size }px` } }
37+
/>
38+
);
2339
}
2440

2541
const rating = Number( product.average_rating );
@@ -29,7 +45,10 @@ const ProductPreview = ( { product } ) => {
2945
}
3046

3147
return (
32-
<div className="wc-product-preview">
48+
<div
49+
className="wc-product-preview"
50+
style={ { maxWidth: `${ wc_product_block_data.thumbnail_size }px` } }
51+
>
3352
{ image }
3453
<div
3554
className="wc-product-preview__title"

assets/js/components/product-preview/style.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
.wc-product-preview {
2-
text-align: center;
32
margin-bottom: $gap;
3+
padding: $gap/2;
4+
text-align: center;
45

56
.wc-product-preview__title,
67
.wc-product-preview__price,
78
.wc-product-preview__rating {
89
margin-top: $gap-smallest;
910
}
1011

12+
.wc-product-preview__image {
13+
margin-left: auto;
14+
margin-right: auto;
15+
}
16+
1117
.star-rating {
1218
overflow: hidden;
1319
position: relative;

assets/js/components/product-preview/test/__snapshots__/index.js.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
exports[`ProductPreview should render a single product preview with an image 1`] = `
44
<div
55
className="wc-product-preview"
6+
style={
7+
Object {
8+
"maxWidth": "300px",
9+
}
10+
}
611
>
712
<img
813
alt=""
14+
className="wc-product-preview__image"
915
src="https://example.local/product.jpg"
16+
style={
17+
Object {
18+
"width": "300px",
19+
}
20+
}
1021
/>
1122
<div
1223
className="wc-product-preview__title"
@@ -39,10 +50,21 @@ exports[`ProductPreview should render a single product preview with an image 1`]
3950
exports[`ProductPreview should render a single product preview without an image 1`] = `
4051
<div
4152
className="wc-product-preview"
53+
style={
54+
Object {
55+
"maxWidth": "300px",
56+
}
57+
}
4258
>
4359
<img
4460
alt=""
61+
className="wc-product-preview__image"
4562
src="placeholder.png"
63+
style={
64+
Object {
65+
"width": "300px",
66+
}
67+
}
4668
/>
4769
<div
4870
className="wc-product-preview__title"

assets/php/class-wgpb-block-library.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ public static function print_script_settings() {
274274
'min_rows' => wc_get_theme_support( 'product_blocks::min_rows', 1 ),
275275
'max_rows' => wc_get_theme_support( 'product_blocks::max_rows', 6 ),
276276
'default_rows' => wc_get_theme_support( 'product_blocks::default_rows', 1 ),
277+
'thumbnail_size' => wc_get_theme_support( 'thumbnail_image_width', 300 ),
277278
'placeholderImgSrc' => wc_placeholder_img_src(),
278279
'min_height' => wc_get_theme_support( 'featured_block::min_height', 500 ),
279280
'default_height' => wc_get_theme_support( 'featured_block::default_height', 500 ),

tests/js/setup-globals.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ global.wp = {};
44
// Set up our settings global.
55
global.wc_product_block_data = {
66
placeholderImgSrc: 'placeholder.png',
7+
thumbnail_size: '300',
78
};
89

910
// wcSettings is required by @woocommerce/* packages

0 commit comments

Comments
 (0)