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: Heading element UI controls #42176

Merged
merged 7 commits into from
Aug 5, 2022
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
34 changes: 34 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 @@ -108,6 +108,36 @@ function LinkColorItem( { name, parentMenu } ) {
);
}

function HeadingColorItem( { name, parentMenu } ) {
const supports = getSupportedGlobalStylesPanels( name );
const hasSupport = supports.includes( 'color' );
const [ color ] = useStyle( 'elements.heading.color.text', name );
const [ bgColor ] = useStyle( 'elements.heading.color.background', name );

if ( ! hasSupport ) {
return null;
}

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

function ButtonColorItem( { name, parentMenu } ) {
const supports = getSupportedGlobalStylesPanels( name );
const hasSupport = supports.includes( 'buttonColor' );
Expand Down Expand Up @@ -166,6 +196,10 @@ function ScreenColors( { name } ) {
name={ name }
parentMenu={ parentMenu }
/>
<HeadingColorItem
name={ name }
parentMenu={ parentMenu }
/>
<ButtonColorItem
name={ name }
parentMenu={ parentMenu }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/**
* WordPress dependencies
*/
import { sprintf, __ } from '@wordpress/i18n';
import {
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';
import { __experimentalColorGradientControl as ColorGradientControl } from '@wordpress/block-editor';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import ScreenHeader from './header';
import {
getSupportedGlobalStylesPanels,
useSetting,
useStyle,
useColorsPerOrigin,
useGradientsPerOrigin,
} from './hooks';

function ScreenHeadingColor( { name } ) {
const [ selectedLevel, setCurrentTab ] = useState( 'heading' );

const supports = getSupportedGlobalStylesPanels( name );
const [ solids ] = useSetting( 'color.palette', name );
const [ gradients ] = useSetting( 'color.gradients', name );
const [ areCustomSolidsEnabled ] = useSetting( 'color.custom', name );
const [ areCustomGradientsEnabled ] = useSetting(
'color.customGradient',
name
);
const [ isTextEnabled ] = useSetting( 'color.text', name );
const [ isBackgroundEnabled ] = useSetting( 'color.background', name );

const colorsPerOrigin = useColorsPerOrigin( name );
const gradientsPerOrigin = useGradientsPerOrigin( name );

const hasTextColor =
Copy link
Contributor

Choose a reason for hiding this comment

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

All of these supports (hasTextColor, hasBackgroundColor, hasGradientColor) are quite verbose, and we probably have this same logic elsewhere. Can we extract this to some kind of hook or shared function? That could be a follow-up PR....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I fully agree.

supports.includes( 'color' ) &&
isTextEnabled &&
( solids.length > 0 || areCustomSolidsEnabled );

const hasBackgroundColor =
supports.includes( 'backgroundColor' ) &&
isBackgroundEnabled &&
( solids.length > 0 || areCustomSolidsEnabled );
const hasGradientColor =
supports.includes( 'background' ) &&
( gradients.length > 0 || areCustomGradientsEnabled );

const [ color, setColor ] = useStyle(
'elements.' + selectedLevel + '.color.text',
name
);
const [ userColor ] = useStyle(
'elements.' + selectedLevel + '.color.text',
name,
'user'
);

const [ backgroundColor, setBackgroundColor ] = useStyle(
'elements.' + selectedLevel + '.color.background',
name
);
const [ userBackgroundColor ] = useStyle(
'elements.' + selectedLevel + '.color.background',
name,
'user'
);
const [ gradient, setGradient ] = useStyle(
'elements.' + selectedLevel + '.color.gradient',
name
);
const [ userGradient ] = useStyle(
'elements.' + selectedLevel + '.color.gradient',
name,
'user'
);

if ( ! hasTextColor && ! hasBackgroundColor && ! hasGradientColor ) {
return null;
}

let backgroundSettings = {};
if ( hasBackgroundColor ) {
backgroundSettings = {
colorValue: backgroundColor,
onColorChange: setBackgroundColor,
};
if ( backgroundColor ) {
backgroundSettings.clearable =
backgroundColor === userBackgroundColor;
}
}

let gradientSettings = {};
if ( hasGradientColor ) {
gradientSettings = {
gradientValue: gradient,
onGradientChange: setGradient,
};
if ( gradient ) {
gradientSettings.clearable = gradient === userGradient;
}
}

const controlProps = {
...backgroundSettings,
...gradientSettings,
};

return (
<>
<ScreenHeader
title={ __( 'Headings' ) }
description={ __(
'Set the default color used for headings across the site.'
) }
/>
<div className="edit-site-global-styles-screen-heading-color">
<h4>{ __( 'Select heading level' ) }</h4>

<ToggleGroupControl
label={ __( 'Select heading level' ) }
hideLabelFromVision={ true }
value={ selectedLevel }
onChange={ setCurrentTab }
isBlock
>
<ToggleGroupControlOption
value="heading"
/* translators: 'All' refers to selecting all heading levels
and applying the same style to h1-h6. */
label={ __( 'All' ) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need a translators comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

/>
<ToggleGroupControlOption value="h1" label={ __( 'H1' ) } />
<ToggleGroupControlOption value="h2" label={ __( 'H2' ) } />
<ToggleGroupControlOption value="h3" label={ __( 'H3' ) } />
<ToggleGroupControlOption value="h4" label={ __( 'H4' ) } />
<ToggleGroupControlOption value="h5" label={ __( 'H5' ) } />
<ToggleGroupControlOption value="h6" label={ __( 'H6' ) } />
</ToggleGroupControl>
</div>
{ hasTextColor && (
<div className="edit-site-global-styles-screen-heading-color">
<h4>
{ selectedLevel === 'heading'
? __( 'Text color for all heading levels' )
: sprintf(
/* translators: %s: heading level (h1-h6) */
__( 'Text color for %s' ),
selectedLevel.toUpperCase()
) }
</h4>
<ColorGradientControl
className="edit-site-screen-heading-text-color__control"
colors={ colorsPerOrigin }
disableCustomColors={ ! areCustomSolidsEnabled }
__experimentalHasMultipleOrigins
showTitle={ false }
enableAlpha
__experimentalIsRenderedInSidebar
colorValue={ color }
onColorChange={ setColor }
clearable={ color === userColor }
/>
</div>
) }
{ hasBackgroundColor && (
<div className="edit-site-global-styles-screen-heading-color">
<h4>
{ selectedLevel === 'heading'
? __( 'Background color for all heading levels' )
: sprintf(
/* translators: %s: heading level (h1-h6) */
__( 'Background color for %s' ),
selectedLevel.toUpperCase()
) }
</h4>
<ColorGradientControl
className="edit-site-screen-heading-background-color__control"
colors={ colorsPerOrigin }
gradients={ gradientsPerOrigin }
disableCustomColors={ ! areCustomSolidsEnabled }
disableCustomGradients={ ! areCustomGradientsEnabled }
__experimentalHasMultipleOrigins
showTitle={ false }
enableAlpha
__experimentalIsRenderedInSidebar
{ ...controlProps }
/>
</div>
) }
</>
);
}

export default ScreenHeadingColor;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const elements = {
description: __( 'Manage the fonts and typography used on the links.' ),
title: __( 'Links' ),
},
heading: {
description: __( 'Manage the fonts and typography used on headings.' ),
title: __( 'Headings' ),
},
button: {
description: __( 'Manage the fonts and typography used on buttons.' ),
title: __( 'Buttons' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ function ScreenTypography( { name } ) {
element="link"
label={ __( 'Links' ) }
/>
<Item
name={ name }
parentMenu={ parentMenu }
element="heading"
label={ __( 'Headings' ) }
/>
<Item
name={ name }
parentMenu={ parentMenu }
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/global-styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
border-radius: $radius-block-ui;
}

.edit-site-global-styles-screen-heading-color,
.edit-site-global-styles-screen-typography {
margin: $grid-unit-20;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {
PanelBody,
FontSizePicker,
__experimentalSpacer as Spacer,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
} from '@wordpress/components';

import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -59,9 +62,14 @@ function useHasLetterSpacingControl( name ) {
}

export default function TypographyPanel( { name, element } ) {
const [ selectedLevel, setCurrentTab ] = useState( 'heading' );
const supports = getSupportedGlobalStylesPanels( name );
const prefix =
element === 'text' || ! element ? '' : `elements.${ element }.`;
let prefix = '';
if ( element === 'heading' ) {
prefix = `elements.${ selectedLevel }.`;
} else if ( element && element !== 'text' ) {
prefix = `elements.${ element }.`;
}
const [ fontSizes ] = useSetting( 'typography.fontSizes', name );
const disableCustomFontSizes = ! useSetting(
'typography.customFontSize',
Expand All @@ -78,6 +86,12 @@ export default function TypographyPanel( { name, element } ) {
const hasAppearanceControl = useHasAppearanceControl( name );
const hasLetterSpacingControl = useHasLetterSpacingControl( name );

/* Disable font size controls when the option to style all headings is selected. */
let hasFontSizeEnabled = supports.includes( 'fontSize' );
if ( element === 'heading' && selectedLevel === 'heading' ) {
hasFontSizeEnabled = false;
}

const [ fontFamily, setFontFamily ] = useStyle(
prefix + 'typography.fontFamily',
name
Expand Down Expand Up @@ -130,15 +144,57 @@ export default function TypographyPanel( { name, element } ) {
>
Aa
</div>

{ element === 'heading' && (
<div>
<h4>{ __( 'Select heading level' ) }</h4>
<ToggleGroupControl
label={ __( 'Select heading level' ) }
hideLabelFromVision={ true }
value={ selectedLevel }
onChange={ setCurrentTab }
isBlock
>
<ToggleGroupControlOption
value="heading"
/* translators: 'All' refers to selecting all heading levels
and applying the same style to h1-h6. */
label={ __( 'All' ) }
/>
<ToggleGroupControlOption
value="h1"
label={ __( 'H1' ) }
/>
<ToggleGroupControlOption
value="h2"
label={ __( 'H2' ) }
/>
<ToggleGroupControlOption
value="h3"
label={ __( 'H3' ) }
/>
<ToggleGroupControlOption
value="h4"
label={ __( 'H4' ) }
/>
<ToggleGroupControlOption
value="h5"
label={ __( 'H5' ) }
/>
<ToggleGroupControlOption
value="h6"
label={ __( 'H6' ) }
/>
</ToggleGroupControl>
</div>
) }
{ supports.includes( 'fontFamily' ) && (
<FontFamilyControl
fontFamilies={ fontFamilies }
value={ fontFamily }
onChange={ setFontFamily }
/>
) }
{ supports.includes( 'fontSize' ) && (
{ hasFontSizeEnabled && (
<FontSizePicker
value={ fontSize }
onChange={ setFontSize }
Expand Down
Loading