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

Global Styles: Add block-level Text Alignment UI #61717

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -76,6 +76,7 @@ const VALID_SETTINGS = [
'typography.fontWeight',
'typography.letterSpacing',
'typography.lineHeight',
'typography.textAlign',
'typography.textColumns',
'typography.textDecoration',
'typography.textTransform',
Expand Down Expand Up @@ -372,8 +373,13 @@ export function useSettingsForBlockElement(
? updatedSettings.shadow
: false;

// Text alignment is only available for blocks.
if ( element ) {
updatedSettings.typography.textAlign = false;
}
Comment on lines +382 to +385
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is necessary to prevent the text alignment UI from being displayed in the Text, Links, Headings, Captions and Buttons typography panels.


return updatedSettings;
}, [ parentSettings, supportedStyles, supports ] );
}, [ parentSettings, supportedStyles, supports, element ] );
}

export function useColorsPerOrigin( settings ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import FontFamilyControl from '../font-family';
import FontAppearanceControl from '../font-appearance-control';
import LineHeightControl from '../line-height-control';
import LetterSpacingControl from '../letter-spacing-control';
import TextAlignmentControl from '../text-alignment-control';
import TextTransformControl from '../text-transform-control';
import TextDecorationControl from '../text-decoration-control';
import WritingModeControl from '../writing-mode-control';
Expand All @@ -32,6 +33,7 @@ export function useHasTypographyPanel( settings ) {
const hasLineHeight = useHasLineHeightControl( settings );
const hasFontAppearance = useHasAppearanceControl( settings );
const hasLetterSpacing = useHasLetterSpacingControl( settings );
const hasTextAlign = useHasTextAlignmentControl( settings );
const hasTextTransform = useHasTextTransformControl( settings );
const hasTextDecoration = useHasTextDecorationControl( settings );
const hasWritingMode = useHasWritingModeControl( settings );
Expand All @@ -43,6 +45,7 @@ export function useHasTypographyPanel( settings ) {
hasLineHeight ||
hasFontAppearance ||
hasLetterSpacing ||
hasTextAlign ||
hasTextTransform ||
hasFontSize ||
hasTextDecoration ||
Expand Down Expand Up @@ -91,6 +94,10 @@ function useHasTextTransformControl( settings ) {
return settings?.typography?.textTransform;
}

function useHasTextAlignmentControl( settings ) {
return settings?.typography?.textAlign;
}

function useHasTextDecorationControl( settings ) {
return settings?.typography?.textDecoration;
}
Expand Down Expand Up @@ -150,6 +157,7 @@ const DEFAULT_CONTROLS = {
fontAppearance: true,
lineHeight: true,
letterSpacing: true,
textAlign: true,
textTransform: true,
textDecoration: true,
writingMode: true,
Expand All @@ -165,6 +173,13 @@ export default function TypographyPanel( {
panelId,
defaultControls = DEFAULT_CONTROLS,
} ) {
// If `panelId` has a value, it means this panel will be rendered inside
// the block sidebar. The text alignment UI for individual blocks is rendered
// in the block toolbar, so disable support if it's not in the global style.
if ( panelId ) {
settings.typography.textAlign = false;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the point that bothered me the most. This prevents duplicate UI in inserted blocks, but there may be a better approach.

duplicate-text-align-ui

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than relying on fudging the settings.typography.textAlign value, could we leverage the fact that this typography panel is extended by the site editor's global styles typography panel?

Perhaps the text alignment control could be rendered only where it is needed that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried a different approach with 9c4be9e.

In BlockStyleControls, override settings and disable textAlign support. This component is used to render sidebars for individual blocks, so it does not affect the global styles. This will cause the TypographyPanel to assume it does not support textAlign and will not render the UI.

I can't think of any other good approach at the moment, do you have any ideas?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't think of any other good approach at the moment, do you have any ideas?

Only the suggestion in my last comment. Did you explore adding the global styles only control as a child to the global styles typography panel?

It makes sense to me that if we have a component extending the typography panel for the site editor's global styles panel that something specific to that is added there instead of hacking the settings in an unexpected manner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you explore adding the global styles only control as a child to the global styles typography panel?

The method I came up with is to pass the context to StylesTypographyPanel in the block screen as shown below.

--- a/packages/edit-site/src/components/global-styles/screen-block.js
+++ b/packages/edit-site/src/components/global-styles/screen-block.js
@@ -260,6 +260,7 @@ function ScreenBlock( { name, variation } ) {
                                        value={ style }
                                        onChange={ setStyle }
                                        settings={ settings }
+                                       context="global-styles"
                                />
                        ) }
                        { hasDimensionsPanel && (

And depending on this context, the typography panel controls the UI.

diff --git a/packages/block-editor/src/components/global-styles/typography-panel.js b/packages/block-editor/src/components/global-styles/typography-panel.js
index 3106723945..bbd10e97b2 100644
--- a/packages/block-editor/src/components/global-styles/typography-panel.js
+++ b/packages/block-editor/src/components/global-styles/typography-panel.js
@@ -173,7 +173,11 @@ export default function TypographyPanel( {
        settings,
        panelId,
        defaultControls = DEFAULT_CONTROLS,
+       context,
 } ) {
+       if ( context === 'global-styles' ) {
+               // Do something...
+       }
        const decodeValue = ( rawValue ) =>
                getValueFromVariable( { settings }, '', rawValue );

Is this your intended approach? Sorry if I misunderstood.


const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue );

Expand Down Expand Up @@ -334,6 +349,22 @@ export default function TypographyPanel( {
const hasWritingMode = () => !! value?.typography?.writingMode;
const resetWritingMode = () => setWritingMode( undefined );

// Text Alignment
const hasTextAlignmentControl = useHasTextAlignmentControl( settings );

const textAlign = decodeValue( inheritedValue?.typography?.textAlign );
const setTextAlign = ( newValue ) => {
onChange(
setImmutably(
value,
[ 'typography', 'textAlign' ],
newValue || undefined
)
);
};
const hasTextAlign = () => !! value?.typography?.textAlign;
const resetTextAlign = () => setTextAlign( undefined );

const resetAllFilter = useCallback( ( previousValue ) => {
return {
...previousValue,
Expand Down Expand Up @@ -514,6 +545,22 @@ export default function TypographyPanel( {
/>
</ToolsPanelItem>
) }
{ hasTextAlignmentControl && (
<ToolsPanelItem
label={ __( 'Text alignment' ) }
hasValue={ hasTextAlign }
onDeselect={ resetTextAlign }
isShownByDefault={ defaultControls.textAlign }
panelId={ panelId }
>
<TextAlignmentControl
value={ textAlign }
onChange={ setTextAlign }
size="__unstable-large"
__nextHasNoMarginBottom
/>
</ToolsPanelItem>
) }
</Wrapper>
);
}
7 changes: 6 additions & 1 deletion phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ public function test_get_stylesheet() {
),
),
),
'core/media-text' => array(
'typography' => array(
'textAlign' => 'center',
),
),
'core/post-date' => array(
'color' => array(
'text' => '#123456',
Expand Down Expand Up @@ -547,7 +552,7 @@ public function test_get_stylesheet() {
);

$variables = ':root{--wp--preset--color--grey: grey;--wp--preset--gradient--custom-gradient: linear-gradient(135deg,rgba(0,0,0) 0%,rgb(0,0,0) 100%);--wp--preset--font-size--small: 14px;--wp--preset--font-size--big: 41px;--wp--preset--font-family--arial: Arial, serif;}.wp-block-group{--wp--custom--base-font: 16;--wp--custom--line-height--small: 1.2;--wp--custom--line-height--medium: 1.4;--wp--custom--line-height--large: 1.8;}';
$styles = static::$base_styles . ':where(body){color: var(--wp--preset--color--grey);}:where(a:where(:not(.wp-element-button))){background-color: #333;color: #111;}:where(.wp-element-button, .wp-block-button__link){box-shadow: 10px 10px 5px 0px rgba(0,0,0,0.66);}:where(.wp-block-cover){min-height: unset;aspect-ratio: 16/9;}:where(.wp-block-group){background: var(--wp--preset--gradient--custom-gradient);border-radius: 10px;min-height: 50vh;padding: 24px;}:where(.wp-block-group a:where(:not(.wp-element-button))){color: #111;}:where(.wp-block-heading){color: #123456;}:where(.wp-block-heading a:where(:not(.wp-element-button))){background-color: #333;color: #111;font-size: 60px;}:where(.wp-block-post-date){color: #123456;}:where(.wp-block-post-date a:where(:not(.wp-element-button))){background-color: #777;color: #555;}:where(.wp-block-post-excerpt){column-count: 2;}:where(.wp-block-image){margin-bottom: 30px;}:where(.wp-block-image img, .wp-block-image .wp-block-image__crop-area, .wp-block-image .components-placeholder){border-top-left-radius: 10px;border-bottom-right-radius: 1em;}:where(.wp-block-image img, .wp-block-image .components-placeholder){filter: var(--wp--preset--duotone--custom-duotone);}';
$styles = static::$base_styles . ':where(body){color: var(--wp--preset--color--grey);}:where(a:where(:not(.wp-element-button))){background-color: #333;color: #111;}:where(.wp-element-button, .wp-block-button__link){box-shadow: 10px 10px 5px 0px rgba(0,0,0,0.66);}:where(.wp-block-cover){min-height: unset;aspect-ratio: 16/9;}:where(.wp-block-group){background: var(--wp--preset--gradient--custom-gradient);border-radius: 10px;min-height: 50vh;padding: 24px;}:where(.wp-block-group a:where(:not(.wp-element-button))){color: #111;}:where(.wp-block-heading){color: #123456;}:where(.wp-block-heading a:where(:not(.wp-element-button))){background-color: #333;color: #111;font-size: 60px;}:where(.wp-block-media-text){text-align: center;}:where(.wp-block-post-date){color: #123456;}:where(.wp-block-post-date a:where(:not(.wp-element-button))){background-color: #777;color: #555;}:where(.wp-block-post-excerpt){column-count: 2;}:where(.wp-block-image){margin-bottom: 30px;}:where(.wp-block-image img, .wp-block-image .wp-block-image__crop-area, .wp-block-image .components-placeholder){border-top-left-radius: 10px;border-bottom-right-radius: 1em;}:where(.wp-block-image img, .wp-block-image .components-placeholder){filter: var(--wp--preset--duotone--custom-duotone);}';
$presets = '.has-grey-color{color: var(--wp--preset--color--grey) !important;}.has-grey-background-color{background-color: var(--wp--preset--color--grey) !important;}.has-grey-border-color{border-color: var(--wp--preset--color--grey) !important;}.has-custom-gradient-gradient-background{background: var(--wp--preset--gradient--custom-gradient) !important;}.has-small-font-size{font-size: var(--wp--preset--font-size--small) !important;}.has-big-font-size{font-size: var(--wp--preset--font-size--big) !important;}.has-arial-font-family{font-family: var(--wp--preset--font-family--arial) !important;}';
$all = $variables . $styles . $presets;

Expand Down
Loading