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 Typography: text orientation (writing mode) #50822

Merged
merged 9 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ Settings related to typography.
| lineHeight | boolean | false | |
| textColumns | boolean | false | |
| textDecoration | boolean | true | |
| writingMode | boolean | false | |
| textTransform | boolean | true | |
| dropCap | boolean | true | |
| fontSizes | array | | fluid, name, size, slug |
Expand Down Expand Up @@ -239,6 +240,7 @@ Typography styles.
| lineHeight | string, object | |
| textColumns | string | |
| textDecoration | string, object | |
| writingMode | string, object | |
| textTransform | string, object | |

---
Expand Down
10 changes: 9 additions & 1 deletion lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function gutenberg_register_typography_support( $block_type ) {
$has_text_columns_support = _wp_array_get( $typography_supports, array( 'textColumns' ), false );
$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
$has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
$has_writing_mode_support = _wp_array_get( $typography_supports, array( '__experimentalWritingMode' ), false );

$has_typography_support = $has_font_family_support
|| $has_font_size_support
Expand All @@ -38,7 +39,8 @@ function gutenberg_register_typography_support( $block_type ) {
|| $has_line_height_support
|| $has_text_columns_support
|| $has_text_decoration_support
|| $has_text_transform_support;
|| $has_text_transform_support
|| $has_writing_mode_support;

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
Expand Down Expand Up @@ -96,6 +98,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_text_columns_support = _wp_array_get( $typography_supports, array( 'textColumns' ), false );
$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
$has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
$has_writing_mode_support = _wp_array_get( $typography_supports, array( '__experimentalWritingMode' ), false );

// Whether to skip individual block support features.
$should_skip_font_size = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' );
Expand All @@ -107,6 +110,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$should_skip_text_decoration = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' );
$should_skip_text_transform = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' );
$should_skip_letter_spacing = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' );
$should_skip_writing_mode = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'writingMode' );

$typography_block_styles = array();
if ( $has_font_size_support && ! $should_skip_font_size ) {
Expand Down Expand Up @@ -158,6 +162,10 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['letterSpacing'], 'letter-spacing' );
}

if ( $has_writing_mode_support && ! $should_skip_writing_mode && isset( $block_attributes['style']['typography']['writingMode'] ) ) {
$typography_block_styles['writingMode'] = _wp_array_get( $block_attributes, array( 'style', 'typography', 'writingMode' ), null );
}

