-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathindex.js
More file actions
303 lines (260 loc) · 7.99 KB
/
index.js
File metadata and controls
303 lines (260 loc) · 7.99 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* Internal dependencies
*/
import AdvancedControl, { extractControlProps } from '../base-control2'
import { ResetButton } from '../base-control2/reset-button'
import { useControlHandlers } from '../base-control2/hooks'
import { ColorPalettePopup } from './color-palette-popup'
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n'
import {
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
} from '@wordpress/block-editor'
import { memo } from '@wordpress/element'
import {
Button,
ColorIndicator,
Dropdown,
FlexItem,
__experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis
} from '@wordpress/components'
import { useSelect, select } from '@wordpress/data'
import { applyFilters, addFilter } from '@wordpress/hooks'
/**
* External dependencies
*/
import { cloneDeep } from 'lodash'
import { i18n } from 'stackable'
import classnames from 'classnames'
import { useBlockColorSchemes } from '~stackable/hooks'
const popoverProps = {
placement: 'left-start',
offset: 36,
shift: true,
}
const PASSTHRUOP = v => v
addFilter( 'stackable.color-palette-control.colors', 'stackable/global-color-schemes-color-palette-control', ( { colors: _colors, gradients: _gradients } ) => {
// Get colors from the color schemes.
const { getColorGroups } = useBlockColorSchemes()
const { colorSchemeColors, colorSchemeGradients } = getColorGroups()
let colors = cloneDeep( _colors )
let gradients = cloneDeep( _gradients )
gradients = [
...colorSchemeGradients,
...gradients,
]
colors = [
...colorSchemeColors,
..._colors,
]
return { colors, gradients }
} )
addFilter( 'stackable.color-palette-control.colors', 'stackable/color-palette-control', ( { colors: _colors, gradients: _gradients } ) => {
const {
stackableColors,
stackableGradients,
} = select( 'stackable/global-colors' ).getSettings()
let colors = cloneDeep( _colors )
let gradients = cloneDeep( _gradients )
if ( stackableGradients && stackableGradients.length ) {
gradients = [
{
name: __( 'Global Gradients', i18n ),
gradients: cloneDeep( stackableGradients ),
id: 'stk-global-gradients',
},
...gradients,
]
}
if ( stackableColors && stackableColors.length ) {
colors = [
{
name: __( 'Global Colors', i18n ),
colors: cloneDeep( stackableColors ),
id: 'stk-global-colors',
},
...colors,
]
}
return { colors, gradients }
} )
addFilter( 'stackable.color-palette-control.color-value', 'stackable/color-palette-control', value => {
if ( typeof value === 'string' && value.includes( '--stk-global-color' ) && value.match( /#[\d\w]{6,}/ ) ) {
return value.match( /#[\d\w]{6,}/ )[ 0 ]
}
return value
} )
const ColorPaletteControl = memo( props => {
const {
label,
className = '',
} = props
const [ _value, _onChange ] = useControlHandlers( props.attribute, props.responsive, props.hover, props.valueCallback, props.changeCallback )
const [ _propsToPass, controlProps ] = extractControlProps( props )
const {
hideThemeColors,
hideDefaultColors,
hideSiteEditorColors,
} = useSelect( 'stackable/global-colors' ).getSettings()
let { colors, gradients } = applyFilters( 'stackable.color-palette-control.colors', useMultipleOriginColorsAndGradients() )
// Filter the colors.
colors = colors.filter( group => {
// Since there are no identifying properties for the groups, we'll just use the same names used in Gutenberg.
if ( hideThemeColors && group.name === _x( 'Theme', 'Indicates this palette comes from the theme.' ) ) {
return false
}
if ( hideDefaultColors && group.name === _x( 'Default', 'Indicates this palette comes from WordPress.' ) ) {
return false
}
if ( hideSiteEditorColors && group.name === _x( 'Custom', 'Indicates this palette comes from the theme.' ) ) {
return false
}
return true
} )
gradients = gradients.filter( group => {
// Since there are no identifying properties for the groups, we'll just use the same names used in Gutenberg.
if ( hideThemeColors && group.name === _x( 'Theme', 'Indicates this palette comes from the theme.' ) ) {
return false
}
if ( hideDefaultColors && group.name === _x( 'Default', 'Indicates this palette comes from WordPress.' ) ) {
return false
}
if ( hideSiteEditorColors && group.name === _x( 'Custom', 'Indicates this palette comes from the theme.' ) ) {
return false
}
return true
} )
const allColors = ( [ ...colors, ...gradients ] ).reduce( ( colors, group ) => {
return [
...colors,
...( group.colors || group.gradients ),
]
}, [] )
let value = typeof props.value === 'undefined' ? _value : props.value
const onChange = typeof props.onChange === 'undefined' ? _onChange : props.onChange
value = applyFilters( 'stackable.color-palette-control.color-value', value )
let colorLabel,
colorName = value,
popupPickerValue = value // We assign a value to this so that the popup picker will show a "check" on the selected color.
allColors.some( color => {
if ( color.color === value || color.gradient === value ) {
colorName = color.name
colorLabel = color.name
return true
}
// For Stackable global colors to have a correct name label.
if ( color.slug ) {
if ( `var(--${ color.slug })` === value ) {
colorName = color.name
colorLabel = color.name
popupPickerValue = color.color
return true
}
}
return false
} )
colorLabel = colorName || ( value === 'transparent' ? 'Transparent' : value )
const toggleSettings = {
colorValue: value,
label: props.colorLabel || colorLabel,
additionalToggleProps: props.additionalToggleProps,
}
const colorPalette = (
<ColorPalettePopup
value={ popupPickerValue }
onChange={ onChange }
preOnChange={ props.preOnChange }
colors={ props.isGradient ? gradients : colors }
isGradient={ props.isGradient }
hasGradientPicker={ props.hasGradientPicker }
enableGradient={ props.hasGradientPicker && props.enableGradient }
gradients={ gradients }
/>
)
return (
<AdvancedControl
{ ...controlProps }
className={ classnames( [ className, 'editor-color-palette-control', 'stk-color-palette-control' ] ) }
label={ label }
data-attribute={ props.attribute }
>
{ props.isExpanded && colorPalette }
{ ! props.isExpanded && (
<Dropdown
popoverProps={ popoverProps }
className="block-editor-tools-panel-color-gradient-settings__dropdown"
renderToggle={ renderToggle( toggleSettings ) }
renderContent={ () => (
<div className="stk-color-palette-control__popover-content">
{ colorPalette }
</div>
) }
/>
) }
<ResetButton
allowReset={ props.allowReset }
value={ value }
default={ props.default }
onChange={ onChange }
/>
</AdvancedControl>
)
} )
ColorPaletteControl.defaultProps = {
allowReset: true,
default: '',
attribute: '',
value: undefined,
colorLabel: undefined,
onChange: undefined,
preOnChange: PASSTHRUOP,
isExpanded: false,
isGradient: false,
hasGradientPicker: false,
enableGradient: false,
additionalToggleProps: {},
}
export default ColorPaletteControl
const renderToggle =
settings =>
( { onToggle, isOpen } ) => {
const {
colorValue,
label,
additionalToggleProps,
} = settings
const toggleProps = {
onClick: onToggle,
className: classnames(
'block-editor-panel-color-gradient-settings__dropdown',
{ 'is-open': isOpen }
),
'aria-expanded': isOpen,
...additionalToggleProps,
}
return (
<Button { ...toggleProps }>
<LabeledColorIndicator
colorValue={ colorValue }
label={ label }
/>
</Button>
)
}
const LabeledColorIndicator = ( { colorValue, label } ) => (
<HStack justify="flex-start">
<ColorIndicator
className="stk-color-indicator block-editor-panel-color-gradient-settings__color-indicator"
colorValue={ colorValue }
/>
<FlexItem
className="stk-color-name block-editor-panel-color-gradient-settings__color-name"
title={ label }
>
{ label }
</FlexItem>
</HStack>
)