-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
component.tsx
266 lines (241 loc) · 7.17 KB
/
component.tsx
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
/**
* External dependencies
*/
import type { CSSProperties } from 'react';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { closeSmall } from '@wordpress/icons';
/**
* Internal dependencies
*/
import BorderControlStylePicker from '../border-control-style-picker';
import Button from '../../button';
import ColorIndicator from '../../color-indicator';
import ColorPalette from '../../color-palette';
import Dropdown from '../../dropdown';
import { HStack } from '../../h-stack';
import { VStack } from '../../v-stack';
import type { WordPressComponentProps } from '../../context';
import { contextConnect } from '../../context';
import { useBorderControlDropdown } from './hook';
import { StyledLabel } from '../../base-control/styles/base-control-styles';
import DropdownContentWrapper from '../../dropdown/dropdown-content-wrapper';
import type { ColorObject } from '../../color-palette/types';
import { isMultiplePaletteArray } from '../../color-palette/utils';
import type { DropdownProps as DropdownComponentProps } from '../../dropdown/types';
import type { ColorProps, DropdownProps } from '../types';
const getAriaLabelColorValue = ( colorValue: string ) => {
// Leave hex values as-is. Remove the `var()` wrapper from CSS vars.
return colorValue.replace( /^var\((.+)\)$/, '$1' );
};
const getColorObject = (
colorValue: CSSProperties[ 'borderColor' ],
colors: ColorProps[ 'colors' ] | undefined
) => {
if ( ! colorValue || ! colors ) {
return;
}
if ( isMultiplePaletteArray( colors ) ) {
// Multiple origins
let matchedColor;
colors.some( ( origin ) =>
origin.colors.some( ( color ) => {
if ( color.color === colorValue ) {
matchedColor = color;
return true;
}
return false;
} )
);
return matchedColor;
}
// Single origin
return colors.find( ( color ) => color.color === colorValue );
};
const getToggleAriaLabel = (
colorValue: CSSProperties[ 'borderColor' ],
colorObject: ColorObject | undefined,
style: CSSProperties[ 'borderStyle' ],
isStyleEnabled: boolean
) => {
if ( isStyleEnabled ) {
if ( colorObject ) {
const ariaLabelValue = getAriaLabelColorValue( colorObject.color );
return style
? sprintf(
// translators: %1$s: The name of the color e.g. "vivid red". %2$s: The color's hex code e.g.: "#f00:". %3$s: The current border style selection e.g. "solid".
'Border color and style picker. The currently selected color is called "%1$s" and has a value of "%2$s". The currently selected style is "%3$s".',
colorObject.name,
ariaLabelValue,
style
)
: sprintf(
// translators: %1$s: The name of the color e.g. "vivid red". %2$s: The color's hex code e.g.: "#f00:".
'Border color and style picker. The currently selected color is called "%1$s" and has a value of "%2$s".',
colorObject.name,
ariaLabelValue
);
}
if ( colorValue ) {
const ariaLabelValue = getAriaLabelColorValue( colorValue );
return style
? sprintf(
// translators: %1$s: The color's hex code e.g.: "#f00:". %2$s: The current border style selection e.g. "solid".
'Border color and style picker. The currently selected color has a value of "%1$s". The currently selected style is "%2$s".',
ariaLabelValue,
style
)
: sprintf(
// translators: %1$s: The color's hex code e.g: "#f00".
'Border color and style picker. The currently selected color has a value of "%1$s".',
ariaLabelValue
);
}
return __( 'Border color and style picker.' );
}
if ( colorObject ) {
return sprintf(
// translators: %1$s: The name of the color e.g. "vivid red". %2$s: The color's hex code e.g: "#f00".
'Border color picker. The currently selected color is called "%1$s" and has a value of "%2$s".',
colorObject.name,
getAriaLabelColorValue( colorObject.color )
);
}
if ( colorValue ) {
return sprintf(
// translators: %1$s: The color's hex code e.g: "#f00".
'Border color picker. The currently selected color has a value of "%1$s".',
getAriaLabelColorValue( colorValue )
);
}
return __( 'Border color picker.' );
};
const BorderControlDropdown = (
props: WordPressComponentProps< DropdownProps, 'div' >,
forwardedRef: React.ForwardedRef< any >
) => {
const {
__experimentalIsRenderedInSidebar,
border,
colors,
disableCustomColors,
enableAlpha,
enableStyle,
indicatorClassName,
indicatorWrapperClassName,
isStyleSettable,
onReset,
onColorChange,
onStyleChange,
popoverContentClassName,
popoverControlsClassName,
resetButtonClassName,
showDropdownHeader,
size,
__unstablePopoverProps,
...otherProps
} = useBorderControlDropdown( props );
const { color, style } = border || {};
const colorObject = getColorObject( color, colors );
const toggleAriaLabel = getToggleAriaLabel(
color,
colorObject,
style,
enableStyle
);
const showResetButton = color || ( style && style !== 'none' );
const dropdownPosition = __experimentalIsRenderedInSidebar
? 'bottom left'
: undefined;
const renderToggle: DropdownComponentProps[ 'renderToggle' ] = ( {
onToggle,
} ) => (
<Button
onClick={ onToggle }
variant="tertiary"
aria-label={ toggleAriaLabel }
tooltipPosition={ dropdownPosition }
label={ __( 'Border color and style picker' ) }
showTooltip
__next40pxDefaultSize={ size === '__unstable-large' ? true : false }
>
<span className={ indicatorWrapperClassName }>
<ColorIndicator
className={ indicatorClassName }
colorValue={ color }
/>
</span>
</Button>
);
const renderContent: DropdownComponentProps[ 'renderContent' ] = ( {
onClose,
} ) => (
<>
<DropdownContentWrapper paddingSize="medium">
<VStack className={ popoverControlsClassName } spacing={ 6 }>
{ showDropdownHeader ? (
<HStack>
<StyledLabel>{ __( 'Border color' ) }</StyledLabel>
<Button
size="small"
label={ __( 'Close border color' ) }
icon={ closeSmall }
onClick={ onClose }
/>
</HStack>
) : undefined }
<ColorPalette
className={ popoverContentClassName }
value={ color }
onChange={ onColorChange }
{ ...{ colors, disableCustomColors } }
__experimentalIsRenderedInSidebar={
__experimentalIsRenderedInSidebar
}
clearable={ false }
enableAlpha={ enableAlpha }
/>
{ enableStyle && isStyleSettable && (
<BorderControlStylePicker
label={ __( 'Style' ) }
value={ style }
onChange={ onStyleChange }
/>
) }
</VStack>
</DropdownContentWrapper>
{ showResetButton && (
<DropdownContentWrapper paddingSize="none">
<Button
className={ resetButtonClassName }
variant="tertiary"
onClick={ () => {
onReset();
onClose();
} }
>
{ __( 'Reset' ) }
</Button>
</DropdownContentWrapper>
) }
</>
);
return (
<Dropdown
renderToggle={ renderToggle }
renderContent={ renderContent }
popoverProps={ {
...__unstablePopoverProps,
} }
{ ...otherProps }
ref={ forwardedRef }
/>
);
};
const ConnectedBorderControlDropdown = contextConnect(
BorderControlDropdown,
'BorderControlDropdown'
);
export default ConnectedBorderControlDropdown;