2023-05-12 12:42:16 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
2023-05-30 13:45:59 +00:00
|
|
|
import { isString, isObject } from '@woocommerce/types';
|
|
|
|
import type { Style as StyleEngineProperties } from '@wordpress/style-engine/src/types';
|
|
|
|
import type { CSSProperties } from 'react';
|
2023-05-12 12:42:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useTypographyProps } from './use-typography-props';
|
|
|
|
import {
|
|
|
|
getColorClassesAndStyles,
|
|
|
|
getBorderClassesAndStyles,
|
|
|
|
getSpacingClassesAndStyles,
|
|
|
|
} from '../utils';
|
|
|
|
|
2023-05-30 13:45:59 +00:00
|
|
|
export type StyleProps = {
|
|
|
|
className: string;
|
|
|
|
style: CSSProperties;
|
2023-05-12 12:42:16 +00:00
|
|
|
};
|
|
|
|
|
2023-07-03 10:50:06 +00:00
|
|
|
type BlockAttributes = Record< string, unknown > & {
|
2023-05-30 13:45:59 +00:00
|
|
|
style?: StyleEngineProperties | string | undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
type StyleAttributes = Record< string, unknown > & {
|
|
|
|
style: StyleEngineProperties;
|
2023-05-12 12:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-05-30 13:45:59 +00:00
|
|
|
* Parses incoming props.
|
2023-05-12 12:42:16 +00:00
|
|
|
*
|
2023-05-30 13:45:59 +00:00
|
|
|
* This may include style properties at the top level, or may include a nested `style` object. This ensures the expected
|
|
|
|
* values are present and converts any string based values to objects as required.
|
2023-05-12 12:42:16 +00:00
|
|
|
*/
|
2023-05-30 13:45:59 +00:00
|
|
|
const parseStyleAttributes = ( rawProps: BlockAttributes ): StyleAttributes => {
|
|
|
|
const props = isObject( rawProps )
|
|
|
|
? rawProps
|
2023-05-12 12:42:16 +00:00
|
|
|
: {
|
|
|
|
style: {},
|
|
|
|
};
|
|
|
|
|
2023-05-30 13:45:59 +00:00
|
|
|
let style = props.style;
|
2023-05-12 12:42:16 +00:00
|
|
|
|
2023-05-30 13:45:59 +00:00
|
|
|
if ( isString( style ) ) {
|
|
|
|
style = JSON.parse( style ) || {};
|
|
|
|
}
|
2023-05-12 12:42:16 +00:00
|
|
|
|
2023-05-30 13:45:59 +00:00
|
|
|
if ( ! isObject( style ) ) {
|
|
|
|
style = {};
|
|
|
|
}
|
2023-05-12 12:42:16 +00:00
|
|
|
|
2023-05-30 13:45:59 +00:00
|
|
|
return {
|
|
|
|
...props,
|
2023-05-12 12:42:16 +00:00
|
|
|
style,
|
2023-05-30 13:45:59 +00:00
|
|
|
};
|
|
|
|
};
|
2023-05-12 12:42:16 +00:00
|
|
|
|
2023-05-30 13:45:59 +00:00
|
|
|
/**
|
|
|
|
* Returns the CSS class names and inline styles for a block when provided with its props/attributes.
|
|
|
|
*
|
|
|
|
* This hook (and its utilities) borrow functionality from the Gutenberg Block Editor package--something we don't want
|
|
|
|
* to import on the frontend.
|
|
|
|
*/
|
|
|
|
export const useStyleProps = ( props: BlockAttributes ): StyleProps => {
|
|
|
|
const styleAttributes = parseStyleAttributes( props );
|
|
|
|
const colorProps = getColorClassesAndStyles( styleAttributes );
|
|
|
|
const borderProps = getBorderClassesAndStyles( styleAttributes );
|
|
|
|
const spacingProps = getSpacingClassesAndStyles( styleAttributes );
|
|
|
|
const typographyProps = useTypographyProps( styleAttributes );
|
2023-05-12 12:42:16 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
className: classnames(
|
|
|
|
typographyProps.className,
|
|
|
|
colorProps.className,
|
|
|
|
borderProps.className,
|
|
|
|
spacingProps.className
|
|
|
|
),
|
|
|
|
style: {
|
|
|
|
...typographyProps.style,
|
|
|
|
...colorProps.style,
|
|
|
|
...borderProps.style,
|
|
|
|
...spacingProps.style,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|