-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
271 lines (253 loc) · 6.54 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// @ts-nocheck
/**
* External dependencies
*/
import { map } from 'lodash';
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { useCallback, useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import Dropdown from '../dropdown';
import { ColorPicker } from '../color-picker';
import CircularOptionPicker from '../circular-option-picker';
import { VStack } from '../v-stack';
import { Flex, FlexItem } from '../flex';
import { Truncate } from '../truncate';
import { ColorHeading } from './styles';
extend( [ namesPlugin, a11yPlugin ] );
function SinglePalette( {
className,
clearColor,
colors,
onChange,
value,
actions,
} ) {
const colorOptions = useMemo( () => {
return map( colors, ( { color, name } ) => {
const colordColor = colord( color );
return (
<CircularOptionPicker.Option
key={ color }
isSelected={ value === color }
selectedIconProps={
value === color
? {
fill:
colordColor.contrast() >
colordColor.contrast( '#000' )
? '#fff'
: '#000',
}
: {}
}
tooltipText={
name ||
// translators: %s: color hex code e.g: "#f00".
sprintf( __( 'Color code: %s' ), color )
}
style={ { backgroundColor: color, color } }
onClick={
value === color ? clearColor : () => onChange( color )
}
aria-label={
name
? // translators: %s: The name of the color e.g: "vivid red".
sprintf( __( 'Color: %s' ), name )
: // translators: %s: color hex code e.g: "#f00".
sprintf( __( 'Color code: %s' ), color )
}
/>
);
} );
}, [ colors, value, onChange, clearColor ] );
return (
<CircularOptionPicker
className={ className }
options={ colorOptions }
actions={ actions }
/>
);
}
function MultiplePalettes( {
className,
clearColor,
colors,
onChange,
value,
actions,
} ) {
return (
<VStack spacing={ 3 } className={ className }>
{ colors.map( ( { name, colors: colorPalette }, index ) => {
return (
<VStack spacing={ 2 } key={ index }>
<ColorHeading>{ name }</ColorHeading>
<SinglePalette
clearColor={ clearColor }
colors={ colorPalette }
onChange={ onChange }
value={ value }
actions={
colors.length === index + 1 ? actions : null
}
/>
</VStack>
);
} ) }
</VStack>
);
}
export function CustomColorPickerDropdown( { isRenderedInSidebar, ...props } ) {
return (
<Dropdown
contentClassName="components-color-palette__custom-color-dropdown-content"
popoverProps={
isRenderedInSidebar
? { placement: 'left-start', offset: 20 }
: undefined
}
{ ...props }
/>
);
}
export const extractColorNameFromCurrentValue = (
currentValue,
colors = [],
showMultiplePalettes = false
) => {
if ( ! currentValue ) {
return '';
}
const currentValueIsCssVariable = /^var\(/.test( currentValue );
const normalizedCurrentValue = currentValueIsCssVariable
? currentValue
: colord( currentValue ).toHex();
// Normalize format of `colors` to simplify the following loop
const colorPalettes = showMultiplePalettes ? colors : [ { colors } ];
for ( const { colors: paletteColors } of colorPalettes ) {
for ( const { name: colorName, color: colorValue } of paletteColors ) {
const normalizedColorValue = currentValueIsCssVariable
? colorValue
: colord( colorValue ).toHex();
if ( normalizedCurrentValue === normalizedColorValue ) {
return colorName;
}
}
}
// translators: shown when the user has picked a custom color (i.e not in the palette of colors).
return __( 'Custom' );
};
export default function ColorPalette( {
clearable = true,
className,
colors,
disableCustomColors = false,
enableAlpha,
onChange,
value,
__experimentalHasMultipleOrigins = false,
__experimentalIsRenderedInSidebar = false,
} ) {
const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );
const showMultiplePalettes =
__experimentalHasMultipleOrigins && colors?.length;
const Component = showMultiplePalettes ? MultiplePalettes : SinglePalette;
const renderCustomColorPicker = () => (
<ColorPicker
color={ value }
onChange={ ( color ) => onChange( color ) }
enableAlpha={ enableAlpha }
/>
);
const colordColor = colord( value );
const valueWithoutLeadingHash = value?.startsWith( '#' )
? value.substring( 1 )
: value ?? '';
const buttonLabelName = useMemo(
() =>
extractColorNameFromCurrentValue(
value,
colors,
showMultiplePalettes
),
[ value, colors, showMultiplePalettes ]
);
const customColorAccessibleLabel = !! valueWithoutLeadingHash
? sprintf(
// translators: %1$s: The name of the color e.g: "vivid red". %2$s: The color's hex code e.g: "#f00".
__(
'Custom color picker. The currently selected color is called "%1$s" and has a value of "%2$s".'
),
buttonLabelName,
valueWithoutLeadingHash
)
: __( 'Custom color picker.' );
return (
<VStack spacing={ 3 } className={ className }>
{ ! disableCustomColors && (
<CustomColorPickerDropdown
isRenderedInSidebar={ __experimentalIsRenderedInSidebar }
renderContent={ renderCustomColorPicker }
renderToggle={ ( { isOpen, onToggle } ) => (
<Flex
as={ 'button' }
justify="space-between"
align="flex-start"
className="components-color-palette__custom-color"
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
aria-label={ customColorAccessibleLabel }
style={ {
background: value,
color:
colordColor.contrast() >
colordColor.contrast( '#000' )
? '#fff'
: '#000',
} }
>
<FlexItem
isBlock
as={ Truncate }
className="components-color-palette__custom-color-name"
>
{ buttonLabelName }
</FlexItem>
<FlexItem
as="span"
className="components-color-palette__custom-color-value"
>
{ valueWithoutLeadingHash }
</FlexItem>
</Flex>
) }
/>
) }
<Component
clearable={ clearable }
clearColor={ clearColor }
colors={ colors }
onChange={ onChange }
value={ value }
actions={
!! clearable && (
<CircularOptionPicker.ButtonAction
onClick={ clearColor }
>
{ __( 'Clear' ) }
</CircularOptionPicker.ButtonAction>
)
}
/>
</VStack>
);
}