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
Product Title block: add support global style #5515
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9d7c30
Product title: add support global style #4965
gigitux 517f80a
add specific type
gigitux 48709c7
add custom save function
gigitux 6d79824
move hooks in a specific folder
gigitux 4bcdc58
Merge branch 'trunk' of github.com:woocommerce/woocommerce-gutenberg-…
gigitux a17ca36
fix crash on WP 5.8
gigitux 516f570
Merge branch 'trunk' of github.com:woocommerce/woocommerce-gutenberg-…
gigitux 2f7bc2f
fix global style title color (#5548)
dinhtungdu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,103 @@ | ||
/* 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'; | ||
|
||
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 {}; | ||
}; | ||
|
||
const parseSpacingStyle = ( | ||
spacing: Record< string, unknown > | ||
): Record< string, unknown > => { | ||
const keys = [ 'margin' ]; | ||
|
||
const getValueOrDefault = ( value: unknown ) => { | ||
return isString( value ) && value.length > 0 ? value : '0'; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should return |
||
|
||
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 ) }`, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, we should only use shorthand when all four sides are set, otherwise, we should use single side rules. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for this! I updated the function! |
||
} | ||
|
||
return acc; | ||
}, {} ); | ||
}; | ||
|
||
export const useSpacingProps = ( attributes: unknown ): WithStyle => { | ||
const style = isObject( attributes ) ? parseStyle( attributes.style ) : {}; | ||
const spacingStyles = isObject( style.spacing ) ? style.spacing : {}; | ||
|
||
return { | ||
style: parseSpacingStyle( spacingStyles ), | ||
}; | ||
}; | ||
|
||
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 => { | ||
dinhtungdu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( ! isFeaturePluginBuild() ) { | ||
return { | ||
className: '', | ||
style: {}, | ||
}; | ||
} | ||
|
||
const attributesObject = isObject( attributes ) ? attributes : {}; | ||
const style = parseStyle( attributesObject.style ); | ||
|
||
return __experimentalUseColorProps( { ...attributesObject, style } ); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why text color is disabled here? Given that we allow changing the background, it makes more sense to me to allow changing text color too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By #4965, the color for the text should be disabled, but I agree with you.
I added the support for text color, too, now I will add a comment on the main issue.