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

Try: Add new textAlign block support #59531

Merged
merged 34 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4091d64
Try: Add new `textAlign` block support
t-hamano Mar 3, 2024
909a3e7
Merge branch 'trunk' into add/text-align-block-support
t-hamano Mar 5, 2024
a6818b5
Remove `justify` option
t-hamano Mar 5, 2024
7303bd1
Add unit test
t-hamano Mar 5, 2024
ff07c27
Move definition from `textAlign` to `typography.textAlign`
t-hamano Mar 5, 2024
f68e1f0
Update documentation
t-hamano Mar 5, 2024
3e67645
Update block.json schema
t-hamano Mar 5, 2024
59dc958
Try to fix block.json schema unit test
t-hamano Mar 5, 2024
a4a43b0
Merge branch 'trunk' into add/text-align-block-support
t-hamano Mar 19, 2024
ff9a5ea
Add support for dynamic blocks
t-hamano Mar 19, 2024
7a2604c
Add skipSerialization support
t-hamano Mar 19, 2024
3c63337
Fix prop order in lib/theme.json
t-hamano Mar 20, 2024
f968646
Update useTypographyProps hook
t-hamano Mar 20, 2024
11fba78
Merge branch 'trunk' into add/text-align-block-support
t-hamano Mar 20, 2024
28b655d
Fix PHPCS errors
t-hamano Mar 20, 2024
7594e2b
Update fixtures
t-hamano Mar 20, 2024
7293e8a
Fix JS hok namespace and attribute definition
t-hamano Mar 21, 2024
f083864
Dont't use HTML_TAG_Processor to apply text-align class
t-hamano Mar 22, 2024
856f23a
Merge branch 'trunk' into add/text-align-block-support
t-hamano Mar 27, 2024
697b670
Apply support as inline style instead of class name
t-hamano Mar 27, 2024
d07af76
Remove unnecessary code
t-hamano Mar 28, 2024
3094465
Merge branch 'trunk' into add/text-align-block-support
t-hamano Apr 3, 2024
36575d5
Output as class name instead of inline style
t-hamano Apr 3, 2024
125639e
Update fixtures
t-hamano Apr 3, 2024
bb24696
Merge branch 'trunk' into add/text-align-block-support
t-hamano Apr 5, 2024
e1edff4
Add `TEXT_ALIGN_SUPPORT_KEY` to `TYPOGRAPHY_SUPPORT_KEYS` and remove …
t-hamano Apr 5, 2024
c38bcc9
Include `text-align` when pasting styles
t-hamano Apr 5, 2024
c1b5a43
Update theme.json schema
t-hamano Apr 5, 2024
6a8ecaa
Update Global Settings & Styles documentation
t-hamano Apr 5, 2024
6c31e6d
Update fixtures
t-hamano Apr 5, 2024
787658b
Add `textAlign` to `useBlockSettings`
t-hamano Apr 5, 2024
13acc91
Merge branch 'trunk' into add/text-align-block-support
t-hamano Apr 13, 2024
611a0b2
Make styles work in the Site Editor
t-hamano Apr 13, 2024
2dbaeb1
Merge branch 'trunk' into add/text-align-block-support
t-hamano Apr 16, 2024
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
1 change: 1 addition & 0 deletions docs/how-to-guides/themes/global-settings-and-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ The settings section has the following structure:
"fontWeight": true,
"letterSpacing": true,
"lineHeight": false,
"textAlign": true,
"textColumns": false,
"textDecoration": true,
"textTransform": true
Expand Down
63 changes: 55 additions & 8 deletions docs/reference-guides/block-api/block-supports.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ When a background image is selected, the image data is stored in the `style.back
When a background images is selected and its position or size are changed, the background-position is stored in the `style.background.backgroundPosition` and its background-size in `style.background.backgroundSize` attribute.

- `style`: an attribute of `object` type with no default assigned. This is added when `backgroundImage` or `backgroundSize` support is declared. It stores the custom values set by the user.
- `background`: an attribute of `object` type.
- `backgroundImage`: an attribute of `object` type, containing information about the selected image
- `background`: an attribute of `object` type.
- `backgroundImage`: an attribute of `object` type, containing information about the selected image
- `url`: type `string`, URL to the image
- `id`: type `int`, media attachment ID
- `source`: type `string`, at the moment the only value is `file`
- `title`: type `string`, title of the media attachment
- `backgroundPosition`: an attribute of `string` type, defining the background images position, selected by FocalPointPicker and used in CSS as the [`background-position`](https://developer.mozilla.org/en-US/docs/Web/CSS/background-position) value.
- `title`: type `string`, title of the media attachment
- `backgroundPosition`: an attribute of `string` type, defining the background images position, selected by FocalPointPicker and used in CSS as the [`background-position`](https://developer.mozilla.org/en-US/docs/Web/CSS/background-position) value.
- `backgroundSize`: an attribute of `string` type. defining the CSS [`background-size`](https://developer.mozilla.org/en-US/docs/Web/CSS/background-size) value.

The block can apply a default background image, position and size by specifying its own attribute with a default. For example:
Expand Down Expand Up @@ -484,10 +484,10 @@ When the block declares support for `color.link`, the attributes definition is e
color: {
text: 'var:preset|color|contrast',
},
":hover": {
color: {
text: "#000000"
}
":hover": {
color: {
text: "#000000"
}
}
}
}
Expand Down Expand Up @@ -973,6 +973,7 @@ supports: {
- Subproperties:
- `fontSize`: type `boolean`, default value `false`
- `lineHeight`: type `boolean`, default value `false`
- `textAlign`: type `boolean` or `array`, default value `false`

The presence of this object signals that a block supports some typography related properties. When it does, the block editor will show a typography UI allowing the user to control their values.

Expand All @@ -983,6 +984,8 @@ supports: {
fontSize: true,
// Enable support and UI control for line-height.
lineHeight: true,
// Enable support and UI control for text alignment.
textAlign: true,
},
}
```
Expand Down Expand Up @@ -1063,3 +1066,47 @@ attributes: {
}
}
```

### typography.textAlign

_**Note:** Since WordPress 6.6._

- Type: `boolean` or `array`
- Default value: `false`

This property adds block toolbar controls which allow to change block's text alignment.

```js
supports: {
typography: {
// Declare support for block's text alignment.
// This adds support for all the options:
// left, center, right.
textAlign: true
}
}
```

```js
supports: {
typography: {
// Declare support for specific text alignment options.
textAlign: [ 'left', 'right' ]
}
}
```

When the block declares support for `textAlign`, the attributes definition is extended to include a new attribute `style` of `object` type with no default assigned. It stores the custom value set by the user. The block can apply a default style by specifying its own `style` attribute with a default. For example:

```js
attributes: {
style: {
type: 'object',
default: {
typography: {
textAlign: 'value'
}
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ Settings related to typography.
| fluid | undefined | false | |
| letterSpacing | boolean | true | |
| lineHeight | boolean | false | |
| textAlign | boolean | true | |
| textColumns | boolean | false | |
| textDecoration | boolean | true | |
| writingMode | boolean | false | |
Expand Down Expand Up @@ -268,6 +269,7 @@ Typography styles.
| fontWeight | string, object | |
| letterSpacing | string, object | |
| lineHeight | string, object | |
| textAlign | string | |
| textColumns | string | |
| textDecoration | string, object | |
| writingMode | string, object | |
Expand Down
19 changes: 18 additions & 1 deletion lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function gutenberg_register_typography_support( $block_type ) {
$has_font_weight_support = $typography_supports['__experimentalFontWeight'] ?? false;
$has_letter_spacing_support = $typography_supports['__experimentalLetterSpacing'] ?? false;
$has_line_height_support = $typography_supports['lineHeight'] ?? false;
$has_text_align_support = $typography_supports['textAlign'] ?? false;
$has_text_columns_support = $typography_supports['textColumns'] ?? false;
$has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
$has_text_transform_support = $typography_supports['__experimentalTextTransform'] ?? false;
Expand All @@ -37,6 +38,7 @@ function gutenberg_register_typography_support( $block_type ) {
|| $has_font_weight_support
|| $has_letter_spacing_support
|| $has_line_height_support
|| $has_text_align_support
|| $has_text_columns_support
|| $has_text_decoration_support
|| $has_text_transform_support
Expand Down Expand Up @@ -95,6 +97,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_font_weight_support = $typography_supports['__experimentalFontWeight'] ?? false;
$has_letter_spacing_support = $typography_supports['__experimentalLetterSpacing'] ?? false;
$has_line_height_support = $typography_supports['lineHeight'] ?? false;
$has_text_align_support = $typography_supports['textAlign'] ?? false;
$has_text_columns_support = $typography_supports['textColumns'] ?? false;
$has_text_decoration_support = $typography_supports['__experimentalTextDecoration'] ?? false;
$has_text_transform_support = $typography_supports['__experimentalTextTransform'] ?? false;
Expand All @@ -106,6 +109,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$should_skip_font_style = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' );
$should_skip_font_weight = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' );
$should_skip_line_height = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' );
$should_skip_text_align = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textAlign' );
$should_skip_text_columns = wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textColumns' );
$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' );
Expand Down Expand Up @@ -143,6 +147,10 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$typography_block_styles['lineHeight'] = $block_attributes['style']['typography']['lineHeight'] ?? null;
}

if ( $has_text_align_support && ! $should_skip_text_align ) {
$typography_block_styles['textAlign'] = $block_attributes['style']['typography']['textAlign'] ?? null;
}

if ( $has_text_columns_support && ! $should_skip_text_columns && isset( $block_attributes['style']['typography']['textColumns'] ) ) {
$typography_block_styles['textColumns'] = $block_attributes['style']['typography']['textColumns'] ?? null;
}
Expand All @@ -167,13 +175,22 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
}

$attributes = array();
$classnames = array();
$styles = gutenberg_style_engine_get_styles(
array( 'typography' => $typography_block_styles ),
array( 'convert_vars_to_classnames' => true )
);

if ( ! empty( $styles['classnames'] ) ) {
$attributes['class'] = $styles['classnames'];
$classnames[] = $styles['classnames'];
}

if ( $has_text_align_support && ! $should_skip_text_align && isset( $block_attributes['style']['typography']['textAlign'] ) ) {
$classnames[] = 'has-text-align-' . $block_attributes['style']['typography']['textAlign'];
}

if ( ! empty( $classnames ) ) {
$attributes['class'] = implode( ' ', $classnames );
}

if ( ! empty( $styles['css'] ) ) {
Expand Down
3 changes: 3 additions & 0 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class WP_Theme_JSON_Gutenberg {
'border-left-width' => array( 'border', 'left', 'width' ),
'border-left-style' => array( 'border', 'left', 'style' ),
'color' => array( 'color', 'text' ),
'text-align' => array( 'typography', 'textAlign' ),
'column-count' => array( 'typography', 'textColumns' ),
'font-family' => array( 'typography', 'fontFamily' ),
'font-size' => array( 'typography', 'fontSize' ),
Expand Down Expand Up @@ -436,6 +437,7 @@ class WP_Theme_JSON_Gutenberg {
'fontWeight' => null,
'letterSpacing' => null,
'lineHeight' => null,
'textAlign' => null,
'textColumns' => null,
'textDecoration' => null,
'textTransform' => null,
Expand Down Expand Up @@ -531,6 +533,7 @@ class WP_Theme_JSON_Gutenberg {
'fontWeight' => null,
'letterSpacing' => null,
'lineHeight' => null,
'textAlign' => null,
'textColumns' => null,
'textDecoration' => null,
'textTransform' => null,
Expand Down
1 change: 1 addition & 0 deletions lib/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@
"fontWeight": true,
"letterSpacing": true,
"lineHeight": false,
"textAlign": false,
"textColumns": false,
"textDecoration": true,
"textTransform": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
hasAlignSupport,
hasBorderSupport,
hasBackgroundColorSupport,
hasTextAlignSupport,
hasTextColorSupport,
hasGradientSupport,
hasCustomClassNameSupport,
Expand Down Expand Up @@ -59,6 +60,7 @@ const STYLE_ATTRIBUTES = {
align: hasAlignSupport,
borderColor: ( nameOrType ) => hasBorderSupport( nameOrType, 'color' ),
backgroundColor: hasBackgroundColorSupport,
textAlign: hasTextAlignSupport,
textColor: hasTextColorSupport,
gradient: hasGradientSupport,
className: hasCustomClassNameSupport,
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import dimensions from './dimensions';
import duotone from './duotone';
import fontFamily from './font-family';
import fontSize from './font-size';
import textAlign from './text-align';
import border from './border';
import position from './position';
import layout from './layout';
Expand All @@ -34,6 +35,7 @@ import './use-bindings-attributes';
createBlockEditFilter(
[
align,
textAlign,
anchor,
customClassName,
style,
Expand All @@ -47,6 +49,7 @@ createBlockEditFilter(
);
createBlockListBlockFilter( [
align,
textAlign,
background,
style,
color,
Expand All @@ -60,6 +63,7 @@ createBlockListBlockFilter( [
] );
createBlockSaveFilter( [
align,
textAlign,
anchor,
ariaLabel,
customClassName,
Expand Down
24 changes: 24 additions & 0 deletions packages/block-editor/src/hooks/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const FONT_STYLE_SUPPORT_KEY = 'typography.__experimentalFontStyle';
* Key within block settings' support array indicating support for font weight.
*/
const FONT_WEIGHT_SUPPORT_KEY = 'typography.__experimentalFontWeight';
/**
* Key within block settings' supports array indicating support for text
* align e.g. settings found in `block.json`.
*/
const TEXT_ALIGN_SUPPORT_KEY = 'typography.textAlign';
/**
* Key within block settings' supports array indicating support for text
* columns e.g. settings found in `block.json`.
Expand Down Expand Up @@ -53,6 +58,7 @@ const TYPOGRAPHY_SUPPORT_KEYS = [
FONT_STYLE_SUPPORT_KEY,
FONT_WEIGHT_SUPPORT_KEY,
FONT_FAMILY_SUPPORT_KEY,
TEXT_ALIGN_SUPPORT_KEY,
TEXT_COLUMNS_SUPPORT_KEY,
TEXT_DECORATION_SUPPORT_KEY,
TEXT_TRANSFORM_SUPPORT_KEY,
Expand Down Expand Up @@ -212,6 +218,24 @@ export const hasBackgroundColorSupport = ( nameOrType ) => {
return colorSupport && colorSupport.background !== false;
};

/**
* Returns true if the block defines support for text-align.
*
* @param {string|Object} nameOrType Block name or type object.
* @return {boolean} Whether the block supports the feature.
*/
export const hasTextAlignSupport = ( nameOrType ) =>
hasBlockSupport( nameOrType, TEXT_ALIGN_SUPPORT_KEY );

/**
* Returns the block support value for text-align, if defined.
*
* @param {string|Object} nameOrType Block name or type object.
* @return {unknown} The block support value.
*/
export const getTextAlignSupport = ( nameOrType ) =>
getBlockSupport( nameOrType, TEXT_ALIGN_SUPPORT_KEY );

/**
* Returns true if the block defines support for background color.
*
Expand Down
Loading
Loading