-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add: Font Family picking mechanism #24868
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,53 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEmpty } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SelectControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useEditorFeature from '../use-editor-feature'; | ||
|
||
export default function FontFamilyControl( { | ||
value = '', | ||
onChange, | ||
fontFamilies, | ||
...props | ||
} ) { | ||
const blockLevelFontFamilies = useEditorFeature( | ||
'typography.fontFamilies' | ||
); | ||
if ( ! fontFamilies ) { | ||
fontFamilies = blockLevelFontFamilies; | ||
} | ||
|
||
if ( isEmpty( fontFamilies ) ) { | ||
return null; | ||
} | ||
|
||
const options = [ | ||
{ value: '', label: __( 'Default' ) }, | ||
...fontFamilies.map( ( { fontFamily, name } ) => { | ||
return { | ||
value: fontFamily, | ||
label: name || fontFamily, | ||
}; | ||
} ), | ||
]; | ||
return ( | ||
<SelectControl | ||
label={ __( 'Font family' ) } | ||
options={ options } | ||
value={ value } | ||
onChange={ onChange } | ||
labelPosition="top" | ||
{ ...props } | ||
/> | ||
); | ||
} |
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,88 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { cleanEmptyObject } from './utils'; | ||
import useEditorFeature from '../components/use-editor-feature'; | ||
import FontFamilyControl from '../components/font-family'; | ||
|
||
export const FONT_FAMILY_SUPPORT_KEY = '__experimentalFontFamily'; | ||
|
||
const getFontFamilyFromAttributeValue = ( fontFamilies, value ) => { | ||
const attributeParsed = /var:preset\|font-family\|(.+)/.exec( value ); | ||
if ( attributeParsed && attributeParsed[ 1 ] ) { | ||
return find( fontFamilies, ( { slug } ) => { | ||
return slug === attributeParsed[ 1 ]; | ||
} ).fontFamily; | ||
} | ||
return value; | ||
}; | ||
|
||
export function FontFamilyEdit( { | ||
name, | ||
setAttributes, | ||
attributes: { style = {} }, | ||
} ) { | ||
const fontFamilies = useEditorFeature( 'typography.fontFamilies' ); | ||
const isDisable = useIsFontFamilyDisabled( { name } ); | ||
|
||
if ( isDisable ) { | ||
return null; | ||
} | ||
|
||
const value = getFontFamilyFromAttributeValue( | ||
fontFamilies, | ||
style.typography?.fontFamily | ||
); | ||
|
||
function onChange( newValue ) { | ||
const predefinedFontFamily = find( | ||
fontFamilies, | ||
( { fontFamily } ) => fontFamily === newValue | ||
); | ||
setAttributes( { | ||
style: cleanEmptyObject( { | ||
...style, | ||
typography: { | ||
...( style.typography || {} ), | ||
fontFamily: predefinedFontFamily | ||
? `var:preset|font-family|${ predefinedFontFamily.slug }` | ||
: newValue || undefined, | ||
}, | ||
} ), | ||
} ); | ||
} | ||
|
||
return ( | ||
<FontFamilyControl | ||
className="block-editor-hooks-font-family-control" | ||
fontFamilies={ fontFamilies } | ||
value={ value } | ||
onChange={ onChange } | ||
/> | ||
); | ||
} | ||
|
||
/** | ||
* Custom hook that checks if font-family functionality is disabled. | ||
* | ||
* @param {string} name The name of the block. | ||
* @return {boolean} Whether setting is disabled. | ||
*/ | ||
export function useIsFontFamilyDisabled( { name } ) { | ||
const fontFamilies = useEditorFeature( 'typography.fontFamilies' ); | ||
return ( | ||
! fontFamilies || | ||
fontFamilies.length === 0 || | ||
! hasBlockSupport( name, FONT_FAMILY_SUPPORT_KEY ) | ||
); | ||
} |
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 |
---|---|---|
|
@@ -54,6 +54,7 @@ | |
"color": { | ||
"textColor": true, | ||
"backgroundColor": true | ||
} | ||
}, | ||
"__experimentalFontFamily": true | ||
} | ||
} |
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
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
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.
Under which situations a font-family value is not in the form of
var:preset|font-family|etc
?Unlike other style properties, users can't provide custom values, so I don't see how this path is reached. I presume we can remove it. The fact that this has been using
style.COLOR.fontFamily
without a bug being reported seems to confirm this is a code path we don't need. cc @jorgefilipecosta @aristathFor context, this question comes from a PR I'm working on at #31910 to use classes instead of CSS Custom Properties for font-family. I'm looking at removing this code path there.
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.
I guess a situation where font-family may not be preset is in the case of a block pattern where the pattern is excitedly using a specific font family in a block.
Regarding the usage of classes for font-family presets I think we can use variables for now it is not clear classes bring any advantage in that use case. For the colors, we are outputting CSS vars anyway so a block could use the vars directly and we need to support that. For the font family, I guess we can keep with just vars for now and see what feedback we get.
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.
Nice thinking, thanks! I'll keep that use case then.
I'd think we want to do it the other way around: what's the advantage of font-family using custom properties instead of classes, as the other style properties do?