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

Add Letter-spacing and enable this for site title and site tagline #31118

Merged
merged 22 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ function gutenberg_register_typography_support( $block_type ) {
$has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
$has_text_decoration_support = _wp_array_get( $block_type->supports, array( '__experimentalTextDecoration' ), false );
$has_text_transform_support = _wp_array_get( $block_type->supports, array( '__experimentalTextTransform' ), false );
$has_letter_spacing_support = _wp_array_get( $block_type->supports, array( '__experimentalLetterSpacing' ), false );

$has_typography_support = $has_font_size_support
|| $has_font_weight_support
|| $has_font_style_support
|| $has_line_height_support
|| $has_text_transform_support
|| $has_text_decoration_support;
|| $has_text_decoration_support
|| $has_letter_spacing_support;

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
Expand Down Expand Up @@ -71,6 +73,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
$has_text_decoration_support = _wp_array_get( $block_type->supports, array( '__experimentalTextDecoration' ), false );
$has_text_transform_support = _wp_array_get( $block_type->supports, array( '__experimentalTextTransform' ), false );
$has_letter_spacing_support = _wp_array_get( $block_type->supports, array( '__experimentalLetterSpacing' ), false );

$skip_font_size_support_serialization = _wp_array_get( $block_type->supports, array( '__experimentalSkipFontSizeSerialization' ), false );

Expand Down Expand Up @@ -147,6 +150,14 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
}
}

// Letter-spacing.
if ( $has_letter_spacing_support ) {
$letter_spacing_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' );
if ( $letter_spacing_style ) {
$styles[] = $letter_spacing_style;
}
}

