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

Commit

Permalink
Product Gallery block: Add support to Interactivity API (#10716)
Browse files Browse the repository at this point in the history
* Add support to Interactivity API

* Fix php cs errors

* Rename numberOfThumbnailImages to numberOfThumbnails

* Use frontend.tsx with InteractivityAPI

* Replace viewScript in block.json
  • Loading branch information
thealexandrelara authored Aug 25, 2023
1 parent 8a5dab6 commit 6f58d5a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
6 changes: 4 additions & 2 deletions assets/js/blocks/product-gallery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"keywords": [ "WooCommerce" ],
"supports": {
"align": true,
"multiple": false
"multiple": false,
"interactivity": true
},
"textdomain": "woo-gutenberg-products-block",
"providesContext": {
Expand Down Expand Up @@ -47,5 +48,6 @@
"type": "boolean",
"default": false
}
}
},
"viewScript": "wc-product-gallery-interactivity-frontend"
}
39 changes: 39 additions & 0 deletions assets/js/blocks/product-gallery/frontend.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* External dependencies
*/
import { store as interactivityApiStore } from '@woocommerce/interactivity';

interface State {
[ key: string ]: unknown;
}

interface Context {
productGallery: { numberOfThumbnails: number };
}

interface Selectors {
productGallery: {
getNumberOfPages: ( store: unknown ) => number;
};
}

interface Store {
state: State;
context: Context;
selectors: Selectors;
ref: HTMLElement;
}

type SelectorsStore = Pick< Store, 'context' | 'selectors' >;

interactivityApiStore( {
selectors: {
productGallery: {
getNumberOfPages: ( store: SelectorsStore ) => {
const { context } = store;

return context.productGallery.numberOfThumbnails;
},
},
},
} as Store );
47 changes: 47 additions & 0 deletions src/BlockTypes/ProductGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ protected function get_block_type_style() {
return null;
}

/**
* Include and render the block.
*
* @param array $attributes Block attributes. Default empty array.
* @param string $content Block content. Default empty string.
* @param WP_Block $block Block instance.
* @return string Rendered block type output.
*/
protected function render( $attributes, $content, $block ) {
$classname = $attributes['className'] ?? '';
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( sprintf( 'woocommerce %1$s', $classname ) ) ) );
$html = sprintf(
'<div data-wc-interactive %1$s>
%2$s
</div>',
$wrapper_attributes,
$content
);
$p = new \WP_HTML_Tag_Processor( $content );

if ( $p->next_tag() ) {
$p->set_attribute( 'data-wc-interactive', true );
$p->set_attribute(
'data-wc-context',
wp_json_encode( array( 'productGallery' => array( 'numberOfThumbnails' => 0 ) ) )
);
$html = $p->get_updated_html();
}

return $html;
}

/**
* It isn't necessary register block assets because it is a server side block.
*/
Expand All @@ -29,4 +61,19 @@ protected function register_block_type_assets() {

return null;
}

/**
* Get the Interactivity API's view script handle for this block type.
*
* @param string $key Data to get, or default to everything.
*/
protected function get_block_type_script( $key = null ) {
$script = [
'handle' => 'wc-' . $this->block_name . '-interactivity-frontend',
'path' => $this->asset_api->get_block_asset_build_path( $this->block_name . '-interactivity-frontend' ),
'dependencies' => [ 'wc-interactivity' ],
];

return $key ? $script[ $key ] : $script;
}
}

0 comments on commit 6f58d5a

Please sign in to comment.