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

Commit

Permalink
Stock Indicator block: Add support for global style (#5525)
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

* Stock indicator block: add support for global style #4965

Stock indicator block: add support for global style

* fix import after merge
  • Loading branch information
gigitux authored Jan 17, 2022
1 parent 9a95518 commit 0d7acec
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
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;
}
}
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,
},
};

0 comments on commit 0d7acec

Please sign in to comment.