Skip to content

Commit 7904ade

Browse files
author
Mario Aguiar
authored
Merge pull request #419 from material-components/feature/354-color-palette
Core color picker
2 parents 9ef425e + a7f99b8 commit 7904ade

File tree

19 files changed

+865
-559
lines changed

19 files changed

+865
-559
lines changed

.github/workflows/lint-tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ jobs:
3535
fail-fast: false
3636
matrix:
3737
php_versions: [7.4]
38-
wp_versions: [5.7, 5.8.2]
38+
wp_versions: [5.9, 6.0]
3939
include:
4040
- php_versions: 7.3
41-
wp_versions: 5.6
41+
wp_versions: 5.9
4242

4343
env:
4444
NODE_ENV: teste2e
4545
WP_VERSION: ${{ matrix.wp_versions }}
4646
PHP_VERSION: php${{ matrix.php_versions }}-apache
47+
WORDPRESS_DEBUG: 0
4748

4849
steps:
4950
- uses: actions/checkout@v2

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ services:
5454
- ./composer.lock:/var/www/html/wp-content/themes/material-design-google/composer.lock
5555
restart: always
5656
environment:
57-
WORDPRESS_DEBUG: 1
57+
WORDPRESS_DEBUG: ${WORDPRESS_DEBUG-1}
5858
WORDPRESS_DB_USER: ${WP_DB_USER}
5959
WORDPRESS_DB_PASSWORD: ${WP_DB_PASSWORD}
6060

plugin/assets/src/block-editor/blocks/button/edit.js

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ import ButtonGroup from '../../components/button-group';
4747
import ImageRadioControl from '../../components/image-radio-control';
4848
import { withId } from '../../components/with-id';
4949
import GlobalShapeSize from '../../components/global-shape-size';
50-
import GlobalColor, {
51-
GlobalColorContrastChecker,
52-
} from '../../components/global-color';
5350
import ToolbarUrlInputPopover from '../../components/toolbar-url-input-popover';
5451
import genericAttributesSetter from '../../utils/generic-attributes-setter';
5552
import { name as ContactFormBlockName } from '../contact-form';
53+
import { getColor } from '../../components/with-global-default';
54+
import ColorPanel from '../../components/global-color/color-panel';
5655

5756
/**
5857
* @typedef MdcButtonProps
@@ -202,6 +201,26 @@ const ButtonEdit = ( {
202201
}
203202
}, [ isSubmitButton ] ); // eslint-disable-line
204203

204+
const colorSettings = {
205+
text: {
206+
label: __( 'Text Color', 'material-design' ),
207+
colorValue: getColor( 'on_primary_color', textColor ),
208+
onColorChange: setter( 'textColor' ),
209+
gradients: [], // Disable gradients
210+
disableCustomGradients: true,
211+
},
212+
container: {
213+
label: __( 'Container Color', 'material-design' ),
214+
colorValue: getColor( 'primary_color', backgroundColor ),
215+
onColorChange: setter( 'backgroundColor' ),
216+
gradients: [], // Disable gradients
217+
disableCustomGradients: true,
218+
globalPropName: hasBg( elevationStyle )
219+
? 'on_primary_color'
220+
: 'primary_color',
221+
},
222+
};
223+
205224
/**
206225
* Sets ref and linkTarget when the toggle is touched.
207226
*
@@ -332,63 +351,9 @@ const ButtonEdit = ( {
332351
/>
333352
) }
334353
</PanelBody>
335-
<PanelBody
336-
title={ __( 'Colors', 'material-design' ) }
337-
initialOpen={ true }
338-
>
339-
<div className="components-base-control">
340-
{ __(
341-
'Overrides will only apply to this button. Change Primary Color in ',
342-
'material-design'
343-
) }
344-
<a
345-
href={ getConfig( 'customizerUrls' ).colors }
346-
target="_blank"
347-
rel="noreferrer noopener"
348-
>
349-
{ __(
350-
'Material Design Options',
351-
'material-design'
352-
) }
353-
</a>
354-
{ __( ' to update all buttons.', 'material-design' ) }
355-
</div>
356-
357-
{ hasBg( elevationStyle ) && type === 'text' && (
358-
<GlobalColor
359-
label={ __( 'Container Color', 'material-design' ) }
360-
value={ backgroundColor }
361-
onChange={ setter( 'backgroundColor' ) }
362-
globalPropName={
363-
hasBg( elevationStyle )
364-
? 'primary_color'
365-
: 'on_primary_color'
366-
}
367-
/>
368-
) }
369-
<GlobalColor
370-
label={ __(
371-
'Text and icons Color',
372-
'material-design'
373-
) }
374-
value={ textColor }
375-
onChange={ setter( 'textColor' ) }
376-
globalPropName={
377-
hasBg( elevationStyle )
378-
? 'on_primary_color'
379-
: 'primary_color'
380-
}
381-
/>
382354

383-
{ hasBg( elevationStyle ) && type === 'text' && (
384-
<GlobalColorContrastChecker
385-
textColor={ textColor }
386-
backgroundColor={ backgroundColor }
387-
textProp="on_primary_color"
388-
backgroundProp="primary_color"
389-
/>
390-
) }
391-
</PanelBody>
355+
<ColorPanel colors={ colorSettings } />
356+
392357
{ type === 'text' && (
393358
<PanelBody
394359
title={ __( 'Corner Styles', 'material-design' ) }

plugin/assets/src/block-editor/blocks/icon/edit.js

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ import { PanelBody, RangeControl } from '@wordpress/components';
3636
*/
3737
import './style.css';
3838
import { ICON_SIZES } from './options';
39-
import { getConfig, getIconName } from '../../utils';
39+
import { getIconName } from '../../utils';
4040
import IconPicker from '../../components/icon-picker';
41-
import GlobalColor from '../../components/global-color';
4241
import genericAttributesSetter from '../../utils/generic-attributes-setter';
4342
import ButtonGroup from '../../components/button-group';
43+
import { getColor } from '../../components/with-global-default';
44+
import ColorPanel from '../../components/global-color/color-panel';
4445

