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

Commit

Permalink
Tidy up variable names so its clearer attributes are not required spe…
Browse files Browse the repository at this point in the history
…cifically from blocks
  • Loading branch information
mikejolley committed May 4, 2023
1 parent 74978b3 commit 8164c12
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
35 changes: 16 additions & 19 deletions assets/js/base/hooks/use-style-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,43 @@ import {
WithStyle,
} from '../utils';

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

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

/**
* Parse the style object saved with blocks and returns the CSS class names and inline styles.
* 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 = ( attributes: unknown ): StyleProps => {
const attributesObject = isObject( attributes ) ? attributes : {};
export const useStyleProps = ( props: blockAttributes ): StyleProps => {
const propsObject = isObject( props )
? props
: {
style: {},
};

const typographyProps = useTypographyProps( attributesObject );
const style = parseStyle( attributesObject.style );
const styleObject = parseStyle( propsObject.style );

const colorProps = getColorClassesAndStyles( {
style,
style: styleObject,
} as WithStyle );

const borderProps = getBorderClassesAndStyles( {
style,
style: styleObject,
} as WithStyle );

const spacingProps = getSpacingClassesAndStyles( {
style,
style: styleObject,
} as WithStyle );

/* TODO
const { borderColor } = attributesObject;
if ( borderColor ) {
const borderColorObject = getMultiOriginColor( {
colors,
namedColor: borderColor,
} );
borderProps.style.borderColor = borderColorObject.color;
}
*/
const typographyProps = useTypographyProps( propsObject );

return {
className: classnames(
Expand Down
24 changes: 14 additions & 10 deletions assets/js/base/hooks/use-typography-props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/**
* External dependencies
*/
Expand All @@ -13,27 +12,32 @@ type WithStyle = {
style: Record< string, unknown >;
};

type blockAttributes = {
style?: Record< string, unknown > | string | undefined;
fontSize?: string | undefined;
fontFamily?: string | undefined;
};

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

const classNameFallback = isString( typography.fontFamily )
? typography.fontFamily
: '';
const className = attributesObject.fontFamily
? `has-${ attributesObject.fontFamily }-font-family`
const className = props.fontFamily
? `has-${ props.fontFamily }-font-family`
: classNameFallback;

return {
className,
style: {
fontSize: attributesObject.fontSize
? `var(--wp--preset--font-size--${ attributesObject.fontSize })`
fontSize: props.fontSize
? `var(--wp--preset--font-size--${ props.fontSize })`
: typography.fontSize,
fontStyle: typography.fontStyle,
fontWeight: typography.fontWeight,
Expand Down

0 comments on commit 8164c12

Please sign in to comment.