$attributes = array();
if ( ! empty( $classes ) ) {
$attributes['class'] = implode( ' ', $classes );
Expand Down
5 changes: 5 additions & 0 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class WP_Theme_JSON {
'fontSize' => null,
'fontStyle' => null,
'fontWeight' => null,
'letterSpacing' => null,
'lineHeight' => null,
'textDecoration' => null,
'textTransform' => null,
Expand Down Expand Up @@ -98,6 +99,7 @@ class WP_Theme_JSON {
'customFontSize' => null,
'customFontStyle' => null,
'customFontWeight' => null,
'customLetterSpacing' => null,
'customLineHeight' => null,
'customTextDecorations' => null,
'customTextTransforms' => null,
Expand Down Expand Up @@ -234,6 +236,9 @@ class WP_Theme_JSON {
'font-weight' => array(
'value' => array( 'typography', 'fontWeight' ),
),
'letter-spacing' => array(
'value' => array( 'typography', 'letterSpacing' ),
),
'line-height' => array(
'value' => array( 'typography', 'lineHeight' ),
),
Expand Down
1 change: 1 addition & 0 deletions lib/experimental-default-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
"customFontWeight": true,
"customTextTransforms": true,
"customTextDecorations": true,
"customLetterSpacing": true,
"fontSizes": [
{
"name": "Small",
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export { default as __experimentalGradientPickerControl } from './gradient-picke
export { default as __experimentalGradientPickerPanel } from './gradient-picker/panel';
export { default as __experimentalFontAppearanceControl } from './font-appearance-control';
export { default as __experimentalFontFamilyControl } from './font-family';
export { default as __experimentalLetterSpacingControl } from './letter-spacing-control';
export { default as __experimentalColorGradientControl } from './colors-gradients/control';
export { default as __experimentalPanelColorGradientSettings } from './colors-gradients/panel-color-gradient-settings';
export { default as __experimentalImageSizeControl } from './image-size-control';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* WordPress dependencies
*/
import {
BaseControl,
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
import { Platform } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

const isWeb = Platform.OS === 'web';

const CSS_UNITS = [
{
value: 'px',
label: isWeb ? 'px' : __( 'Pixels (px)' ),
default: '2',
},
{
value: 'em',
label: isWeb ? 'em' : __( 'Relative to parent font size (em)' ),
default: '.2',
},
{
value: 'rem',
label: isWeb ? 'rem' : __( 'Relative to root font size (rem)' ),
default: '.2',
},
];

/**
* Control for letter-spacing.
*
* @param {Object} props Component props.
* @param {string} props.value Currently selected letter-spacing.
* @param {Function} props.onChange Handles change in letter-spacing selection.
* @return {WPElement} Letter-spacing control.
*/
export default function LetterSpacingControl( { value, onChange } ) {
return (
<BaseControl className="block-editor-letter-spacing-control">
<BaseControl.VisualLabel>
{ __( 'Letter-spacing' ) }
</BaseControl.VisualLabel>
<UnitControl
aria-label={ __( 'Letter-spacing' ) }
value={ value }
__unstableInputWidth="60px"
units={ CSS_UNITS }
onChange={ onChange }
/>
</BaseControl>
carolinan marked this conversation as resolved.
Show resolved Hide resolved
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.block-editor-letter-spacing-control .components-base-control__label {
display: block;
margin-bottom: 8px;
}
73 changes: 73 additions & 0 deletions packages/block-editor/src/hooks/letter-spacing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* WordPress dependencies
*/
import { hasBlockSupport } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import LetterSpacingControl from '../components/letter-spacing-control';
import useEditorFeature from '../components/use-editor-feature';
import { cleanEmptyObject } from './utils';

/**
* Key within block settings' supports array indicating support for letter-spacing
* e.g. settings found in `block.json`.
*/
export const LETTER_SPACING_SUPPORT_KEY = '__experimentalLetterSpacing';

/**
* Inspector control panel containing the letter-spacing options.
*
* @param {Object} props Block properties.
* @return {WPElement} Letter-spacing edit element.
*/
export function LetterSpacingEdit( props ) {
const {
attributes: { style },
setAttributes,
} = props;

const isDisabled = useIsLetterSpacingDisabled( props );

if ( isDisabled ) {
return null;
}

function onChange( newSpacing ) {
setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
letterSpacing: newSpacing,
},
} ),
} );
}

return (
<LetterSpacingControl
value={ style?.typography?.letterSpacing }
onChange={ onChange }
/>
);
}

/**
* Checks if letter-spacing settings have been disabled.
*
* @param {string} name Name of the block.
* @return {boolean} Whether or not the setting is disabled.
*/
export function useIsLetterSpacingDisabled( { name: blockName } = {} ) {
const notSupported = ! hasBlockSupport(
blockName,
LETTER_SPACING_SUPPORT_KEY
);
const hasLetterSpacing = useEditorFeature(
'typography.customLetterSpacing'
);

return notSupported || ! hasLetterSpacing;
}
8 changes: 8 additions & 0 deletions packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ import {
TEXT_TRANSFORM_SUPPORT_KEY,
useIsTextTransformDisabled,
} from './text-transform';
import {
LETTER_SPACING_SUPPORT_KEY,
LetterSpacingEdit,
useIsLetterSpacingDisabled,
} from './letter-spacing';

export const TYPOGRAPHY_SUPPORT_KEYS = [
LINE_HEIGHT_SUPPORT_KEY,
Expand All @@ -56,6 +61,7 @@ export const TYPOGRAPHY_SUPPORT_KEYS = [
FONT_FAMILY_SUPPORT_KEY,
TEXT_DECORATION_SUPPORT_KEY,
TEXT_TRANSFORM_SUPPORT_KEY,
LETTER_SPACING_SUPPORT_KEY,
];

export function TypographyPanel( props ) {
Expand All @@ -75,6 +81,7 @@ export function TypographyPanel( props ) {
<FontAppearanceEdit { ...props } />
<LineHeightEdit { ...props } />
<TextDecorationAndTransformEdit { ...props } />
<LetterSpacingEdit { ...props } />
</ComponentSystemProvider>
</PanelBody>
</InspectorControls>
Expand All @@ -98,6 +105,7 @@ function useIsTypographyDisabled( props = {} ) {
useIsFontFamilyDisabled( props ),
useIsTextDecorationDisabled( props ),
useIsTextTransformDisabled( props ),
useIsLetterSpacingDisabled( props ),
];

return configs.filter( Boolean ).length === configs.length;
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@import "./components/duotone-control/style.scss";
@import "./components/font-appearance-control/style.scss";
@import "./components/justify-content-control/style.scss";
@import "./components/letter-spacing-control/style.scss";
@import "./components/link-control/style.scss";
@import "./components/line-height-control/style.scss";
@import "./components/image-size-control/style.scss";
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/site-tagline/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalTextTransform": true
"__experimentalTextTransform": true,
"__experimentalLetterSpacing": true
}
}
3 changes: 2 additions & 1 deletion packages/block-library/src/site-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalTextTransform": true
"__experimentalTextTransform": true,
"__experimentalLetterSpacing": true
}
}
4 changes: 4 additions & 0 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
value: [ 'typography', 'textTransform' ],
support: [ '__experimentalTextTransform' ],
},
letterSpacing: {
value: [ 'typography', 'letterSpacing' ],
support: [ '__experimentalLetterSpacing' ],
},
};
26 changes: 25 additions & 1 deletion packages/edit-site/src/components/sidebar/typography-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LineHeightControl,
__experimentalFontFamilyControl as FontFamilyControl,
__experimentalFontAppearanceControl as FontAppearanceControl,
__experimentalLetterSpacingControl as LetterSpacingControl,
} from '@wordpress/block-editor';
import { PanelBody, FontSizePicker } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
Expand All @@ -17,8 +18,12 @@ import { useEditorFeature } from '../editor/utils';
export function useHasTypographyPanel( { supports, name } ) {
const hasLineHeight = useHasLineHeightControl( { supports, name } );
const hasFontAppearance = useHasAppearanceControl( { supports, name } );
const hasLetterSpacing = useHasLetterSpacingControl( { supports, name } );
return (
hasLineHeight || hasFontAppearance || supports.includes( 'fontSize' )
hasLineHeight ||
hasFontAppearance ||
hasLetterSpacing ||
supports.includes( 'fontSize' )
);
}

Expand All @@ -39,6 +44,13 @@ function useHasAppearanceControl( { supports, name } ) {
return hasFontStyles || hasFontWeights;
}

function useHasLetterSpacingControl( { supports, name } ) {
return (
useEditorFeature( 'typography.customLetterSpacing', name ) &&
supports.includes( 'letterSpacing' )
);
}

export default function TypographyPanel( {
context: { supports, name },
getStyle,
Expand All @@ -58,6 +70,10 @@ export default function TypographyPanel( {
supports.includes( 'fontWeight' );
const hasLineHeightEnabled = useHasLineHeightControl( { supports, name } );
const hasAppearanceControl = useHasAppearanceControl( { supports, name } );
const hasLetterSpacingControl = useHasLetterSpacingControl( {
supports,
name,
} );

return (
<PanelBody title={ __( 'Typography' ) } initialOpen={ true }>
Expand Down Expand Up @@ -102,6 +118,14 @@ export default function TypographyPanel( {
hasFontWeights={ hasFontWeights }
/>
) }
{ hasLetterSpacingControl && (
<LetterSpacingControl
value={ getStyle( name, 'letterSpacing' ) }
onChange={ ( value ) =>
setStyle( name, 'letterSpacing', value )
}
/>
) }
</PanelBody>
);
}