This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Product title: add support global style #4965
- Loading branch information
Showing
3 changed files
with
154 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* eslint-disable @wordpress/no-unsafe-wp-apis */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __experimentalUseColorProps } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isFeaturePluginBuild } from '../settings/blocks/feature-flags'; | ||
import { isString, isObject } from '../types/type-guards'; | ||
|
||
const parseStyle = ( style: unknown ): Record< string, unknown > => { | ||
if ( isString( style ) ) { | ||
return JSON.parse( style ) || {}; | ||
} | ||
|
||
if ( isObject( style ) ) { | ||
return style; | ||
} | ||
|
||
return {}; | ||
}; | ||
|
||
const parseSpacingStyle = ( | ||
spacing: Record< string, unknown > | ||
): Record< string, unknown > => { | ||
const keys = [ 'margin' ]; | ||
|
||
const getValueOrDefault = ( value: unknown ) => { | ||
return isString( value ) && value.length > 0 ? value : '0'; | ||
}; | ||
|
||
return Object.keys( spacing ).reduce( ( acc, key ) => { | ||
const spacingProperty = isObject( spacing[ key ] ) | ||
? ( spacing[ key ] as Record< string, unknown > ) | ||
: {}; | ||
|
||
if ( keys.includes( key ) ) { | ||
return { | ||
...acc, | ||
[ key ]: `${ getValueOrDefault( | ||
spacingProperty.top | ||
) } ${ getValueOrDefault( | ||
spacingProperty.right | ||
) } ${ getValueOrDefault( | ||
spacingProperty.bottom | ||
) } ${ getValueOrDefault( spacingProperty.left ) }`, | ||
}; | ||
} | ||
|
||
return acc; | ||
}, {} ); | ||
}; | ||
|
||
export const useSpacingProps = ( | ||
attributes: unknown | ||
): { | ||
style: Record< string, unknown >; | ||
} => { | ||
const style = isObject( attributes ) ? parseStyle( attributes.style ) : {}; | ||
const spacingStyles = isObject( style.spacing ) ? style.spacing : {}; | ||
|
||
return { | ||
style: parseSpacingStyle( spacingStyles ), | ||
}; | ||
}; | ||
|
||
export const useTypographyProps = ( | ||
attributes: unknown | ||
): { | ||
style: Record< string, unknown >; | ||
} => { | ||
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 || typography.fontSize, | ||
lineHeight: typography.lineHeight, | ||
fontWeight: typography.fontWeight, | ||
textTransform: typography.textTransform, | ||
fontFamily: attributesObject.fontFamily, | ||
}, | ||
}; | ||
}; | ||
|
||
export const useColorProps = ( | ||
attributes: unknown | ||
): { | ||
className: string; | ||
style: Record< string, unknown >; | ||
} => { | ||
if ( ! isFeaturePluginBuild() ) { | ||
return { | ||
className: '', | ||
style: {}, | ||
}; | ||
} | ||
|
||
return __experimentalUseColorProps( attributes ); | ||
}; |