Skip to content

Commit

Permalink
abstract the color caption component to work for all elements
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieCabrera authored and scruffian committed Oct 19, 2022
1 parent 525c104 commit 498f64f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 97 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __experimentalColorGradientControl as ColorGradientControl } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import ScreenHeader from './header';
import {
getSupportedGlobalStylesPanels,
useSetting,
useStyle,
useColorsPerOrigin,
} from './hooks';

const elements = {
text: {
description: __( '' ),
title: __( 'Text' ),
},
link: {
description: __( '' ),
title: __( 'Links' ),
},
heading: {
description: __( '' ),
title: __( 'Headings' ),
},
caption: {
description: __( 'Set the colors used for captions across the site' ),
title: __( 'Captions' ),
},
button: {
description: __(
'Set the default colors used for buttons across the site.'
),
title: __( 'Buttons' ),
},
};

function ScreenColorElement( { name, element } ) {
const supports = getSupportedGlobalStylesPanels( name );
const colorsPerOrigin = useColorsPerOrigin( name );
const [ solids ] = useSetting( 'color.palette', name );
const [ areCustomSolidsEnabled ] = useSetting( 'color.custom', name );

const hasElementColor =
supports.includes( 'color' ) &&
( solids.length > 0 || areCustomSolidsEnabled );

const [ elementTextColor, setElementTextColor ] = useStyle(
'elements.' + element + '.color.text',
name
);
const [ userElementTextColor ] = useStyle(
'elements.' + element + '.color.text',
name,
'user'
);

const [ elementBgColor, setElementBgColor ] = useStyle(
'elements.' + element + '.color.background',
name
);
const [ userElementBgColor ] = useStyle(
'elements.' + element + '.color.background',
name,
'user'
);

if ( ! hasElementColor ) {
return null;
}

return (
<>
<ScreenHeader
title={ elements[ element ].title }
description={ elements[ element ].description }
/>
<div className="edit-site-global-styles-screen-element-color">
<h4>{ __( 'Text color' ) }</h4>

<ColorGradientControl
className="edit-site-screen-element-color__control"
colors={ colorsPerOrigin }
disableCustomColors={ ! areCustomSolidsEnabled }
__experimentalHasMultipleOrigins
showTitle={ false }
enableAlpha
__experimentalIsRenderedInSidebar
colorValue={ elementTextColor }
onColorChange={ setElementTextColor }
clearable={ elementTextColor === userElementTextColor }
/>

<h4>{ __( 'Background color' ) }</h4>

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

export default ScreenColorElement;
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/global-styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
max-width: 100%;
}

.edit-site-global-styles-screen-caption-color,
.edit-site-global-styles-screen-element-color,
.edit-site-global-styles-screen-heading-color,
.edit-site-global-styles-screen-typography {
margin: $grid-unit-20;
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/global-styles/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ScreenTypographyElement from './screen-typography-element';
import ScreenColors from './screen-colors';
import ScreenColorPalette from './screen-color-palette';
import ScreenBackgroundColor from './screen-background-color';
import ScreenCaptionColor from './screen-caption-color';
import ScreenColorElement from './screen-color-element';
import ScreenTextColor from './screen-text-color';
import ScreenLinkColor from './screen-link-color';
import ScreenHeadingColor from './screen-heading-color';
Expand Down Expand Up @@ -112,7 +112,7 @@ function ContextScreens( { name } ) {
<GlobalStylesNavigationScreen
path={ parentMenu + '/colors/caption' }
>
<ScreenCaptionColor name={ name } />
<ScreenColorElement name={ name } element="caption" />
</GlobalStylesNavigationScreen>

<GlobalStylesNavigationScreen
Expand Down

0 comments on commit 498f64f

Please sign in to comment.