2022-08-11 00:04:12 +00:00
|
|
|
/* eslint-disable @wordpress/no-unsafe-wp-apis */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-02-03 09:54:16 +00:00
|
|
|
import { isObject, isString } from '@woocommerce/types';
|
2022-08-11 00:04:12 +00:00
|
|
|
import { parseStyle } from '@woocommerce/base-utils';
|
|
|
|
|
2023-02-03 09:54:16 +00:00
|
|
|
type WithClass = {
|
|
|
|
className: string;
|
|
|
|
};
|
|
|
|
|
2022-08-11 00:04:12 +00:00
|
|
|
type WithStyle = {
|
|
|
|
style: Record< string, unknown >;
|
|
|
|
};
|
|
|
|
|
2023-02-03 09:54:16 +00:00
|
|
|
export const useTypographyProps = (
|
|
|
|
attributes: unknown
|
|
|
|
): WithStyle & WithClass => {
|
2022-08-11 00:04:12 +00:00
|
|
|
const attributesObject = isObject( attributes ) ? attributes : {};
|
|
|
|
const style = parseStyle( attributesObject.style );
|
|
|
|
const typography = isObject( style.typography )
|
2023-02-03 09:54:16 +00:00
|
|
|
? ( style.typography as Record< string, string > )
|
2022-08-11 00:04:12 +00:00
|
|
|
: {};
|
|
|
|
|
2023-02-03 09:54:16 +00:00
|
|
|
const classNameFallback = isString( typography.fontFamily )
|
|
|
|
? typography.fontFamily
|
|
|
|
: '';
|
|
|
|
const className = attributesObject.fontFamily
|
|
|
|
? `has-${ attributesObject.fontFamily }-font-family`
|
|
|
|
: classNameFallback;
|
|
|
|
|
2022-08-11 00:04:12 +00:00
|
|
|
return {
|
2023-02-03 09:54:16 +00:00
|
|
|
className,
|
2022-08-11 00:04:12 +00:00
|
|
|
style: {
|
|
|
|
fontSize: attributesObject.fontSize
|
|
|
|
? `var(--wp--preset--font-size--${ attributesObject.fontSize })`
|
|
|
|
: typography.fontSize,
|
2022-12-12 14:53:42 +00:00
|
|
|
fontStyle: typography.fontStyle,
|
2023-02-03 09:54:16 +00:00
|
|
|
fontWeight: typography.fontWeight,
|
|
|
|
letterSpacing: typography.letterSpacing,
|
|
|
|
lineHeight: typography.lineHeight,
|
|
|
|
textDecoration: typography.textDecoration,
|
2022-08-11 00:04:12 +00:00
|
|
|
textTransform: typography.textTransform,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|