4546
/**
4647
* Material icon edit component.
@@ -68,6 +69,16 @@ const IconEdit = ( {
6869
style[ 'font-size' ] = `${ customSize }px`;
6970
}
7071

72+
const colorSettings = {
73+
text: {
74+
label: __( 'Text Color', 'material-design' ),
75+
colorValue: getColor( 'primary_color', textColor ),
76+
onColorChange: setter( 'textColor' ),
77+
gradients: [], // Disable gradients
78+
disableCustomGradients: true,
79+
},
80+
};
81+
7182
const blockProps = useBlockProps( {
7283
className: classNames( className, {
7384
[ `has-text-align-${ align }` ]: align,
@@ -104,35 +115,9 @@ const IconEdit = ( {
104115
/>
105116
}
106117
</PanelBody>
107-
<PanelBody
108-
title={ __( 'Colors', 'material-design' ) }
109-
initialOpen={ true }
110-
>
111-
<div className="components-base-control">
112-
{ __(
113-
'Overrides will only apply to this Icon. Change Primary Color in ',
114-
'material-design'
115-
) }
116-
<a
117-
href={ getConfig( 'customizerUrls' ).colors }
118-
target="_blank"
119-
rel="noreferrer noopener"
120-
>
121-
{ __(
122-
'Material Design Options',
123-
'material-design'
124-
) }
125-
</a>
126-
{ __( ' to update all Icons.', 'material-design' ) }
127-
</div>
128118

129-
<GlobalColor
130-
label={ __( 'Icon Color', 'material-design' ) }
131-
value={ textColor }
132-
onChange={ setter( 'textColor' ) }
133-
globalPropName={ 'primary_color' }
134-
/>
135-
</PanelBody>
119+
<ColorPanel colors={ colorSettings } />
120+
136121
<PanelBody
137122
title={ __( 'Size', 'material-design' ) }
138123
initialOpen={ true }
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* WordPress dependencies
19+
*/
20+
21+
import {
22+
__experimentalColorGradientSettingsDropdown as ColorGradientDropdown, // eslint-disable-line @wordpress/no-unsafe-wp-apis
23+
ContrastChecker,
24+
useSetting,
25+
} from '@wordpress/block-editor';
26+
import {
27+
PanelBody,
28+
__experimentalSpacer as Spacer, // eslint-disable-line @wordpress/no-unsafe-wp-apis
29+
} from '@wordpress/components';
30+
import { __ } from '@wordpress/i18n';
31+
32+
/**
33+
* @param {string} color Color input in 'var(--md-sys-color-on-primary)' format.
34+
*
35+
* @return {string} variable name example: '--md-sys-color-on-primary'.
36+
*/
37+
const getColorFromVarString = color => {
38+
if ( color.includes( 'var(' ) ) {
39+
const colorVar = color.split( '(' )[ 1 ].split( ')' )[ 0 ];
40+
const style = getComputedStyle( document.body );
41+
return style.getPropertyValue( colorVar );
42+
}
43+
return color;
44+
};
45+
46+
const ColorPanel = ( { colors } ) => {
47+
const { text, container } = colors;
48+
const settings = [ text, container ];
49+
const colorSettings = useSetting( 'color.palette' );
50+
const colorPalette = [
51+
{
52+
name: __( 'Theme', 'material-design' ),
53+
colors: colorSettings,
54+
},
55+
];
56+
57+
const colorText = text ? getColorFromVarString( text.colorValue ) : null;
58+
const containerText = container
59+
? getColorFromVarString( container.colorValue )
60+
: null;
61+
62+
return (
63+
<PanelBody
64+
title={ __( 'Color', 'material-design' ) }
65+
initialOpen={ true }
66+
>
67+
<ColorGradientDropdown
68+
settings={ settings }
69+
colors={ colorPalette }
70+
__experimentalHasMultipleOrigins={ true } // Allow multiple color palettes.
71+
/>
72+
73+
<Spacer marginY={ 4 } />
74+
75+
{ colorText && containerText && (
76+
<ContrastChecker
77+
textColor={ colorText }
78+
backgroundColor={ containerText }
79+
/>
80+
) }
81+
</PanelBody>
82+
);
83+
};
84+
85+
export default ColorPanel;

plugin/assets/src/block-editor/components/global-color/index.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)