Skip to content
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

Font family: switch from CSS Custom Property to classes #31910

Merged
merged 82 commits into from Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
b5ff7fd
Client hooks: use class names for font family
ockham May 17, 2021
c62fd9d
Server supports: use class
ockham Jul 2, 2021
64013f1
theme.json: enqueue classes for font-family
oandregal Jul 2, 2021
274bdd8
Remove unnecessary code path
oandregal Jul 2, 2021
13c52a5
Do not use inline styles
oandregal Jul 2, 2021
07dea1d
Add class name in edit
oandregal Jul 2, 2021
ccee841
Support custom font families
oandregal Jul 5, 2021
f3f207d
Update tests
oandregal Sep 23, 2021
818333a
Add deprecation for the button block
oandregal Sep 23, 2021
68d1c75
clean empty object extracted to utils
oandregal Sep 23, 2021
11fd19d
Add deprecation for the list block
oandregal Sep 23, 2021
2b58a20
Add deprecation for the post title block
oandregal Sep 24, 2021
0b745b9
Add deprecation for the post title block
oandregal Sep 24, 2021
84c22f2
fixup site-title
oandregal Sep 24, 2021
082e10c
Add deprecation for the verse block
oandregal Sep 24, 2021
07ec4bc
Add deprecation for the site-tagline block
oandregal Sep 24, 2021
4cc0263
Add deprecation for the navigation block
oandregal Sep 24, 2021
67e6f17
Add deprecation for the query-title block
oandregal Sep 24, 2021
230c70c
Update lib/block-supports/typography.php
oandregal Sep 24, 2021
a6b63f5
Update test after rebase
oandregal Oct 8, 2021
bc70e9f
Update test after rebase
oandregal Oct 8, 2021
d5c8ba0
Extract font family migration to file
oandregal Oct 14, 2021
2124eab
Add some deprecation coverage for the Button block
ockham Oct 14, 2021
bf9d385
Rename oldFontFamilyMigration to migrateFontFamily
ockham Oct 14, 2021
2f48b3e
List: add fixtures and update deprecation list
oandregal Oct 15, 2021
856f14e
Post Title: update deprecation list
oandregal Oct 15, 2021
cd15d94
Post title: add fixtures
oandregal Oct 15, 2021
b8d0b6f
Express list deprecation through current block.json and save
ockham Oct 15, 2021
76439ed
Express PostTitle block deprecation attributes through block.json
ockham Oct 15, 2021
16354c8
Simplify import
ockham Oct 15, 2021
2147a77
Express Site Tagline block deprecation attributes through block.json
ockham Oct 15, 2021
6e7deeb
Version Site Tagline deprecations
ockham Oct 15, 2021
2288e9a
Add Site Tagline fixtures
ockham Oct 15, 2021
f36e8f6
Express Site Title block deprecation attributes through block.json
ockham Oct 15, 2021
e0be5d2
Version Site Title deprecations
ockham Oct 15, 2021
d4aeb54
Add Site Title fixtures
ockham Oct 15, 2021
65bdb3e
Express Query Title block deprecation attributes through block.json
ockham Oct 15, 2021
61b2ea9
Version Query Title deprecations
ockham Oct 15, 2021
0c9093b
Add Query Title fixtures
ockham Oct 15, 2021
8c47da1
Version Verse block deprecations
ockham Oct 15, 2021
7e32d7a
Verse deprecation: Inline v1 block attributes
ockham Oct 15, 2021
cad9e21
Express Verse block v2 deprecation through block.json and save
ockham Oct 15, 2021
1e9ed9d
Rename Verse deprecation fixture
ockham Oct 15, 2021
fb2b405
Add fixtures for Verse block deprecation v2
ockham Oct 15, 2021
7a6a04c
Express Button block latest deprecation through block.json and save
ockham Oct 15, 2021
673462a
Express Navigation block latest deprecation through block.json and save
ockham Oct 15, 2021
fdf8566
Add fixtures for Navigation block font family deprecation
ockham Oct 15, 2021
e16fdaf
Move latest Navigation block deprecation to the top
ockham Oct 15, 2021
cf59565
Move latest Verse block deprecation to the top
ockham Oct 15, 2021
3350bd0
Move latest Button block deprecation to the top
ockham Oct 15, 2021
c67ec86
Un-Express Button block latest deprecation through block.json and save
ockham Oct 18, 2021
57f9cc5
Revert "Express list deprecation through current block.json and save"
ockham Oct 18, 2021
8e0a592
List deprecation: Add __experimentalSlashInserter attribute
ockham Oct 18, 2021
5708f0d
Revert "Express PostTitle block deprecation attributes through block.…
ockham Oct 18, 2021
64d5227
Un-Express Navigation block latest deprecation through block.json and…
ockham Oct 18, 2021
2a5110e
Un-Express Query Title block deprecation attributes through block.json
ockham Oct 18, 2021
c9d592e
Un-Express Site Tagline block deprecation attributes through block.json
ockham Oct 18, 2021
68362f3
Un-Express Site Title block deprecation attributes through block.json
ockham Oct 18, 2021
f74dc65
Un-Express Verse block v2 deprecation through block.json and save
ockham Oct 18, 2021
a58340f
Make sure slugs are kebab-cased properly
oandregal Oct 19, 2021
a33ab7f
Update comment
oandregal Oct 19, 2021
39445aa
Post title: update comment
oandregal Oct 19, 2021
2e81ca2
Query title: update comment
oandregal Oct 19, 2021
cfa9e13
Site tagline: update comment
oandregal Oct 19, 2021
2505ef3
Site title: update comment
oandregal Oct 19, 2021
9ae7032
Verse: update comment
oandregal Oct 19, 2021
0a9b799
Button: extract font-family deprecation to own constant
oandregal Oct 19, 2021
12e75bf
Button: rename fixtures
oandregal Oct 19, 2021
86fecba
Button: add font family migration back to other ones
oandregal Oct 19, 2021
cd45d0a
Navigation: extract deprecation and add missing block supports
oandregal Oct 19, 2021
7c09eed
Navigation: rename test for better consistency
oandregal Oct 19, 2021
e0d5ce7
Navigation: add font family migration to older ones
oandregal Oct 19, 2021
fc6abd4
Make deprecation safe to discard
oandregal Oct 20, 2021
5f25219
Post Comment Author
oandregal Oct 20, 2021
62f6302
Post Comment Date
oandregal Oct 20, 2021
240504a
Post Date
oandregal Oct 20, 2021
19dbeee
Add tests and fix legacy code path
oandregal Oct 20, 2021
0ccce1f
Button: revert unnecessary font family migration
oandregal Oct 20, 2021
3ab2ae2
Button: revert unnecessary font family migration
oandregal Oct 20, 2021
f6e54f2
Button: revert unnecessary font family migration
oandregal Oct 20, 2021
c65cf6e
Post Title block deprecation: Add spacing supports (sync from block.j…
ockham Oct 20, 2021
99b4a9f
Query Title block deprecation: Add spacing supports (sync from block.…
ockham Oct 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/block-supports/typography.php
Expand Up @@ -105,17 +105,21 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
}

if ( $has_font_family_support ) {
$has_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );
if ( $has_font_family ) {
$font_family = $block_attributes['style']['typography']['fontFamily'];
if ( strpos( $font_family, 'var:preset|font-family' ) !== false ) {
// Get the name from the string and add proper styles.
$index_to_splice = strrpos( $font_family, '|' ) + 1;
$font_family_name = substr( $font_family, $index_to_splice );
$styles[] = sprintf( 'font-family: var(--wp--preset--font-family--%s);', $font_family_name );
} else {
$styles[] = sprintf( 'font-family: %s;', $block_attributes['style']['typography']['fontFamily'] );
$has_named_font_family = array_key_exists( 'fontFamily', $block_attributes );
$has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );

if ( $has_named_font_family ) {
$classes[] = sprintf( 'has-%s-font-family', gutenberg_experimental_to_kebab_case( $block_attributes['fontFamily'] ) );
} elseif ( $has_custom_font_family ) {
// Before using classes, the value was serialized as a CSS Custom Property.
// We don't need this code path when it lands in core.
$font_family_custom = $block_attributes['style']['typography']['fontFamily'];
if ( strpos( $font_family_custom, 'var:preset|font-family' ) !== false ) {
$index_to_splice = strrpos( $font_family_custom, '|' ) + 1;
$font_family_slug = gutenberg_experimental_to_kebab_case( substr( $font_family_custom, $index_to_splice ) );
$font_family_custom = sprintf( 'var(--wp--preset--font-family--%s)', $font_family_slug );
}
$styles[] = sprintf( 'font-family: %s;', $font_family_custom );
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/class-wp-theme-json-gutenberg.php
Expand Up @@ -205,7 +205,7 @@ class WP_Theme_JSON_Gutenberg {
'path' => array( 'typography', 'fontFamilies' ),
'value_key' => 'fontFamily',
'css_vars' => '--wp--preset--font-family--$slug',
'classes' => array(),
'classes' => array( '.has-$slug-font-family' => 'font-family' ),
'properties' => array( 'font-family' ),
),
);
Expand Down
134 changes: 107 additions & 27 deletions packages/block-editor/src/hooks/font-family.js
@@ -1,39 +1,111 @@
/**
* External dependencies
*/
import { find } from 'lodash';
import { find, kebabCase } from 'lodash';

/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
import TokenList from '@wordpress/token-list';

/**
* Internal dependencies
*/
import { cleanEmptyObject } from './utils';
import useSetting from '../components/use-setting';
import FontFamilyControl from '../components/font-family';

export const FONT_FAMILY_SUPPORT_KEY = 'typography.__experimentalFontFamily';

const getFontFamilyFromAttributeValue = ( fontFamilies, value ) => {
const attributeParsed = /var:preset\|font-family\|(.+)/.exec( value );
if ( attributeParsed && attributeParsed[ 1 ] ) {
const fontFamilyObject = find( fontFamilies, ( { slug } ) => {
return slug === attributeParsed[ 1 ];
/**
* Filters registered block settings, extending attributes to include
* the `fontFamily` attribute.
*
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
*/
function addAttributes( settings ) {
if ( ! hasBlockSupport( settings, FONT_FAMILY_SUPPORT_KEY ) ) {
return settings;
}

// Allow blocks to specify a default value if needed.
if ( ! settings.attributes.fontFamily ) {
Object.assign( settings.attributes, {
fontFamily: {
type: 'string',
},
} );
if ( fontFamilyObject ) {
return fontFamilyObject.fontFamily;
}
}
return value;
};

return settings;
}

/**
* Override props assigned to save component to inject font family.
*
* @param {Object} props Additional props applied to save element
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {Object} Filtered props applied to save element
*/
function addSaveProps( props, blockType, attributes ) {
if ( ! hasBlockSupport( blockType, FONT_FAMILY_SUPPORT_KEY ) ) {
return props;
}

if (
hasBlockSupport(
blockType,
'__experimentalSkipTypographySerialization'
)
) {
return props;
}

if ( ! attributes?.fontFamily ) {
return props;
}

// Use TokenList to dedupe classes.
const classes = new TokenList( props.className );
classes.add( `has-${ kebabCase( attributes?.fontFamily ) }-font-family` );
const newClassName = classes.value;
props.className = newClassName ? newClassName : undefined;

return props;
}

/**
* Filters registered block settings to expand the block edit wrapper
* by applying the desired styles and classnames.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
function addEditProps( settings ) {
if ( ! hasBlockSupport( settings, FONT_FAMILY_SUPPORT_KEY ) ) {
return settings;
}

const existingGetEditWrapperProps = settings.getEditWrapperProps;
settings.getEditWrapperProps = ( attributes ) => {
let props = {};
if ( existingGetEditWrapperProps ) {
props = existingGetEditWrapperProps( attributes );
}
return addSaveProps( props, settings, attributes );
};

return settings;
}

export function FontFamilyEdit( {
name,
setAttributes,
attributes: { style = {} },
attributes: { fontFamily },
} ) {
const fontFamilies = useSetting( 'typography.fontFamilies' );
const isDisable = useIsFontFamilyDisabled( { name } );
Expand All @@ -42,26 +114,16 @@ export function FontFamilyEdit( {
return null;
}

const value = getFontFamilyFromAttributeValue(
fontFamilies,
style.typography?.fontFamily
);
const value = find( fontFamilies, ( { slug } ) => fontFamily === slug )
?.fontFamily;

function onChange( newValue ) {
const predefinedFontFamily = find(
fontFamilies,
( { fontFamily } ) => fontFamily === newValue
( { fontFamily: f } ) => f === newValue
);
setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...( style.typography || {} ),
fontFamily: predefinedFontFamily
? `var:preset|font-family|${ predefinedFontFamily.slug }`
: newValue || undefined,
},
} ),
fontFamily: predefinedFontFamily?.slug,
} );
}

Expand Down Expand Up @@ -89,3 +151,21 @@ export function useIsFontFamilyDisabled( { name } ) {
! hasBlockSupport( name, FONT_FAMILY_SUPPORT_KEY )
);
}

addFilter(
'blocks.registerBlockType',
'core/fontFamily/addAttribute',
addAttributes
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/fontFamily/addSaveProps',
addSaveProps
);

addFilter(
'blocks.registerBlockType',
'core/fontFamily/addEditProps',
addEditProps
);