/
color-palette-panel.js
104 lines (99 loc) · 2.9 KB
/
color-palette-panel.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
/**
* WordPress dependencies
*/
import { useViewportMatch } from '@wordpress/compose';
import {
__experimentalPaletteEdit as PaletteEdit,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import ColorVariations from './variations-color';
import { useCurrentMergeThemeStyleVariationsWithUserConfig } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
const { useGlobalSetting } = unlock( blockEditorPrivateApis );
const mobilePopoverProps = { placement: 'bottom-start', offset: 8 };
export default function ColorPalettePanel( { name } ) {
const [ themeColors, setThemeColors ] = useGlobalSetting(
'color.palette.theme',
name
);
const [ baseThemeColors ] = useGlobalSetting(
'color.palette.theme',
name,
'base'
);
const [ defaultColors, setDefaultColors ] = useGlobalSetting(
'color.palette.default',
name
);
const [ baseDefaultColors ] = useGlobalSetting(
'color.palette.default',
name,
'base'
);
const [ customColors, setCustomColors ] = useGlobalSetting(
'color.palette.custom',
name
);
const [ defaultPaletteEnabled ] = useGlobalSetting(
'color.defaultPalette',
name
);
const colorVariations = useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'color',
filter: ( variation ) =>
variation?.settings?.color &&
Object.keys( variation?.settings?.color ).length,
} );
const isMobileViewport = useViewportMatch( 'small', '<' );
const popoverProps = isMobileViewport ? mobilePopoverProps : undefined;
return (
<VStack
className="edit-site-global-styles-color-palette-panel"
spacing={ 10 }
>
{ !! themeColors && !! themeColors.length && (
<PaletteEdit
canReset={ themeColors !== baseThemeColors }
canOnlyChangeValues
colors={ themeColors }
onChange={ setThemeColors }
paletteLabel={ __( 'Theme' ) }
paletteLabelHeadingLevel={ 3 }
popoverProps={ popoverProps }
/>
) }
{ !! defaultColors &&
!! defaultColors.length &&
!! defaultPaletteEnabled && (
<PaletteEdit
canReset={ defaultColors !== baseDefaultColors }
canOnlyChangeValues
colors={ defaultColors }
onChange={ setDefaultColors }
paletteLabel={ __( 'Default' ) }
paletteLabelHeadingLevel={ 3 }
popoverProps={ popoverProps }
/>
) }
{ !! colorVariations.length && (
<ColorVariations variations={ colorVariations } />
) }
<PaletteEdit
colors={ customColors }
onChange={ setCustomColors }
paletteLabel={ __( 'Custom' ) }
paletteLabelHeadingLevel={ 3 }
emptyMessage={ __(
'Custom colors are empty! Add some colors to create your own color palette.'
) }
slugPrefix="custom-"
popoverProps={ popoverProps }
/>
</VStack>
);
}