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

Commit

Permalink
Experiment: Replace style hooks coming from @wordpress/block-editor (
Browse files Browse the repository at this point in the history
…#9251)

* Replace all style hooks with useStyleProps hook

* Remove border/color/spacing hooks

* Style Props Hook

* Make use of `change-case` package

* Tidy up block wrappers

* Attribute filter does not use frontend.ts nor styles within block

* Remove frontend from filter blocks and unused styleprops usage

* Tidy up variable names so its clearer attributes are not required specifically from blocks

* Update assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-title-label-block/block.tsx

Co-authored-by: Albert Juhé Lluveras <[email protected]>

* Update assets/js/blocks/attribute-filter/block-wrapper.tsx

Co-authored-by: Albert Juhé Lluveras <[email protected]>

* Update assets/js/blocks/active-filters/block-wrapper.tsx

Co-authored-by: Albert Juhé Lluveras <[email protected]>

* Update assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-checkout-button-block/block.tsx

Co-authored-by: Tom Cafferkey <[email protected]>

* Update assets/js/blocks/rating-filter/block-wrapper.tsx

Co-authored-by: Tom Cafferkey <[email protected]>

* Update assets/js/blocks/stock-filter/block-wrapper.tsx

Co-authored-by: Tom Cafferkey <[email protected]>

* Update assets/js/blocks/price-filter/block-wrapper.tsx

Co-authored-by: Tom Cafferkey <[email protected]>

* Update assets/js/blocks/mini-cart/mini-cart-contents/inner-blocks/mini-cart-cart-button-block/block.tsx

Co-authored-by: Tom Cafferkey <[email protected]>

* Simplify styleprop

* Styleprops simplify

* Fix withFeaturedItem styles

* Like the original hook, flatten props and combine with parsed styles

---------

Co-authored-by: Albert Juhé Lluveras <[email protected]>
Co-authored-by: Tom Cafferkey <[email protected]>
  • Loading branch information
3 people authored May 12, 2023
1 parent b9148b4 commit a992c64
Show file tree
Hide file tree
Showing 42 changed files with 471 additions and 501 deletions.
80 changes: 24 additions & 56 deletions assets/js/atomic/blocks/product-elements/button/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import {
useStoreEvents,
useStoreAddToCart,
} from '@woocommerce/base-context/hooks';
import {
useBorderProps,
useColorProps,
useTypographyProps,
useSpacingProps,
} from '@woocommerce/base-hooks';
import { useStyleProps } from '@woocommerce/base-hooks';
import { decodeEntities } from '@wordpress/html-entities';
import { CART_URL } from '@woocommerce/block-settings';
import { getSetting } from '@woocommerce/settings';
Expand All @@ -35,22 +30,18 @@ import type {
/**
* Product Button Block Component.
*
* @param {Object} props Incoming props.
* @param {Object} [props.product] Product.
* @param {Object} [props.colorStyles] Object contains CSS class and CSS style for color.
* @param {Object} [props.borderStyles] Object contains CSS class and CSS style for border.
* @param {Object} [props.typographyStyles] Object contains CSS class and CSS style for typography.
* @param {Object} [props.spacingStyles] Object contains CSS style for spacing.
* @param {Object} [props.textAlign] Text alignment.
* @param {Object} props Incoming props.
* @param {Object} [props.product] Product.
* @param {Object} [props.style] Object contains CSS Styles.
* @param {string} [props.className] String contains CSS class.
* @param {Object} [props.textAlign] Text alignment.
*
* @return {*} The component.
*/
const AddToCartButton = ( {
product,
colorStyles,
borderStyles,
typographyStyles,
spacingStyles,
className,
style,
textAlign,
}: AddToCartButtonAttributes ): JSX.Element => {
const {
Expand Down Expand Up @@ -114,14 +105,15 @@ const AddToCartButton = ( {

return (
<ButtonTag
{ ...buttonProps }
aria-label={ buttonAriaLabel }
disabled={ addingToCart }
className={ classnames(
className,
'wp-block-button__link',
'wp-element-button',
'add_to_cart_button',
'wc-block-components-product-button__button',
colorStyles.className,
borderStyles.className,
{
loading: addingToCart,
added: addedToCart,
Expand All @@ -130,14 +122,7 @@ const AddToCartButton = ( {
[ `has-text-align-${ textAlign }` ]: textAlign,
}
) }
style={ {
...colorStyles.style,
...borderStyles.style,
...typographyStyles.style,
...spacingStyles.style,
} }
disabled={ addingToCart }
{ ...buttonProps }
style={ style }
>
{ buttonText }
</ButtonTag>
Expand All @@ -147,19 +132,15 @@ const AddToCartButton = ( {
/**
* Product Button Block Component.
*
* @param {Object} props Incoming props.
* @param {Object} [props.colorStyles] Object contains CSS class and CSS style for color.
* @param {Object} [props.borderStyles] Object contains CSS class and CSS style for border.
* @param {Object} [props.typographyStyles] Object contains CSS class and CSS style for typography.
* @param {Object} [props.spacingStyles] Object contains CSS style for spacing.
* @param {Object} props Incoming props.
* @param {Object} [props.style] Object contains CSS Styles.
* @param {string} [props.className] String contains CSS class.
*
* @return {*} The component.
*/
const AddToCartButtonPlaceholder = ( {
colorStyles,
borderStyles,
typographyStyles,
spacingStyles,
className,
style,
}: AddToCartButtonPlaceholderAttributes ): JSX.Element => {
return (
<button
Expand All @@ -169,15 +150,9 @@ const AddToCartButtonPlaceholder = ( {
'add_to_cart_button',
'wc-block-components-product-button__button',
'wc-block-components-product-button__button--placeholder',
colorStyles.className,
borderStyles.className
className
) }
style={ {
...colorStyles.style,
...borderStyles.style,
...typographyStyles.style,
...spacingStyles.style,
} }
style={ style }
disabled={ true }
/>
);
Expand All @@ -193,12 +168,9 @@ const AddToCartButtonPlaceholder = ( {
*/
export const Block = ( props: BlockAttributes ): JSX.Element => {
const { className, textAlign } = props;
const styleProps = useStyleProps( props );
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const colorProps = useColorProps( props );
const borderProps = useBorderProps( props );
const typographyProps = useTypographyProps( props );
const spacingProps = useSpacingProps( props );

return (
<div
Expand All @@ -218,17 +190,13 @@ export const Block = ( props: BlockAttributes ): JSX.Element => {
{ product.id ? (
<AddToCartButton
product={ product }
colorStyles={ colorProps }
borderStyles={ borderProps }
typographyStyles={ typographyProps }
spacingStyles={ spacingProps }
style={ styleProps.style }
className={ styleProps.className }
/>
) : (
<AddToCartButtonPlaceholder
colorStyles={ colorProps }
borderStyles={ borderProps }
typographyStyles={ typographyProps }
spacingStyles={ spacingProps }
style={ styleProps.style }
className={ styleProps.className }
/>
) }
</div>
Expand Down
6 changes: 2 additions & 4 deletions assets/js/atomic/blocks/product-elements/button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export interface BlockAttributes {
}

export interface AddToCartButtonPlaceholderAttributes {
borderStyles: WithClass & WithStyle;
colorStyles: WithClass & WithStyle;
spacingStyles: WithStyle;
typographyStyles: WithStyle;
className: string;
style: Record< string, unknown >;
}

export interface AddToCartButtonAttributes
Expand Down
26 changes: 6 additions & 20 deletions assets/js/atomic/blocks/product-elements/image/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import {
useBorderProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
import { useStyleProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import { useStoreEvents } from '@woocommerce/base-context/hooks';
import type { HTMLAttributes } from 'react';
Expand Down Expand Up @@ -87,12 +83,10 @@ export const Block = ( props: Props ): JSX.Element | null => {
saleBadgeAlign = 'right',
...restProps
} = props;
const styleProps = useStyleProps( props );
const { parentClassName } = useInnerBlockLayoutContext();
const { product, isLoading } = useProductDataContext();
const { dispatchStoreEvent } = useStoreEvents();
const typographyProps = useTypographyProps( props );
const borderProps = useBorderProps( props );
const spacingProps = useSpacingProps( props );

if ( ! product.id ) {
return (
Expand All @@ -104,13 +98,9 @@ export const Block = ( props: Props ): JSX.Element | null => {
[ `${ parentClassName }__product-image` ]:
parentClassName,
},
borderProps.className
styleProps.className
) }
style={ {
...typographyProps.style,
...borderProps.style,
...spacingProps.style,
} }
style={ styleProps.style }
>
<ImagePlaceholder />
</div>
Expand Down Expand Up @@ -142,13 +132,9 @@ export const Block = ( props: Props ): JSX.Element | null => {
{
[ `${ parentClassName }__product-image` ]: parentClassName,
},
borderProps.className
styleProps.className
) }
style={ {
...typographyProps.style,
...borderProps.style,
...spacingProps.style,
} }
style={ styleProps.style }
>
<ParentComponent { ...( showProductLink && anchorProps ) }>
{ !! showSaleBadge && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ import { ImageSizing } from '../types';

jest.mock( '@woocommerce/base-hooks', () => ( {
__esModule: true,
useBorderProps: jest.fn( () => ( {
useStyleProps: jest.fn( () => ( {
className: '',
style: {},
} ) ),
useTypographyProps: jest.fn( () => ( {
style: {},
} ) ),
useSpacingProps: jest.fn( () => ( {
style: {},
} ) ),
} ) );

const productWithoutImages: ProductResponseItem = {
Expand Down
28 changes: 7 additions & 21 deletions assets/js/atomic/blocks/product-elements/price/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import {
useColorProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
import { useStyleProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import { CurrencyCode } from '@woocommerce/type-defs/currency';
import type { HTMLAttributes } from 'react';
Expand Down Expand Up @@ -41,23 +37,20 @@ interface PriceProps {

export const Block = ( props: Props ): JSX.Element | null => {
const { className, textAlign, isDescendentOfSingleProductTemplate } = props;
const styleProps = useStyleProps( props );
const { parentName, parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();

const isDescendentOfAllProductsBlock =
parentName === 'woocommerce/all-products';
const colorProps = useColorProps( props );
const spacingProps = useSpacingProps( props );
const typographyProps = useTypographyProps( props );

const wrapperClassName = classnames(
'wc-block-components-product-price',
className,
colorProps.className,
styleProps.className,
{
[ `${ parentClassName }__product-price` ]: parentClassName,
},
typographyProps.className
}
);

if ( ! product.id && ! isDescendentOfSingleProductTemplate ) {
Expand All @@ -74,13 +67,6 @@ export const Block = ( props: Props ): JSX.Element | null => {
return productPriceComponent;
}

const style = {
...colorProps.style,
...typographyProps.style,
};
const spacingStyle = {
...spacingProps.style,
};
const prices: PriceProps = product.prices;
const currency = isDescendentOfSingleProductTemplate
? getCurrencyFromPriceResponse()
Expand All @@ -97,8 +83,9 @@ export const Block = ( props: Props ): JSX.Element | null => {
<ProductPrice
align={ textAlign }
className={ wrapperClassName }
regularPriceStyle={ style }
priceStyle={ style }
style={ styleProps.style }
regularPriceStyle={ styleProps.style }
priceStyle={ styleProps.style }
priceClassName={ priceClassName }
currency={ currency }
price={
Expand All @@ -119,7 +106,6 @@ export const Block = ( props: Props ): JSX.Element | null => {
[ `${ parentClassName }__product-price__regular` ]:
parentClassName,
} ) }
spacingStyle={ spacingStyle }
/>
);
if ( isDescendentOfAllProductsBlock ) {
Expand Down
21 changes: 4 additions & 17 deletions assets/js/atomic/blocks/product-elements/rating/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import {
useColorProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
import { useStyleProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import { isNumber, ProductResponseItem } from '@woocommerce/types';

Expand Down Expand Up @@ -136,17 +132,15 @@ interface ProductRatingProps {

export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
const { textAlign, isDescendentOfSingleProductBlock } = props;
const styleProps = useStyleProps( props );
const { parentClassName } = useInnerBlockLayoutContext();
const { product } = useProductDataContext();
const rating = getAverageRating( product );
const colorProps = useColorProps( props );
const typographyProps = useTypographyProps( props );
const spacingProps = useSpacingProps( props );
const reviews = getRatingCount( product );
const href = getReviewsHref( product );

const className = classnames(
colorProps.className,
styleProps.className,
'wc-block-components-product-rating',
{
[ `${ parentClassName }__product-rating` ]: parentClassName,
Expand All @@ -165,14 +159,7 @@ export const Block = ( props: ProductRatingProps ): JSX.Element | null => {
);

return (
<div
className={ className }
style={ {
...colorProps.style,
...typographyProps.style,
...spacingProps.style,
} }
>
<div className={ className } style={ styleProps.style }>
<div className="wc-block-components-product-rating__container">
{ content }
{ reviews && isDescendentOfSingleProductBlock ? (
Expand Down
Loading

0 comments on commit a992c64

Please sign in to comment.