-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel-color.js
106 lines (94 loc) · 3.06 KB
/
panel-color.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Color block inspector panel.
*
* @author Justin Tadlock <justintadlock@gmail.com>
* @copyright Copyright (c) 2022, Justin Tadlock
* @link https://github.com/x3p0-dev/x3p0-progress
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
import { useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import {
colorAttribute,
colorSetting
} from '../common/utils-color';
import {
gradientAttribute,
gradientSetting
} from '../common/utils-gradient';
import {
PanelColorSettings,
useSetting,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients
} from '@wordpress/block-editor';
const ColorPanel = ( {
attributes: {
foregroundColor,
foregroundGradient,
backgroundColor,
backgroundGradient
},
setAttributes
} ) => {
// Get the base color and gradient options to pass into individual color
// settings for our Color panel.
const colorGradientOptions = useMultipleOriginColorsAndGradients();
// Get all color palettes.
const userColors = useSetting( 'color.palette.custom' );
const themeColors = useSetting( 'color.palette.theme' );
const defaultColors = useSetting ( 'color.palette.default' );
// Get all gradient palettes.
const userGradients = useSetting( 'color.gradients.custom' );
const themeGradients = useSetting( 'color.gradients.theme' );
const defaultGradients = useSetting( 'color.gradients.default' );
// Flattened array with all color palettes.
const colors = useMemo( () => [
...( defaultColors || [] ),
...( themeColors || [] ),
...( userColors || [] )
] );
// Flattened array with all gradient palettes.
const gradients = useMemo( () => [
...( defaultGradients || [] ),
...( themeGradients || [] ),
...( userGradients || [] )
] );
const colorSettings = [
{
label: __( 'Progress (Foreground)', 'x3p0-progress' ),
value: colorSetting( foregroundColor, colors ),
gradientValue: gradientSetting( foregroundGradient, gradients ),
onChange: ( value ) => { setAttributes( {
foregroundColor: colorAttribute( value, colors )
} ) },
onGradientChange: ( value ) => { setAttributes( {
foregroundGradient: gradientAttribute( value, gradients )
} ) },
...colorGradientOptions
},
{
label: __( 'Goal (Background)', 'x3p0-progress' ),
value: colorSetting( backgroundColor, colors ),
gradientValue: gradientSetting( backgroundGradient, gradients ),
onChange: ( value ) => { setAttributes( {
backgroundColor: colorAttribute( value, colors )
} ) },
onGradientChange: ( value ) => { setAttributes( {
backgroundGradient: gradientAttribute( value, gradients )
} ) },
...colorGradientOptions
}
];
// Creates a color settings panel. We have two custom colors for handling
// the `<progress>` element background and value.
return (
<PanelColorSettings
title={ __( 'Color', 'x3p0-progress' ) }
colorSettings={ colorSettings }
enableAlpha={ true }
__experimentalIsRenderedInSidebar={ true }
__experimentalHasMultipleOrigins={ true }
/>
);
};
export default ColorPanel;