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

Category List block: add support for global style #5516

Merged
merged 8 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion assets/js/atomic/blocks/product-elements/category-list/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 >;

Expand All @@ -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;
}
Expand All @@ -39,10 +47,12 @@ const Block = ( { className }: Props ): JSX.Element | null => {
className={ classnames(
className,
'wc-block-components-product-category-list',
colorProps.className,
Copy link
Member

Choose a reason for hiding this comment

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

Even colorProps.className can be undefined, we don't need to check for its value because undefined is an expected value and will be skipped by classnames().

{
[ `${ parentClassName }__product-category-list` ]: parentClassName,
}
) }
style={ { ...colorProps.style, ...typographyProps.style } }
>
{ __( 'Categories:', 'woo-gutenberg-products-block' ) }{ ' ' }
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,13 +19,15 @@ interface Props {
}

const Edit = ( { attributes }: Props ): JSX.Element => {
const blockProps = useBlockProps();

return (
<>
<div { ...blockProps }>
<EditProductLink />
<Disabled>
<Block { ...attributes } />
</Disabled>
</>
</div>
);
};

Expand Down
20 changes: 19 additions & 1 deletion assets/js/atomic/blocks/product-elements/category-list/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* External dependencies
*/
import { registerExperimentalBlockType } from '@woocommerce/block-settings';
import {
isFeaturePluginBuild,
registerExperimentalBlockType,
} from '@woocommerce/block-settings';
import { BlockConfiguration } from '@wordpress/blocks';

/**
Expand All @@ -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,
};

Expand Down
21 changes: 21 additions & 0 deletions assets/js/atomic/blocks/product-elements/category-list/save.tsx
Original file line number Diff line number Diff line change
@@ -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 ),
} ) }
/>
);
};