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

Product Gallery Thumbnails: Add View all overlay #11087

Merged
merged 32 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1a217fa
Product Gallery Thumbnails: Add View All link to the last thumbnail (…
danieldudzic Oct 2, 2023
59fe8df
Product Gallery Thumbnails: Add interactivity to the View All overlay
danieldudzic Oct 2, 2023
4151bf5
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 2, 2023
6f726be
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
danieldudzic Oct 9, 2023
0dac111
Product Gallery Thumbnails: Refactor View all html to make it more re…
danieldudzic Oct 9, 2023
624035d
Product Gallery Thumbnails: Fix #11100 - Load all thumbnails and hide…
danieldudzic Oct 9, 2023
bb719e8
Product Gallery Thumbnails: Fix #11099 - Enable the dialog for the Vi…
danieldudzic Oct 9, 2023
5e3d585
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 9, 2023
f5676d1
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 11, 2023
1dc1f58
Product Gallery Thumbnails: Remove unnecessary concatenation from the…
danieldudzic Oct 11, 2023
265a8ae
Merge branch 'fix/11083-product-gallery-thumbnails-add-view-all' of h…
danieldudzic Oct 11, 2023
b3f5ea3
Product Gallery Thumbnails: Abstract the View All conditions into sep…
danieldudzic Oct 17, 2023
cec7e03
Product Gallery Thumbnails: Add escaping to the View all plain text s…
danieldudzic Oct 18, 2023
fd0701f
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 18, 2023
21d7c78
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 18, 2023
5b0053b
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 19, 2023
8cbe782
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 20, 2023
b7e8db0
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 20, 2023
b6699f8
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 31, 2023
eacfa93
E2E: Fix the Sale Badge and Single Product Template tests by selectin…
danieldudzic Oct 31, 2023
d1a3ae9
Merge branch 'fix/11083-product-gallery-thumbnails-add-view-all' of h…
danieldudzic Oct 31, 2023
41f5353
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 31, 2023
5281fbb
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Oct 31, 2023
38528c9
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
danieldudzic Oct 31, 2023
2f612a3
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 1, 2023
dee277f
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 1, 2023
575a1df
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 1, 2023
074005f
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 2, 2023
8a6b0c8
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 2, 2023
32757e2
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 2, 2023
e8ca720
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 2, 2023
1431191
Merge branch 'trunk' into fix/11083-product-gallery-thumbnails-add-vi…
danieldudzic Nov 2, 2023
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: 1 addition & 0 deletions assets/js/blocks/product-gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"pagerDisplayMode": "pagerDisplayMode",
"hoverZoom": "hoverZoom",
"fullScreenOnClick": "fullScreenOnClick",
"mode": "mode",
"cropImages": "cropImages"
},
"attributes": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ interactivityStore(
context: Context;
event: Event;
} ) => {
if ( ( event.target as HTMLElement ).tagName === 'IMG' ) {
if (
( event.target as HTMLElement ).classList.contains(
'wc-block-product-gallery-dialog-on-click'
)
danieldudzic marked this conversation as resolved.
Show resolved Hide resolved
) {
context.woocommerce.isDialogOpen = true;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Display the Thumbnails of a product.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"usesContext": [ "postId", "thumbnailsPosition", "thumbnailsNumberOfThumbnails", "productGalleryClientId" ],
"usesContext": [ "postId", "thumbnailsPosition", "thumbnailsNumberOfThumbnails", "productGalleryClientId", "mode" ],
"textdomain": "woo-gutenberg-products-block",
"ancestor": [ "woocommerce/product-gallery" ],
"supports": {
Expand Down
29 changes: 29 additions & 0 deletions assets/js/blocks/product-gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,35 @@ $outside-image-max-width: calc(100% - (2 * $outside-image-offset));
width: 100px;
height: 100px;
margin: 5px;
position: relative;

}

.wc-block-product-gallery-thumbnails__thumbnail__overlay {
cursor: pointer;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: absolute;
background-color: rgba(0, 0, 0, 0.4);
top: 0;
width: 100%;
height: 100%;

.wc-block-product-gallery-thumbnails__thumbnail__remaining-thumbnails-count {
@include font-size(large);
font-weight: 700;
}

.wc-block-product-gallery-thumbnails__thumbnail__view-all {
@include font-size(smaller);
text-decoration: underline;
}

.wc-block-product-gallery-thumbnails__thumbnail__remaining-thumbnails-count,
.wc-block-product-gallery-thumbnails__thumbnail__view-all {
color: #fff;
}
}
}
2 changes: 1 addition & 1 deletion src/BlockTypes/ProductGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function render( $attributes, $content, $block ) {

$number_of_thumbnails = $block->attributes['thumbnailsNumberOfThumbnails'] ?? 0;
$classname = $attributes['className'] ?? '';
$dialog = ( true === $attributes['fullScreenOnClick'] && isset( $attributes['mode'] ) && 'full' !== $attributes['mode'] ) ? $this->render_dialog() : '';
$dialog = isset( $attributes['mode'] ) && 'full' !== $attributes['mode'] ? $this->render_dialog() : '';
$post_id = $block->context['postId'] ?? '';
$product = wc_get_product( $post_id );
$product_id = strval( $product->get_id() );
Expand Down
2 changes: 1 addition & 1 deletion src/BlockTypes/ProductGalleryLargeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private function get_main_images_html( $context, $product_id ) {
);

if ( $context['fullScreenOnClick'] ) {
$attributes['class'] .= ' wc-block-woocommerce-product-gallery-large-image__image--full-screen-on-click';
$attributes['class'] .= ' wc-block-woocommerce-product-gallery-large-image__image--full-screen-on-click wc-block-product-gallery-dialog-on-click';
danieldudzic marked this conversation as resolved.
Show resolved Hide resolved
}

if ( $context['hoverZoom'] ) {
Expand Down
97 changes: 88 additions & 9 deletions src/BlockTypes/ProductGalleryThumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,77 @@ protected function get_block_type_style() {
* @return string[]
*/
protected function get_block_type_uses_context() {
return [ 'productGalleryClientId', 'postId', 'thumbnailsNumberOfThumbnails', 'thumbnailsPosition' ];
return [ 'productGalleryClientId', 'postId', 'thumbnailsNumberOfThumbnails', 'thumbnailsPosition', 'mode' ];
}

/**
* Generate the View All markup.
*
* @param int $remaining_thumbnails_count The number of thumbnails that are not displayed.
*
* @return string
*/
protected function generate_view_all_html( $remaining_thumbnails_count ) {
$view_all_html = '<div class="wc-block-product-gallery-thumbnails__thumbnail__overlay wc-block-product-gallery-dialog-on-click" data-wc-on--click="actions.woocommerce.handleClick">
<span class="wc-block-product-gallery-thumbnails__thumbnail__remaining-thumbnails-count wc-block-product-gallery-dialog-on-click">+%1$s</span>
<span class="wc-block-product-gallery-thumbnails__thumbnail__view-all wc-block-product-gallery-dialog-on-click">%2$s</span>
</div>';

return sprintf(
$view_all_html,
esc_html( $remaining_thumbnails_count ),
esc_html__( 'View all', 'woo-gutenberg-products-block' )
);
}

/**
* Inject View All markup into the product thumbnail HTML.
*
* @param string $thumbnail_html The thumbnail HTML.
* @param string $view_all_html The view all HTML.
*
* @return string
*/
protected function inject_view_all( $thumbnail_html, $view_all_html ) {

// Find the position of the last </div>.
$pos = strrpos( $thumbnail_html, '</div>' );

if ( false !== $pos ) {
// Inject the view_all_html at the correct position.
$html = substr_replace( $thumbnail_html, $view_all_html, $pos, 0 );

return $html;
}
}

/**
* Check if the thumbnails should be limited.
*
* @param string $mode Mode of the gallery. Expected values: 'standard'.
* @param int $thumbnails_count Current count of processed thumbnails.
* @param int $number_of_thumbnails Number of thumbnails configured to display.
*
* @return bool
*/
protected function should_limit_thumbnails( $mode, $thumbnails_count, $number_of_thumbnails ) {
return 'standard' === $mode && $thumbnails_count > $number_of_thumbnails;
}

/**
* Check if View All markup should be displayed.
*
* @param string $mode Mode of the gallery. Expected values: 'standard'.
* @param int $thumbnails_count Current count of processed thumbnails.
* @param array $product_gallery_images Array of product gallery image HTML strings.
* @param int $number_of_thumbnails Number of thumbnails configured to display.
*
* @return bool
*/
protected function should_display_view_all( $mode, $thumbnails_count, $product_gallery_images, $number_of_thumbnails ) {
return 'standard' === $mode &&
$thumbnails_count === $number_of_thumbnails &&
count( $product_gallery_images ) > $number_of_thumbnails;
}

/**
Expand Down Expand Up @@ -67,22 +137,31 @@ protected function render( $attributes, $content, $block ) {
if ( $product_gallery_images && $post_thumbnail_id ) {
$html = '';
$number_of_thumbnails = isset( $block->context['thumbnailsNumberOfThumbnails'] ) ? $block->context['thumbnailsNumberOfThumbnails'] : 3;
$mode = $block->context['mode'] ?? '';
$thumbnails_count = 1;

foreach ( $product_gallery_images as $product_gallery_image_html ) {
if ( $thumbnails_count > $number_of_thumbnails ) {
// Limit the number of thumbnails only in the standard mode (and not in dialog).
if ( $this->should_limit_thumbnails( $mode, $thumbnails_count, $number_of_thumbnails ) ) {
break;
}

$processor = new \WP_HTML_Tag_Processor( $product_gallery_image_html );
// If not in dialog and it's the last thumbnail and the number of product gallery images is greater than the number of thumbnails settings output the View All markup.
if ( $this->should_display_view_all( $mode, $thumbnails_count, $product_gallery_images, $number_of_thumbnails ) ) {
$remaining_thumbnails_count = count( $product_gallery_images ) - $number_of_thumbnails;
$product_gallery_image_html = $this->inject_view_all( $product_gallery_image_html, $this->generate_view_all_html( $remaining_thumbnails_count ) );
$html .= $product_gallery_image_html;
} else {
$processor = new \WP_HTML_Tag_Processor( $product_gallery_image_html );

if ( $processor->next_tag( 'img' ) ) {
$processor->set_attribute(
'data-wc-on--click',
'actions.woocommerce.thumbnails.handleClick'
);
if ( $processor->next_tag( 'img' ) ) {
$processor->set_attribute(
'data-wc-on--click',
'actions.woocommerce.thumbnails.handleClick'
);

$html .= $processor->get_updated_html();
$html .= $processor->get_updated_html();
}
}

$thumbnails_count++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ const getBoundingClientRect = async ( {
blockData.selectors[ isFrontend ? 'frontend' : 'editor' ]
.productSaleBadge
)
.first()
.evaluate( ( el ) => el.getBoundingClientRect() ),
productSaleBadgeContainer: await page
.locator(
blockData.selectors[ isFrontend ? 'frontend' : 'editor' ]
.productSaleBadgeContainer
)
.first()
.evaluate( ( el ) => el.getBoundingClientRect() ),
};
};
Expand Down Expand Up @@ -127,7 +129,7 @@ test.describe( `${ blockData.name }`, () => {

const block = await frontendUtils.getBlockByName( blockData.name );

await expect( block ).toBeVisible();
await expect( block.first() ).toBeVisible();
} );

test( `should be not rendered when the product isn't on sale the frontend side`, async ( {
Expand Down
Loading