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

Commit

Permalink
Featured Product block: enable global style #4965
Browse files Browse the repository at this point in the history
Featured Product block: enable global style
  • Loading branch information
gigitux committed Jan 12, 2022
1 parent 1924ec4 commit 2f80512
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 47 deletions.
49 changes: 16 additions & 33 deletions assets/js/blocks/featured-product/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 { withSelect } from '@wordpress/data';
Expand Down Expand Up @@ -44,6 +42,7 @@ import {
getImageSrcFromProduct,
getImageIdFromProduct,
} from '../../utils/products';
import { useColorProps } from '../../hooks/style-attributes';

/**
* Component to handle edit mode of "Featured Product".
Expand All @@ -55,10 +54,8 @@ import {
* @param {function(any):any} props.getProduct Function for getting the product.
* @param {boolean} props.isLoading Whether product is loading or not.
* @param {boolean} props.isSelected Whether block is selected or not.
* @param {Object} props.overlayColor Overlay color object.
* @param {Object} props.product Product object.
* @param {function(any):any} props.setAttributes Setter for attributes.
* @param {function(any):any} props.setOverlayColor Setter for overlay color.
* @param {function():any} props.triggerUrlUpdate Function for triggering a url update for product.
*/
const FeaturedProduct = ( {
Expand All @@ -68,10 +65,8 @@ const FeaturedProduct = ( {
getProduct,
isLoading,
isSelected,
overlayColor,
product,
setAttributes,
setOverlayColor,
triggerUrlUpdate = () => void null,
} ) => {
const renderApiError = () => (
Expand Down Expand Up @@ -210,21 +205,14 @@ const FeaturedProduct = ( {
}
/>
</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 @@ -251,9 +239,9 @@ const FeaturedProduct = ( {
}
/>
) }
</>
) }
</PanelColorSettings>
</PanelBody>
</>
) }
</InspectorControls>
);
};
Expand All @@ -277,17 +265,13 @@ const FeaturedProduct = ( {
'has-background-dim': dimRatio !== 0,
},
dimRatioToClass( dimRatio ),
contentAlign !== 'center' && `has-${ contentAlign }-content`,
className
contentAlign !== 'center' && `has-${ contentAlign }-content`
);

const style = getBackgroundImageStyles(
attributes.mediaSrc || product
);

if ( overlayColor.color ) {
style.backgroundColor = overlayColor.color;
}
if ( focalPoint ) {
const bgPosX = focalPoint.x * 100;
const bgPosY = focalPoint.y * 100;
Expand All @@ -301,7 +285,10 @@ const FeaturedProduct = ( {
return (
<ResizableBox
className={ classes }
size={ { height } }
size={ {
height: '',
width: '',
} }
minHeight={ getSetting( 'min_height', 500 ) }
enable={ { bottom: true } }
onResizeStop={ onResizeStop }
Expand Down Expand Up @@ -451,17 +438,13 @@ FeaturedProduct.propTypes = {
price_html: 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( [
withProduct,
withColors( { overlayColor: 'background-color' } ),
withSpokenMessages,
withSelect( ( select, { clientId }, { dispatch } ) => {
const Block = select( 'core/block-editor' ).getBlock( clientId );
Expand Down
20 changes: 20 additions & 0 deletions assets/js/blocks/featured-product/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';

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

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

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

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

/**
* Register and run the "Featured Product" block.
*/
registerBlockType( 'woocommerce/featured-product', {
apiVersion: 2,
title: __( 'Featured Product', 'woo-gutenberg-products-block' ),
icon: {
src: (
Expand All @@ -37,6 +38,21 @@ registerBlockType( 'woocommerce/featured-product', {
supports: {
align: [ 'wide', 'full' ],
html: false,
color: {
text: true,
background: true,
},
typography: {
fontSize: true,
},
...( isFeaturePluginBuild() && {
__experimentalBorder: {
color: true,
radius: true,
width: true,
__experimentalSkipSerialization: false,
},
} ),
},
example,
attributes: {
Expand Down Expand Up @@ -154,9 +170,7 @@ registerBlockType( 'woocommerce/featured-product', {
*
* @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.
Expand Down
14 changes: 11 additions & 3 deletions assets/js/blocks/featured-product/style.scss
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
.wp-block-woocommerce-featured-product {
border-color: transparent;
overflow: hidden;
color: #fff;
background-color: #000;
}

.wc-block-featured-product {
position: relative;
background-color: $gray-900;
background-color: inherit;
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 @@ -49,10 +56,11 @@
.wc-block-featured-product__variation,
.wc-block-featured-product__description,
.wc-block-featured-product__price {
color: $white;
line-height: 1.25;
margin-bottom: 0;
text-align: center;
color: inherit;
font-size: inherit;

a,
a:hover,
Expand Down
26 changes: 20 additions & 6 deletions src/BlockTypes/FeaturedProduct.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;

/**
* FeaturedProduct class.
*/
Expand Down Expand Up @@ -29,6 +31,13 @@ class FeaturedProduct extends AbstractDynamicBlock {
'showPrice' => true,
);

/**
* Global style enabled for this block.
*
* @var array
*/
protected $global_style_wrapper = array( 'text_color', 'font_size', 'border_color', 'border_radius', 'border_width', 'background_color', 'text_color' );

/**
* Render the Featured Product block.
*
Expand Down Expand Up @@ -68,7 +77,10 @@ protected function render( $attributes, $content ) {
wp_kses_post( $product->get_price_html() )
);

$output = sprintf( '<div class="%1$s" style="%2$s">', esc_attr( $this->get_classes( $attributes ) ), esc_attr( $this->get_styles( $attributes, $product ) ) );
$styles = $this->get_styles( $attributes, $product );
$classes = $this->get_classes( $attributes );

$output = sprintf( '<div class="%1$s wp-block-woocommerce-featured-product" style="%2$s">', esc_attr( trim( $classes ) ), esc_attr( $styles ) );
$output .= '<div class="wc-block-featured-product__wrapper">';
$output .= $title;
if ( $attributes['showDesc'] ) {
Expand Down Expand Up @@ -118,12 +130,15 @@ public function get_styles( $attributes, $product ) {

if ( is_array( $attributes['focalPoint'] ) && 2 === count( $attributes['focalPoint'] ) ) {
$style .= sprintf(
'background-position: %s%% %s%%',
'background-position: %s%% %s%%;',
$attributes['focalPoint']['x'] * 100,
$attributes['focalPoint']['y'] * 100
);
}

$global_style_style = StyleAttributesUtils::get_styles_by_attributes( $attributes, $this->global_style_wrapper );
$style .= $global_style_style;

return $style;
}

Expand Down Expand Up @@ -152,14 +167,13 @@ public function get_classes( $attributes ) {
$classes[] = "has-{$attributes['contentAlign']}-content";
}

if ( isset( $attributes['overlayColor'] ) ) {
$classes[] = "has-{$attributes['overlayColor']}-background-color";
}

if ( isset( $attributes['className'] ) ) {
$classes[] = $attributes['className'];
}

$global_style_classes = StyleAttributesUtils::get_classes_by_attributes( $attributes, $this->global_style_wrapper );
$classes[] = $global_style_classes;

return implode( ' ', $classes );
}

Expand Down

0 comments on commit 2f80512

Please sign in to comment.