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

Featured Category block: add support for global style #5542

Merged
merged 13 commits into from
Jan 26, 2022
58 changes: 17 additions & 41 deletions assets/js/blocks/featured-category/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
InnerBlocks,
InspectorControls,
MediaReplaceFlow,
PanelColorSettings,
withColors,
RichText,
} from '@wordpress/block-editor';
import {
Expand Down Expand Up @@ -56,8 +54,6 @@ import { withCategory } from '../../hocs';
* @param {function(any):any} props.getCategory Function for getting category details.
* @param {boolean} props.isLoading Whether loading or not.
* @param {Object} props.category The product category object.
* @param {Object} props.overlayColor Overlay color object for content.
* @param {function(any):any} props.setOverlayColor Setter for overlay color.
* @param {function(any):any} props.debouncedSpeak Function for delayed speak.
* @param {function():void} props.triggerUrlUpdate Function to update Shop now button Url.
*/
Expand All @@ -69,8 +65,6 @@ const FeaturedCategory = ( {
getCategory,
isLoading,
category,
overlayColor,
setOverlayColor,
debouncedSpeak,
triggerUrlUpdate = () => void null,
} ) => {
Expand Down Expand Up @@ -148,21 +142,14 @@ const FeaturedCategory = ( {
}
/>
</PanelBody>
<PanelColorSettings
title={ __( 'Overlay', 'woo-gutenberg-products-block' ) }
colorSettings={ [
{
value: overlayColor.color,
onChange: setOverlayColor,
label: __(
'Overlay Color',
{ !! url && (
<>
<PanelBody
title={ __(
'Overlay',
'woo-gutenberg-products-block'
),
},
] }
>
{ !! url && (
<>
) }
>
<RangeControl
label={ __(
'Background Opacity',
Expand All @@ -189,9 +176,9 @@ const FeaturedCategory = ( {
}
/>
) }
</>
) }
</PanelColorSettings>
</PanelBody>
</>
) }
</InspectorControls>
);
};
Expand Down Expand Up @@ -287,14 +274,8 @@ const FeaturedCategory = ( {
};

const renderCategory = () => {
const {
className,
contentAlign,
dimRatio,
focalPoint,
height,
showDesc,
} = attributes;
const { contentAlign, dimRatio, focalPoint, showDesc } = attributes;

const classes = classnames(
'wc-block-featured-category',
{
Expand All @@ -304,14 +285,10 @@ const FeaturedCategory = ( {
'has-background-dim': dimRatio !== 0,
},
dimRatioToClass( dimRatio ),
contentAlign !== 'center' && `has-${ contentAlign }-content`,
className
contentAlign !== 'center' && `has-${ contentAlign }-content`
);
const mediaSrc = attributes.mediaSrc || getCategoryImageSrc( category );
const style = !! category ? getBackgroundImageStyles( mediaSrc ) : {};
if ( overlayColor.color ) {
style.backgroundColor = overlayColor.color;
}
if ( focalPoint ) {
const bgPosX = focalPoint.x * 100;
const bgPosY = focalPoint.y * 100;
Expand All @@ -325,7 +302,10 @@ const FeaturedCategory = ( {
return (
<ResizableBox
className={ classes }
size={ { height } }
size={ {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that we should not limit the height in the editor.
Now the user can change the font-size. If the font-size is high, the block should become bigger otherwise the user can't see the entire text.

height: '',
width: '',
} }
minHeight={ getSetting( 'min_height', 500 ) }
enable={ { bottom: true } }
onResizeStop={ onResizeStop }
Expand Down Expand Up @@ -416,17 +396,13 @@ FeaturedCategory.propTypes = {
description: PropTypes.node,
permalink: PropTypes.string,
} ),
// from withColors
overlayColor: PropTypes.object,
setOverlayColor: PropTypes.func.isRequired,
// from withSpokenMessages
debouncedSpeak: PropTypes.func.isRequired,
triggerUrlUpdate: PropTypes.func,
};

export default compose( [
withCategory,
withColors( { overlayColor: 'background-color' } ),
withSpokenMessages,
withSelect( ( select, { clientId }, { dispatch } ) => {
const Block = select( 'core/block-editor' ).getBlock( clientId );
Expand Down
23 changes: 23 additions & 0 deletions assets/js/blocks/featured-category/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import Block from './block';

export const Edit = ( props: unknown ): JSX.Element => {
const blockProps = useBlockProps();

// The useBlockProps function returns the style with the `color`.
// We need to remove it to avoid the block to be styled with the color.
const { color, ...styles } = blockProps.style;

return (
<div { ...blockProps } style={ styles }>
<Block { ...props } />
</div>
);
};
25 changes: 20 additions & 5 deletions assets/js/blocks/featured-category/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import { InnerBlocks } from '@wordpress/block-editor';
import { registerBlockType } from '@wordpress/blocks';
import { getSetting } from '@woocommerce/settings';
import { Icon, folderStarred } from '@woocommerce/icons';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';

/**
* Internal dependencies
*/
import './style.scss';
import './editor.scss';
import Block from './block';
import { example } from './example';
import { Edit } from './edit';

/**
* Register and run the "Featured Category" block.
*/
registerBlockType( 'woocommerce/featured-category', {
apiVersion: 2,
title: __( 'Featured Category', 'woo-gutenberg-products-block' ),
icon: {
src: (
Expand All @@ -37,6 +39,21 @@ registerBlockType( 'woocommerce/featured-category', {
supports: {
align: [ 'wide', 'full' ],
html: false,
color: {
text: true,
background: true,
},
Copy link
Member

Choose a reason for hiding this comment

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

Background and text color are enabled by default so this can be shortened like this:

Suggested change
color: {
text: true,
background: true,
},
color: true,

typography: {
fontSize: true,
},
...( isFeaturePluginBuild() && {
__experimentalBorder: {
color: true,
radius: true,
width: true,
__experimentalSkipSerialization: false,
},
} ),
},
example,
attributes: {
Expand Down Expand Up @@ -146,14 +163,12 @@ registerBlockType( 'woocommerce/featured-category', {
*
* @param {Object} props Props to pass to block.
*/
edit( props ) {
return <Block { ...props } />;
},
edit: Edit,

/**
* Block content is rendered in PHP, not via save function.
*/
save() {
save: () => {
return <InnerBlocks.Content />;
},
} );
20 changes: 18 additions & 2 deletions assets/js/blocks/featured-category/style.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
.wp-block-woocommerce-featured-category {
border-color: transparent;
overflow: hidden;
color: #fff;
background-color: #000;
Copy link
Member

Choose a reason for hiding this comment

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

Can we order CSS properties alphabetically? It's easier to maintain.

}

.wc-block-featured-category {
position: relative;
background-color: $gray-900;
background-size: cover;
background-position: center center;
width: 100%;
margin: 0 0 1.5em 0;
margin: 0 0 0 0;
display: flex;
justify-content: center;
align-items: center;
Expand Down Expand Up @@ -63,6 +69,8 @@
.wc-block-featured-category__description,
.wc-block-featured-category__price,
.wc-block-featured-category__link {
color: inherit;
font-size: inherit;
width: 100%;
padding: 0 48px 16px 48px;
z-index: 1;
Expand All @@ -71,12 +79,19 @@
.wc-block-featured-category__title {
margin-top: 0;

div {
color: inherit;
font-size: inherit;
}

&::before {
display: none;
}
}

.wc-block-featured-category__description {
color: inherit;
font-size: inherit;
p {
margin: 0;
}
Expand All @@ -94,6 +109,7 @@
bottom: 0;
right: 0;
background-color: inherit;
border-radius: inherit;
opacity: 0.5;
z-index: 1;
}
Expand Down
17 changes: 17 additions & 0 deletions assets/js/hooks/style-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {
__experimentalUseColorProps,
__experimentalGetSpacingClassesAndStyles,
__experimentalUseBorderProps,
} from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -80,3 +81,19 @@ export const useColorProps = ( attributes: unknown ): WithStyle & WithClass => {

return __experimentalUseColorProps( { ...attributesObject, style } );
};

export const useBorderProps = (
attributes: unknown
): WithStyle & WithClass => {
if ( ! isFeaturePluginBuild() ) {
return {
className: '',
style: {},
};
}

const attributesObject = isObject( attributes ) ? attributes : {};
const style = parseStyle( attributesObject.style );

return __experimentalUseBorderProps( { ...attributesObject, style } );
};
Loading