Skip to content

Commit

Permalink
Global Styles: Caption element UI controls for color and typography (#…
Browse files Browse the repository at this point in the history
…49141)

* abstract the button color screen into a generic element one

* use screen color element with the text element

* added color and typography controls for caption element

* hide caption color control on blocks that don't support it

* copypaste fail

* removed background controls
  • Loading branch information
MaggieCabrera committed Mar 20, 2023
1 parent c23706e commit 9612e1c
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 170 deletions.
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/global-styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ROOT_BLOCK_SUPPORTS = [
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'fontFamily',
'fontSize',
Expand Down Expand Up @@ -103,6 +104,7 @@ export const STYLE_PATH_TO_CSS_VAR_INFIX = {
'elements.link.typography.fontSize': 'font-size',
'elements.button.color.text': 'color',
'elements.button.color.background': 'color',
'elements.caption.color.text': 'color',
'elements.button.typography.fontFamily': 'font-family',
'elements.button.typography.fontSize': 'font-size',
'elements.heading.color': 'color',
Expand Down
4 changes: 4 additions & 0 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
value: [ 'elements', 'link', 'color', 'text' ],
support: [ 'color', 'link' ],
},
captionColor: {
value: [ 'elements', 'caption', 'color', 'text' ],
support: [ 'color', 'caption' ],
},
buttonColor: {
value: [ 'elements', 'button', 'color', 'text' ],
support: [ 'color', 'button' ],
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ROOT_BLOCK_SUPPORTS = [
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'fontFamily',
'fontSize',
Expand Down
3 changes: 3 additions & 0 deletions packages/blocks/src/store/test/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe( 'private selectors', () => {
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'fontFamily',
'fontSize',
Expand All @@ -51,6 +52,7 @@ describe( 'private selectors', () => {
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'fontFamily',
'fontSize',
Expand All @@ -77,6 +79,7 @@ describe( 'private selectors', () => {
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'fontFamily',
'fontStyle',
Expand Down
104 changes: 0 additions & 104 deletions packages/edit-site/src/components/global-styles/screen-button-color.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
__experimentalColorGradientControl as ColorGradientControl,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import ScreenHeader from './header';
import { useSupportedStyles, useColorsPerOrigin } from './hooks';
import { unlock } from '../../private-apis';

const { useGlobalSetting, useGlobalStyle } = unlock( blockEditorPrivateApis );

const elements = {
text: {
description: __(
'Set the default color used for text accross the site.'
),
title: __( 'Text' ),
},
caption: {
description: __(
'Set the default color used for captions accross the site.'
),
title: __( 'Captions' ),
},
button: {
description: __(
'Set the default color used for buttons accross the site.'
),
title: __( 'Buttons' ),
},
};

function ScreenColorElement( { name, element, variation = '' } ) {
const prefix = variation ? `variations.${ variation }.` : '';
const supports = useSupportedStyles( name );
const colorsPerOrigin = useColorsPerOrigin( name );
const [ isTextEnabled ] = useGlobalSetting( 'color.text', name );
const [ areCustomSolidsEnabled ] = useGlobalSetting( 'color.custom', name );
let [ isBackgroundEnabled ] = useGlobalSetting( 'color.background', name );

let textColorElementSelector = 'elements.' + element + '.color.text';
const backgroundColorElementSelector =
'elements.' + element + '.color.background';

let hasElementColor =
supports.includes( 'buttonColor' ) &&
( colorsPerOrigin.length > 0 || areCustomSolidsEnabled );

if ( element === 'text' ) {
isBackgroundEnabled = false;
textColorElementSelector = 'color.text';
hasElementColor =
supports.includes( 'color' ) &&
isTextEnabled &&
( colorsPerOrigin.length > 0 || areCustomSolidsEnabled );
} else if ( element === 'button' ) {
hasElementColor =
supports.includes( 'buttonColor' ) &&
isBackgroundEnabled &&
( colorsPerOrigin.length > 0 || areCustomSolidsEnabled );
} else if ( element === 'caption' ) {
isBackgroundEnabled = false;
}

const [ elementTextColor, setElementTextColor ] = useGlobalStyle(
prefix + textColorElementSelector,
name
);
const [ userElementTextColor ] = useGlobalStyle(
textColorElementSelector,
name,
'user'
);

const [ elementBgColor, setElementBgColor ] = useGlobalStyle(
backgroundColorElementSelector,
name
);
const [ userElementBgColor ] = useGlobalStyle(
backgroundColorElementSelector,
name,
'user'
);

if ( ! hasElementColor ) {
return null;
}

return (
<>
<ScreenHeader
title={ elements[ element ].title }
description={ elements[ element ].description }
/>

<h3 className="edit-site-global-styles-section-title">
{ __( 'Text color' ) }
</h3>

<ColorGradientControl
className="edit-site-screen-element-color__control"
colors={ colorsPerOrigin }
disableCustomColors={ ! areCustomSolidsEnabled }
showTitle={ false }
enableAlpha
__experimentalIsRenderedInSidebar
colorValue={ elementTextColor }
onColorChange={ setElementTextColor }
clearable={ elementTextColor === userElementTextColor }
headingLevel={ 4 }
/>
{ isBackgroundEnabled && (
<>
<h3 className="edit-site-global-styles-section-title">
{ __( 'Background color' ) }
</h3>

<ColorGradientControl
className="edit-site-screen-element-color__control"
colors={ colorsPerOrigin }
disableCustomColors={ ! areCustomSolidsEnabled }
showTitle={ false }
enableAlpha
__experimentalIsRenderedInSidebar
colorValue={ elementBgColor }
onColorChange={ setElementBgColor }
clearable={ elementBgColor === userElementBgColor }
headingLevel={ 4 }
/>
</>
) }
</>
);
}

export default ScreenColorElement;
35 changes: 35 additions & 0 deletions packages/edit-site/src/components/global-styles/screen-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,37 @@ function HeadingColorItem( { name, parentMenu, variation = '' } ) {
);
}

function CaptionColorItem( { name, parentMenu, variation = '' } ) {
const prefix = variation ? `variations.${ variation }.` : '';
const urlPrefix = variation ? `/variations/${ variation }` : '';
const supports = useSupportedStyles( name );
const hasSupport = supports.includes( 'captionColor' );
const [ color ] = useGlobalStyle(
prefix + 'elements.caption.color.text',
name
);

if ( ! hasSupport ) {
return null;
}

return (
<NavigationButtonAsItem
path={ parentMenu + urlPrefix + '/colors/caption' }
aria-label={ __( 'Colors caption styles' ) }
>
<HStack justify="flex-start">
<ZStack isLayered={ false } offset={ -8 }>
<ColorIndicatorWrapper expanded={ false }>
<ColorIndicator colorValue={ color } />
</ColorIndicatorWrapper>
</ZStack>
<FlexItem>{ __( 'Captions' ) }</FlexItem>
</HStack>
</NavigationButtonAsItem>
);
}

function ButtonColorItem( { name, parentMenu, variation = '' } ) {
const prefix = variation ? `variations.${ variation }.` : '';
const urlPrefix = variation ? `/variations/${ variation }` : '';
Expand Down Expand Up @@ -250,6 +281,10 @@ function ScreenColors( { name, variation = '' } ) {
parentMenu={ parentMenu }
variation={ variation }
/>
<CaptionColorItem
name={ name }
parentMenu={ parentMenu }
/>
<HeadingColorItem
name={ name }
parentMenu={ parentMenu }
Expand Down
Loading

1 comment on commit 9612e1c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 9612e1c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4467793804
📝 Reported issues:

Please sign in to comment.