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 block: add support global style (#5515)
* Product title: add support global style #4965 * add specific type * add custom save function * move hooks in a specific folder * fix crash on WP 5.8 * fix global style title color (#5548) Co-authored-by: Tung Du <[email protected]>
- Loading branch information
1 parent
92a18e8
commit 345bfcd
Showing
8 changed files
with
177 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,3 @@ | ||
.editor-styles-wrapper a.wc-block-components-product-name { | ||
color: inherit; | ||
} |
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,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import classnames from 'classnames'; | ||
|
||
type Props = { | ||
attributes: Record< string, unknown > & { | ||
className?: string; | ||
}; | ||
}; | ||
|
||
export const Save = ( { attributes }: Props ): JSX.Element => { | ||
return ( | ||
<div | ||
{ ...useBlockProps.save( { | ||
className: classnames( 'is-loading', attributes.className ), | ||
} ) } | ||
/> | ||
); | ||
}; |
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,82 @@ | ||
/* eslint-disable @wordpress/no-unsafe-wp-apis */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
__experimentalUseColorProps, | ||
__experimentalGetSpacingClassesAndStyles, | ||
} from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isFeaturePluginBuild } from '../settings/blocks/feature-flags'; | ||
import { isString, isObject } from '../types/type-guards'; | ||
import { hasSpacingStyleSupport } from '../utils/global-style'; | ||
|
||
type WithClass = { | ||
className: string; | ||
}; | ||
|
||
type WithStyle = { | ||
style: Record< string, unknown >; | ||
}; | ||
|
||
const parseStyle = ( style: unknown ): Record< string, unknown > => { | ||
if ( isString( style ) ) { | ||
return JSON.parse( style ) || {}; | ||
} | ||
|
||
if ( isObject( style ) ) { | ||
return style; | ||
} | ||
|
||
return {}; | ||
}; | ||
|
||
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, | ||
} ); | ||
}; | ||
|
||
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 || typography.fontSize, | ||
lineHeight: typography.lineHeight, | ||
fontWeight: typography.fontWeight, | ||
textTransform: typography.textTransform, | ||
fontFamily: attributesObject.fontFamily, | ||
}, | ||
}; | ||
}; | ||
|
||
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 } ); | ||
}; |
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,8 @@ | ||
/* eslint-disable @wordpress/no-unsafe-wp-apis */ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor'; | ||
|
||
export const hasSpacingStyleSupport = () => | ||
typeof __experimentalGetSpacingClassesAndStyles === 'function'; |