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

Product Gallery: Polish Gallery in full view mode #10947

Merged
merged 47 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
5adb704
Product Gallery: add support for On Sale Badge Block
gigitux Aug 29, 2023
fbfebbd
add align support
gigitux Aug 29, 2023
445b943
Add E2E tests
gigitux Aug 29, 2023
bbaaa38
set margin via Block Styles
gigitux Aug 29, 2023
67850b3
Merge branch 'trunk' into 10751-product-gallery-block-add-support-for…
gigitux Aug 29, 2023
003d06d
disable experimental flag
gigitux Aug 29, 2023
cf0f802
Merge branch '10751-product-gallery-block-add-support-for-on-sale-bad…
gigitux Aug 29, 2023
8a540aa
Merge branch 'trunk' into 10751-product-gallery-block-add-support-for…
gigitux Aug 30, 2023
b8752d1
add next previous block
gigitux Aug 30, 2023
f31179c
restore support file
gigitux Aug 30, 2023
f10424a
fix TS error
gigitux Aug 30, 2023
7e480ef
fix layout
gigitux Aug 30, 2023
91fe2f5
change product
gigitux Aug 30, 2023
652a0e9
change product
gigitux Aug 30, 2023
ac15706
Product Gallert Block: Add zoom on hover
gigitux Aug 31, 2023
7c44987
Merge branch '9941-product-gallery-block-add-zoom-to-the-large-image-…
gigitux Aug 31, 2023
9cda386
set to true by default
gigitux Aug 31, 2023
9f0ba2d
Merge branch 'trunk' into 9941-product-gallery-block-add-zoom-to-the-…
gigitux Aug 31, 2023
23761d9
remove block is already registered error
gigitux Aug 31, 2023
06e975a
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Aug 31, 2023
be651ce
remove unecessary await
gigitux Sep 1, 2023
921bb01
Merge branch 'trunk' into 9941-product-gallery-block-add-zoom-to-the-…
gigitux Sep 1, 2023
fbc1f4b
Improve zoom logic
gigitux Sep 4, 2023
24cee21
Merge branch 'trunk' into 9941-product-gallery-block-add-zoom-to-the-…
gigitux Sep 4, 2023
881a45b
Product Gallery Full view mode: Add the logic to render the dedicated…
gigitux Sep 4, 2023
1e3cb4d
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 5, 2023
cc96974
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 8, 2023
c601187
Merge branch 'trunk' of https://github.com/woocommerce/woocommerce-bl…
gigitux Sep 12, 2023
05827cb
use template-part instead template
gigitux Sep 12, 2023
305b1c5
add E2E tests
gigitux Sep 12, 2023
9255b52
update selectors
gigitux Sep 13, 2023
b48ffc5
add feature flag product gallery template part
gigitux Sep 13, 2023
290b865
fix E2E tests
gigitux Sep 13, 2023
dcb6c2d
remove not necessary file
gigitux Sep 13, 2023
2f52717
polish the dialog
gigitux Sep 13, 2023
7e6108d
Merge branch 'trunk' of github.com:woocommerce/woocommerce-blocks int…
gigitux Sep 21, 2023
97a06c8
Merge branch 'trunk' of github.com:woocommerce/woocommerce-blocks int…
gigitux Sep 21, 2023
ae6dc94
fix: dialog show always the selected product
gigitux Sep 21, 2023
ded8a7f
fix: not open the dialog when the user click on icon
gigitux Sep 21, 2023
20b75e3
rename handleClick to handleCloseButtonClick
gigitux Sep 21, 2023
472b5b8
improve style
gigitux Sep 21, 2023
3e0d9e6
fix overlay css
gigitux Sep 22, 2023
241d11e
Merge branch 'trunk' of github.com:woocommerce/woocommerce-blocks int…
gigitux Sep 22, 2023
784e19e
fix registration
gigitux Sep 22, 2023
8be3c80
improve logic
gigitux Sep 22, 2023
f3d262b
improve default template
gigitux Sep 22, 2023
26eb160
Merge branch 'trunk' into product-gallery-full-screen-dialog-css
gigitux Oct 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
3 changes: 2 additions & 1 deletion assets/js/blocks/product-gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"productGalleryClientId": "productGalleryClientId",
"nextPreviousButtonsPosition": "nextPreviousButtonsPosition",
"pagerDisplayMode": "pagerDisplayMode",
"hoverZoom": "hoverZoom"
"hoverZoom": "hoverZoom",
"fullScreenOnClick": "fullScreenOnClick"
},
"usesContext": [ "postId" ],
"attributes": {
Expand Down
24 changes: 12 additions & 12 deletions assets/js/blocks/product-gallery/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,14 @@ const TEMPLATE: InnerBlockTemplate[] = [
],
];

