diff --git a/assets/js/blocks/product-gallery/frontend.tsx b/assets/js/blocks/product-gallery/frontend.tsx index eae6d9c0663..e2b8b6e53e7 100644 --- a/assets/js/blocks/product-gallery/frontend.tsx +++ b/assets/js/blocks/product-gallery/frontend.tsx @@ -10,6 +10,7 @@ interface State { export interface ProductGalleryInteractivityApiContext { woocommerce: { selectedImage: string; + firstMainImageId: string; imageId: string; visibleImagesIds: string[]; dialogVisibleImagesIds: string[]; @@ -40,6 +41,11 @@ interface Actions { handleNextImageButtonClick: { ( store: Store ): void; }; + dialog: { + handleCloseButtonClick: { + ( store: Store ): void; + }; + }; }; } @@ -123,7 +129,9 @@ interactivityApiStore( { // Check if the esc key is pressed. if ( event.keyCode === Keys.ESC ) { - context.woocommerce.isDialogOpen = false; + actions.woocommerce.dialog.handleCloseButtonClick( + store + ); } // Check if left arrow key is pressed. @@ -175,6 +183,10 @@ interactivityApiStore( { dialog: { handleCloseButtonClick: ( { context }: Store ) => { context.woocommerce.isDialogOpen = false; + + // Reset the main image. + context.woocommerce.selectedImage = + context.woocommerce.firstMainImageId; }, }, handleSelectImage: ( { context }: Store ) => { diff --git a/src/BlockTypes/ProductGallery.php b/src/BlockTypes/ProductGallery.php index 7bfafd5ed95..b7f63932c39 100644 --- a/src/BlockTypes/ProductGallery.php +++ b/src/BlockTypes/ProductGallery.php @@ -139,6 +139,7 @@ protected function render( $attributes, $content, $block ) { array( 'woocommerce' => array( 'selectedImage' => $product->get_image_id(), + 'firstMainImageId' => $product->get_image_id(), 'visibleImagesIds' => ProductGalleryUtils::get_product_gallery_image_ids( $product, $number_of_thumbnails, true ), 'dialogVisibleImagesIds' => ProductGalleryUtils::get_product_gallery_image_ids( $product, null, false ), 'mouseIsOverPreviousOrNextButton' => false, diff --git a/templates/parts/product-gallery.html b/templates/parts/product-gallery.html index 19108d590fd..addbfe10abc 100644 --- a/templates/parts/product-gallery.html +++ b/templates/parts/product-gallery.html @@ -1,6 +1,8 @@