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

Commit

Permalink
Summary Product block: Add support for global style (#5524)
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

* Summary Product block: add support for global style #4965

Summary Product block: add support for global style

* add color global style under feature flag

* fix import after merge

* fix typo
  • Loading branch information
gigitux authored Jan 12, 2022
1 parent 345bfcd commit 22b2ceb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
16 changes: 15 additions & 1 deletion assets/js/atomic/blocks/product-elements/summary/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import classnames from 'classnames';
import Summary from '@woocommerce/base-components/summary';
import { blocksConfig } from '@woocommerce/block-settings';

import {
useInnerBlockLayoutContext,
useProductDataContext,
Expand All @@ -15,6 +16,10 @@ import { withProductDataContext } from '@woocommerce/shared-hocs';
* Internal dependencies
*/
import './style.scss';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Summary Block Component.
Expand All @@ -23,9 +28,13 @@ 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 ) {
return (
Expand Down Expand Up @@ -53,6 +62,7 @@ const Block = ( { className } ) => {
<Summary
className={ classnames(
className,
colorProps.className,
`wc-block-components-product-summary`,
{
[ `${ parentClassName }__product-summary` ]: parentClassName,
Expand All @@ -61,6 +71,10 @@ const Block = ( { className } ) => {
source={ source }
maxLength={ 150 }
countType={ blocksConfig.wordCountType || 'words' }
style={ {
...colorProps.style,
...typographyProps.style,
} }
/>
);
};
Expand Down
8 changes: 7 additions & 1 deletion assets/js/atomic/blocks/product-elements/summary/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -11,7 +12,12 @@ import withProductSelector from '../shared/with-product-selector';
import { BLOCK_TITLE, BLOCK_ICON } from './constants';

const Edit = ( { attributes } ) => {
return <Block { ...attributes } />;
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Block { ...attributes } />
</div>
);
};

export default withProductSelector( {
Expand Down
5 changes: 5 additions & 0 deletions assets/js/atomic/blocks/product-elements/summary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ import { registerBlockType } from '@wordpress/blocks';
import sharedConfig from '../shared/config';
import attributes from './attributes';
import edit from './edit';
import { supports } from './supports';
import {
BLOCK_TITLE as title,
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import { Save } from './save';

const blockConfig = {
apiVersion: 2,
title,
description,
icon: { src: icon },
attributes,
supports,
edit,
save: Save,
};

registerBlockType( 'woocommerce/product-summary', {
Expand Down
21 changes: 21 additions & 0 deletions assets/js/atomic/blocks/product-elements/summary/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 ),
} ) }
/>
);
};
17 changes: 17 additions & 0 deletions assets/js/atomic/blocks/product-elements/summary/supports.js
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,
},
};
11 changes: 10 additions & 1 deletion assets/js/base/components/summary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { RawHTML, useMemo } from '@wordpress/element';
import { WordCountType } from '@woocommerce/block-settings';
import { CSSProperties } from 'react';

/**
* Internal dependencies
Expand All @@ -14,6 +15,7 @@ interface SummaryProps {
source: string;
maxLength?: number;
countType?: WordCountType;
style?: CSSProperties;
}
/**
* Summary component.
Expand All @@ -23,18 +25,25 @@ interface SummaryProps {
* @param {number} props.maxLength Max length of the summary, using countType.
* @param {string} props.countType One of words, characters_excluding_spaces, or characters_including_spaces.
* @param {string} props.className Class name for rendered component.
* @param {CSSProperties} props.style Style Object for rendered component.
*
*/
export const Summary = ( {
source,
maxLength = 15,
countType = 'words',
className = '',
style = {},
}: SummaryProps ): JSX.Element => {
const summaryText = useMemo( () => {
return generateSummary( source, maxLength, countType );
}, [ source, maxLength, countType ] );

return <RawHTML className={ className }>{ summaryText }</RawHTML>;
return (
<RawHTML style={ style } className={ className }>
{ summaryText }
</RawHTML>
);
};

export default Summary;

0 comments on commit 22b2ceb

Please sign in to comment.