/
screen-background-color.js
114 lines (102 loc) · 2.89 KB
/
screen-background-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
107
108
109
110
111
112
113
114
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __experimentalPanelColorGradientSettings as PanelColorGradientSettings } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import ScreenHeader from './header';
import {
getSupportedGlobalStylesPanels,
useColorsPerOrigin,
useGradientsPerOrigin,
useSetting,
useStyle,
} from './hooks';
function ScreenBackgroundColor( { name } ) {
const parentMenu = name === undefined ? '' : '/blocks/' + name;
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 colorsPerOrigin = useColorsPerOrigin( name );
const gradientsPerOrigin = useGradientsPerOrigin( name );
const [ isBackgroundEnabled ] = useSetting( 'color.background', name );
const hasBackgroundColor =
supports.includes( 'backgroundColor' ) &&
isBackgroundEnabled &&
( solids.length > 0 || areCustomSolidsEnabled );
const hasGradientColor =
supports.includes( 'background' ) &&
( gradients.length > 0 || areCustomGradientsEnabled );
const [ backgroundColor, setBackgroundColor ] = useStyle(
'color.background',
name
);
const [ userBackgroundColor ] = useStyle(
'color.background',
name,
'user'
);
const [ gradient, setGradient ] = useStyle( 'color.gradient', name );
const [ userGradient ] = useStyle( 'color.gradient', name, 'user' );
if ( ! hasBackgroundColor && ! hasGradientColor ) {
return null;
}
const settings = [];
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;
}
}
settings.push( {
...backgroundSettings,
...gradientSettings,
label: __( 'Background color' ),
} );
return (
<>
<ScreenHeader
back={ parentMenu + '/colors' }
title={ __( 'Background' ) }
description={ __(
'Set a background color or gradient for the whole site.'
) }
/>
<PanelColorGradientSettings
title={ __( 'Color' ) }
settings={ settings }
colors={ colorsPerOrigin }
gradients={ gradientsPerOrigin }
disableCustomColors={ ! areCustomSolidsEnabled }
disableCustomGradients={ ! areCustomGradientsEnabled }
__experimentalHasMultipleOrigins
showTitle={ false }
enableAlpha
__experimentalIsRenderedInSidebar
/>
</>
);
}
export default ScreenBackgroundColor;