Category List block: add support for global style (https://github.com/woocommerce/woocommerce-blocks/pull/5516)

* Product title: add support global style woocommerce/woocommerce-blocks#4965

* add specific type

* Enable global style for category list block woocommerce/woocommerce-blocks#4965

Enable global style for category list block

* fix import after merge

* add save function

* add feature flag
This commit is contained in:
Luigi Teschio 2022-02-14 11:29:17 +01:00 committed by GitHub
parent d178761252
commit f2dad05e0d
4 changed files with 56 additions and 4 deletions

View File

@ -16,6 +16,10 @@ import { HTMLAttributes } from 'react';
*/
import './style.scss';
import { Attributes } from './types';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';
type Props = Attributes & HTMLAttributes< HTMLDivElement >;
@ -26,10 +30,14 @@ type Props = Attributes & HTMLAttributes< HTMLDivElement >;
* @param {string} [props.className] CSS Class name for the component.
* @return {*} The component.
*/
const Block = ( { className }: Props ): JSX.Element | null => {
const Block = ( props: Props ): JSX.Element | null => {
const { className } = props;
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const colorProps = useColorProps( props );
const typographyProps = useTypographyProps( props );
if ( isEmpty( product.categories ) ) {
return null;
}
@ -39,10 +47,12 @@ const Block = ( { className }: Props ): JSX.Element | null => {
className={ classnames(
className,
'wc-block-components-product-category-list',
colorProps.className,
{
[ `${ parentClassName }__product-category-list` ]: parentClassName,
}
) }
style={ { ...colorProps.style, ...typographyProps.style } }
>
{ __( 'Categories:', 'woo-gutenberg-products-block' ) }{ ' ' }
<ul>

View File

@ -4,6 +4,7 @@
import { __ } from '@wordpress/i18n';
import { Disabled } from '@wordpress/components';
import EditProductLink from '@woocommerce/editor-components/edit-product-link';
import { useBlockProps } from '@wordpress/block-editor';
/**
* Internal dependencies
@ -18,13 +19,15 @@ interface Props {
}
const Edit = ( { attributes }: Props ): JSX.Element => {
const blockProps = useBlockProps();
return (
<>
<div { ...blockProps }>
<EditProductLink />
<Disabled>
<Block { ...attributes } />
</Disabled>
</>
</div>
);
};

View File

@ -1,7 +1,10 @@
/**
* External dependencies
*/
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
import {
isFeaturePluginBuild,
registerExperimentalBlockType,
} from '@woocommerce/block-settings';
import { BlockConfiguration } from '@wordpress/blocks';
/**
@ -15,13 +18,28 @@ import {
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import { Save } from './save';
const blockConfig: BlockConfiguration = {
...sharedConfig,
apiVersion: 2,
title,
description,
icon: { src: icon },
attributes,
supports: {
...( isFeaturePluginBuild() && {
color: {
text: true,
link: true,
background: false,
},
} ),
typography: {
fontSize: true,
},
},
save: Save,
edit,
};

View File

@ -0,0 +1,21 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { useBlockProps } from '@wordpress/block-editor';
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 ),
} ) }
/>
);
};