Product SKU: Adds support for color, typography, and spacing (https://github.com/woocommerce/woocommerce-blocks/pull/8913)

* Add color, typography, and spacing support to Product SKU block

This commit introduces a new file `supports.ts` for the Product SKU block in the WooCommerce Blocks plugin, which adds color, typography, and spacing support. The following configuration has been added:

1. In `assets/js/atomic/blocks/product-elements/sku/supports.ts`, a new `supports` object is created, which extends the shared configuration and adds support for the following properties:
  - `text` and `background` color
  - `fontSize`, `lineHeight`, `__experimentalFontWeight`, `__experimentalFontFamily`, `__experimentalFontStyle`, `__experimentalTextTransform`, `__experimentalTextDecoration`, and `__experimentalLetterSpacing` for typography
  - `margin` and `padding` for spacing

* Add color, typography, and spacing support for Product SKU block in All Products block

This commit extends the support for color, typography, and spacing to the Product SKU block when used within the All Products block. The following changes have been made:

1. In `assets/js/atomic/blocks/product-elements/sku/block.tsx`, the `useColorProps`, `useSpacingProps`, and `useTypographyProps` hooks are imported and used to add the appropriate className and style properties to the rendered SKU element.
2. In `assets/js/atomic/blocks/product-elements/sku/edit.tsx`, the `style` property is separated from `useBlockProps()` and used directly on the `div` element for the SKU block.

* Add server-side rendering class for Product SKU Gutenberg block

This commit adds the ".wp-block-woocommerce-product-sku" class to the Product SKU Gutenberg block for server-side rendering purposes. The inclusion of this class ensures consistent styling between the editor and the front-end view of the block, providing a seamless experience for users.

The class name is generated based on the Gutenberg block prefix "wp-block", the namespace "woocommerce", and the block name "product-sku", creating a unique and identifiable class for the block. This class enables developers to target the block specifically in both the editor and front-end styles, maintaining the integrity of the block's appearance and functionality across different environments.

By adding this class to the server-side rendered version of the block, we ensure a unified and coherent styling experience throughout the block's usage.

* Fix minor issues

* Gate only experimental features behind isFeaturePluginBuild flag

This commit refactors the Product SKU block supports to enable color, typography, and spacing features for all builds, while keeping experimental features specific to the feature plugin build. It also adds necessary lint and TypeScript error handling for the experimental spacing API.

* Add overflow-wrap to Product SKU block and update comment

This commit adds the overflow-wrap property to the Product SKU block styles to handle long SKU values gracefully.
It also updates the comment for the experimental spacing API to better reflect the usage check.

* Refactor Product SKU block classname and style handling

This commit refactors the Product SKU block classname and style handling. It combines classnames for better readability and removes the hardcoded textTransform style in favor of default styles.
This also fix the issue where textTransform style was not working as expected.
This commit is contained in:
Manish Menaria 2023-04-06 11:30:27 +05:30 committed by GitHub
parent 7a89cecb6b
commit c497190936
9 changed files with 109 additions and 7 deletions

View File

@ -16,6 +16,10 @@ export const blockAttributes: BlockAttributes = {
type: 'boolean',
default: false,
},
isDescendantOfAllProducts: {
type: 'boolean',
default: false,
},
showProductSelector: {
type: 'boolean',
default: false,

View File

@ -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';
/**
* Internal dependencies
@ -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>
@ -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
@ -62,6 +74,22 @@ const Block = ( props: Props ): JSX.Element | null => {
className={ className }
parentClassName={ parentClassName }
sku={ sku }
{ ...( props.isDescendantOfAllProducts && {
className: classnames(
className,
'wc-block-components-product-sku wp-block-woocommerce-product-sku',
{
[ colorProps.className ]: colorProps.className,
[ typographyProps.className ]:
typographyProps.className,
}
),
style: {
...colorProps.style,
...typographyProps.style,
...spacingProps.style,
},
} ) }
/>
);
};

View File

@ -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',
} );
const blockAttrs = {
...attributes,
...context,
@ -31,10 +34,22 @@ const Edit = ( {
);
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 ? undefined : style
}
>
<Block { ...blockAttrs } />
</div>
</>
);
};

View File

@ -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,
@ -33,6 +34,7 @@ const blockConfig: BlockConfiguration = {
'woocommerce/product-meta',
],
edit,
supports,
};
registerBlockType( 'woocommerce/product-sku', { ...blockConfig } );

View File

@ -2,4 +2,5 @@
display: block;
text-transform: uppercase;
@include font-size(small);
overflow-wrap: break-word;
}

View File

@ -0,0 +1,40 @@
/**
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import {
// @ts-expect-error We check if this exists before using it.
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalGetSpacingClassesAndStyles,
} from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import sharedConfig from '../shared/config';
export const supports = {
...sharedConfig.supports,
color: {
text: true,
background: true,
},
typography: {
fontSize: true,
lineHeight: true,
...( isFeaturePluginBuild() && {
__experimentalFontWeight: true,
__experimentalFontFamily: true,
__experimentalFontStyle: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalLetterSpacing: true,
} ),
},
...( typeof __experimentalGetSpacingClassesAndStyles === 'function' && {
spacing: {
margin: true,
padding: true,
},
} ),
};

View File

@ -3,4 +3,5 @@ export interface Attributes {
isDescendentOfQueryLoop: boolean;
isDescendentOfSingleProductTemplate: boolean;
showProductSelector: boolean;
isDescendantOfAllProducts: boolean;
}

View File

@ -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,
},
];
} );

View File

@ -1,6 +1,8 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
/**
* ProductSKU class.
*/
@ -65,11 +67,15 @@ class ProductSKU extends AbstractBlock {
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
);
}