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

Add Style Attributes Hooks to blocks/hooks Directory. #6870

Merged
merged 5 commits into from
Aug 11, 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: 6 additions & 6 deletions assets/js/atomic/blocks/product-elements/button/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import {
useStoreEvents,
useStoreAddToCart,
} from '@woocommerce/base-context/hooks';
import {
useBorderProps,
useColorProps,
useTypographyProps,
useSpacingProps,
} from '@woocommerce/base-hooks';
import { decodeEntities } from '@wordpress/html-entities';
import { CART_URL } from '@woocommerce/block-settings';
import { getSetting } from '@woocommerce/settings';
Expand All @@ -21,12 +27,6 @@ import { withProductDataContext } from '@woocommerce/shared-hocs';
* Internal dependencies
*/
import './style.scss';
import {
useBorderProps,
useColorProps,
useTypographyProps,
useSpacingProps,
} from '../../../../hooks/style-attributes';

/**
* Product Button Block Component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { isEmpty } from 'lodash';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import { HTMLAttributes } from 'react';
Expand All @@ -16,10 +17,6 @@ import { HTMLAttributes } from 'react';
*/
import './style.scss';
import { Attributes } from './types';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

type Props = Attributes & HTMLAttributes< HTMLDivElement >;

Expand Down
10 changes: 5 additions & 5 deletions assets/js/atomic/blocks/product-elements/image/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import {
useBorderProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import { useStoreEvents } from '@woocommerce/base-context/hooks';

Expand All @@ -18,11 +23,6 @@ import { useStoreEvents } from '@woocommerce/base-context/hooks';
*/
import ProductSaleBadge from './../sale-badge/block';
import './style.scss';
import {
useBorderProps,
useSpacingProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Image Block Component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ProductDataContextProvider } from '@woocommerce/shared-context';
*/
import { Block } from '../block';

jest.mock( '../../../../../hooks/style-attributes', () => ( {
jest.mock( '@woocommerce/base-hooks', () => ( {
__esModule: true,
useBorderProps: jest.fn( () => ( {
className: '',
Expand Down
5 changes: 1 addition & 4 deletions assets/js/atomic/blocks/product-elements/price/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Price Block Component.
Expand Down
10 changes: 5 additions & 5 deletions assets/js/atomic/blocks/product-elements/rating/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import {
useColorProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import './style.scss';
import {
useColorProps,
useSpacingProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Rating Block Component.
Expand Down
12 changes: 6 additions & 6 deletions assets/js/atomic/blocks/product-elements/sale-badge/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import {
useBorderProps,
useColorProps,
useSpacingProps,
useTypographyProps,
} from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import './style.scss';
import {
useBorderProps,
useColorProps,
useSpacingProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Sale Badge Block Component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import './style.scss';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Stock Indicator Block Component.
Expand Down
5 changes: 1 addition & 4 deletions assets/js/atomic/blocks/product-elements/summary/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import './style.scss';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Summary Block Component.
Expand Down
5 changes: 1 addition & 4 deletions assets/js/atomic/blocks/product-elements/tag-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
import { isEmpty } from 'lodash';
import { withProductDataContext } from '@woocommerce/shared-hocs';

/**
* Internal dependencies
*/
import './style.scss';
import {
useColorProps,
useTypographyProps,
} from '../../../../hooks/style-attributes';

/**
* Product Tag List Block Component.
Expand Down
10 changes: 5 additions & 5 deletions assets/js/atomic/blocks/product-elements/title/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import ProductName from '@woocommerce/base-components/product-name';
import { useStoreEvents } from '@woocommerce/base-context/hooks';
import {
useSpacingProps,
useTypographyProps,
useColorProps,
} from '@woocommerce/base-hooks';

/**
* Internal dependencies
*/
import './style.scss';
import { Attributes } from './types';
import {
useSpacingProps,
useTypographyProps,
useColorProps,
} from '../../../../hooks/style-attributes';

type Props = Attributes & HTMLAttributes< HTMLDivElement >;

Expand Down
4 changes: 4 additions & 0 deletions assets/js/base/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export * from './use-position-relative-to-viewport';
export * from './use-previous';
export * from './use-shallow-equal';
export * from './use-throw-error';
export * from './use-spacing-props';
export * from './use-typography-props';
export * from './use-color-props';
export * from './use-border-props';
32 changes: 32 additions & 0 deletions assets/js/base/hooks/use-border-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/**
* External dependencies
*/
import { __experimentalUseBorderProps } from '@wordpress/block-editor';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { isObject } from '@woocommerce/types';
import { parseStyle } from '@woocommerce/base-utils';

type WithClass = {
className: string;
};

type WithStyle = {
style: Record< string, unknown >;
};

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 } );
};
30 changes: 30 additions & 0 deletions assets/js/base/hooks/use-color-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/**
* External dependencies
*/
import { __experimentalUseColorProps } from '@wordpress/block-editor';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { isObject } from '@woocommerce/types';
import { parseStyle } from '@woocommerce/base-utils';

type WithClass = {
className: string;
};

type WithStyle = {
style: Record< string, unknown >;
};

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

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

return __experimentalUseColorProps( { ...attributesObject, style } );
};
28 changes: 28 additions & 0 deletions assets/js/base/hooks/use-spacing-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/**
* External dependencies
*/
import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import { isObject } from '@woocommerce/types';
import { hasSpacingStyleSupport } from '@woocommerce/utils';
import { parseStyle } from '@woocommerce/base-utils';

type WithStyle = {
style: Record< string, unknown >;
};

export const useSpacingProps = ( attributes: unknown ): WithStyle => {
if ( ! isFeaturePluginBuild() || ! hasSpacingStyleSupport() ) {
return {
style: {},
};
}
const attributesObject = isObject( attributes ) ? attributes : {};
const style = parseStyle( attributesObject.style );

return __experimentalGetSpacingClassesAndStyles( {
...attributesObject,
style,
} );
};
30 changes: 30 additions & 0 deletions assets/js/base/hooks/use-typography-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/**
* External dependencies
*/
import { isObject } from '@woocommerce/types';
import { parseStyle } from '@woocommerce/base-utils';

type WithStyle = {
style: Record< string, unknown >;
};

export const useTypographyProps = ( attributes: unknown ): WithStyle => {
const attributesObject = isObject( attributes ) ? attributes : {};
const style = parseStyle( attributesObject.style );
const typography = isObject( style.typography )
? ( style.typography as Record< string, unknown > )
: {};

return {
style: {
fontSize: attributesObject.fontSize
? `var(--wp--preset--font-size--${ attributesObject.fontSize })`
: typography.fontSize,
lineHeight: typography.lineHeight,
fontWeight: typography.fontWeight,
textTransform: typography.textTransform,
fontFamily: attributesObject.fontFamily,
},
};
};
1 change: 1 addition & 0 deletions assets/js/base/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './get-valid-block-attributes';
export * from './product-data';
export * from './derive-selected-shipping-rates';
export * from './get-icons-from-payment-methods';
export * from './parse-style';
16 changes: 16 additions & 0 deletions assets/js/base/utils/parse-style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* External dependencies
*/
import { isString, isObject } from '@woocommerce/types';

export const parseStyle = ( style: unknown ): Record< string, unknown > => {
if ( isString( style ) ) {
return JSON.parse( style ) || {};
}

if ( isObject( style ) ) {
return style;
}

return {};
};
7 changes: 5 additions & 2 deletions assets/js/blocks/attribute-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { usePrevious, useShallowEqual } from '@woocommerce/base-hooks';
import {
usePrevious,
useShallowEqual,
useBorderProps,
} from '@woocommerce/base-hooks';
import {
useCollection,
useQueryStateByKey,
Expand Down Expand Up @@ -40,7 +44,6 @@ import classNames from 'classnames';
import { getAttributeFromID } from '../../utils/attributes';
import { updateAttributeFilter } from '../../utils/attributes-query';
import { previewAttributeObject, previewOptions } from './preview';
import { useBorderProps } from '../../hooks/style-attributes';
import './style.scss';
import {
formatParams,
Expand Down
Loading