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

Commit

Permalink
Sale Product Block: Enable global style (#5565)
Browse files Browse the repository at this point in the history
* Product title: add support global style #4965

* add specific type

* add custom save function

* move hooks in a specific folder

* fix crash on WP 5.8

* Featured Category block: Add support for global style #4965

Featured Category block: Add support for global style

* fix border color

* Sale Product block: enable global style #4965

Sale Product block: enable global style #4965

* add comment
  • Loading branch information
gigitux authored Feb 14, 2022
1 parent ac3e8f7 commit c324204
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 4 deletions.
24 changes: 22 additions & 2 deletions assets/js/atomic/blocks/product-elements/sale-badge/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { withProductDataContext } from '@woocommerce/shared-hocs';
* Internal dependencies
*/
import './style.scss';
import {
useBorderProps,
useColorProps,
useSpacingProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Sale Badge Block Component.
Expand All @@ -24,9 +30,15 @@ import './style.scss';
* @param {string} [props.align] Alignment of the badge.
* @return {*} The component.
*/
const Block = ( { className, align } ) => {
const Block = ( props ) => {
const { className, align } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const borderProps = useBorderProps( props );
const colorProps = useColorProps( props );

const typographyProps = useTypographyProps( props );
const spacingProps = useSpacingProps( props );

if ( ! product.id || ! product.on_sale ) {
return null;
Expand All @@ -45,8 +57,16 @@ const Block = ( { className, align } ) => {
alignClass,
{
[ `${ parentClassName }__product-onsale` ]: parentClassName,
}
},
colorProps.className,
borderProps.className
) }
style={ {
...colorProps.style,
...borderProps.style,
...typographyProps.style,
...spacingProps.style,
} }
>
<Label
label={ __( 'Sale', 'woo-gutenberg-products-block' ) }
Expand Down
8 changes: 7 additions & 1 deletion assets/js/atomic/blocks/product-elements/sale-badge/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -11,7 +12,12 @@ import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';

const Edit = ( { attributes } ) => {
return <Block { ...attributes } />;
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Block { ...attributes } />
</div>
);
};

export default withProductSelector( {
Expand Down
24 changes: 24 additions & 0 deletions assets/js/atomic/blocks/product-elements/sale-badge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,40 @@ import {
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import { Save } from './save';

const blockConfig = {
title,
description,
icon: { src: icon },
apiVersion: 2,
supports: {
html: false,
color: {
gradients: true,
background: true,
link: false,
__experimentalSkipSerialization: true,
},
typography: {
fontSize: true,
__experimentalSkipSerialization: true,
},
__experimentalBorder: {
color: true,
radius: true,
width: true,
__experimentalSkipSerialization: true,
},
spacing: {
padding: true,
__experimentalSkipSerialization: true,
},
__experimentalSelector: '.wc-block-components-product-sale-badge',
},
attributes,
edit,
save: Save,
};

registerBlockType( 'woocommerce/product-sale-badge', {
Expand Down
21 changes: 21 additions & 0 deletions assets/js/atomic/blocks/product-elements/sale-badge/save.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import classnames from 'classnames';

type Props = {
attributes: Record< string, unknown > & {
className?: string;
};
};

export const Save = ( { attributes }: Props ): JSX.Element => {
return (
<div
{ ...useBlockProps.save( {
className: classnames( 'is-loading', attributes.className ),
} ) }
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@
font-weight: 600;
z-index: 9;
position: static;

span {
color: inherit;
background-color: inherit;
}

}
56 changes: 56 additions & 0 deletions src/BlockTypes/ProductSaleBadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

/**
* ProductTag class.
*/
class ProductSaleBadge extends AbstractBlock {

/**
* Block name.
*
* @var string
*/
protected $block_name = 'product-sale-badge';

/**
* API version name.
*
* @var string
*/
protected $api_version = '2';

/**
* Get block attributes.
*
* @return array
*/
protected function get_block_type_supports() {
return array(
'color' =>
array(
'gradients' => true,
'background' => true,
'link' => true,
),
'typography' =>
array(
'fontSize' => true,
'lineHeight' => true,
),
'__experimentalBorder' =>
array(
'color' => true,
'radius' => true,
'width' => true,
),
'spacing' =>
array(
'padding' => true,
'__experimentalSkipSerialization' => true,
),
'__experimentalSelector' => '.wc-block-components-product-sale-badge',
);
}

}
3 changes: 2 additions & 1 deletion src/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function hide_legacy_widgets_with_block_equivalent( $widget_types ) {
protected function get_block_types() {
global $wp_version, $pagenow;

// @todo Add a comment why some atomic blocks are included in this array.
$block_types = [
'AllReviews',
'FeaturedCategory',
Expand All @@ -178,6 +179,7 @@ protected function get_block_types() {
'ProductTitle',
'ProductSummary',
'ProductStockIndicator',
'ProductSaleBadge',
];

if ( Package::feature()->is_feature_plugin_build() ) {
Expand Down Expand Up @@ -233,7 +235,6 @@ protected function get_atomic_blocks() {
'product-image',
'product-price',
'product-rating',
'product-sale-badge',
'product-sku',
'product-category-list',
'product-tag-list',
Expand Down

0 comments on commit c324204

Please sign in to comment.