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: Caption element UI controls for color and typography #49141

Merged
merged 6 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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