const setMode = (
currentTemplateId: string,
templateType: string,
setAttributes: ( attrs: Partial< ProductGalleryAttributes > ) => void
) => {
const getMode = ( currentTemplateId: string, templateType: string ) => {
if (
templateType === 'wp_template_part' &&
currentTemplateId.includes( 'product-gallery' )
) {
setAttributes( {
mode: 'full',
} );
return 'full';
}
return 'standard';
};

export const Edit = ( {
Expand All @@ -123,17 +118,22 @@ export const Edit = ( {
);

useEffect( () => {
setMode( currentTemplateId, templateType, setAttributes );
}, [ currentTemplateId, setAttributes, templateType ] );
const mode = getMode( currentTemplateId, templateType );

useEffect( () => {
setAttributes( {
...attributes,
mode,
productGalleryClientId: clientId,
} );
// Move the Thumbnails block to the correct above or below the Large Image based on the thumbnailsPosition attribute.
moveInnerBlocksToPosition( attributes, clientId );
}, [ setAttributes, attributes, clientId ] );
}, [
setAttributes,
attributes,
clientId,
currentTemplateId,
templateType,
] );

return (
<div { ...blockProps }>
Expand Down
7 changes: 6 additions & 1 deletion assets/js/blocks/product-gallery/frontend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interactivityApiStore( {
: 0.2;
},
isDialogOpen: ( { context }: Store ) => {
return context?.woocommerce.isDialogOpen;
return context.woocommerce.isDialogOpen;
},
},
},
Expand All @@ -72,6 +72,11 @@ interactivityApiStore( {
context.woocommerce.imageId;
},
},
dialog: {
handleCloseButtonClick: ( { context }: Store ) => {
context.woocommerce.isDialogOpen = false;
},
},
handleSelectImage: ( { context }: Store ) => {
context.woocommerce.selectedImage = context.woocommerce.imageId;
},
Expand Down
18 changes: 6 additions & 12 deletions assets/js/blocks/product-gallery/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* External dependencies
*/
import { registerBlockSingleProductTemplate } from '@woocommerce/atomic-utils';
import { isExperimentalBuild } from '@woocommerce/block-settings';
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -18,16 +18,10 @@ import './inner-blocks/product-gallery-pager';
import './inner-blocks/product-gallery-thumbnails';

if ( isExperimentalBuild() ) {
registerBlockSingleProductTemplate( {
blockName: metadata.name,
// @ts-expect-error: `metadata` currently does not have a type definition in WordPress core
blockMetadata: metadata,
blockSettings: {
icon,
// @ts-expect-error `edit` can be extended to include other attributes
edit: Edit,
save: Save,
},
isAvailableOnPostEditor: true,
// @ts-expect-error: `metadata` currently does not have a type definition in WordPress core.
registerBlockType( metadata, {
icon,
edit: Edit,
save: Save,
} );
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"description": "Display the Large Image of a product.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"usesContext": [ "nextPreviousButtonsPosition", "postId", "hoverZoom"],
"usesContext": [ "nextPreviousButtonsPosition", "postId", "hoverZoom", "fullScreenOnClick"],
"supports": {
"interactivity": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,16 @@ interactivityStore(
context.woocommerce.styles.transform = `scale(1.0)`;
context.woocommerce.styles[ 'transform-origin' ] = '';
},
handleClick: ( { context }: { context: Context } ) => {
context.woocommerce.isDialogOpen = true;
handleClick: ( {
context,
event,
}: {
context: Context;
event: Event;
} ) => {
if ( ( event.target as HTMLElement ).tagName === 'IMG' ) {
context.woocommerce.isDialogOpen = true;
}
},
},
},
Expand Down
44 changes: 40 additions & 4 deletions assets/js/blocks/product-gallery/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,39 @@ $outside-image-max-width: calc(100% - (2 * $outside-image-offset));

// Product Gallery
#{$gallery} {
.wc-block-product-gallery-dialog__overlay {
height: 100vh;
width: 100vw;
position: fixed;
top: 0;
left: 0;
background-color: #808080;
z-index: 9999;
}

dialog {
position: fixed;
width: 90vw;
width: calc(100vw - 100px);
border: none;
border-radius: 10px;
height: 90vh;
top: 0;
margin: $gap-largest;
margin-top: $gap-largest;
margin-bottom: $gap-largest;
z-index: 9999;

.wc-block-product-galler-dialog__header-right {
display: flex;
justify-content: flex-end;

.wc-block-product-gallery-dialog__close {
border: none;
background-color: transparent;
outline: none;
cursor: pointer;
}
}

}
}

Expand All @@ -37,9 +63,16 @@ $outside-image-max-width: calc(100% - (2 * $outside-image-offset));

img {
display: block;
position: relative;
margin: 0 auto;
z-index: 1;
transition: all 0.1s linear;

// Keep the order in this way. The hoverZoom class should override the full-screen-on-click class when both are applied.
&.wc-block-woocommerce-product-gallery-large-image__image--full-screen-on-click {
cursor: pointer;
}

&.wc-block-woocommerce-product-gallery-large-image__image--hoverZoom {
cursor: zoom-in;
}
Expand All @@ -53,8 +86,6 @@ $outside-image-max-width: calc(100% - (2 * $outside-image-offset));
display: flex;
flex-direction: column;
position: absolute;
pointer-events: none;
z-index: 1;
width: 100%;
height: 100%;
top: 0;
Expand All @@ -76,6 +107,11 @@ $outside-image-max-width: calc(100% - (2 * $outside-image-offset));
width: 100%;
height: 100%;

svg {
z-index: 3;
pointer-events: all;
}

.is-vertically-aligned-top {
align-items: flex-start;
}
Expand Down
2 changes: 1 addition & 1 deletion assets/js/blocks/product-gallery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface ProductGalleryBlockAttributes {
cropImages?: boolean;
hoverZoom?: boolean;
fullScreenOnClick?: boolean;
mode: 'standard' | 'full';
mode?: 'standard' | 'full';
}

export interface ProductGalleryThumbnailsBlockAttributes {
Expand Down
34 changes: 33 additions & 1 deletion src/BlockTypes/ProductGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,39 @@ function( $carry, $item ) {
''
);

$gallery_dialog = '<dialog data-wc-bind--open="selectors.woocommerce.isDialogOpen">' . $html . '</dialog>';
$html_processor = new \WP_HTML_Tag_Processor( $html );

$html_processor->next_tag(
array(
'class_name' => 'wp-block-woocommerce-product-gallery',
)
);

$html_processor->remove_attribute( 'data-wc-context' );

$gallery_dialog = strtr(
'
<div class="wc-block-product-gallery-dialog__overlay" hidden data-wc-bind--hidden="!selectors.woocommerce.isDialogOpen">
<dialog data-wc-bind--open="selectors.woocommerce.isDialogOpen">
<div class="wc-block-product-gallery-dialog__header">
<div class="wc-block-product-galler-dialog__header-right">
<button class="wc-block-product-gallery-dialog__close" data-wc-on--click="actions.woocommerce.dialog.handleCloseButtonClick">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect width="24" height="24" rx="2"/>
<path d="M13 11.8L19.1 5.5L18.1 4.5L12 10.7L5.9 4.5L4.9 5.5L11 11.8L4.5 18.5L5.5 19.5L12 12.9L18.5 19.5L19.5 18.5L13 11.8Z" fill="black"/>
</svg>
</button>
</div>
</div>
<div class="wc-block-product-gallery-dialog__body">
{{html}}
</div>
</dialog>
</div>',
array(
'{{html}}' => $html_processor->get_updated_html(),
)
);
return $gallery_dialog;
}

Expand Down
43 changes: 38 additions & 5 deletions src/BlockTypes/ProductGalleryLargeImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function get_block_type_style() {
* @return string[]
*/
protected function get_block_type_uses_context() {
return [ 'postId', 'hoverZoom' ];
return [ 'postId', 'hoverZoom', 'fullScreenOnClick' ];
}

/**
Expand All @@ -41,7 +41,7 @@ protected function get_block_type_uses_context() {
* @param WP_Block $block The block object.
*/
protected function enqueue_assets( array $attributes, $content, $block ) {
if ( $block->context['hoverZoom'] ) {
if ( $block->context['hoverZoom'] || $block->context['fullScreenOnClick'] ) {
parent::enqueue_assets( $attributes, $content, $block );
}
}
Expand Down Expand Up @@ -124,6 +124,10 @@ 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';
}

if ( $context['hoverZoom'] ) {
$attributes['class'] .= ' wc-block-woocommerce-product-gallery-large-image__image--hoverZoom';
$attributes['data-wc-bind--style'] = 'selectors.woocommerce.styles';
Expand All @@ -147,17 +151,30 @@ private function get_main_images_html( $context, $product_id ) {
}

/**
* Get directives for the hover zoom.
* Get directives for the block.
*
* @param array $block_context The block context.
*
* @return array
*/
private function get_directives( $block_context ) {
return array_merge(
$this->get_zoom_directives( $block_context ),
$this->get_open_dialog_directives( $block_context )
);
}

/**
* Get directives for zoom.
*
* @param array $block_context The block context.
*
* @return array
*/
private function get_zoom_directives( $block_context ) {
if ( ! $block_context['hoverZoom'] ) {
return array();
}

$context = array(
'woocommerce' => array(
'styles' => array(
Expand All @@ -170,8 +187,24 @@ private function get_directives( $block_context ) {
return array(
'data-wc-on--mousemove' => 'actions.woocommerce.handleMouseMove',
'data-wc-on--mouseleave' => 'actions.woocommerce.handleMouseLeave',
'data-wc-on--click' => 'actions.woocommerce.handleClick',
'data-wc-context' => wp_json_encode( $context, JSON_NUMERIC_CHECK ),
);
}

/**
* Get directives for opening the dialog.
*
* @param array $block_context The block context.
*
* @return array
*/
private function get_open_dialog_directives( $block_context ) {
if ( ! $block_context['fullScreenOnClick'] ) {
return array();
}

return array(
'data-wc-on--click' => 'actions.woocommerce.handleClick',
);
}
}
Loading