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

Product SKU: Adds support for color, typography, and spacing #8913

Merged
merged 12 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions assets/js/atomic/blocks/product-elements/sku/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const blockAttributes: BlockAttributes = {
type: 'boolean',
default: false,
},
isDescendantOfAllProducts: {
type: 'boolean',
default: false,
},
showProductSelector: {
type: 'boolean',
default: false,
Expand Down
33 changes: 32 additions & 1 deletion assets/js/atomic/blocks/product-elements/sku/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import {
} from '@woocommerce/shared-context';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import type { HTMLAttributes } from 'react';
import {
useColorProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
kmanijak marked this conversation as resolved.
Show resolved Hide resolved

/**
* Internal dependencies
Expand All @@ -22,15 +27,18 @@ const Preview = ( {
parentClassName,
sku,
className,
style,
}: {
parentClassName: string;
sku: string;
className?: string | undefined;
style?: React.CSSProperties | undefined;
} ) => (
<div
className={ classnames( className, 'wc-block-components-product-sku', {
className={ classnames( className, {
[ `${ parentClassName }__product-sku` ]: parentClassName,
} ) }
style={ style }
>
{ __( 'SKU:', 'woo-gutenberg-products-block' ) }{ ' ' }
<strong>{ sku }</strong>
Expand All @@ -43,6 +51,10 @@ const Block = ( props: Props ): JSX.Element | null => {
const { product } = useProductDataContext();
const sku = product.sku;

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

if ( props.isDescendentOfSingleProductTemplate ) {
return (
<Preview
Expand All @@ -62,6 +74,25 @@ const Block = ( props: Props ): JSX.Element | null => {
className={ className }
parentClassName={ parentClassName }
sku={ sku }
{ ...( props.isDescendantOfAllProducts && {
className: classnames(
className,
'wp-block-woocommerce-product-sku',
{
[ colorProps.className ]: colorProps.className,
[ typographyProps.className ]:
typographyProps.className,
}
),
style: {
...colorProps.style,
...typographyProps.style,
...spacingProps.style,
// Default value of textTransform is uppercase.
textTransform:
typographyProps.style?.textTransform || 'uppercase',
},
} ) }
/>
);
};
Expand Down
29 changes: 25 additions & 4 deletions assets/js/atomic/blocks/product-elements/sku/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const Edit = ( {
setAttributes,
context,
}: BlockEditProps< Attributes > & { context: Context } ): JSX.Element => {
const blockProps = useBlockProps();
const { style, ...blockProps } = useBlockProps( {
className:
'wc-block-components-product-sku wp-block-woocommerce-product-sku',
kmanijak marked this conversation as resolved.
Show resolved Hide resolved
} );
const blockAttrs = {
...attributes,
...context,
Expand All @@ -30,11 +33,29 @@ const Edit = ( {
[ setAttributes, isDescendentOfQueryLoop ]
);

const textTransformStyle = {
textTransform: style?.textTransform || 'uppercase',
};

return (
<div { ...blockProps }>
<>
<EditProductLink />
<Block { ...blockAttrs } />
</div>
<div
{ ...blockProps }
/**
* If block is decendant of the All Products block, we don't want to
* apply style here because it will be applied inside Block using
* useColors, useTypography, and useSpacing hooks.
*/
style={
attributes.isDescendantOfAllProducts
? textTransformStyle
: { ...style, ...textTransformStyle }
}
>
<Block { ...blockAttrs } />
</div>
</>
);
};

Expand Down
2 changes: 2 additions & 0 deletions assets/js/atomic/blocks/product-elements/sku/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { BlockConfiguration } from '@wordpress/blocks';
import sharedConfig from '../shared/config';
import attributes from './attributes';
import edit from './edit';
import { supports } from './supports';
import {
BLOCK_TITLE as title,
BLOCK_ICON as icon,
Expand All @@ -33,6 +34,7 @@ const blockConfig: BlockConfiguration = {
'woocommerce/product-meta',
],
edit,
supports,
};

registerBlockType( 'woocommerce/product-sku', { ...blockConfig } );
37 changes: 37 additions & 0 deletions assets/js/atomic/blocks/product-elements/sku/supports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor';
Copy link
Contributor

Choose a reason for hiding this comment

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

Heads-up that this line introduced an ESLint warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ESLint warning is present wherever we use __experimentalGetSpacingClassesAndStyles, therefore I thought it was acceptable. However, I will add the following lines to ignore the warning going forward 🙂

// @ts-expect-error We check if this exists before using it.
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis


/**
* Internal dependencies
*/
import sharedConfig from '../shared/config';

export const supports = {
...sharedConfig.supports,
...( isFeaturePluginBuild() && {
color: {
text: true,
background: true,
},
typography: {
fontSize: true,
lineHeight: true,
Copy link
Contributor

@Aljullu Aljullu Apr 3, 2023

Choose a reason for hiding this comment

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

If we can remove __experimentalSelector, then we could make these styles available in WC core as well, so only the experimental ones would be gated to the feature plugin. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I will make the necessary changes 🤝

__experimentalFontWeight: true,
__experimentalFontFamily: true,
__experimentalFontStyle: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalLetterSpacing: true,
},
__experimentalSelector: '.wc-block-components-product-sku',
Copy link
Contributor

Choose a reason for hiding this comment

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

Is __experimentalSelector required? I tried removing it and everything seemed to keep working as expected. 🤔

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 catch! I think we can remove __experimentalSelector flag.

} ),
...( typeof __experimentalGetSpacingClassesAndStyles === 'function' && {
spacing: {
margin: true,
padding: true,
},
} ),
};
1 change: 1 addition & 0 deletions assets/js/atomic/blocks/product-elements/sku/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export interface Attributes {
isDescendentOfQueryLoop: boolean;
isDescendentOfSingleProductTemplate: boolean;
showProductSelector: boolean;
isDescendantOfAllProducts: boolean;
}
5 changes: 5 additions & 0 deletions assets/js/blocks/products/base-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export const getProductLayoutConfig = ( innerBlocks ) => {
block.attributes?.width,
} ),
} ),
/**
* For product elements, special handing is required if product
* elements are used in the "All Products" block.
*/
isDescendantOfAllProducts: true,
},
];
} );
Expand Down
10 changes: 8 additions & 2 deletions src/BlockTypes/ProductSKU.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;

/**
* ProductSKU class.
*/
Expand Down Expand Up @@ -65,11 +67,15 @@ protected function render( $attributes, $content, $block ) {
return '';
}

$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

return sprintf(
'<div class="wc-block-components-product-sku wc-block-grid__product-sku">
'<div class="wc-block-components-product-sku wc-block-grid__product-sku wp-block-woocommerce-product-sku %1$s" style="%2$s">
SKU:
<strong>%s</strong>
<strong>%3$s</strong>
</div>',
esc_attr( $styles_and_classes['classes'] ),
esc_attr( $styles_and_classes['styles'] ?? '' ),
$product_sku
);
}
Expand Down