$attributes = array();
$styles = gutenberg_style_engine_get_styles(
array( 'typography' => $typography_block_styles ),
Expand Down
5 changes: 5 additions & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class WP_Theme_JSON_Gutenberg {
* `--wp--style--root--padding-*`, and `box-shadow` properties,
* removed the `--wp--style--block-gap` property.
* @since 6.2.0 Added `outline-*`, and `min-height` properties.
* @since 6.3.0 Added `writing-mode` property.
*
* @var array
*/
Expand Down Expand Up @@ -260,6 +261,7 @@ class WP_Theme_JSON_Gutenberg {
'text-transform' => array( 'typography', 'textTransform' ),
'filter' => array( 'filter', 'duotone' ),
'box-shadow' => array( 'shadow' ),
'writing-mode' => array( 'typography', 'writingMode' ),
);

/**
Expand Down Expand Up @@ -337,6 +339,7 @@ class WP_Theme_JSON_Gutenberg {
* @since 6.1.0 Added `layout.definitions` and `useRootPaddingAwareAlignments`.
* @since 6.2.0 Added `dimensions.minHeight`, 'shadow.presets', 'shadow.defaultPresets',
* `position.fixed` and `position.sticky`.
* @since 6.3.0 Added `typography.writingMode`.
* @var array
*/
const VALID_SETTINGS = array(
Expand Down Expand Up @@ -404,6 +407,7 @@ class WP_Theme_JSON_Gutenberg {
'textColumns' => null,
'textDecoration' => null,
'textTransform' => null,
'writingMode' => null,
),
'behaviors' => null,
);
Expand Down Expand Up @@ -466,6 +470,7 @@ class WP_Theme_JSON_Gutenberg {
'textColumns' => null,
'textDecoration' => null,
'textTransform' => null,
'writingMode' => null,
),
'css' => null,
);
Expand Down
3 changes: 2 additions & 1 deletion lib/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@
"lineHeight": false,
"textColumns": false,
"textDecoration": true,
"textTransform": true
"textTransform": true,
"writingMode": false
},
"blocks": {
"core/button": {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const VALID_SETTINGS = [
'typography.textColumns',
'typography.textDecoration',
'typography.textTransform',
'typography.writingMode',
];

export const useGlobalStylesReset = () => {
Expand Down Expand Up @@ -292,6 +293,7 @@ export function useSettingsForBlockElement(
'letterSpacing',
'textTransform',
'textDecoration',
'writingMode',
].forEach( ( key ) => {
if ( ! supportedStyles.includes( key ) ) {
updatedSettings.typography = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import LineHeightControl from '../line-height-control';
import LetterSpacingControl from '../letter-spacing-control';
import TextTransformControl from '../text-transform-control';
import TextDecorationControl from '../text-decoration-control';
import WritingModeControl from '../writing-mode-control';
import { getValueFromVariable } from './utils';
import { setImmutably } from '../../utils/object';

Expand All @@ -32,6 +33,7 @@ export function useHasTypographyPanel( settings ) {
const hasLetterSpacing = useHasLetterSpacingControl( settings );
const hasTextTransform = useHasTextTransformControl( settings );
const hasTextDecoration = useHasTextDecorationControl( settings );
const hasWritingMode = useHasWritingModeControl( settings );
const hasTextColumns = useHasTextColumnsControl( settings );
const hasFontSize = useHasFontSizeControl( settings );

Expand All @@ -43,6 +45,7 @@ export function useHasTypographyPanel( settings ) {
hasTextTransform ||
hasFontSize ||
hasTextDecoration ||
hasWritingMode ||
hasTextColumns
);
}
Expand Down Expand Up @@ -100,6 +103,10 @@ function useHasTextDecorationControl( settings ) {
return settings?.typography?.textDecoration;
}

function useHasWritingModeControl( settings ) {
return settings?.typography?.writingMode;
}

function useHasTextColumnsControl( settings ) {
return settings?.typography?.textColumns;
}
Expand Down Expand Up @@ -135,6 +142,7 @@ const DEFAULT_CONTROLS = {
letterSpacing: true,
textTransform: true,
textDecoration: true,
writingMode: true,
textColumns: true,
};

Expand Down Expand Up @@ -307,6 +315,21 @@ export default function TypographyPanel( {
const hasTextDecoration = () => !! value?.typography?.textDecoration;
const resetTextDecoration = () => setTextDecoration( undefined );

// Text Orientation
const hasWritingModeControl = useHasWritingModeControl( settings );
const writingMode = decodeValue( inheritedValue?.typography?.writingMode );
const setWritingMode = ( newValue ) => {
onChange(
setImmutably(
value,
[ 'typography', 'writingMode' ],
newValue || undefined
)
);
};
const hasWritingMode = () => !! value?.typography?.writingMode;
const resetWritingMode = () => setWritingMode( undefined );

const resetAllFilter = useCallback( ( previousValue ) => {
return {
...previousValue,
Expand Down Expand Up @@ -453,6 +476,23 @@ export default function TypographyPanel( {
/>
</ToolsPanelItem>
) }
{ hasWritingModeControl && (
<ToolsPanelItem
className="single-column"
label={ __( 'Text orientation' ) }
hasValue={ hasWritingMode }
onDeselect={ resetWritingMode }
isShownByDefault={ defaultControls.writingMode }
panelId={ panelId }
>
<WritingModeControl
value={ writingMode }
onChange={ setWritingMode }
size="__unstable-large"
__nextHasNoMarginBottom
/>
</ToolsPanelItem>
) }
{ hasTextTransformControl && (
<ToolsPanelItem
label={ __( 'Letter case' ) }
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 @@ -49,6 +49,7 @@ export { default as __experimentalFontFamilyControl } from './font-family';
export { default as __experimentalLetterSpacingControl } from './letter-spacing-control';
export { default as __experimentalTextDecorationControl } from './text-decoration-control';
export { default as __experimentalTextTransformControl } from './text-transform-control';
export { default as __experimentalWritingModeControl } from './writing-mode-control';
export { default as __experimentalColorGradientControl } from './colors-gradients/control';
export { default as __experimentalColorGradientSettingsDropdown } from './colors-gradients/dropdown';
export { default as __experimentalPanelColorGradientSettings } from './colors-gradients/panel-color-gradient-settings';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { BaseControl, Button } from '@wordpress/components';
import { __, isRTL } from '@wordpress/i18n';
import { textHorizontal, textVertical } from '@wordpress/icons';

const WRITING_MODES = [
{
name: __( 'Horizontal' ),
value: 'horizontal-tb',
icon: textHorizontal,
},
{
name: __( 'Vertical' ),
value: isRTL() ? 'vertical-lr' : 'vertical-rl',
icon: textVertical,
},
];

/**
* Control to facilitate writing mode selections.
*
* @param {Object} props Component props.
* @param {string} props.className Class name to add to the control.
* @param {string} props.value Currently selected writing mode.
* @param {Function} props.onChange Handles change in the writing mode selection.
*
* @return {WPElement} Writing Mode control.
*/
export default function WritingModeControl( { className, value, onChange } ) {
return (
<fieldset
className={ classnames(
'block-editor-writing-mode-control',
className
) }
>
<BaseControl.VisualLabel as="legend">
{ __( 'Orientation' ) }
</BaseControl.VisualLabel>
<div className="block-editor-writing-mode-control__buttons">
{ WRITING_MODES.map( ( writingMode ) => {
return (
<Button
key={ writingMode.value }
icon={ writingMode.icon }
label={ writingMode.name }
isPressed={ writingMode.value === value }
onClick={ () => {
onChange(
writingMode.value === value
? undefined
: writingMode.value
);
} }
/>
);
} ) }
</div>
</fieldset>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.block-editor-writing-mode-control {
border: 0;
margin: 0;
padding: 0;

.block-editor-writing-mode-control__buttons {
// 4px of padding makes the row 40px high, same as an input.
padding: $grid-unit-05 0;
display: flex;
}

.components-button.has-icon {
height: $grid-unit-40;
margin-right: $grid-unit-05;
min-width: $grid-unit-40;
padding: 0;
}
}
7 changes: 7 additions & 0 deletions packages/block-editor/src/hooks/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ const TEXT_COLUMNS_SUPPORT_KEY = 'typography.textColumns';
* decorations e.g. settings found in `block.json`.
*/
const TEXT_DECORATION_SUPPORT_KEY = 'typography.__experimentalTextDecoration';
/**
* Key within block settings' supports array indicating support for writing mode
* e.g. settings found in `block.json`.
*/
const WRITING_MODE_SUPPORT_KEY = 'typography.__experimentalWritingMode';
/**
* Key within block settings' supports array indicating support for text
* transforms e.g. settings found in `block.json`.
*/
const TEXT_TRANSFORM_SUPPORT_KEY = 'typography.__experimentalTextTransform';

/**
* Key within block settings' supports array indicating support for letter-spacing
* e.g. settings found in `block.json`.
Expand All @@ -50,6 +56,7 @@ const TYPOGRAPHY_SUPPORT_KEYS = [
TEXT_COLUMNS_SUPPORT_KEY,
TEXT_DECORATION_SUPPORT_KEY,
TEXT_TRANSFORM_SUPPORT_KEY,
WRITING_MODE_SUPPORT_KEY,
LETTER_SPACING_SUPPORT_KEY,
];
const SPACING_SUPPORT_KEY = 'spacing';
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const TEXT_DECORATION_SUPPORT_KEY = 'typography.__experimentalTextDecoration';
const TEXT_COLUMNS_SUPPORT_KEY = 'typography.textColumns';
const FONT_STYLE_SUPPORT_KEY = 'typography.__experimentalFontStyle';
const FONT_WEIGHT_SUPPORT_KEY = 'typography.__experimentalFontWeight';
const WRITING_MODE_SUPPORT_KEY = 'typography.__experimentalWritingMode';
export const TYPOGRAPHY_SUPPORT_KEY = 'typography';
export const TYPOGRAPHY_SUPPORT_KEYS = [
LINE_HEIGHT_SUPPORT_KEY,
Expand All @@ -39,6 +40,7 @@ export const TYPOGRAPHY_SUPPORT_KEYS = [
FONT_FAMILY_SUPPORT_KEY,
TEXT_COLUMNS_SUPPORT_KEY,
TEXT_DECORATION_SUPPORT_KEY,
WRITING_MODE_SUPPORT_KEY,
TEXT_TRANSFORM_SUPPORT_KEY,
LETTER_SPACING_SUPPORT_KEY,
];
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function useBlockSettings( name, parentLayout ) {
const lineHeight = useSetting( 'typography.lineHeight' );
const textColumns = useSetting( 'typography.textColumns' );
const textDecoration = useSetting( 'typography.textDecoration' );
const writingMode = useSetting( 'typography.writingMode' );
const textTransform = useSetting( 'typography.textTransform' );
const letterSpacing = useSetting( 'typography.letterSpacing' );
const padding = useSetting( 'spacing.padding' );
Expand Down Expand Up @@ -211,6 +212,7 @@ export function useBlockSettings( name, parentLayout ) {
textDecoration,
textTransform,
letterSpacing,
writingMode,
},
spacing: {
spacingSizes: {
Expand Down Expand Up @@ -244,6 +246,7 @@ export function useBlockSettings( name, parentLayout ) {
textDecoration,
textTransform,
letterSpacing,
writingMode,
padding,
margin,
blockGap,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/paragraph/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"__experimentalFontWeight": true,
"__experimentalLetterSpacing": true,
"__experimentalTextTransform": true,
"__experimentalWritingMode": true,
"__experimentalDefaultControls": {
"fontSize": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalWritingMode": true,
"__experimentalDefaultControls": {
"fontSize": true
}
Expand Down
Loading