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

Stock Indicator block: Add support for global style #5525

Merged
merged 8 commits into from
Jan 17, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import { withProductDataContext } from '@woocommerce/shared-hocs';
* Internal dependencies
*/
import './style.scss';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Stock Indicator Block Component.
Expand All @@ -22,9 +26,12 @@ import './style.scss';
* @param {string} [props.className] CSS Class name for the component.
* @return {*} The component.
*/
const Block = ( { className } ) => {
const Block = ( props ) => {
const { className } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const colorProps = useColorProps( props );
const typographyProps = useTypographyProps( props );

if ( ! product.id || ! product.is_purchasable ) {
return null;
Expand All @@ -38,6 +45,7 @@ const Block = ( { className } ) => {
<div
className={ classnames(
className,
colorProps.className,
'wc-block-components-product-stock-indicator',
{
[ `${ parentClassName }__stock-indicator` ]: parentClassName,
Expand All @@ -47,6 +55,7 @@ const Block = ( { className } ) => {
'wc-block-components-product-stock-indicator--available-on-backorder': !! isBackordered,
}
) }
style={ { ...colorProps.style, ...typographyProps.style } }
>
{ lowStock
? lowStockText( lowStock )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
import { useBlockProps } from '@wordpress/block-editor';

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ import { registerExperimentalBlockType } from '@woocommerce/block-settings';
import sharedConfig from '../shared/config';
import attributes from './attributes';
import edit from './edit';
import { Save } from './save';
import { supports } from './supports';

import {
BLOCK_TITLE as title,
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';

const blockConfig = {
apiVersion: 2,
title,
description,
icon: { src: icon },
attributes,
supports,
edit,
save: Save,
};

registerExperimentalBlockType( 'woocommerce/product-stock-indicator', {
Expand Down
21 changes: 21 additions & 0 deletions assets/js/atomic/blocks/product-elements/stock-indicator/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 @@ -3,15 +3,4 @@
margin-bottom: em($gap-small);
display: block;
@include font-size(small);

&--in-stock {
color: $in-stock-color;
}
&--out-of-stock {
color: $no-stock-color;
}
&--low-stock,
&--available-on-backorder {
color: $low-stock-color;
}
Comment on lines -7 to -16
Copy link
Member

Choose a reason for hiding this comment

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

We are removing the feature that store owners actually need here. The current Supports API doesn't allow multiple colors, so I don't think it's a good idea to remove styles for stock statuses. Instead, we should accept the limitation of the Supports API. In the other words, ignoring the color, only add global style support for typography.

We can also create custom inspector settings to control colors for each state, and it will be local block style only, not global style.

Copy link
Contributor Author

@gigitux gigitux Jan 13, 2022

Choose a reason for hiding this comment

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

We are removing the feature that store owners actually need here. The current Supports API doesn't allow multiple colors, so I don't think it's a good idea to remove styles for stock statuses.

Before proceeding, I asked the rest of the team. Please check this discussion (p1641461102003800-slack-C02FL3X7KR6), maybe we can continue the discussion there.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';

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

Choose a reason for hiding this comment

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

It makes sense to me to support all typography properties for this block instead of only font size. At least, font-weight and text-transform should be supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to main issue #4965, we should enable only font-size, but yeah we can enable them easily. @Aljullu, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, makes sense to me. I tested the Post Categories block from Gutenberg and it offers many more typography options than what we offer in Stock indicator.

I guess the same could be said about the Product Category List, Product Summary and Product Tag List.

@vivialice what are your thoughts on this? Should we enable all typography properties in those blocks?

},
};