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

Sale Product Block: Enable global style #5565

Merged
merged 15 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
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
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',
);
}

}
2 changes: 1 addition & 1 deletion src/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ protected function get_block_types() {
'ProductTitle',
'ProductSummary',
'ProductStockIndicator',
'ProductSaleBadge',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a comment here explaining why we register an atomic block here will be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I added a @todo comment. In this way, in the end, I will re-organize the order of the elements in the array (maybe we can move down the atomic blocks and add a comment that explains why)

];

if ( Package::feature()->is_feature_plugin_build() ) {
Expand Down Expand Up @@ -233,7 +234,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